From 24bf43f9f9d0a9cc4e4a1b57d3d565119be2fb59 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 25 Dec 2023 18:57:34 +0300 Subject: [PATCH 01/23] feat(folks-lending-pools): Create FolksLendingPool class, create fetchFolksLendingPool util function to fetch the pool info from the contract and set FolksLendingPool instance with the given params --- src/folks-lending-pools/constants.ts | 6 ++ src/folks-lending-pools/index.ts | 97 ++++++++++++++++++++++++++++ src/folks-lending-pools/utils.ts | 27 ++++++++ 3 files changed, 130 insertions(+) create mode 100644 src/folks-lending-pools/constants.ts create mode 100644 src/folks-lending-pools/index.ts create mode 100644 src/folks-lending-pools/utils.ts diff --git a/src/folks-lending-pools/constants.ts b/src/folks-lending-pools/constants.ts new file mode 100644 index 00000000..24b68320 --- /dev/null +++ b/src/folks-lending-pools/constants.ts @@ -0,0 +1,6 @@ +// eslint-disable-next-line no-magic-numbers +const SECONDS_IN_YEAR = 365 * 24 * 60 * 60; +const ONE_14_DP = 1e14; +const ONE_16_DP = 1e16; + +export {SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP}; diff --git a/src/folks-lending-pools/index.ts b/src/folks-lending-pools/index.ts new file mode 100644 index 00000000..4b179497 --- /dev/null +++ b/src/folks-lending-pools/index.ts @@ -0,0 +1,97 @@ +/* eslint-disable no-magic-numbers */ +import algosdk from "algosdk"; + +import {parseState} from "./utils"; +import {ONE_14_DP, ONE_16_DP, SECONDS_IN_YEAR} from "./constants"; + +class FolksLendingPool { + escrowAddress: string; + + // eslint-disable-next-line max-params + constructor( + public appId: number, + public managerAppId: number, + public depositInterestRate: number, + public depositInterestIndex: number, + public updatedAt: Date, + public originalAssetId: number, + public fAssetId: number + ) { + this.escrowAddress = algosdk.getApplicationAddress(this.appId); + } + + private calcDepositInterestIndex(timestamp: number): number { + const dt = Math.floor(timestamp - Math.floor(this.updatedAt.getTime() / 1000)); + + return Math.floor( + (this.depositInterestIndex * + Math.floor(ONE_16_DP + (this.depositInterestRate * dt) / SECONDS_IN_YEAR)) / + ONE_16_DP + ); + } + + private getLastTimestamp(): number { + return Math.floor(new Date().getTime() / 1000); + } + + /** + * Calculates the amount fAsset received when adding liquidity with original asset. + */ + convertAddAmount(amount: number): number { + const interestIndex = this.calcDepositInterestIndex(this.getLastTimestamp()); + + return Math.floor((amount * ONE_14_DP) / interestIndex); + } + + /** + * Calculates the amount original asset received according to fAsset amount when removing liquidity from lending pool. + */ + convertRemoveAmount(amount: number, options: {ceil?: boolean} = {}): number { + const interestIndex = this.calcDepositInterestIndex(this.getLastTimestamp()); + const converted = (amount * interestIndex) / ONE_14_DP; + + if (options.ceil) { + return Math.ceil(converted); + } + + return Math.floor(converted); + } +} + +/** + * Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. + */ +async function fetchFolksLendingPool( + algod: algosdk.Algodv2, + appId: number +): Promise { + const appInfo = await algod.getApplicationByID(appId).do(); + const rawState = appInfo.params["global-state"]; + const state = parseState(rawState); + + const managerAppId = Number(Buffer.from(state.pm, "base64").readBigUInt64BE(0)); + + const assetsIds = Buffer.from(state.a, "base64"); + + const originalAssetId = Number(assetsIds.readBigUInt64BE(0)); + const fAssetId = Number(assetsIds.readBigUInt64BE(8)); + + const interestInfo = Buffer.from(state.i, "base64"); + + const depositInterestRate = Number(interestInfo.readBigUInt64BE(32)); + const depositInterestIndex = Number(interestInfo.readBigUInt64BE(40)); + const updatedAt = Number(interestInfo.readBigUInt64BE(48)); + + return new FolksLendingPool( + appId, + managerAppId, + depositInterestRate, + depositInterestIndex, + new Date(updatedAt * 1000), + originalAssetId, + fAssetId + ); +} + +export {fetchFolksLendingPool, FolksLendingPool}; +/* eslint-enable no-magic-numbers */ diff --git a/src/folks-lending-pools/utils.ts b/src/folks-lending-pools/utils.ts new file mode 100644 index 00000000..20163323 --- /dev/null +++ b/src/folks-lending-pools/utils.ts @@ -0,0 +1,27 @@ +/** + * Utility function for converting the Algorand key-value schema into a plain object. + * + * Algorand store keys in base64 encoding and store values as either bytes or unsigned integers depending + * on the type. This function decodes this information into a more human friendly structure. + * + * @param kv Algorand key-value data structure to parse. + * + * @returns Key value dictionary parsed from the argument. + */ +export function parseState(kv: any) { + // Transform algorand key-value schema. + const res: any = {}; + + for (const elem of kv) { + const key = new TextDecoder().decode(Buffer.from(elem.key, "base64")); + let val: string | number; + + if (elem.value.type === 1) { + val = elem.value.bytes; + } else { + val = elem.value.uint; + } + res[key] = val; + } + return res; +} From 3701c232f1c541de2d9c795dfd7ffc14e9dabdbc Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 25 Dec 2023 19:33:31 +0300 Subject: [PATCH 02/23] feat(index): Export folks-lending-pool utils --- src/index.ts | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/index.ts b/src/index.ts index 1a98b2b0..397c2f66 100644 --- a/src/index.ts +++ b/src/index.ts @@ -133,6 +133,8 @@ export { REDEEM_PROCESS_TXN_COUNT } from "./redeem"; +export {FolksLendingPool, fetchFolksLendingPool} from "./folks-lending-pools/index"; + export {prepareCommitTransactions, getStakingAppID} from "./stake"; export {tinymanJSSDKConfig} from "./config"; export {combineAndRegroupSignerTxns} from "./util/transaction/transactionUtils"; From 61bbc09b96d78734a40ddf2dc3eec25f980f5494 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 25 Dec 2023 19:51:40 +0300 Subject: [PATCH 03/23] fix(index): Remove index directory --- src/folks-lending-pools/index.ts | 2 -- src/index.ts | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/src/folks-lending-pools/index.ts b/src/folks-lending-pools/index.ts index 4b179497..9175e48d 100644 --- a/src/folks-lending-pools/index.ts +++ b/src/folks-lending-pools/index.ts @@ -1,4 +1,3 @@ -/* eslint-disable no-magic-numbers */ import algosdk from "algosdk"; import {parseState} from "./utils"; @@ -94,4 +93,3 @@ async function fetchFolksLendingPool( } export {fetchFolksLendingPool, FolksLendingPool}; -/* eslint-enable no-magic-numbers */ diff --git a/src/index.ts b/src/index.ts index 397c2f66..b3634c9e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -133,7 +133,7 @@ export { REDEEM_PROCESS_TXN_COUNT } from "./redeem"; -export {FolksLendingPool, fetchFolksLendingPool} from "./folks-lending-pools/index"; +export {FolksLendingPool, fetchFolksLendingPool} from "./folks-lending-pools"; export {prepareCommitTransactions, getStakingAppID} from "./stake"; export {tinymanJSSDKConfig} from "./config"; From e66320c08fe6f7ebc27438fc6722e670c4d3c05e Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 25 Dec 2023 20:13:05 +0300 Subject: [PATCH 04/23] chore(folks-lending-pools): Create dist files of the utils functions --- dist/folks-lending-pools/constants.d.ts | 4 ++++ dist/folks-lending-pools/index.d.ts | 29 +++++++++++++++++++++++++ dist/folks-lending-pools/utils.d.ts | 11 ++++++++++ dist/index.d.ts | 1 + dist/index.js | 2 +- 5 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 dist/folks-lending-pools/constants.d.ts create mode 100644 dist/folks-lending-pools/index.d.ts create mode 100644 dist/folks-lending-pools/utils.d.ts diff --git a/dist/folks-lending-pools/constants.d.ts b/dist/folks-lending-pools/constants.d.ts new file mode 100644 index 00000000..1e480829 --- /dev/null +++ b/dist/folks-lending-pools/constants.d.ts @@ -0,0 +1,4 @@ +declare const SECONDS_IN_YEAR: number; +declare const ONE_14_DP = 100000000000000; +declare const ONE_16_DP = 10000000000000000; +export { SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP }; diff --git a/dist/folks-lending-pools/index.d.ts b/dist/folks-lending-pools/index.d.ts new file mode 100644 index 00000000..cba36fd3 --- /dev/null +++ b/dist/folks-lending-pools/index.d.ts @@ -0,0 +1,29 @@ +import algosdk from "algosdk"; +declare class FolksLendingPool { + appId: number; + managerAppId: number; + depositInterestRate: number; + depositInterestIndex: number; + updatedAt: Date; + originalAssetId: number; + fAssetId: number; + escrowAddress: string; + constructor(appId: number, managerAppId: number, depositInterestRate: number, depositInterestIndex: number, updatedAt: Date, originalAssetId: number, fAssetId: number); + private calcDepositInterestIndex; + private getLastTimestamp; + /** + * Calculates the amount fAsset received when adding liquidity with original asset. + */ + convertAddAmount(amount: number): number; + /** + * Calculates the amount original asset received according to fAsset amount when removing liquidity from lending pool. + */ + convertRemoveAmount(amount: number, options?: { + ceil?: boolean; + }): number; +} +/** + * Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. + */ +declare function fetchFolksLendingPool(algod: algosdk.Algodv2, appId: number): Promise; +export { fetchFolksLendingPool, FolksLendingPool }; diff --git a/dist/folks-lending-pools/utils.d.ts b/dist/folks-lending-pools/utils.d.ts new file mode 100644 index 00000000..f098c6e3 --- /dev/null +++ b/dist/folks-lending-pools/utils.d.ts @@ -0,0 +1,11 @@ +/** + * Utility function for converting the Algorand key-value schema into a plain object. + * + * Algorand store keys in base64 encoding and store values as either bytes or unsigned integers depending + * on the type. This function decodes this information into a more human friendly structure. + * + * @param kv Algorand key-value data structure to parse. + * + * @returns Key value dictionary parsed from the argument. + */ +export declare function parseState(kv: any): any; diff --git a/dist/index.d.ts b/dist/index.d.ts index 14c5123d..7d1647ed 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -37,6 +37,7 @@ export { SwapQuoteType } from "./swap/types"; export { SwapQuoteError }; export { SwapQuoteErrorType } from "./util/error/SwapQuoteError"; export { redeemExcessAsset, redeemAllExcessAsset, generateRedeemTxns, REDEEM_PROCESS_TXN_COUNT } from "./redeem"; +export { FolksLendingPool, fetchFolksLendingPool } from "./folks-lending-pools"; export { prepareCommitTransactions, getStakingAppID } from "./stake"; export { tinymanJSSDKConfig } from "./config"; export { combineAndRegroupSignerTxns } from "./util/transaction/transactionUtils"; diff --git a/dist/index.js b/dist/index.js index b29614ad..72d2bae2 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: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; +"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,k=1e5,P=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=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Rt=xt("swap"),Ft=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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,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=[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],h=[u[0].pool.address,u[1].pool.address],x=h[0],v=h[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:[x,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(Pt[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(Pt[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=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=wt(n.network,C.V1_1),a=x(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+k*(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)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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=xe(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,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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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}(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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Rn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.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,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=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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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,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,mt(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 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,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,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=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===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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function Zr(){return(Zr=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new Hr(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $r(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function ta(){return(ta=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=$r({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=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.FolksLendingPool=Hr,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 ta.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=he,exports.tinymanJSSDKConfig=Tt; From 03d2edb8cffe62712ab46dceb1cbd32390dec6b9 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Thu, 28 Dec 2023 16:30:45 +0300 Subject: [PATCH 05/23] feat(folks-lending-pools): Create generateTxns functions for add and remove liq --- .../add-liquidity/index.d.ts | 14 +++ dist/folks-lending-pools/constants.d.ts | 4 +- dist/folks-lending-pools/index.d.ts | 11 +- .../remove-liquidity/index.d.ts | 22 ++++ dist/folks-lending-pools/types.d.ts | 5 + dist/index.d.ts | 2 +- dist/index.js | 2 +- .../add-liquidity/index.ts | 110 ++++++++++++++++++ src/folks-lending-pools/constants.ts | 10 +- src/folks-lending-pools/index.ts | 8 +- .../remove-liquidity/index.ts | 86 ++++++++++++++ src/folks-lending-pools/types.ts | 16 +++ src/index.ts | 6 +- 13 files changed, 285 insertions(+), 11 deletions(-) create mode 100644 dist/folks-lending-pools/add-liquidity/index.d.ts create mode 100644 dist/folks-lending-pools/remove-liquidity/index.d.ts create mode 100644 dist/folks-lending-pools/types.d.ts create mode 100644 src/folks-lending-pools/add-liquidity/index.ts create mode 100644 src/folks-lending-pools/remove-liquidity/index.ts create mode 100644 src/folks-lending-pools/types.ts diff --git a/dist/folks-lending-pools/add-liquidity/index.d.ts b/dist/folks-lending-pools/add-liquidity/index.d.ts new file mode 100644 index 00000000..f8dc83e6 --- /dev/null +++ b/dist/folks-lending-pools/add-liquidity/index.d.ts @@ -0,0 +1,14 @@ +import algosdk, { Algodv2 } from "algosdk"; +import { SupportedNetwork } from "../../util/commonTypes"; +import { FolksLendingAssetInfo } from "../types"; +export declare function generateTxns({ client, network, poolAddress, poolTokenId, lendingManagerId, asset1In, asset2In, initiatorAddr, shouldOptInToPoolToken }: { + client: Algodv2; + network: SupportedNetwork; + poolAddress: string; + poolTokenId: number; + lendingManagerId: number; + asset1In: FolksLendingAssetInfo; + asset2In: FolksLendingAssetInfo; + initiatorAddr: string; + shouldOptInToPoolToken: boolean; +}): Promise; diff --git a/dist/folks-lending-pools/constants.d.ts b/dist/folks-lending-pools/constants.d.ts index 1e480829..5b6ec907 100644 --- a/dist/folks-lending-pools/constants.d.ts +++ b/dist/folks-lending-pools/constants.d.ts @@ -1,4 +1,6 @@ +import { SupportedNetwork } from "../util/commonTypes"; declare const SECONDS_IN_YEAR: number; declare const ONE_14_DP = 100000000000000; declare const ONE_16_DP = 10000000000000000; -export { SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP }; +declare const FOLKS_WRAPPER_APP_ID: Record; +export { SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID }; diff --git a/dist/folks-lending-pools/index.d.ts b/dist/folks-lending-pools/index.d.ts index cba36fd3..2038aeef 100644 --- a/dist/folks-lending-pools/index.d.ts +++ b/dist/folks-lending-pools/index.d.ts @@ -1,5 +1,7 @@ import algosdk from "algosdk"; -declare class FolksLendingPool { +import * as AddLiquidity from "./add-liquidity"; +import * as RemoveLiquidity from "./remove-liquidity"; +export declare class FolksLendingPool { appId: number; managerAppId: number; depositInterestRate: number; @@ -25,5 +27,8 @@ declare class FolksLendingPool { /** * Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. */ -declare function fetchFolksLendingPool(algod: algosdk.Algodv2, appId: number): Promise; -export { fetchFolksLendingPool, FolksLendingPool }; +export declare function fetchFolksLendingPool(algod: algosdk.Algodv2, appId: number): Promise; +export declare const LendingPool: { + AddLiquidity: typeof AddLiquidity; + RemoveLiquidity: typeof RemoveLiquidity; +}; diff --git a/dist/folks-lending-pools/remove-liquidity/index.d.ts b/dist/folks-lending-pools/remove-liquidity/index.d.ts new file mode 100644 index 00000000..973a2b01 --- /dev/null +++ b/dist/folks-lending-pools/remove-liquidity/index.d.ts @@ -0,0 +1,22 @@ +import algosdk, { Algodv2 } from "algosdk"; +import { V2PoolInfo } from "../../util/pool/poolTypes"; +import { SupportedNetwork } from "../../util/commonTypes"; +export declare function generateTxns({ client, pool, poolTokenIn, initiatorAddr, lendingAsset1, lendingAsset2, lendingManagerId, network }: { + client: Algodv2; + pool: V2PoolInfo; + poolTokenIn: number | bigint; + initiatorAddr: string; + minAsset1Amount: number | bigint; + minAsset2Amount: number | bigint; + lendingAsset1: { + id: number; + appId: number; + }; + lendingAsset2: { + id: number; + appId: number; + }; + lendingManagerId: number; + slippage: number; + network: SupportedNetwork; +}): Promise; diff --git a/dist/folks-lending-pools/types.d.ts b/dist/folks-lending-pools/types.d.ts new file mode 100644 index 00000000..d74c16ee --- /dev/null +++ b/dist/folks-lending-pools/types.d.ts @@ -0,0 +1,5 @@ +import { AssetWithIdAndAmount } from "../util/asset/assetModels"; +export type FolksLendingAssetInfo = AssetWithIdAndAmount & { + fAssetId: number; + lendingAppId: number; +}; diff --git a/dist/index.d.ts b/dist/index.d.ts index 7d1647ed..3024582b 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -37,7 +37,7 @@ export { SwapQuoteType } from "./swap/types"; export { SwapQuoteError }; export { SwapQuoteErrorType } from "./util/error/SwapQuoteError"; export { redeemExcessAsset, redeemAllExcessAsset, generateRedeemTxns, REDEEM_PROCESS_TXN_COUNT } from "./redeem"; -export { FolksLendingPool, fetchFolksLendingPool } from "./folks-lending-pools"; +export { fetchFolksLendingPool, LendingPool, FolksLendingPool } from "./folks-lending-pools"; export { prepareCommitTransactions, getStakingAppID } from "./stake"; export { tinymanJSSDKConfig } from "./config"; export { combineAndRegroupSignerTxns } from "./util/transaction/transactionUtils"; diff --git a/dist/index.js b/dist/index.js index 72d2bae2..a136af0f 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,k=1e5,P=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=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Rt=xt("swap"),Ft=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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,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=[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],h=[u[0].pool.address,u[1].pool.address],x=h[0],v=h[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:[x,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(Pt[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(Pt[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=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=wt(n.network,C.V1_1),a=x(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+k*(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)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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=xe(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,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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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}(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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Rn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.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,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=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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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,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,mt(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 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,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,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=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===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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function Zr(){return(Zr=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new Hr(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $r(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function ta(){return(ta=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=$r({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=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.FolksLendingPool=Hr,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 ta.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=he,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=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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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,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=[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(Ut(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===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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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(Pt[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(Pt[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=Ut(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=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=wt(n.network,C.V1_1),a=x(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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(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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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 Ur=(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 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,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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr={testnet:548587153,mainnet:NaN};function Zr(){return(Zr=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Hr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),e.abrupt("return",t.assignGroupID(E));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var $r=Object.freeze({__proto__:null,generateTxns:function(t){return Zr.apply(this,arguments)}});function ta(){return(ta=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.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.lendingAsset1,c=n.lendingAsset2,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Hr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.appId),t.encodeUint64(c.appId)],accounts:[m],foreignAssets:[s.asset1ID,s.asset2ID,u.id,c.id],foreignApps:[u.appId,c.appId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.id,c.id],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,e.abrupt("return",t.assignGroupID([A,I,x]));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}}),na=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function ra(){return(ra=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new na(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var aa={AddLiquidity:$r,RemoveLiquidity:ea};function sa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function oa(){return(oa=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=sa({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=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.FolksLendingPool=na,exports.LendingPool=aa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 oa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,exports.tinymanJSSDKConfig=Tt; diff --git a/src/folks-lending-pools/add-liquidity/index.ts b/src/folks-lending-pools/add-liquidity/index.ts new file mode 100644 index 00000000..f61f48a2 --- /dev/null +++ b/src/folks-lending-pools/add-liquidity/index.ts @@ -0,0 +1,110 @@ +import algosdk, { + ALGORAND_MIN_TX_FEE, + Algodv2, + decodeAddress, + encodeUint64 +} from "algosdk"; + +import {SupportedNetwork} from "../../util/commonTypes"; +import {isAlgo, prepareAssetPairData} from "../../util/asset/assetUtils"; +import {FOLKS_WRAPPER_APP_ID} from "../constants"; +import {encodeString} from "../../util/util"; +import {FolksLendingAssetInfo} from "../types"; +import {CONTRACT_VERSION} from "../../contract/constants"; +import {getValidatorAppID} from "../../validator"; + +export async function generateTxns({ + client, + network, + poolAddress, + poolTokenId, + lendingManagerId, + asset1In, + asset2In, + initiatorAddr, + shouldOptInToPoolToken +}: { + client: Algodv2; + network: SupportedNetwork; + poolAddress: string; + poolTokenId: number; + lendingManagerId: number; + asset1In: FolksLendingAssetInfo; + asset2In: FolksLendingAssetInfo; + initiatorAddr: string; + shouldOptInToPoolToken: boolean; +}) { + const wrapperAppAddress = algosdk.getApplicationAddress(FOLKS_WRAPPER_APP_ID[network]); + const suggestedParams = await client.getTransactionParams().do(); + const [asset1, asset2] = prepareAssetPairData(asset1In, asset2In); + const isAlgoPool = isAlgo(asset2.id); + + const asset1InTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ + from: initiatorAddr, + to: wrapperAppAddress, + assetIndex: asset1.id, + amount: asset1.amount, + suggestedParams + }); + + const asset2InTxn = isAlgoPool + ? algosdk.makePaymentTxnWithSuggestedParamsFromObject({ + from: initiatorAddr, + to: wrapperAppAddress, + amount: asset2.amount, + suggestedParams + }) + : algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ + from: initiatorAddr, + to: wrapperAppAddress, + assetIndex: asset2.id, + amount: asset2.amount, + suggestedParams + }); + + const appCallTxn1 = algosdk.makeApplicationNoOpTxnFromObject({ + from: initiatorAddr, + appIndex: FOLKS_WRAPPER_APP_ID[network], + appArgs: [ + encodeString("add_liquidity"), + decodeAddress(poolAddress).publicKey, + encodeUint64(asset1.lendingAppId), + encodeUint64(asset2.lendingAppId) + ], + foreignAssets: [asset1.id, asset2.id, asset1.fAssetId, asset2.fAssetId], + foreignApps: [asset1.lendingAppId, asset2.lendingAppId, lendingManagerId], + accounts: [poolAddress], + suggestedParams + }); + + appCallTxn1.fee = ALGORAND_MIN_TX_FEE * 16; + + const validatorAppID = getValidatorAppID(network, CONTRACT_VERSION.V2); + const appCallTxn2 = algosdk.makeApplicationNoOpTxnFromObject({ + from: initiatorAddr, + appIndex: FOLKS_WRAPPER_APP_ID[network], + appArgs: [encodeString("noop")], + foreignAssets: [poolTokenId], + foreignApps: [validatorAppID], + accounts: [poolAddress], + suggestedParams + }); + + appCallTxn2.fee = ALGORAND_MIN_TX_FEE; + + let txnGroup = [asset1InTxn, asset2InTxn, appCallTxn1, appCallTxn2]; + + if (shouldOptInToPoolToken) { + const optInTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ + from: initiatorAddr, + to: initiatorAddr, + assetIndex: poolTokenId, + amount: 0, + suggestedParams + }); + + txnGroup.unshift(optInTxn); + } + + return algosdk.assignGroupID(txnGroup); +} diff --git a/src/folks-lending-pools/constants.ts b/src/folks-lending-pools/constants.ts index 24b68320..349171c2 100644 --- a/src/folks-lending-pools/constants.ts +++ b/src/folks-lending-pools/constants.ts @@ -1,6 +1,14 @@ +import {SupportedNetwork} from "../util/commonTypes"; + // eslint-disable-next-line no-magic-numbers const SECONDS_IN_YEAR = 365 * 24 * 60 * 60; const ONE_14_DP = 1e14; const ONE_16_DP = 1e16; -export {SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP}; +const FOLKS_WRAPPER_APP_ID: Record = { + testnet: 548587153, + // TODO: Update mainnet vault app id when it is available + mainnet: NaN +}; + +export {SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID}; diff --git a/src/folks-lending-pools/index.ts b/src/folks-lending-pools/index.ts index 9175e48d..ade7d8c4 100644 --- a/src/folks-lending-pools/index.ts +++ b/src/folks-lending-pools/index.ts @@ -2,8 +2,10 @@ import algosdk from "algosdk"; import {parseState} from "./utils"; import {ONE_14_DP, ONE_16_DP, SECONDS_IN_YEAR} from "./constants"; +import * as AddLiquidity from "./add-liquidity"; +import * as RemoveLiquidity from "./remove-liquidity"; -class FolksLendingPool { +export class FolksLendingPool { escrowAddress: string; // eslint-disable-next-line max-params @@ -60,7 +62,7 @@ class FolksLendingPool { /** * Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. */ -async function fetchFolksLendingPool( +export async function fetchFolksLendingPool( algod: algosdk.Algodv2, appId: number ): Promise { @@ -92,4 +94,4 @@ async function fetchFolksLendingPool( ); } -export {fetchFolksLendingPool, FolksLendingPool}; +export const LendingPool = {AddLiquidity, RemoveLiquidity}; diff --git a/src/folks-lending-pools/remove-liquidity/index.ts b/src/folks-lending-pools/remove-liquidity/index.ts new file mode 100644 index 00000000..97d381b8 --- /dev/null +++ b/src/folks-lending-pools/remove-liquidity/index.ts @@ -0,0 +1,86 @@ +import algosdk, { + ALGORAND_MIN_TX_FEE, + Algodv2, + decodeAddress, + encodeUint64 +} from "algosdk"; + +import {V2PoolInfo} from "../../util/pool/poolTypes"; +import {FOLKS_WRAPPER_APP_ID} from "../constants"; +import {SupportedNetwork} from "../../util/commonTypes"; +import {encodeString} from "../../util/util"; +import {getValidatorAppID} from "../../validator"; +import {CONTRACT_VERSION} from "../../contract/constants"; + +export async function generateTxns({ + client, + pool, + poolTokenIn, + initiatorAddr, + lendingAsset1, + lendingAsset2, + lendingManagerId, + network +}: { + client: Algodv2; + pool: V2PoolInfo; + poolTokenIn: number | bigint; + initiatorAddr: string; + minAsset1Amount: number | bigint; + minAsset2Amount: number | bigint; + lendingAsset1: {id: number; appId: number}; + lendingAsset2: {id: number; appId: number}; + lendingManagerId: number; + slippage: number; + network: SupportedNetwork; +}) { + const wrapperAppAddress = algosdk.getApplicationAddress(FOLKS_WRAPPER_APP_ID[network]); + + const suggestedParams = await client.getTransactionParams().do(); + const poolAddress = pool.account.address(); + const poolTokenId = pool.poolTokenID; + + if (!poolTokenId) { + throw new Error("Pool token asset ID is missing"); + } + + const assetTransferTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ + from: initiatorAddr, + to: wrapperAppAddress, + assetIndex: poolTokenId, + amount: poolTokenIn, + suggestedParams + }); + + const appCallTxn1 = algosdk.makeApplicationNoOpTxnFromObject({ + from: initiatorAddr, + appIndex: FOLKS_WRAPPER_APP_ID[network], + appArgs: [ + encodeString("remove_liquidity"), + decodeAddress(poolAddress).publicKey, + encodeUint64(lendingAsset1.appId), + encodeUint64(lendingAsset2.appId) + ], + accounts: [poolAddress], + foreignAssets: [pool.asset1ID, pool.asset2ID, lendingAsset1.id, lendingAsset2.id], + foreignApps: [lendingAsset1.appId, lendingAsset2.appId, lendingManagerId], + suggestedParams + }); + + appCallTxn1.fee = 15 * ALGORAND_MIN_TX_FEE; + + const validatorAppID = getValidatorAppID(network, CONTRACT_VERSION.V2); + const appCallTxn2 = algosdk.makeApplicationNoOpTxnFromObject({ + from: initiatorAddr, + appIndex: FOLKS_WRAPPER_APP_ID[network], + appArgs: [encodeString("noop")], + accounts: [poolAddress], + foreignAssets: [poolTokenId, lendingAsset1.id, lendingAsset2.id], + foreignApps: [validatorAppID], + suggestedParams + }); + + appCallTxn2.fee = ALGORAND_MIN_TX_FEE; + + return algosdk.assignGroupID([assetTransferTxn, appCallTxn1, appCallTxn2]); +} diff --git a/src/folks-lending-pools/types.ts b/src/folks-lending-pools/types.ts new file mode 100644 index 00000000..2e7ae66a --- /dev/null +++ b/src/folks-lending-pools/types.ts @@ -0,0 +1,16 @@ +import {AssetWithIdAndAmount} from "../util/asset/assetModels"; + +export type FolksLendingAssetInfo = AssetWithIdAndAmount & { + fAssetId: number; + lendingAppId: number; +}; + +export interface FolksLendingPool { + appId: number; + managerAppId: number; + depositInterestRate: number; + depositInterestIndex: number; + updatedAt: Date; + originalAssetId: number; + fAssetId: number; +} diff --git a/src/index.ts b/src/index.ts index b3634c9e..4e416850 100644 --- a/src/index.ts +++ b/src/index.ts @@ -133,7 +133,11 @@ export { REDEEM_PROCESS_TXN_COUNT } from "./redeem"; -export {FolksLendingPool, fetchFolksLendingPool} from "./folks-lending-pools"; +export { + fetchFolksLendingPool, + LendingPool, + FolksLendingPool +} from "./folks-lending-pools"; export {prepareCommitTransactions, getStakingAppID} from "./stake"; export {tinymanJSSDKConfig} from "./config"; From 22da4e72c21a915b99b1e495b308b90034479d24 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Fri, 29 Dec 2023 17:35:48 +0300 Subject: [PATCH 06/23] fix(folks/add-liq): return txns in SignerTransaction[] type --- src/folks-lending-pools/add-liquidity/index.ts | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/folks-lending-pools/add-liquidity/index.ts b/src/folks-lending-pools/add-liquidity/index.ts index f61f48a2..3eb6c8ad 100644 --- a/src/folks-lending-pools/add-liquidity/index.ts +++ b/src/folks-lending-pools/add-liquidity/index.ts @@ -5,7 +5,7 @@ import algosdk, { encodeUint64 } from "algosdk"; -import {SupportedNetwork} from "../../util/commonTypes"; +import {SignerTransaction, SupportedNetwork} from "../../util/commonTypes"; import {isAlgo, prepareAssetPairData} from "../../util/asset/assetUtils"; import {FOLKS_WRAPPER_APP_ID} from "../constants"; import {encodeString} from "../../util/util"; @@ -33,7 +33,7 @@ export async function generateTxns({ asset2In: FolksLendingAssetInfo; initiatorAddr: string; shouldOptInToPoolToken: boolean; -}) { +}): Promise { const wrapperAppAddress = algosdk.getApplicationAddress(FOLKS_WRAPPER_APP_ID[network]); const suggestedParams = await client.getTransactionParams().do(); const [asset1, asset2] = prepareAssetPairData(asset1In, asset2In); @@ -92,7 +92,7 @@ export async function generateTxns({ appCallTxn2.fee = ALGORAND_MIN_TX_FEE; - let txnGroup = [asset1InTxn, asset2InTxn, appCallTxn1, appCallTxn2]; + let txns = [asset1InTxn, asset2InTxn, appCallTxn1, appCallTxn2]; if (shouldOptInToPoolToken) { const optInTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ @@ -103,8 +103,12 @@ export async function generateTxns({ suggestedParams }); - txnGroup.unshift(optInTxn); + txns.unshift(optInTxn); } - return algosdk.assignGroupID(txnGroup); + const txnGroup = algosdk.assignGroupID(txns); + + return txnGroup.map((txn) => { + return {txn, signers: [initiatorAddr]}; + }); } From b0eecdd0522450f03a987a983dfee345d0cee645 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Fri, 29 Dec 2023 17:39:16 +0300 Subject: [PATCH 07/23] fix(folks/add-liq): return txns in SignerTransaction[] type --- dist/folks-lending-pools/add-liquidity/index.d.ts | 6 +++--- dist/folks-lending-pools/types.d.ts | 9 +++++++++ dist/index.js | 2 +- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/dist/folks-lending-pools/add-liquidity/index.d.ts b/dist/folks-lending-pools/add-liquidity/index.d.ts index f8dc83e6..c6af8f75 100644 --- a/dist/folks-lending-pools/add-liquidity/index.d.ts +++ b/dist/folks-lending-pools/add-liquidity/index.d.ts @@ -1,5 +1,5 @@ -import algosdk, { Algodv2 } from "algosdk"; -import { SupportedNetwork } from "../../util/commonTypes"; +import { Algodv2 } from "algosdk"; +import { SignerTransaction, SupportedNetwork } from "../../util/commonTypes"; import { FolksLendingAssetInfo } from "../types"; export declare function generateTxns({ client, network, poolAddress, poolTokenId, lendingManagerId, asset1In, asset2In, initiatorAddr, shouldOptInToPoolToken }: { client: Algodv2; @@ -11,4 +11,4 @@ export declare function generateTxns({ client, network, poolAddress, poolTokenId asset2In: FolksLendingAssetInfo; initiatorAddr: string; shouldOptInToPoolToken: boolean; -}): Promise; +}): Promise; diff --git a/dist/folks-lending-pools/types.d.ts b/dist/folks-lending-pools/types.d.ts index d74c16ee..acd0e15c 100644 --- a/dist/folks-lending-pools/types.d.ts +++ b/dist/folks-lending-pools/types.d.ts @@ -3,3 +3,12 @@ export type FolksLendingAssetInfo = AssetWithIdAndAmount & { fAssetId: number; lendingAppId: number; }; +export interface FolksLendingPool { + appId: number; + managerAppId: number; + depositInterestRate: number; + depositInterestIndex: number; + updatedAt: Date; + originalAssetId: number; + fAssetId: number; +} diff --git a/dist/index.js b/dist/index.js index a136af0f..36d56747 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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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,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=[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(Ut(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===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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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(Pt[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(Pt[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=Ut(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=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=wt(n.network,C.V1_1),a=x(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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(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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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 Ur=(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 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,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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr={testnet:548587153,mainnet:NaN};function Zr(){return(Zr=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Hr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),e.abrupt("return",t.assignGroupID(E));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var $r=Object.freeze({__proto__:null,generateTxns:function(t){return Zr.apply(this,arguments)}});function ta(){return(ta=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.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.lendingAsset1,c=n.lendingAsset2,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Hr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.appId),t.encodeUint64(c.appId)],accounts:[m],foreignAssets:[s.asset1ID,s.asset2ID,u.id,c.id],foreignApps:[u.appId,c.appId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.id,c.id],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,e.abrupt("return",t.assignGroupID([A,I,x]));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}}),na=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function ra(){return(ra=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new na(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var aa={AddLiquidity:$r,RemoveLiquidity:ea};function sa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function oa(){return(oa=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=sa({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=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.FolksLendingPool=na,exports.LendingPool=aa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 oa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,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=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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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,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=[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(Ut(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===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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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(Pt[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(Pt[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=Ut(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=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=wt(n.network,C.V1_1),a=x(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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(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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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 Ur=(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 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,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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr={testnet:548587153,mainnet:NaN};function Zr(){return(Zr=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Hr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var $r=Object.freeze({__proto__:null,generateTxns:function(t){return Zr.apply(this,arguments)}});function ta(){return(ta=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.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.lendingAsset1,c=n.lendingAsset2,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Hr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.appId),t.encodeUint64(c.appId)],accounts:[m],foreignAssets:[s.asset1ID,s.asset2ID,u.id,c.id],foreignApps:[u.appId,c.appId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.id,c.id],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,e.abrupt("return",t.assignGroupID([A,I,x]));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}}),na=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function ra(){return(ra=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new na(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var aa={AddLiquidity:$r,RemoveLiquidity:ea};function sa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function oa(){return(oa=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=sa({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=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.FolksLendingPool=na,exports.LendingPool=aa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 oa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,exports.tinymanJSSDKConfig=Tt; From 0e2e2bf4ddfca4a66c2daccca1dd24145f68242c Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Tue, 2 Jan 2024 15:08:38 +0300 Subject: [PATCH 08/23] fix(folks-lending/remove): Return promise in SignerTransaction array type --- dist/folks-lending-pools/remove-liquidity/index.d.ts | 6 +++--- dist/index.js | 2 +- src/folks-lending-pools/remove-liquidity/index.ts | 10 +++++++--- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/dist/folks-lending-pools/remove-liquidity/index.d.ts b/dist/folks-lending-pools/remove-liquidity/index.d.ts index 973a2b01..32fdcc19 100644 --- a/dist/folks-lending-pools/remove-liquidity/index.d.ts +++ b/dist/folks-lending-pools/remove-liquidity/index.d.ts @@ -1,6 +1,6 @@ -import algosdk, { Algodv2 } from "algosdk"; +import { Algodv2 } from "algosdk"; import { V2PoolInfo } from "../../util/pool/poolTypes"; -import { SupportedNetwork } from "../../util/commonTypes"; +import { SignerTransaction, SupportedNetwork } from "../../util/commonTypes"; export declare function generateTxns({ client, pool, poolTokenIn, initiatorAddr, lendingAsset1, lendingAsset2, lendingManagerId, network }: { client: Algodv2; pool: V2PoolInfo; @@ -19,4 +19,4 @@ export declare function generateTxns({ client, pool, poolTokenIn, initiatorAddr, lendingManagerId: number; slippage: number; network: SupportedNetwork; -}): Promise; +}): Promise; diff --git a/dist/index.js b/dist/index.js index 36d56747..97e64ec2 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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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,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=[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(Ut(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===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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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(Pt[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(Pt[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=Ut(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=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=wt(n.network,C.V1_1),a=x(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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(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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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 Ur=(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 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,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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr={testnet:548587153,mainnet:NaN};function Zr(){return(Zr=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Hr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var $r=Object.freeze({__proto__:null,generateTxns:function(t){return Zr.apply(this,arguments)}});function ta(){return(ta=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.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.lendingAsset1,c=n.lendingAsset2,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Hr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.appId),t.encodeUint64(c.appId)],accounts:[m],foreignAssets:[s.asset1ID,s.asset2ID,u.id,c.id],foreignApps:[u.appId,c.appId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.id,c.id],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,e.abrupt("return",t.assignGroupID([A,I,x]));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}}),na=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function ra(){return(ra=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new na(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var aa={AddLiquidity:$r,RemoveLiquidity:ea};function sa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function oa(){return(oa=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=sa({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=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.FolksLendingPool=na,exports.LendingPool=aa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 oa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,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=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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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,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=[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(Ut(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===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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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(Pt[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(Pt[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=Ut(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=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=wt(n.network,C.V1_1),a=x(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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(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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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 Ur=(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 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,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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr={testnet:548587153,mainnet:NaN};function Zr(){return(Zr=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Hr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var $r=Object.freeze({__proto__:null,generateTxns:function(t){return Zr.apply(this,arguments)}});function ta(){return(ta=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v;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.lendingAsset1,c=n.lendingAsset2,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Hr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.appId),t.encodeUint64(c.appId)],accounts:[m],foreignAssets:[s.asset1ID,s.asset2ID,u.id,c.id],foreignApps:[u.appId,c.appId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.id,c.id],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,v=t.assignGroupID([A,I,x]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}}),na=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function ra(){return(ra=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new na(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var aa={AddLiquidity:$r,RemoveLiquidity:ea};function sa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function oa(){return(oa=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=sa({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=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.FolksLendingPool=na,exports.LendingPool=aa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 oa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,exports.tinymanJSSDKConfig=Tt; diff --git a/src/folks-lending-pools/remove-liquidity/index.ts b/src/folks-lending-pools/remove-liquidity/index.ts index 97d381b8..8b8b20a4 100644 --- a/src/folks-lending-pools/remove-liquidity/index.ts +++ b/src/folks-lending-pools/remove-liquidity/index.ts @@ -7,7 +7,7 @@ import algosdk, { import {V2PoolInfo} from "../../util/pool/poolTypes"; import {FOLKS_WRAPPER_APP_ID} from "../constants"; -import {SupportedNetwork} from "../../util/commonTypes"; +import {SignerTransaction, SupportedNetwork} from "../../util/commonTypes"; import {encodeString} from "../../util/util"; import {getValidatorAppID} from "../../validator"; import {CONTRACT_VERSION} from "../../contract/constants"; @@ -33,7 +33,7 @@ export async function generateTxns({ lendingManagerId: number; slippage: number; network: SupportedNetwork; -}) { +}): Promise { const wrapperAppAddress = algosdk.getApplicationAddress(FOLKS_WRAPPER_APP_ID[network]); const suggestedParams = await client.getTransactionParams().do(); @@ -82,5 +82,9 @@ export async function generateTxns({ appCallTxn2.fee = ALGORAND_MIN_TX_FEE; - return algosdk.assignGroupID([assetTransferTxn, appCallTxn1, appCallTxn2]); + const txnGroup = algosdk.assignGroupID([assetTransferTxn, appCallTxn1, appCallTxn2]); + + return txnGroup.map((txn) => { + return {txn, signers: [initiatorAddr]}; + }); } From 6119d4c3de93e657e63cb370699c5fcbd51a31a1 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Tue, 2 Jan 2024 15:22:26 +0300 Subject: [PATCH 09/23] chore(lending-pool/remove): Remove unused params --- dist/folks-lending-pools/remove-liquidity/index.d.ts | 3 --- src/folks-lending-pools/remove-liquidity/index.ts | 3 --- 2 files changed, 6 deletions(-) diff --git a/dist/folks-lending-pools/remove-liquidity/index.d.ts b/dist/folks-lending-pools/remove-liquidity/index.d.ts index 32fdcc19..5190ab08 100644 --- a/dist/folks-lending-pools/remove-liquidity/index.d.ts +++ b/dist/folks-lending-pools/remove-liquidity/index.d.ts @@ -6,8 +6,6 @@ export declare function generateTxns({ client, pool, poolTokenIn, initiatorAddr, pool: V2PoolInfo; poolTokenIn: number | bigint; initiatorAddr: string; - minAsset1Amount: number | bigint; - minAsset2Amount: number | bigint; lendingAsset1: { id: number; appId: number; @@ -17,6 +15,5 @@ export declare function generateTxns({ client, pool, poolTokenIn, initiatorAddr, appId: number; }; lendingManagerId: number; - slippage: number; network: SupportedNetwork; }): Promise; diff --git a/src/folks-lending-pools/remove-liquidity/index.ts b/src/folks-lending-pools/remove-liquidity/index.ts index 8b8b20a4..e3ecf40b 100644 --- a/src/folks-lending-pools/remove-liquidity/index.ts +++ b/src/folks-lending-pools/remove-liquidity/index.ts @@ -26,12 +26,9 @@ export async function generateTxns({ pool: V2PoolInfo; poolTokenIn: number | bigint; initiatorAddr: string; - minAsset1Amount: number | bigint; - minAsset2Amount: number | bigint; lendingAsset1: {id: number; appId: number}; lendingAsset2: {id: number; appId: number}; lendingManagerId: number; - slippage: number; network: SupportedNetwork; }): Promise { const wrapperAppAddress = algosdk.getApplicationAddress(FOLKS_WRAPPER_APP_ID[network]); From fe1f2f5bec35460f72d692c6697f4a29309b04e4 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Tue, 2 Jan 2024 17:44:46 +0300 Subject: [PATCH 10/23] fix(folks-lending/remove): Get original asset ids as param as well --- .../remove-liquidity/index.d.ts | 15 +++++-------- dist/index.js | 2 +- .../remove-liquidity/index.ts | 21 ++++++++++--------- 3 files changed, 17 insertions(+), 21 deletions(-) diff --git a/dist/folks-lending-pools/remove-liquidity/index.d.ts b/dist/folks-lending-pools/remove-liquidity/index.d.ts index 5190ab08..77b4bd12 100644 --- a/dist/folks-lending-pools/remove-liquidity/index.d.ts +++ b/dist/folks-lending-pools/remove-liquidity/index.d.ts @@ -1,19 +1,14 @@ import { Algodv2 } from "algosdk"; import { V2PoolInfo } from "../../util/pool/poolTypes"; import { SignerTransaction, SupportedNetwork } from "../../util/commonTypes"; -export declare function generateTxns({ client, pool, poolTokenIn, initiatorAddr, lendingAsset1, lendingAsset2, lendingManagerId, network }: { +import { FolksLendingAssetInfo } from "../types"; +export declare function generateTxns({ client, pool, poolTokenIn, initiatorAddr, asset1Out, asset2Out, lendingManagerId, network }: { client: Algodv2; - pool: V2PoolInfo; + pool: Pick; poolTokenIn: number | bigint; initiatorAddr: string; - lendingAsset1: { - id: number; - appId: number; - }; - lendingAsset2: { - id: number; - appId: number; - }; + asset1Out: Omit; + asset2Out: Omit; lendingManagerId: number; network: SupportedNetwork; }): Promise; diff --git a/dist/index.js b/dist/index.js index 97e64ec2..4fd6cb84 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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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,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=[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(Ut(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===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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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(Pt[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(Pt[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=Ut(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=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=wt(n.network,C.V1_1),a=x(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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(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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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 Ur=(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 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,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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr={testnet:548587153,mainnet:NaN};function Zr(){return(Zr=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Hr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var $r=Object.freeze({__proto__:null,generateTxns:function(t){return Zr.apply(this,arguments)}});function ta(){return(ta=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v;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.lendingAsset1,c=n.lendingAsset2,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Hr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.appId),t.encodeUint64(c.appId)],accounts:[m],foreignAssets:[s.asset1ID,s.asset2ID,u.id,c.id],foreignApps:[u.appId,c.appId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.id,c.id],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,v=t.assignGroupID([A,I,x]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}}),na=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function ra(){return(ra=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new na(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var aa={AddLiquidity:$r,RemoveLiquidity:ea};function sa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function oa(){return(oa=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=sa({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=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.FolksLendingPool=na,exports.LendingPool=aa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 oa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,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=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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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,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=[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(Ut(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===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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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(Pt[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(Pt[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=Ut(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=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=wt(n.network,C.V1_1),a=x(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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(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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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 Ur=(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 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,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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr={testnet:548587153,mainnet:NaN};function Zr(){return(Zr=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Hr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var $r=Object.freeze({__proto__:null,generateTxns:function(t){return Zr.apply(this,arguments)}});function ta(){return(ta=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Hr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,v=t.assignGroupID([A,I,x]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}}),na=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function ra(){return(ra=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new na(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var aa={AddLiquidity:$r,RemoveLiquidity:ea};function sa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function oa(){return(oa=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=sa({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=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.FolksLendingPool=na,exports.LendingPool=aa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 oa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,exports.tinymanJSSDKConfig=Tt; diff --git a/src/folks-lending-pools/remove-liquidity/index.ts b/src/folks-lending-pools/remove-liquidity/index.ts index e3ecf40b..aac65db7 100644 --- a/src/folks-lending-pools/remove-liquidity/index.ts +++ b/src/folks-lending-pools/remove-liquidity/index.ts @@ -11,23 +11,24 @@ import {SignerTransaction, SupportedNetwork} from "../../util/commonTypes"; import {encodeString} from "../../util/util"; import {getValidatorAppID} from "../../validator"; import {CONTRACT_VERSION} from "../../contract/constants"; +import {FolksLendingAssetInfo} from "../types"; export async function generateTxns({ client, pool, poolTokenIn, initiatorAddr, - lendingAsset1, - lendingAsset2, + asset1Out, + asset2Out, lendingManagerId, network }: { client: Algodv2; - pool: V2PoolInfo; + pool: Pick; poolTokenIn: number | bigint; initiatorAddr: string; - lendingAsset1: {id: number; appId: number}; - lendingAsset2: {id: number; appId: number}; + asset1Out: Omit; + asset2Out: Omit; lendingManagerId: number; network: SupportedNetwork; }): Promise { @@ -55,12 +56,12 @@ export async function generateTxns({ appArgs: [ encodeString("remove_liquidity"), decodeAddress(poolAddress).publicKey, - encodeUint64(lendingAsset1.appId), - encodeUint64(lendingAsset2.appId) + encodeUint64(asset1Out.lendingAppId), + encodeUint64(asset2Out.lendingAppId) ], accounts: [poolAddress], - foreignAssets: [pool.asset1ID, pool.asset2ID, lendingAsset1.id, lendingAsset2.id], - foreignApps: [lendingAsset1.appId, lendingAsset2.appId, lendingManagerId], + foreignAssets: [asset1Out.id, asset2Out.id, asset1Out.fAssetId, asset2Out.fAssetId], + foreignApps: [asset1Out.lendingAppId, asset2Out.lendingAppId, lendingManagerId], suggestedParams }); @@ -72,7 +73,7 @@ export async function generateTxns({ appIndex: FOLKS_WRAPPER_APP_ID[network], appArgs: [encodeString("noop")], accounts: [poolAddress], - foreignAssets: [poolTokenId, lendingAsset1.id, lendingAsset2.id], + foreignAssets: [poolTokenId, asset1Out.fAssetId, asset2Out.fAssetId], foreignApps: [validatorAppID], suggestedParams }); From 461e89d843a13f090869005994f2ec0db2e184b2 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Tue, 2 Jan 2024 18:35:38 +0300 Subject: [PATCH 11/23] fix(folks-lending): Remove unused fields from FolksLendingPool class and set internal params as private --- dist/folks-lending-pools/index.d.ts | 16 ++++----------- dist/index.js | 2 +- src/folks-lending-pools/index.ts | 32 ++++------------------------- 3 files changed, 9 insertions(+), 41 deletions(-) diff --git a/dist/folks-lending-pools/index.d.ts b/dist/folks-lending-pools/index.d.ts index 2038aeef..2ab8ab46 100644 --- a/dist/folks-lending-pools/index.d.ts +++ b/dist/folks-lending-pools/index.d.ts @@ -4,25 +4,17 @@ import * as RemoveLiquidity from "./remove-liquidity"; export declare class FolksLendingPool { appId: number; managerAppId: number; - depositInterestRate: number; - depositInterestIndex: number; - updatedAt: Date; - originalAssetId: number; - fAssetId: number; + private depositInterestRate; + private depositInterestIndex; + private updatedAt; escrowAddress: string; - constructor(appId: number, managerAppId: number, depositInterestRate: number, depositInterestIndex: number, updatedAt: Date, originalAssetId: number, fAssetId: number); + constructor(appId: number, managerAppId: number, depositInterestRate: number, depositInterestIndex: number, updatedAt: Date); private calcDepositInterestIndex; private getLastTimestamp; /** * Calculates the amount fAsset received when adding liquidity with original asset. */ convertAddAmount(amount: number): number; - /** - * Calculates the amount original asset received according to fAsset amount when removing liquidity from lending pool. - */ - convertRemoveAmount(amount: number, options?: { - ceil?: boolean; - }): number; } /** * Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. diff --git a/dist/index.js b/dist/index.js index 4fd6cb84..f9b87388 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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="logic eval error:",q="exceeds schema integer count",G=/transaction \w+:/,W=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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,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=[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(Ut(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===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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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(Pt[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(Pt[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=Ut(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=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=wt(n.network,C.V1_1),a=x(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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,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=Z({stateArray:I}),x=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=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=Z({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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(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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=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 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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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 Ur=(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 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,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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e14,Kr=1e16,Hr={testnet:548587153,mainnet:NaN};function Zr(){return(Zr=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Hr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Hr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var $r=Object.freeze({__proto__:null,generateTxns:function(t){return Zr.apply(this,arguments)}});function ta(){return(ta=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Hr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Hr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,v=t.assignGroupID([A,I,x]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}}),na=function(){function e(n,r,a,s,o,u,c){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.originalAssetId=u,this.fAssetId=c,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Kr+this.depositInterestRate*e/31536e3)/Kr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(t*Yr/e)}},{key:"convertRemoveAmount",value:function(t){var e=arguments.length>1&&void 0!==arguments[1]?arguments[1]:{},n=t*this.calcDepositInterestIndex(this.getLastTimestamp())/Yr;return e.ceil?Math.ceil(n):Math.floor(n)}}]),e}();function ra(){return(ra=o(a().mark((function t(e,n){var 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 t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.a,"base64"),c=Number(u.readBigUInt64BE(0)),p=Number(u.readBigUInt64BE(8)),l=Buffer.from(o.i,"base64"),d=Number(l.readBigUInt64BE(32)),f=Number(l.readBigUInt64BE(40)),m=Number(l.readBigUInt64BE(48)),t.abrupt("return",new na(n,i,d,f,new Date(1e3*m),c,p));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var aa={AddLiquidity:$r,RemoveLiquidity:ea};function sa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function oa(){return(oa=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=sa({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=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.FolksLendingPool=na,exports.LendingPool=aa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 oa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=he,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=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,F=28500,R=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=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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,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=[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],h=[u[0].pool.address,u[1].pool.address],x=h[0],v=h[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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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=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=wt(n.network,C.V1_1),a=x(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)+F*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,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=Z({stateArray:I}),x=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=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=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 Re.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(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,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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e16,Kr={testnet:548587153,mainnet:NaN};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,h,v,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Kr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Kr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Kr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Zr=Object.freeze({__proto__:null,generateTxns:function(t){return Hr.apply(this,arguments)}});function $r(){return($r=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Kr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Kr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Kr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,v=t.assignGroupID([A,I,x]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ta=Object.freeze({__proto__:null,generateTxns:function(t){return $r.apply(this,arguments)}}),ea=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Yr+this.depositInterestRate*e/31536e3)/Yr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(1e14*t/e)}}]),e}();function na(){return(na=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=Number(u.readBigUInt64BE(32)),p=Number(u.readBigUInt64BE(40)),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new ea(n,i,c,p,new Date(1e3*l)));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ra={AddLiquidity:Zr,RemoveLiquidity:ta};function aa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function sa(){return(sa=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=aa({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=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.FolksLendingPool=ea,exports.LendingPool=ra,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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 sa.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=he,exports.tinymanJSSDKConfig=Tt; diff --git a/src/folks-lending-pools/index.ts b/src/folks-lending-pools/index.ts index ade7d8c4..46f098f4 100644 --- a/src/folks-lending-pools/index.ts +++ b/src/folks-lending-pools/index.ts @@ -12,11 +12,9 @@ export class FolksLendingPool { constructor( public appId: number, public managerAppId: number, - public depositInterestRate: number, - public depositInterestIndex: number, - public updatedAt: Date, - public originalAssetId: number, - public fAssetId: number + private depositInterestRate: number, + private depositInterestIndex: number, + private updatedAt: Date ) { this.escrowAddress = algosdk.getApplicationAddress(this.appId); } @@ -43,20 +41,6 @@ export class FolksLendingPool { return Math.floor((amount * ONE_14_DP) / interestIndex); } - - /** - * Calculates the amount original asset received according to fAsset amount when removing liquidity from lending pool. - */ - convertRemoveAmount(amount: number, options: {ceil?: boolean} = {}): number { - const interestIndex = this.calcDepositInterestIndex(this.getLastTimestamp()); - const converted = (amount * interestIndex) / ONE_14_DP; - - if (options.ceil) { - return Math.ceil(converted); - } - - return Math.floor(converted); - } } /** @@ -72,13 +56,7 @@ export async function fetchFolksLendingPool( const managerAppId = Number(Buffer.from(state.pm, "base64").readBigUInt64BE(0)); - const assetsIds = Buffer.from(state.a, "base64"); - - const originalAssetId = Number(assetsIds.readBigUInt64BE(0)); - const fAssetId = Number(assetsIds.readBigUInt64BE(8)); - const interestInfo = Buffer.from(state.i, "base64"); - const depositInterestRate = Number(interestInfo.readBigUInt64BE(32)); const depositInterestIndex = Number(interestInfo.readBigUInt64BE(40)); const updatedAt = Number(interestInfo.readBigUInt64BE(48)); @@ -88,9 +66,7 @@ export async function fetchFolksLendingPool( managerAppId, depositInterestRate, depositInterestIndex, - new Date(updatedAt * 1000), - originalAssetId, - fAssetId + new Date(updatedAt * 1000) ); } From 315c1d9f205e0dc85340d4b5e23f03d8cc79ded1 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Wed, 3 Jan 2024 21:29:05 +0300 Subject: [PATCH 12/23] fix(folks-lending): If the lastUpdate is available return it as the lastTimestamp --- dist/folks-lending-pools/constants.d.ts | 4 ++-- dist/folks-lending-pools/index.d.ts | 4 ++-- dist/index.js | 2 +- src/folks-lending-pools/constants.ts | 4 ++-- src/folks-lending-pools/index.ts | 17 +++++++++-------- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/dist/folks-lending-pools/constants.d.ts b/dist/folks-lending-pools/constants.d.ts index 5b6ec907..a7c15d0b 100644 --- a/dist/folks-lending-pools/constants.d.ts +++ b/dist/folks-lending-pools/constants.d.ts @@ -1,6 +1,6 @@ import { SupportedNetwork } from "../util/commonTypes"; -declare const SECONDS_IN_YEAR: number; +declare const YEAR_IN_SECONDS: number; declare const ONE_14_DP = 100000000000000; declare const ONE_16_DP = 10000000000000000; declare const FOLKS_WRAPPER_APP_ID: Record; -export { SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID }; +export { YEAR_IN_SECONDS, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID }; diff --git a/dist/folks-lending-pools/index.d.ts b/dist/folks-lending-pools/index.d.ts index 2ab8ab46..c4f37897 100644 --- a/dist/folks-lending-pools/index.d.ts +++ b/dist/folks-lending-pools/index.d.ts @@ -6,9 +6,9 @@ export declare class FolksLendingPool { managerAppId: number; private depositInterestRate; private depositInterestIndex; - private updatedAt; + private lastUpdate; escrowAddress: string; - constructor(appId: number, managerAppId: number, depositInterestRate: number, depositInterestIndex: number, updatedAt: Date); + constructor(appId: number, managerAppId: number, depositInterestRate: number, depositInterestIndex: number, lastUpdate: number); private calcDepositInterestIndex; private getLastTimestamp; /** diff --git a/dist/index.js b/dist/index.js index f9b87388..1610ad00 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,F=28500,R=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=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 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=x(ft(e),2),s=a[0],o=a[1],i=x(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 ht(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 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 xt("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,Ft=xt("swap"),Rt=(p(Dt={},Ot.FixedInput,xt("fixed-input")),p(Dt,Ot.FixedOutput,xt("fixed-output")),Dt),Bt={ASSET_OPT_IN:xt("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,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=[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],h=[u[0].pool.address,u[1].pool.address],x=h[0],v=h[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:[Ft,Rt[i],t.encodeUint64(y)],foreignApps:[wt(o,C.V2)],foreignAssets:[d,f,m],accounts:[x,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=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=wt(n.network,C.V1_1),a=x(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)+F*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,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=Z({stateArray:I}),x=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=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=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 Re.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(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,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=J(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=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:[xt("bootstrap"),t.encodeUint64(f),t.encodeUint64(A)],foreignAssets:h?[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(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=Y(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,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,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=wt(s,C.V2),l=t.getApplicationAddress(p),d=J(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=K(g),(v=t.makeApplicationOptInTxnFromObject({from:I,appIndex:p,appArgs:[xt("bootstrap")],note:Tt.getAppCallTxnNoteWithClientName(C.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+F*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,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=Y(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 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 hn,xn,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(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=xt("add_liquidity"),Fn=(p(Tn={},C.V1_1,[xt("mint")]),p(Tn,C.V2,{INITIAL_LIQUIDITY:[xt("add_initial_liquidity")],SINGLE_ASSET_MODE:[Ln,xt("single")],FLEXIBLE_MODE:[Ln,xt("flexible")]}),Tn);function Rn(){return(Rn=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=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:Fn.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}),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,mt(n,[o]);case 8:return p=t.sent,l=x(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,(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 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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 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: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,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=ct("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: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]=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,mt(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={},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=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 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=[xt("swap"),o===Ot.FixedInput?xt("fi"):xt("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}),h=o===Ot.FixedOutput?ct("positive",i,p.assetInAmount):p.assetInAmount,x=l===j?t.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:h,suggestedParams:g}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:h,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,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,mt(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=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,mt(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 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,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: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:Fr};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===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=x(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=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: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=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 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:[xt("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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e16,Kr={testnet:548587153,mainnet:NaN};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,h,v,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Kr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=x(g,2),I=A[0],h=A[1],v=K(h.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:h.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:h.id,amount:h.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Kr[s],appArgs:[xt("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(h.lendingAppId)],foreignAssets:[I.id,h.id,I.fAssetId,h.fAssetId],foreignApps:[I.lendingAppId,h.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Kr[s],appArgs:[xt("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Zr=Object.freeze({__proto__:null,generateTxns:function(t){return Hr.apply(this,arguments)}});function $r(){return($r=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,h,x,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Kr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Kr[l],appArgs:[xt("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,h=wt(l,C.V2),(x=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Kr[l],appArgs:[xt("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[h],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,v=t.assignGroupID([A,I,x]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ta=Object.freeze({__proto__:null,generateTxns:function(t){return $r.apply(this,arguments)}}),ea=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.updatedAt=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=Math.floor(t-Math.floor(this.updatedAt.getTime()/1e3));return Math.floor(this.depositInterestIndex*Math.floor(Yr+this.depositInterestRate*e/31536e3)/Yr)}},{key:"getLastTimestamp",value:function(){return Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(1e14*t/e)}}]),e}();function na(){return(na=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=Number(u.readBigUInt64BE(32)),p=Number(u.readBigUInt64BE(40)),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new ea(n,i,c,p,new Date(1e3*l)));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ra={AddLiquidity:Zr,RemoveLiquidity:ta};function aa(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:$([xt("tinymanStaking/v1:b"),c,t.encodeUint64(s),u])})}function sa(){return(sa=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=aa({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=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.FolksLendingPool=ea,exports.LendingPool=ra,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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 sa.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=he,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,F=28500,R=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"},U="- would result negative",Q="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,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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=Ut(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)+F*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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e16,Kr={testnet:548587153,mainnet:NaN};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,x,v,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Kr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=h(g,2),I=A[0],x=A[1],v=K(x.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:x.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:x.id,amount:x.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Kr[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(x.lendingAppId)],foreignAssets:[I.id,x.id,I.fAssetId,x.fAssetId],foreignApps:[I.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Kr[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Zr=Object.freeze({__proto__:null,generateTxns:function(t){return Hr.apply(this,arguments)}});function $r(){return($r=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Kr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Kr[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,x=wt(l,C.V2),(h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Kr[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ta=Object.freeze({__proto__:null,generateTxns:function(t){return $r.apply(this,arguments)}}),ea=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=t-this.lastUpdate;return Math.floor(this.depositInterestIndex*Math.floor(Yr+this.depositInterestRate*e/31536e3)/Yr)}},{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(1e14*t/e)}}]),e}();function na(){return(na=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=Number(u.readBigUInt64BE(32)),p=Number(u.readBigUInt64BE(40)),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new ea(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ra={AddLiquidity:Zr,RemoveLiquidity:ta};function aa(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 sa(){return(sa=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=aa({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.FolksLendingPool=ea,exports.LendingPool=ra,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 sa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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/folks-lending-pools/constants.ts b/src/folks-lending-pools/constants.ts index 349171c2..08922cd0 100644 --- a/src/folks-lending-pools/constants.ts +++ b/src/folks-lending-pools/constants.ts @@ -1,7 +1,7 @@ import {SupportedNetwork} from "../util/commonTypes"; // eslint-disable-next-line no-magic-numbers -const SECONDS_IN_YEAR = 365 * 24 * 60 * 60; +const YEAR_IN_SECONDS = 365 * 24 * 60 * 60; const ONE_14_DP = 1e14; const ONE_16_DP = 1e16; @@ -11,4 +11,4 @@ const FOLKS_WRAPPER_APP_ID: Record = { mainnet: NaN }; -export {SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID}; +export {YEAR_IN_SECONDS, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID}; diff --git a/src/folks-lending-pools/index.ts b/src/folks-lending-pools/index.ts index 46f098f4..5daa9d3d 100644 --- a/src/folks-lending-pools/index.ts +++ b/src/folks-lending-pools/index.ts @@ -1,36 +1,37 @@ import algosdk from "algosdk"; import {parseState} from "./utils"; -import {ONE_14_DP, ONE_16_DP, SECONDS_IN_YEAR} from "./constants"; +import {ONE_14_DP, ONE_16_DP, YEAR_IN_SECONDS} from "./constants"; import * as AddLiquidity from "./add-liquidity"; import * as RemoveLiquidity from "./remove-liquidity"; export class FolksLendingPool { escrowAddress: string; - // eslint-disable-next-line max-params constructor( public appId: number, public managerAppId: number, private depositInterestRate: number, private depositInterestIndex: number, - private updatedAt: Date + private lastUpdate: number ) { this.escrowAddress = algosdk.getApplicationAddress(this.appId); } private calcDepositInterestIndex(timestamp: number): number { - const dt = Math.floor(timestamp - Math.floor(this.updatedAt.getTime() / 1000)); + const timestampDelta = timestamp - this.lastUpdate; return Math.floor( (this.depositInterestIndex * - Math.floor(ONE_16_DP + (this.depositInterestRate * dt) / SECONDS_IN_YEAR)) / + Math.floor( + ONE_16_DP + (this.depositInterestRate * timestampDelta) / YEAR_IN_SECONDS + )) / ONE_16_DP ); } private getLastTimestamp(): number { - return Math.floor(new Date().getTime() / 1000); + return this.lastUpdate ?? Math.floor(new Date().getTime() / 1000); } /** @@ -59,14 +60,14 @@ export async function fetchFolksLendingPool( const interestInfo = Buffer.from(state.i, "base64"); const depositInterestRate = Number(interestInfo.readBigUInt64BE(32)); const depositInterestIndex = Number(interestInfo.readBigUInt64BE(40)); - const updatedAt = Number(interestInfo.readBigUInt64BE(48)); + const lastUpdate = Number(interestInfo.readBigUInt64BE(48)); return new FolksLendingPool( appId, managerAppId, depositInterestRate, depositInterestIndex, - new Date(updatedAt * 1000) + lastUpdate ); } From 4fa921dc3bd6dc8c084e0d06aa74600d2a4918f7 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Thu, 4 Jan 2024 00:59:35 +0300 Subject: [PATCH 13/23] feat(folks-lending-pool): Implement calculateDepositReturn and calculateWithdrawReturn functions --- dist/folks-lending-pools/constants.d.ts | 8 +-- dist/folks-lending-pools/index.d.ts | 10 ++-- dist/folks-lending-pools/utils.d.ts | 2 + dist/index.js | 2 +- src/folks-lending-pools/constants.ts | 8 +-- src/folks-lending-pools/index.ts | 49 +++++++++++-------- .../remove-liquidity/index.ts | 2 - src/folks-lending-pools/utils.ts | 8 +++ 8 files changed, 55 insertions(+), 34 deletions(-) diff --git a/dist/folks-lending-pools/constants.d.ts b/dist/folks-lending-pools/constants.d.ts index a7c15d0b..d71ea5e5 100644 --- a/dist/folks-lending-pools/constants.d.ts +++ b/dist/folks-lending-pools/constants.d.ts @@ -1,6 +1,6 @@ import { SupportedNetwork } from "../util/commonTypes"; -declare const YEAR_IN_SECONDS: number; -declare const ONE_14_DP = 100000000000000; -declare const ONE_16_DP = 10000000000000000; +declare const SECONDS_IN_YEAR: bigint; +declare const ONE_14_DP: bigint; +declare const ONE_16_DP: bigint; declare const FOLKS_WRAPPER_APP_ID: Record; -export { YEAR_IN_SECONDS, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID }; +export { SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID }; diff --git a/dist/folks-lending-pools/index.d.ts b/dist/folks-lending-pools/index.d.ts index c4f37897..84c74605 100644 --- a/dist/folks-lending-pools/index.d.ts +++ b/dist/folks-lending-pools/index.d.ts @@ -8,13 +8,17 @@ export declare class FolksLendingPool { private depositInterestIndex; private lastUpdate; escrowAddress: string; - constructor(appId: number, managerAppId: number, depositInterestRate: number, depositInterestIndex: number, lastUpdate: number); - private calcDepositInterestIndex; + constructor(appId: number, managerAppId: number, depositInterestRate: bigint, depositInterestIndex: bigint, lastUpdate: number); private getLastTimestamp; + private getDepositInterestIndex; /** * Calculates the amount fAsset received when adding liquidity with original asset. */ - convertAddAmount(amount: number): number; + calculateDepositReturn(depositAmount: number): bigint; + /** + * Calculates the amount original asset received when removing liquidity from fAsset pool. + */ + calculateWithdrawReturn(withdrawAmount: number): bigint; } /** * Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. diff --git a/dist/folks-lending-pools/utils.d.ts b/dist/folks-lending-pools/utils.d.ts index f098c6e3..7f06ccfa 100644 --- a/dist/folks-lending-pools/utils.d.ts +++ b/dist/folks-lending-pools/utils.d.ts @@ -9,3 +9,5 @@ * @returns Key value dictionary parsed from the argument. */ export declare function parseState(kv: any): any; +export declare function mulScale(n1: bigint, n2: bigint, scale: bigint): bigint; +export declare function divScale(n1: bigint, n2: bigint, scale: bigint): bigint; diff --git a/dist/index.js b/dist/index.js index 1610ad00..8de61bfe 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,F=28500,R=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"},U="- would result negative",Q="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,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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=Ut(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)+F*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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}var Yr=1e16,Kr={testnet:548587153,mainnet:NaN};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,x,v,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress(Kr[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=h(g,2),I=A[0],x=A[1],v=K(x.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:x.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:x.id,amount:x.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Kr[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(x.lendingAppId)],foreignAssets:[I.id,x.id,I.fAssetId,x.fAssetId],foreignApps:[I.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:Kr[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Zr=Object.freeze({__proto__:null,generateTxns:function(t){return Hr.apply(this,arguments)}});function $r(){return($r=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress(Kr[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Kr[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,x=wt(l,C.V2),(h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:Kr[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE,v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 17:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ta=Object.freeze({__proto__:null,generateTxns:function(t){return $r.apply(this,arguments)}}),ea=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"calcDepositInterestIndex",value:function(t){var e=t-this.lastUpdate;return Math.floor(this.depositInterestIndex*Math.floor(Yr+this.depositInterestRate*e/31536e3)/Yr)}},{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"convertAddAmount",value:function(t){var e=this.calcDepositInterestIndex(this.getLastTimestamp());return Math.floor(1e14*t/e)}}]),e}();function na(){return(na=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=Number(u.readBigUInt64BE(32)),p=Number(u.readBigUInt64BE(40)),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new ea(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ra={AddLiquidity:Zr,RemoveLiquidity:ta};function aa(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 sa(){return(sa=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=aa({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.FolksLendingPool=ea,exports.LendingPool=ra,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 sa.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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(Pt[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(Pt[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=Ut(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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=kn(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=kn(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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:NaN};function ta(){return(ta=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=h(g,2),I=A[0],x=A[1],v=K(x.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:x.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:x.id,amount:x.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(x.lendingAppId)],foreignAssets:[I.id,x.id,I.fAssetId,x.fAssetId],foreignApps:[I.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}});function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ra=Object.freeze({__proto__:null,generateTxns:function(t){return na.apply(this,arguments)}}),aa=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"getDepositInterestIndex",value:function(){var t=BigInt(Math.floor((new Date).getTime()/1e3)-this.getLastTimestamp());return Yr(this.depositInterestIndex,Zr+this.depositInterestRate*t/Kr,Zr)}},{key:"calculateDepositReturn",value:function(t){var e=this.getDepositInterestIndex();return BigInt(t)*Hr/e}},{key:"calculateWithdrawReturn",value:function(t){var e=this.getDepositInterestIndex();return Yr(BigInt(t),e,Hr)}}]),e}();function sa(){return(sa=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new aa(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var oa={AddLiquidity:ea,RemoveLiquidity:ra};function ia(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 ua(){return(ua=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=ia({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.FolksLendingPool=aa,exports.LendingPool=oa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 ua.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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/folks-lending-pools/constants.ts b/src/folks-lending-pools/constants.ts index 08922cd0..3a0f8d8d 100644 --- a/src/folks-lending-pools/constants.ts +++ b/src/folks-lending-pools/constants.ts @@ -1,9 +1,9 @@ import {SupportedNetwork} from "../util/commonTypes"; // eslint-disable-next-line no-magic-numbers -const YEAR_IN_SECONDS = 365 * 24 * 60 * 60; -const ONE_14_DP = 1e14; -const ONE_16_DP = 1e16; +const SECONDS_IN_YEAR = BigInt(365 * 24 * 60 * 60); +const ONE_14_DP = BigInt(1e14); +const ONE_16_DP = BigInt(1e16); const FOLKS_WRAPPER_APP_ID: Record = { testnet: 548587153, @@ -11,4 +11,4 @@ const FOLKS_WRAPPER_APP_ID: Record = { mainnet: NaN }; -export {YEAR_IN_SECONDS, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID}; +export {SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID}; diff --git a/src/folks-lending-pools/index.ts b/src/folks-lending-pools/index.ts index 5daa9d3d..0e8d2d34 100644 --- a/src/folks-lending-pools/index.ts +++ b/src/folks-lending-pools/index.ts @@ -1,7 +1,7 @@ import algosdk from "algosdk"; -import {parseState} from "./utils"; -import {ONE_14_DP, ONE_16_DP, YEAR_IN_SECONDS} from "./constants"; +import {divScale, mulScale, parseState} from "./utils"; +import {ONE_14_DP, ONE_16_DP, SECONDS_IN_YEAR} from "./constants"; import * as AddLiquidity from "./add-liquidity"; import * as RemoveLiquidity from "./remove-liquidity"; @@ -11,36 +11,45 @@ export class FolksLendingPool { constructor( public appId: number, public managerAppId: number, - private depositInterestRate: number, - private depositInterestIndex: number, + private depositInterestRate: bigint, + private depositInterestIndex: bigint, private lastUpdate: number ) { this.escrowAddress = algosdk.getApplicationAddress(this.appId); } - private calcDepositInterestIndex(timestamp: number): number { - const timestampDelta = timestamp - this.lastUpdate; + private getLastTimestamp(): number { + return this.lastUpdate ?? Math.floor(new Date().getTime() / 1000); + } - return Math.floor( - (this.depositInterestIndex * - Math.floor( - ONE_16_DP + (this.depositInterestRate * timestampDelta) / YEAR_IN_SECONDS - )) / - ONE_16_DP + private getDepositInterestIndex() { + const timestampDelta = BigInt( + Math.floor(new Date().getTime() / 1000) - this.getLastTimestamp() ); - } - private getLastTimestamp(): number { - return this.lastUpdate ?? Math.floor(new Date().getTime() / 1000); + return mulScale( + this.depositInterestIndex, + ONE_16_DP + (this.depositInterestRate * timestampDelta) / SECONDS_IN_YEAR, + ONE_16_DP + ); } /** * Calculates the amount fAsset received when adding liquidity with original asset. */ - convertAddAmount(amount: number): number { - const interestIndex = this.calcDepositInterestIndex(this.getLastTimestamp()); + calculateDepositReturn(depositAmount: number) { + const depositInterestIndex = this.getDepositInterestIndex(); + + return divScale(BigInt(depositAmount), depositInterestIndex, ONE_14_DP); + } + + /** + * Calculates the amount original asset received when removing liquidity from fAsset pool. + */ + calculateWithdrawReturn(withdrawAmount: number) { + const depositInterestIndex = this.getDepositInterestIndex(); - return Math.floor((amount * ONE_14_DP) / interestIndex); + return mulScale(BigInt(withdrawAmount), depositInterestIndex, ONE_14_DP); } } @@ -58,8 +67,8 @@ export async function fetchFolksLendingPool( const managerAppId = Number(Buffer.from(state.pm, "base64").readBigUInt64BE(0)); const interestInfo = Buffer.from(state.i, "base64"); - const depositInterestRate = Number(interestInfo.readBigUInt64BE(32)); - const depositInterestIndex = Number(interestInfo.readBigUInt64BE(40)); + const depositInterestRate = interestInfo.readBigUInt64BE(32); + const depositInterestIndex = interestInfo.readBigUInt64BE(40); const lastUpdate = Number(interestInfo.readBigUInt64BE(48)); return new FolksLendingPool( diff --git a/src/folks-lending-pools/remove-liquidity/index.ts b/src/folks-lending-pools/remove-liquidity/index.ts index aac65db7..072077f7 100644 --- a/src/folks-lending-pools/remove-liquidity/index.ts +++ b/src/folks-lending-pools/remove-liquidity/index.ts @@ -78,8 +78,6 @@ export async function generateTxns({ suggestedParams }); - appCallTxn2.fee = ALGORAND_MIN_TX_FEE; - const txnGroup = algosdk.assignGroupID([assetTransferTxn, appCallTxn1, appCallTxn2]); return txnGroup.map((txn) => { diff --git a/src/folks-lending-pools/utils.ts b/src/folks-lending-pools/utils.ts index 20163323..550bf5b5 100644 --- a/src/folks-lending-pools/utils.ts +++ b/src/folks-lending-pools/utils.ts @@ -25,3 +25,11 @@ export function parseState(kv: any) { } return res; } + +export function mulScale(n1: bigint, n2: bigint, scale: bigint): bigint { + return (n1 * n2) / scale; +} + +export function divScale(n1: bigint, n2: bigint, scale: bigint): bigint { + return (n1 * scale) / n2; +} From bf133a994176ae6857880f17ef64652f11e663e4 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Thu, 4 Jan 2024 19:12:42 +0300 Subject: [PATCH 14/23] feat(lending-pools): Calculate total fee for lending pool operations --- .../add-liquidity/index.d.ts | 4 +- dist/folks-lending-pools/constants.d.ts | 3 +- .../remove-liquidity/index.d.ts | 1 + dist/index.js | 2 +- .../add-liquidity/index.ts | 40 +++++++++---------- src/folks-lending-pools/constants.ts | 10 ++++- .../remove-liquidity/index.ts | 13 +++++- 7 files changed, 44 insertions(+), 29 deletions(-) diff --git a/dist/folks-lending-pools/add-liquidity/index.d.ts b/dist/folks-lending-pools/add-liquidity/index.d.ts index c6af8f75..15b0f716 100644 --- a/dist/folks-lending-pools/add-liquidity/index.d.ts +++ b/dist/folks-lending-pools/add-liquidity/index.d.ts @@ -1,7 +1,7 @@ import { Algodv2 } from "algosdk"; import { SignerTransaction, SupportedNetwork } from "../../util/commonTypes"; import { FolksLendingAssetInfo } from "../types"; -export declare function generateTxns({ client, network, poolAddress, poolTokenId, lendingManagerId, asset1In, asset2In, initiatorAddr, shouldOptInToPoolToken }: { +export declare function generateTxns({ client, network, poolAddress, poolTokenId, lendingManagerId, asset1In, asset2In, initiatorAddr }: { client: Algodv2; network: SupportedNetwork; poolAddress: string; @@ -10,5 +10,5 @@ export declare function generateTxns({ client, network, poolAddress, poolTokenId asset1In: FolksLendingAssetInfo; asset2In: FolksLendingAssetInfo; initiatorAddr: string; - shouldOptInToPoolToken: boolean; }): Promise; +export declare function getAddLiquidityTotalFee(): number; diff --git a/dist/folks-lending-pools/constants.d.ts b/dist/folks-lending-pools/constants.d.ts index d71ea5e5..a5b8149a 100644 --- a/dist/folks-lending-pools/constants.d.ts +++ b/dist/folks-lending-pools/constants.d.ts @@ -3,4 +3,5 @@ declare const SECONDS_IN_YEAR: bigint; declare const ONE_14_DP: bigint; declare const ONE_16_DP: bigint; declare const FOLKS_WRAPPER_APP_ID: Record; -export { SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID }; +declare const FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT = 14; +export { SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID, FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT }; diff --git a/dist/folks-lending-pools/remove-liquidity/index.d.ts b/dist/folks-lending-pools/remove-liquidity/index.d.ts index 77b4bd12..8192e37b 100644 --- a/dist/folks-lending-pools/remove-liquidity/index.d.ts +++ b/dist/folks-lending-pools/remove-liquidity/index.d.ts @@ -12,3 +12,4 @@ export declare function generateTxns({ client, pool, poolTokenIn, initiatorAddr, lendingManagerId: number; network: SupportedNetwork; }): Promise; +export declare function getRemoveLiquidityTotalFee(): number; diff --git a/dist/index.js b/dist/index.js index 8de61bfe..dd9a66b0 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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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(Pt[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(Pt[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=Ut(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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=kn(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=kn(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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:NaN};function ta(){return(ta=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=n.shouldOptInToPoolToken,f=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return m=e.sent,g=J(c,p),A=h(g,2),I=A[0],x=A[1],v=K(x.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:I.id,amount:I.amount,suggestedParams:m}),y=v?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:f,amount:x.amount,suggestedParams:m}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:f,assetIndex:x.id,amount:x.amount,suggestedParams:m}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(I.lendingAppId),t.encodeUint64(x.lendingAppId)],foreignAssets:[I.id,x.id,I.fAssetId,x.fAssetId],foreignApps:[I.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:m})).fee=16*t.ALGORAND_MIN_TX_FEE,_=wt(s,C.V2),(b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:m})).fee=t.ALGORAND_MIN_TX_FEE,E=[T,y,w,b],d&&(D=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:l,assetIndex:i,amount:0,suggestedParams:m}),E.unshift(D)),S=t.assignGroupID(E),e.abrupt("return",S.map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ea=Object.freeze({__proto__:null,generateTxns:function(t){return ta.apply(this,arguments)}});function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=15*t.ALGORAND_MIN_TX_FEE,x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ra=Object.freeze({__proto__:null,generateTxns:function(t){return na.apply(this,arguments)}}),aa=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"getDepositInterestIndex",value:function(){var t=BigInt(Math.floor((new Date).getTime()/1e3)-this.getLastTimestamp());return Yr(this.depositInterestIndex,Zr+this.depositInterestRate*t/Kr,Zr)}},{key:"calculateDepositReturn",value:function(t){var e=this.getDepositInterestIndex();return BigInt(t)*Hr/e}},{key:"calculateWithdrawReturn",value:function(t){var e=this.getDepositInterestIndex();return Yr(BigInt(t),e,Hr)}}]),e}();function sa(){return(sa=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new aa(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var oa={AddLiquidity:ea,RemoveLiquidity:ra};function ia(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 ua(){return(ua=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=ia({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.FolksLendingPool=aa,exports.LendingPool=oa,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 ua.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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(Pt[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(Pt[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=Ut(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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=kn(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=kn(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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:NaN},ta=14;function ea(){return(ea=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=J(c,p),g=h(m,2),A=g[0],I=g[1],x=K(I.id),v=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),T=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(y=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),w=wt(s,C.V2),_=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[w],accounts:[o],suggestedParams:f}),b=t.assignGroupID([v,T,y,_]),e.abrupt("return",b.map((function(t){return{txn:t,signers:[l]}})));case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var na=Object.freeze({__proto__:null,generateTxns:function(t){return ea.apply(this,arguments)},getAddLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(4+ta)}});function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}}),sa=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"getDepositInterestIndex",value:function(){var t=BigInt(Math.floor((new Date).getTime()/1e3)-this.getLastTimestamp());return Yr(this.depositInterestIndex,Zr+this.depositInterestRate*t/Kr,Zr)}},{key:"calculateDepositReturn",value:function(t){var e=this.getDepositInterestIndex();return BigInt(t)*Hr/e}},{key:"calculateWithdrawReturn",value:function(t){var e=this.getDepositInterestIndex();return Yr(BigInt(t),e,Hr)}}]),e}();function oa(){return(oa=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new sa(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ia={AddLiquidity:na,RemoveLiquidity:aa};function ua(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 ca(){return(ca=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=ua({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.FolksLendingPool=sa,exports.LendingPool=ia,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 ca.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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/folks-lending-pools/add-liquidity/index.ts b/src/folks-lending-pools/add-liquidity/index.ts index 3eb6c8ad..33051960 100644 --- a/src/folks-lending-pools/add-liquidity/index.ts +++ b/src/folks-lending-pools/add-liquidity/index.ts @@ -7,7 +7,10 @@ import algosdk, { import {SignerTransaction, SupportedNetwork} from "../../util/commonTypes"; import {isAlgo, prepareAssetPairData} from "../../util/asset/assetUtils"; -import {FOLKS_WRAPPER_APP_ID} from "../constants"; +import { + FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT, + FOLKS_WRAPPER_APP_ID +} from "../constants"; import {encodeString} from "../../util/util"; import {FolksLendingAssetInfo} from "../types"; import {CONTRACT_VERSION} from "../../contract/constants"; @@ -21,8 +24,7 @@ export async function generateTxns({ lendingManagerId, asset1In, asset2In, - initiatorAddr, - shouldOptInToPoolToken + initiatorAddr }: { client: Algodv2; network: SupportedNetwork; @@ -32,7 +34,6 @@ export async function generateTxns({ asset1In: FolksLendingAssetInfo; asset2In: FolksLendingAssetInfo; initiatorAddr: string; - shouldOptInToPoolToken: boolean; }): Promise { const wrapperAppAddress = algosdk.getApplicationAddress(FOLKS_WRAPPER_APP_ID[network]); const suggestedParams = await client.getTransactionParams().do(); @@ -77,7 +78,8 @@ export async function generateTxns({ suggestedParams }); - appCallTxn1.fee = ALGORAND_MIN_TX_FEE * 16; + appCallTxn1.fee = + ALGORAND_MIN_TX_FEE * (FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT + 1); const validatorAppID = getValidatorAppID(network, CONTRACT_VERSION.V2); const appCallTxn2 = algosdk.makeApplicationNoOpTxnFromObject({ @@ -90,25 +92,19 @@ export async function generateTxns({ suggestedParams }); - appCallTxn2.fee = ALGORAND_MIN_TX_FEE; - - let txns = [asset1InTxn, asset2InTxn, appCallTxn1, appCallTxn2]; - - if (shouldOptInToPoolToken) { - const optInTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ - from: initiatorAddr, - to: initiatorAddr, - assetIndex: poolTokenId, - amount: 0, - suggestedParams - }); - - txns.unshift(optInTxn); - } - - const txnGroup = algosdk.assignGroupID(txns); + const txnGroup = algosdk.assignGroupID([ + asset1InTxn, + asset2InTxn, + appCallTxn1, + appCallTxn2 + ]); return txnGroup.map((txn) => { return {txn, signers: [initiatorAddr]}; }); } + +export function getAddLiquidityTotalFee() { + // 1 asset transfer txn, 1 payment/asset transfer txn, 1 app call txn and 1 app call txn with inner txns + return ALGORAND_MIN_TX_FEE * (4 + FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT); +} diff --git a/src/folks-lending-pools/constants.ts b/src/folks-lending-pools/constants.ts index 3a0f8d8d..10581e60 100644 --- a/src/folks-lending-pools/constants.ts +++ b/src/folks-lending-pools/constants.ts @@ -11,4 +11,12 @@ const FOLKS_WRAPPER_APP_ID: Record = { mainnet: NaN }; -export {SECONDS_IN_YEAR, ONE_14_DP, ONE_16_DP, FOLKS_WRAPPER_APP_ID}; +const FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT = 14; + +export { + SECONDS_IN_YEAR, + ONE_14_DP, + ONE_16_DP, + FOLKS_WRAPPER_APP_ID, + FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT +}; diff --git a/src/folks-lending-pools/remove-liquidity/index.ts b/src/folks-lending-pools/remove-liquidity/index.ts index 072077f7..b2bcd465 100644 --- a/src/folks-lending-pools/remove-liquidity/index.ts +++ b/src/folks-lending-pools/remove-liquidity/index.ts @@ -6,7 +6,10 @@ import algosdk, { } from "algosdk"; import {V2PoolInfo} from "../../util/pool/poolTypes"; -import {FOLKS_WRAPPER_APP_ID} from "../constants"; +import { + FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT, + FOLKS_WRAPPER_APP_ID +} from "../constants"; import {SignerTransaction, SupportedNetwork} from "../../util/commonTypes"; import {encodeString} from "../../util/util"; import {getValidatorAppID} from "../../validator"; @@ -65,7 +68,8 @@ export async function generateTxns({ suggestedParams }); - appCallTxn1.fee = 15 * ALGORAND_MIN_TX_FEE; + appCallTxn1.fee = + ALGORAND_MIN_TX_FEE * (FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT + 1); const validatorAppID = getValidatorAppID(network, CONTRACT_VERSION.V2); const appCallTxn2 = algosdk.makeApplicationNoOpTxnFromObject({ @@ -84,3 +88,8 @@ export async function generateTxns({ return {txn, signers: [initiatorAddr]}; }); } + +export function getRemoveLiquidityTotalFee() { + // 1 asset transfer txn, 1 app call txn and 1 app call txn with inner txns + return ALGORAND_MIN_TX_FEE * (3 + FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT); +} From db608b5f90a9441a2d657ccdb7976533e6061891 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 8 Jan 2024 11:01:55 +0300 Subject: [PATCH 15/23] feat(lending-add-liquidity): Add wrapper app asset opt-in txns to the generateTxns function --- .../add-liquidity/utils.d.ts | 7 +++ dist/index.js | 2 +- .../add-liquidity/index.ts | 43 ++++++++++++++++--- .../add-liquidity/utils.ts | 26 +++++++++++ 4 files changed, 70 insertions(+), 8 deletions(-) create mode 100644 dist/folks-lending-pools/add-liquidity/utils.d.ts create mode 100644 src/folks-lending-pools/add-liquidity/utils.ts diff --git a/dist/folks-lending-pools/add-liquidity/utils.d.ts b/dist/folks-lending-pools/add-liquidity/utils.d.ts new file mode 100644 index 00000000..db834e41 --- /dev/null +++ b/dist/folks-lending-pools/add-liquidity/utils.d.ts @@ -0,0 +1,7 @@ +import { Algodv2 } from "algosdk"; +import { SupportedNetwork } from "../../util/commonTypes"; +export declare function getFolksWrapperAppOptInRequiredAssetIDs({ client, network, assetIDs }: { + client: Algodv2; + network: SupportedNetwork; + assetIDs: number[]; +}): Promise; diff --git a/dist/index.js b/dist/index.js index dd9a66b0..6d2c4b6a 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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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(Pt[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(Pt[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=Ut(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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=kn(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=kn(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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:NaN},ta=14;function ea(){return(ea=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;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=J(c,p),g=h(m,2),A=g[0],I=g[1],x=K(I.id),v=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),T=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(y=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),w=wt(s,C.V2),_=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[w],accounts:[o],suggestedParams:f}),b=t.assignGroupID([v,T,y,_]),e.abrupt("return",b.map((function(t){return{txn:t,signers:[l]}})));case 15:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var na=Object.freeze({__proto__:null,generateTxns:function(t){return ea.apply(this,arguments)},getAddLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(4+ta)}});function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}}),sa=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"getDepositInterestIndex",value:function(){var t=BigInt(Math.floor((new Date).getTime()/1e3)-this.getLastTimestamp());return Yr(this.depositInterestIndex,Zr+this.depositInterestRate*t/Kr,Zr)}},{key:"calculateDepositReturn",value:function(t){var e=this.getDepositInterestIndex();return BigInt(t)*Hr/e}},{key:"calculateWithdrawReturn",value:function(t){var e=this.getDepositInterestIndex();return Yr(BigInt(t),e,Hr)}}]),e}();function oa(){return(oa=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new sa(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ia={AddLiquidity:na,RemoveLiquidity:aa};function ua(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 ca(){return(ca=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=ua({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.FolksLendingPool=sa,exports.LendingPool=ia,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 ca.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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(Pt[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(Pt[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=Ut(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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=kn(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=kn(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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:NaN},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=J(c,p),g=h(m,2),A=g[0],I=g[1],x=K(I.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),y=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),_=wt(s,C.V2),b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:f}),E=[T,y,w,b],e.next=16,ea({client:r,network:s,assetIDs:[A.id,I.id,A.fAssetId,I.fAssetId,i]});case 16:return(D=e.sent).length&&((S=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*D.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(D.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(D),suggestedParams:f})])[1].fee=(D.length+1)*t.ALGORAND_MIN_TX_FEE,E.unshift.apply(E,S)),e.abrupt("return",t.assignGroupID(E).map((function(t){return{txn:t,signers:[l]}})));case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(4+ta)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}}),ia=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"getDepositInterestIndex",value:function(){var t=BigInt(Math.floor((new Date).getTime()/1e3)-this.getLastTimestamp());return Yr(this.depositInterestIndex,Zr+this.depositInterestRate*t/Kr,Zr)}},{key:"calculateDepositReturn",value:function(t){var e=this.getDepositInterestIndex();return BigInt(t)*Hr/e}},{key:"calculateWithdrawReturn",value:function(t){var e=this.getDepositInterestIndex();return Yr(BigInt(t),e,Hr)}}]),e}();function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new ia(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:aa,RemoveLiquidity:oa};function pa(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 la(){return(la=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=pa({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.FolksLendingPool=ia,exports.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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/folks-lending-pools/add-liquidity/index.ts b/src/folks-lending-pools/add-liquidity/index.ts index 33051960..4bfde143 100644 --- a/src/folks-lending-pools/add-liquidity/index.ts +++ b/src/folks-lending-pools/add-liquidity/index.ts @@ -15,6 +15,8 @@ import {encodeString} from "../../util/util"; import {FolksLendingAssetInfo} from "../types"; import {CONTRACT_VERSION} from "../../contract/constants"; import {getValidatorAppID} from "../../validator"; +import {getFolksWrapperAppOptInRequiredAssetIDs} from "./utils"; +import {MINIMUM_BALANCE_REQUIRED_PER_ASSET} from "../../util/constant"; export async function generateTxns({ client, @@ -92,14 +94,41 @@ export async function generateTxns({ suggestedParams }); - const txnGroup = algosdk.assignGroupID([ - asset1InTxn, - asset2InTxn, - appCallTxn1, - appCallTxn2 - ]); + let txns = [asset1InTxn, asset2InTxn, appCallTxn1, appCallTxn2]; - return txnGroup.map((txn) => { + const optInRequiredAssetIds = await getFolksWrapperAppOptInRequiredAssetIDs({ + client, + network, + assetIDs: [asset1.id, asset2.id, asset1.fAssetId, asset2.fAssetId, poolTokenId] + }); + + if (optInRequiredAssetIds.length) { + const wrapperAppAssetOptInTxns = [ + algosdk.makePaymentTxnWithSuggestedParamsFromObject({ + from: initiatorAddr, + to: wrapperAppAddress, + amount: MINIMUM_BALANCE_REQUIRED_PER_ASSET * optInRequiredAssetIds.length, + suggestedParams + }), + algosdk.makeApplicationNoOpTxnFromObject({ + from: initiatorAddr, + appIndex: FOLKS_WRAPPER_APP_ID[network], + appArgs: [ + encodeString("asset_optin"), + ...optInRequiredAssetIds.map((assetId) => encodeUint64(assetId)) + ], + foreignAssets: [...optInRequiredAssetIds], + suggestedParams + }) + ]; + + wrapperAppAssetOptInTxns[1].fee = + (optInRequiredAssetIds.length + 1) * ALGORAND_MIN_TX_FEE; + + txns.unshift(...wrapperAppAssetOptInTxns); + } + + return algosdk.assignGroupID(txns).map((txn) => { return {txn, signers: [initiatorAddr]}; }); } diff --git a/src/folks-lending-pools/add-liquidity/utils.ts b/src/folks-lending-pools/add-liquidity/utils.ts new file mode 100644 index 00000000..e7d24a1b --- /dev/null +++ b/src/folks-lending-pools/add-liquidity/utils.ts @@ -0,0 +1,26 @@ +import {Algodv2, getApplicationAddress} from "algosdk"; + +import {SupportedNetwork} from "../../util/commonTypes"; +import {FOLKS_WRAPPER_APP_ID} from "../constants"; +import {getAccountInformation} from "../../util/account/accountUtils"; +import {ALGO_ASSET_ID} from "../../util/asset/assetConstants"; + +export async function getFolksWrapperAppOptInRequiredAssetIDs({ + client, + network, + assetIDs +}: { + client: Algodv2; + network: SupportedNetwork; + assetIDs: number[]; +}) { + const wrapperAppAddress = getApplicationAddress(FOLKS_WRAPPER_APP_ID[network]); + const appOptedInAssetIDs = ( + await getAccountInformation(client, wrapperAppAddress) + ).assets.map((asset) => asset["asset-id"]); + + return assetIDs.filter( + (assetID: number) => + assetID !== ALGO_ASSET_ID && !appOptedInAssetIDs.includes(assetID) + ); +} From 44f1c6ff6d295ce3458a55eab6656957104d76cf Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 8 Jan 2024 16:53:26 +0300 Subject: [PATCH 16/23] feat(folks-lending/constants): Add mainnet app id --- src/folks-lending-pools/constants.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/folks-lending-pools/constants.ts b/src/folks-lending-pools/constants.ts index 10581e60..c8ced171 100644 --- a/src/folks-lending-pools/constants.ts +++ b/src/folks-lending-pools/constants.ts @@ -7,8 +7,7 @@ const ONE_16_DP = BigInt(1e16); const FOLKS_WRAPPER_APP_ID: Record = { testnet: 548587153, - // TODO: Update mainnet vault app id when it is available - mainnet: NaN + mainnet: 1385499515 }; const FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT = 14; From 2a4cf095bcf71a23a2731783636bfa7d3d238b44 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 8 Jan 2024 17:36:45 +0300 Subject: [PATCH 17/23] chore(index): Get build --- dist/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dist/index.js b/dist/index.js index 6d2c4b6a..0038fc83 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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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(Pt[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(Pt[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=Ut(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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=kn(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=kn(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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:NaN},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=J(c,p),g=h(m,2),A=g[0],I=g[1],x=K(I.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),y=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),_=wt(s,C.V2),b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:f}),E=[T,y,w,b],e.next=16,ea({client:r,network:s,assetIDs:[A.id,I.id,A.fAssetId,I.fAssetId,i]});case 16:return(D=e.sent).length&&((S=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*D.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(D.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(D),suggestedParams:f})])[1].fee=(D.length+1)*t.ALGORAND_MIN_TX_FEE,E.unshift.apply(E,S)),e.abrupt("return",t.assignGroupID(E).map((function(t){return{txn:t,signers:[l]}})));case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(4+ta)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}}),ia=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"getDepositInterestIndex",value:function(){var t=BigInt(Math.floor((new Date).getTime()/1e3)-this.getLastTimestamp());return Yr(this.depositInterestIndex,Zr+this.depositInterestRate*t/Kr,Zr)}},{key:"calculateDepositReturn",value:function(t){var e=this.getDepositInterestIndex();return BigInt(t)*Hr/e}},{key:"calculateWithdrawReturn",value:function(t){var e=this.getDepositInterestIndex();return Yr(BigInt(t),e,Hr)}}]),e}();function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new ia(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:aa,RemoveLiquidity:oa};function pa(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 la(){return(la=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=pa({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.FolksLendingPool=ia,exports.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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(Pt[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(Pt[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=Ut(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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=kn(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=kn(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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:1385499515},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=J(c,p),g=h(m,2),A=g[0],I=g[1],x=K(I.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),y=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),_=wt(s,C.V2),b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:f}),E=[T,y,w,b],e.next=16,ea({client:r,network:s,assetIDs:[A.id,I.id,A.fAssetId,I.fAssetId,i]});case 16:return(D=e.sent).length&&((S=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*D.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(D.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(D),suggestedParams:f})])[1].fee=(D.length+1)*t.ALGORAND_MIN_TX_FEE,E.unshift.apply(E,S)),e.abrupt("return",t.assignGroupID(E).map((function(t){return{txn:t,signers:[l]}})));case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(4+ta)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}}),ia=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"getDepositInterestIndex",value:function(){var t=BigInt(Math.floor((new Date).getTime()/1e3)-this.getLastTimestamp());return Yr(this.depositInterestIndex,Zr+this.depositInterestRate*t/Kr,Zr)}},{key:"calculateDepositReturn",value:function(t){var e=this.getDepositInterestIndex();return BigInt(t)*Hr/e}},{key:"calculateWithdrawReturn",value:function(t){var e=this.getDepositInterestIndex();return Yr(BigInt(t),e,Hr)}}]),e}();function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new ia(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:aa,RemoveLiquidity:oa};function pa(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 la(){return(la=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=pa({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.FolksLendingPool=ia,exports.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=mt,exports.sumUpTxnFees=At,exports.tinymanContract_v1_1=ge,exports.tinymanContract_v2=xe,exports.tinymanJSSDKConfig=Tt; From 0bbb7638175111e7231aeaa66d4da9b9e67b70c7 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Tue, 9 Jan 2024 14:14:52 +0300 Subject: [PATCH 18/23] fix(lending-pools): Convert lending pool info and the helper functions into functional components, remove the class component --- .../add-liquidity/index.d.ts | 2 +- dist/folks-lending-pools/index.d.ts | 63 +++++++----- dist/folks-lending-pools/types.d.ts | 8 +- dist/index.d.ts | 3 +- dist/index.js | 2 +- .../add-liquidity/index.ts | 10 +- src/folks-lending-pools/index.ts | 95 ++++++++++--------- src/folks-lending-pools/types.ts | 8 +- src/index.ts | 7 +- 9 files changed, 112 insertions(+), 86 deletions(-) diff --git a/dist/folks-lending-pools/add-liquidity/index.d.ts b/dist/folks-lending-pools/add-liquidity/index.d.ts index 15b0f716..59a73ee7 100644 --- a/dist/folks-lending-pools/add-liquidity/index.d.ts +++ b/dist/folks-lending-pools/add-liquidity/index.d.ts @@ -11,4 +11,4 @@ export declare function generateTxns({ client, network, poolAddress, poolTokenId asset2In: FolksLendingAssetInfo; initiatorAddr: string; }): Promise; -export declare function getAddLiquidityTotalFee(): number; +export declare function getAddLiquidityTotalFee(wrapperAppOptInRequiredAssetIdCount?: number): number; diff --git a/dist/folks-lending-pools/index.d.ts b/dist/folks-lending-pools/index.d.ts index 84c74605..1d474eb7 100644 --- a/dist/folks-lending-pools/index.d.ts +++ b/dist/folks-lending-pools/index.d.ts @@ -1,30 +1,47 @@ import algosdk from "algosdk"; -import * as AddLiquidity from "./add-liquidity"; -import * as RemoveLiquidity from "./remove-liquidity"; -export declare class FolksLendingPool { - appId: number; - managerAppId: number; - private depositInterestRate; - private depositInterestIndex; - private lastUpdate; - escrowAddress: string; - constructor(appId: number, managerAppId: number, depositInterestRate: bigint, depositInterestIndex: bigint, lastUpdate: number); - private getLastTimestamp; - private getDepositInterestIndex; - /** - * Calculates the amount fAsset received when adding liquidity with original asset. - */ - calculateDepositReturn(depositAmount: number): bigint; - /** - * Calculates the amount original asset received when removing liquidity from fAsset pool. - */ - calculateWithdrawReturn(withdrawAmount: number): bigint; -} +import { getFolksWrapperAppOptInRequiredAssetIDs } from "./add-liquidity/utils"; +import { FolksLendingPool } from "./types"; +/** + * Calculates the amount fAsset received when adding liquidity with original asset. + */ +declare function calculateDepositReturn(depositAmount: number, depositInterestIndex: bigint, depositInterestRate: bigint): bigint; +/** + * Calculates the amount original asset received when removing liquidity from fAsset pool. + */ +declare function calculateWithdrawReturn(withdrawAmount: number, depositInterestIndex: bigint, depositInterestRate: bigint): bigint; /** * Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. */ export declare function fetchFolksLendingPool(algod: algosdk.Algodv2, appId: number): Promise; export declare const LendingPool: { - AddLiquidity: typeof AddLiquidity; - RemoveLiquidity: typeof RemoveLiquidity; + AddLiquidity: { + calculateDepositReturn: typeof calculateDepositReturn; + generateTxns({ client, network, poolAddress, poolTokenId, lendingManagerId, asset1In, asset2In, initiatorAddr }: { + client: algosdk.Algodv2; + network: import("..").SupportedNetwork; + poolAddress: string; + poolTokenId: number; + lendingManagerId: number; + asset1In: import("./types").FolksLendingAssetInfo; + asset2In: import("./types").FolksLendingAssetInfo; + initiatorAddr: string; + }): Promise; + getAddLiquidityTotalFee(wrapperAppOptInRequiredAssetIdCount?: number | undefined): number; + }; + RemoveLiquidity: { + calculateWithdrawReturn: typeof calculateWithdrawReturn; + generateTxns({ client, pool, poolTokenIn, initiatorAddr, asset1Out, asset2Out, lendingManagerId, network }: { + client: algosdk.Algodv2; + pool: Pick; + poolTokenIn: number | bigint; + initiatorAddr: string; + asset1Out: Omit; + asset2Out: Omit; + lendingManagerId: number; + network: import("..").SupportedNetwork; + }): Promise; + getRemoveLiquidityTotalFee(): number; + }; + getFolksWrapperAppOptInRequiredAssetIDs: typeof getFolksWrapperAppOptInRequiredAssetIDs; }; +export {}; diff --git a/dist/folks-lending-pools/types.d.ts b/dist/folks-lending-pools/types.d.ts index acd0e15c..722b12f0 100644 --- a/dist/folks-lending-pools/types.d.ts +++ b/dist/folks-lending-pools/types.d.ts @@ -6,9 +6,7 @@ export type FolksLendingAssetInfo = AssetWithIdAndAmount & { export interface FolksLendingPool { appId: number; managerAppId: number; - depositInterestRate: number; - depositInterestIndex: number; - updatedAt: Date; - originalAssetId: number; - fAssetId: number; + depositInterestRate: bigint; + depositInterestIndex: bigint; + lastUpdate: number; } diff --git a/dist/index.d.ts b/dist/index.d.ts index 3024582b..f7750df9 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -37,7 +37,8 @@ export { SwapQuoteType } from "./swap/types"; export { SwapQuoteError }; export { SwapQuoteErrorType } from "./util/error/SwapQuoteError"; export { redeemExcessAsset, redeemAllExcessAsset, generateRedeemTxns, REDEEM_PROCESS_TXN_COUNT } from "./redeem"; -export { fetchFolksLendingPool, LendingPool, FolksLendingPool } from "./folks-lending-pools"; +export { fetchFolksLendingPool, LendingPool } from "./folks-lending-pools"; +export { FolksLendingPool } from "./folks-lending-pools/types"; export { prepareCommitTransactions, getStakingAppID } from "./stake"; export { tinymanJSSDKConfig } from "./config"; export { combineAndRegroupSignerTxns } from "./util/transaction/transactionUtils"; diff --git a/dist/index.js b/dist/index.js index 0038fc83..caf53a41 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,k=1e5,P=1e5,L=5e4,F=28500,R=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"},U="- would result negative",Q="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,kt=function(t){return t[t.INPUT_TXN=0]="INPUT_TXN",t[t.APP_CALL_TXN=1]="APP_CALL_TXN",t}({}),Pt=(p(Et={},Ot.FixedInput,1),p(Et,Ot.FixedOutput,2),Et),Lt=2,Ft=ht("swap"),Rt=(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=Qt(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 Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(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(Ut(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:[Ft,Rt[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(Pt[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(Pt[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=Ut(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+k*(t["created-apps"]||[]).length+N*(t["apps-local-state"]||[]).length+L*Number(e&&e["num-byte-slice"]||0)+F*Number(e&&e["num-uint"]||0)+P*(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);fPe)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Pe,"]): ").concat(P.issuedLiquidity));case 48:return n.abrupt("return",P);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 Re.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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Xe.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Ve.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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+F*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=kn(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=kn(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 kn(t,e){return t*BigInt(e)/(BigInt(1e4)-BigInt(e))}function Pn(e){return(Dn[e]+1)*t.ALGORAND_MIN_TX_FEE}var Ln=ht("add_liquidity"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 kr(t){return Pr.apply(this,arguments)}function Pr(){return(Pr=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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 Ur=(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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:1385499515},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=J(c,p),g=h(m,2),A=g[0],I=g[1],x=K(I.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),y=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),_=wt(s,C.V2),b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:f}),E=[T,y,w,b],e.next=16,ea({client:r,network:s,assetIDs:[A.id,I.id,A.fAssetId,I.fAssetId,i]});case 16:return(D=e.sent).length&&((S=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*D.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(D.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(D),suggestedParams:f})])[1].fee=(D.length+1)*t.ALGORAND_MIN_TX_FEE,E.unshift.apply(E,S)),e.abrupt("return",t.assignGroupID(E).map((function(t){return{txn:t,signers:[l]}})));case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(4+ta)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}}),ia=function(){function e(n,r,a,s,o){i(this,e),this.appId=n,this.managerAppId=r,this.depositInterestRate=a,this.depositInterestIndex=s,this.lastUpdate=o,this.escrowAddress=t.getApplicationAddress(this.appId)}return c(e,[{key:"getLastTimestamp",value:function(){var t;return null!==(t=this.lastUpdate)&&void 0!==t?t:Math.floor((new Date).getTime()/1e3)}},{key:"getDepositInterestIndex",value:function(){var t=BigInt(Math.floor((new Date).getTime()/1e3)-this.getLastTimestamp());return Yr(this.depositInterestIndex,Zr+this.depositInterestRate*t/Kr,Zr)}},{key:"calculateDepositReturn",value:function(t){var e=this.getDepositInterestIndex();return BigInt(t)*Hr/e}},{key:"calculateWithdrawReturn",value:function(t){var e=this.getDepositInterestIndex();return Yr(BigInt(t),e,Hr)}}]),e}();function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",new ia(n,i,c,p,l));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:aa,RemoveLiquidity:oa};function pa(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 la(){return(la=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=pa({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.FolksLendingPool=ia,exports.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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,F=28500,R=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,X={id:"".concat(j),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"},U="- would result negative",Q="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,Ft=ht("swap"),Rt=(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=Qt(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Xt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Vt(t){return t[t.length-1].quote.amount_out}function Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(t),assetOut:Vt(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(Ut(u).amount),Number(Vt(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),_=Xt(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:[Ft,Rt[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(Xt(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=Ut(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=Vt(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)+F*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 Re.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 Xe(){return(Xe=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 Ve(){return(Ve=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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Ve.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Xe.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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: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=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+F*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"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 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===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 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.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 Ur=(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 Xr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Vr.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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:1385499515},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=J(c,p),g=h(m,2),A=g[0],I=g[1],x=K(I.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),y=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),_=wt(s,C.V2),b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:f}),E=[T,y,w,b],e.next=16,ea({client:r,network:s,assetIDs:[A.id,I.id,A.fAssetId,I.fAssetId,i]});case 16:return(D=e.sent).length&&((S=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*D.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(D.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(D),suggestedParams:f})])[1].fee=(D.length+1)*t.ALGORAND_MIN_TX_FEE,E.unshift.apply(E,S)),e.abrupt("return",t.assignGroupID(E).map((function(t){return{txn:t,signers:[l]}})));case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(e){return t.ALGORAND_MIN_TX_FEE*(4+ta)+(e?(e+1)*t.ALGORAND_MIN_TX_FEE+e*O:0)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}});function ia(t,e){var n,r=BigInt(Math.floor((new Date).getTime()/1e3)-(null!=n?n:Math.floor((new Date).getTime()/1e3)));return Yr(t,Zr+e*r/Kr,Zr)}function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:r(r({},aa),{},{calculateDepositReturn:function(t,e,n){var r=ia(e,n);return BigInt(t)*Hr/r}}),RemoveLiquidity:r(r({},oa),{},{calculateWithdrawReturn:function(t,e,n){var r=ia(e,n);return Yr(BigInt(t),r,Hr)}}),getFolksWrapperAppOptInRequiredAssetIDs:ea};function pa(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 la(){return(la=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=pa({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=X,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.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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/folks-lending-pools/add-liquidity/index.ts b/src/folks-lending-pools/add-liquidity/index.ts index 4bfde143..4fc431f8 100644 --- a/src/folks-lending-pools/add-liquidity/index.ts +++ b/src/folks-lending-pools/add-liquidity/index.ts @@ -133,7 +133,13 @@ export async function generateTxns({ }); } -export function getAddLiquidityTotalFee() { +export function getAddLiquidityTotalFee(wrapperAppOptInRequiredAssetIdCount?: number) { // 1 asset transfer txn, 1 payment/asset transfer txn, 1 app call txn and 1 app call txn with inner txns - return ALGORAND_MIN_TX_FEE * (4 + FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT); + return ( + ALGORAND_MIN_TX_FEE * (4 + FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT) + + (wrapperAppOptInRequiredAssetIdCount + ? (wrapperAppOptInRequiredAssetIdCount + 1) * ALGORAND_MIN_TX_FEE + + wrapperAppOptInRequiredAssetIdCount * MINIMUM_BALANCE_REQUIRED_PER_ASSET + : 0) + ); } diff --git a/src/folks-lending-pools/index.ts b/src/folks-lending-pools/index.ts index 0e8d2d34..9bd6c564 100644 --- a/src/folks-lending-pools/index.ts +++ b/src/folks-lending-pools/index.ts @@ -4,53 +4,58 @@ import {divScale, mulScale, parseState} from "./utils"; import {ONE_14_DP, ONE_16_DP, SECONDS_IN_YEAR} from "./constants"; import * as AddLiquidity from "./add-liquidity"; import * as RemoveLiquidity from "./remove-liquidity"; +import {getFolksWrapperAppOptInRequiredAssetIDs} from "./add-liquidity/utils"; +import {FolksLendingPool} from "./types"; -export class FolksLendingPool { - escrowAddress: string; - - constructor( - public appId: number, - public managerAppId: number, - private depositInterestRate: bigint, - private depositInterestIndex: bigint, - private lastUpdate: number - ) { - this.escrowAddress = algosdk.getApplicationAddress(this.appId); - } - - private getLastTimestamp(): number { - return this.lastUpdate ?? Math.floor(new Date().getTime() / 1000); - } +function getLatestDepositInterestIndex( + depositInterestIndex: bigint, + depositInterestRate: bigint +) { + const timestampDelta = BigInt( + Math.floor(new Date().getTime() / 1000) - getLastTimestamp() + ); - private getDepositInterestIndex() { - const timestampDelta = BigInt( - Math.floor(new Date().getTime() / 1000) - this.getLastTimestamp() - ); + return mulScale( + depositInterestIndex, + ONE_16_DP + (depositInterestRate * timestampDelta) / SECONDS_IN_YEAR, + ONE_16_DP + ); +} - return mulScale( - this.depositInterestIndex, - ONE_16_DP + (this.depositInterestRate * timestampDelta) / SECONDS_IN_YEAR, - ONE_16_DP - ); - } +function getLastTimestamp(lastUpdate?: number): number { + return lastUpdate ?? Math.floor(new Date().getTime() / 1000); +} - /** - * Calculates the amount fAsset received when adding liquidity with original asset. - */ - calculateDepositReturn(depositAmount: number) { - const depositInterestIndex = this.getDepositInterestIndex(); +/** + * Calculates the amount fAsset received when adding liquidity with original asset. + */ +function calculateDepositReturn( + depositAmount: number, + depositInterestIndex: bigint, + depositInterestRate: bigint +) { + const latestDepositInterestIndex = getLatestDepositInterestIndex( + depositInterestIndex, + depositInterestRate + ); - return divScale(BigInt(depositAmount), depositInterestIndex, ONE_14_DP); - } + return divScale(BigInt(depositAmount), latestDepositInterestIndex, ONE_14_DP); +} - /** - * Calculates the amount original asset received when removing liquidity from fAsset pool. - */ - calculateWithdrawReturn(withdrawAmount: number) { - const depositInterestIndex = this.getDepositInterestIndex(); +/** + * Calculates the amount original asset received when removing liquidity from fAsset pool. + */ +function calculateWithdrawReturn( + withdrawAmount: number, + depositInterestIndex: bigint, + depositInterestRate: bigint +) { + const latestDepositInterestIndex = getLatestDepositInterestIndex( + depositInterestIndex, + depositInterestRate + ); - return mulScale(BigInt(withdrawAmount), depositInterestIndex, ONE_14_DP); - } + return mulScale(BigInt(withdrawAmount), latestDepositInterestIndex, ONE_14_DP); } /** @@ -71,13 +76,17 @@ export async function fetchFolksLendingPool( const depositInterestIndex = interestInfo.readBigUInt64BE(40); const lastUpdate = Number(interestInfo.readBigUInt64BE(48)); - return new FolksLendingPool( + return { appId, managerAppId, depositInterestRate, depositInterestIndex, lastUpdate - ); + }; } -export const LendingPool = {AddLiquidity, RemoveLiquidity}; +export const LendingPool = { + AddLiquidity: {...AddLiquidity, calculateDepositReturn}, + RemoveLiquidity: {...RemoveLiquidity, calculateWithdrawReturn}, + getFolksWrapperAppOptInRequiredAssetIDs +}; diff --git a/src/folks-lending-pools/types.ts b/src/folks-lending-pools/types.ts index 2e7ae66a..48a8c183 100644 --- a/src/folks-lending-pools/types.ts +++ b/src/folks-lending-pools/types.ts @@ -8,9 +8,7 @@ export type FolksLendingAssetInfo = AssetWithIdAndAmount & { export interface FolksLendingPool { appId: number; managerAppId: number; - depositInterestRate: number; - depositInterestIndex: number; - updatedAt: Date; - originalAssetId: number; - fAssetId: number; + depositInterestRate: bigint; + depositInterestIndex: bigint; + lastUpdate: number; } diff --git a/src/index.ts b/src/index.ts index 4e416850..5bca216b 100644 --- a/src/index.ts +++ b/src/index.ts @@ -133,11 +133,8 @@ export { REDEEM_PROCESS_TXN_COUNT } from "./redeem"; -export { - fetchFolksLendingPool, - LendingPool, - FolksLendingPool -} from "./folks-lending-pools"; +export {fetchFolksLendingPool, LendingPool} from "./folks-lending-pools"; +export {FolksLendingPool} from "./folks-lending-pools/types"; export {prepareCommitTransactions, getStakingAppID} from "./stake"; export {tinymanJSSDKConfig} from "./config"; From 2f8c4cbe9361be8b17d7747c1e5deb0c46c20a13 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Tue, 9 Jan 2024 14:30:14 +0300 Subject: [PATCH 19/23] feat(folks-lending-pools): Consider lastUpdate time while calculating the dii --- src/folks-lending-pools/index.ts | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/folks-lending-pools/index.ts b/src/folks-lending-pools/index.ts index 9bd6c564..243785b7 100644 --- a/src/folks-lending-pools/index.ts +++ b/src/folks-lending-pools/index.ts @@ -9,10 +9,11 @@ import {FolksLendingPool} from "./types"; function getLatestDepositInterestIndex( depositInterestIndex: bigint, - depositInterestRate: bigint + depositInterestRate: bigint, + lastUpdate?: number ) { const timestampDelta = BigInt( - Math.floor(new Date().getTime() / 1000) - getLastTimestamp() + Math.floor(new Date().getTime() / 1000) - getLastTimestamp(lastUpdate) ); return mulScale( @@ -32,11 +33,13 @@ function getLastTimestamp(lastUpdate?: number): number { function calculateDepositReturn( depositAmount: number, depositInterestIndex: bigint, - depositInterestRate: bigint + depositInterestRate: bigint, + lastUpdate?: number ) { const latestDepositInterestIndex = getLatestDepositInterestIndex( depositInterestIndex, - depositInterestRate + depositInterestRate, + lastUpdate ); return divScale(BigInt(depositAmount), latestDepositInterestIndex, ONE_14_DP); @@ -48,11 +51,13 @@ function calculateDepositReturn( function calculateWithdrawReturn( withdrawAmount: number, depositInterestIndex: bigint, - depositInterestRate: bigint + depositInterestRate: bigint, + lastUpdate?: number ) { const latestDepositInterestIndex = getLatestDepositInterestIndex( depositInterestIndex, - depositInterestRate + depositInterestRate, + lastUpdate ); return mulScale(BigInt(withdrawAmount), latestDepositInterestIndex, ONE_14_DP); From 595463e9cb29ef941580ace0084d925bf65f4bc5 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Tue, 9 Jan 2024 14:42:20 +0300 Subject: [PATCH 20/23] chore(index): Get the latest build --- dist/folks-lending-pools/index.d.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/dist/folks-lending-pools/index.d.ts b/dist/folks-lending-pools/index.d.ts index 1d474eb7..df3d9e39 100644 --- a/dist/folks-lending-pools/index.d.ts +++ b/dist/folks-lending-pools/index.d.ts @@ -4,11 +4,11 @@ import { FolksLendingPool } from "./types"; /** * Calculates the amount fAsset received when adding liquidity with original asset. */ -declare function calculateDepositReturn(depositAmount: number, depositInterestIndex: bigint, depositInterestRate: bigint): bigint; +declare function calculateDepositReturn(depositAmount: number, depositInterestIndex: bigint, depositInterestRate: bigint, lastUpdate?: number): bigint; /** * Calculates the amount original asset received when removing liquidity from fAsset pool. */ -declare function calculateWithdrawReturn(withdrawAmount: number, depositInterestIndex: bigint, depositInterestRate: bigint): bigint; +declare function calculateWithdrawReturn(withdrawAmount: number, depositInterestIndex: bigint, depositInterestRate: bigint, lastUpdate?: number): bigint; /** * Fetches Folks lending pool application info from the algod, parses the global state and builds FolksLendingPool object. */ From 18d8e5d874c751fc098babbb36aebfd20d9ddb41 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Tue, 9 Jan 2024 23:37:19 +0300 Subject: [PATCH 21/23] fix(lending/add-liq): Remove order by asset id --- dist/index.d.ts | 2 +- dist/index.js | 2 +- .../add-liquidity/index.ts | 31 +++++++++++-------- src/index.ts | 2 +- 4 files changed, 21 insertions(+), 16 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index f7750df9..04649633 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -38,7 +38,7 @@ export { SwapQuoteError }; export { SwapQuoteErrorType } from "./util/error/SwapQuoteError"; export { redeemExcessAsset, redeemAllExcessAsset, generateRedeemTxns, REDEEM_PROCESS_TXN_COUNT } from "./redeem"; export { fetchFolksLendingPool, LendingPool } from "./folks-lending-pools"; -export { FolksLendingPool } from "./folks-lending-pools/types"; +export type { FolksLendingPool } from "./folks-lending-pools/types"; export { prepareCommitTransactions, getStakingAppID } from "./stake"; export { tinymanJSSDKConfig } from "./config"; export { combineAndRegroupSignerTxns } from "./util/transaction/transactionUtils"; diff --git a/dist/index.js b/dist/index.js index caf53a41..e15fd06c 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,F=28500,R=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,X={id:"".concat(j),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"},U="- would result negative",Q="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,Ft=ht("swap"),Rt=(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=Qt(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Xt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Vt(t){return t[t.length-1].quote.amount_out}function Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(t),assetOut:Vt(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(Ut(u).amount),Number(Vt(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),_=Xt(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:[Ft,Rt[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(Xt(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=Ut(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=Vt(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)+F*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 Re.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 Xe(){return(Xe=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 Ve(){return(Ve=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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Ve.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Xe.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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: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=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+F*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"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 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===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 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.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 Ur=(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 Xr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Vr.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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:1385499515},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=J(c,p),g=h(m,2),A=g[0],I=g[1],x=K(I.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),y=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),_=wt(s,C.V2),b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:f}),E=[T,y,w,b],e.next=16,ea({client:r,network:s,assetIDs:[A.id,I.id,A.fAssetId,I.fAssetId,i]});case 16:return(D=e.sent).length&&((S=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*D.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(D.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(D),suggestedParams:f})])[1].fee=(D.length+1)*t.ALGORAND_MIN_TX_FEE,E.unshift.apply(E,S)),e.abrupt("return",t.assignGroupID(E).map((function(t){return{txn:t,signers:[l]}})));case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(e){return t.ALGORAND_MIN_TX_FEE*(4+ta)+(e?(e+1)*t.ALGORAND_MIN_TX_FEE+e*O:0)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}});function ia(t,e){var n,r=BigInt(Math.floor((new Date).getTime()/1e3)-(null!=n?n:Math.floor((new Date).getTime()/1e3)));return Yr(t,Zr+e*r/Kr,Zr)}function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:r(r({},aa),{},{calculateDepositReturn:function(t,e,n){var r=ia(e,n);return BigInt(t)*Hr/r}}),RemoveLiquidity:r(r({},oa),{},{calculateWithdrawReturn:function(t,e,n){var r=ia(e,n);return Yr(BigInt(t),r,Hr)}}),getFolksWrapperAppOptInRequiredAssetIDs:ea};function pa(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 la(){return(la=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=pa({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=X,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.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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,F=28500,R=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,X={id:"".concat(j),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"},U="- would result negative",Q="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,Ft=ht("swap"),Rt=(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=Qt(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Xt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Vt(t){return t[t.length-1].quote.amount_out}function Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(t),assetOut:Vt(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(Ut(u).amount),Number(Vt(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),_=Xt(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:[Ft,Rt[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(Xt(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=Ut(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=Vt(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)+F*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 Re.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 Xe(){return(Xe=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 Ve(){return(Ve=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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Ve.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Xe.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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: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=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+F*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"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 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===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 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.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 Ur=(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 Xr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Vr.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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:1385499515},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,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.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=K(p.id),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:c.id,amount:c.amount,suggestedParams:f}),A=m?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:p.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:p.id,amount:p.amount,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(c.lendingAppId),t.encodeUint64(p.lendingAppId)],foreignAssets:[c.id,p.id,c.fAssetId,p.fAssetId],foreignApps:[c.lendingAppId,p.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(s,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[x],accounts:[o],suggestedParams:f}),T=[g,A,I,h],e.next=15,ea({client:r,network:s,assetIDs:[c.id,p.id,c.fAssetId,p.fAssetId,i]});case 15:return(y=e.sent).length&&((w=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*y.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(y.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(y),suggestedParams:f})])[1].fee=(y.length+1)*t.ALGORAND_MIN_TX_FEE,T.unshift.apply(T,w)),e.abrupt("return",t.assignGroupID(T).map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(e){return t.ALGORAND_MIN_TX_FEE*(4+ta)+(e?(e+1)*t.ALGORAND_MIN_TX_FEE+e*O:0)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}});function ia(t,e,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(t){return null!=t?t:Math.floor((new Date).getTime()/1e3)}(n));return Yr(t,Zr+e*r/Kr,Zr)}function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:r(r({},aa),{},{calculateDepositReturn:function(t,e,n,r){var a=ia(e,n,r);return BigInt(t)*Hr/a}}),RemoveLiquidity:r(r({},oa),{},{calculateWithdrawReturn:function(t,e,n,r){var a=ia(e,n,r);return Yr(BigInt(t),a,Hr)}}),getFolksWrapperAppOptInRequiredAssetIDs:ea};function pa(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 la(){return(la=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=pa({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=X,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.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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/folks-lending-pools/add-liquidity/index.ts b/src/folks-lending-pools/add-liquidity/index.ts index 4fc431f8..83721470 100644 --- a/src/folks-lending-pools/add-liquidity/index.ts +++ b/src/folks-lending-pools/add-liquidity/index.ts @@ -6,7 +6,7 @@ import algosdk, { } from "algosdk"; import {SignerTransaction, SupportedNetwork} from "../../util/commonTypes"; -import {isAlgo, prepareAssetPairData} from "../../util/asset/assetUtils"; +import {isAlgo} from "../../util/asset/assetUtils"; import { FOLKS_LENDING_POOL_APP_CALL_INNER_TXN_COUNT, FOLKS_WRAPPER_APP_ID @@ -39,14 +39,13 @@ export async function generateTxns({ }): Promise { const wrapperAppAddress = algosdk.getApplicationAddress(FOLKS_WRAPPER_APP_ID[network]); const suggestedParams = await client.getTransactionParams().do(); - const [asset1, asset2] = prepareAssetPairData(asset1In, asset2In); - const isAlgoPool = isAlgo(asset2.id); + const isAlgoPool = isAlgo(asset2In.id); const asset1InTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: wrapperAppAddress, - assetIndex: asset1.id, - amount: asset1.amount, + assetIndex: asset1In.id, + amount: asset1In.amount, suggestedParams }); @@ -54,14 +53,14 @@ export async function generateTxns({ ? algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: wrapperAppAddress, - amount: asset2.amount, + amount: asset2In.amount, suggestedParams }) : algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: wrapperAppAddress, - assetIndex: asset2.id, - amount: asset2.amount, + assetIndex: asset2In.id, + amount: asset2In.amount, suggestedParams }); @@ -71,11 +70,11 @@ export async function generateTxns({ appArgs: [ encodeString("add_liquidity"), decodeAddress(poolAddress).publicKey, - encodeUint64(asset1.lendingAppId), - encodeUint64(asset2.lendingAppId) + encodeUint64(asset1In.lendingAppId), + encodeUint64(asset2In.lendingAppId) ], - foreignAssets: [asset1.id, asset2.id, asset1.fAssetId, asset2.fAssetId], - foreignApps: [asset1.lendingAppId, asset2.lendingAppId, lendingManagerId], + foreignAssets: [asset1In.id, asset2In.id, asset1In.fAssetId, asset2In.fAssetId], + foreignApps: [asset1In.lendingAppId, asset2In.lendingAppId, lendingManagerId], accounts: [poolAddress], suggestedParams }); @@ -99,7 +98,13 @@ export async function generateTxns({ const optInRequiredAssetIds = await getFolksWrapperAppOptInRequiredAssetIDs({ client, network, - assetIDs: [asset1.id, asset2.id, asset1.fAssetId, asset2.fAssetId, poolTokenId] + assetIDs: [ + asset1In.id, + asset2In.id, + asset1In.fAssetId, + asset2In.fAssetId, + poolTokenId + ] }); if (optInRequiredAssetIds.length) { diff --git a/src/index.ts b/src/index.ts index 5bca216b..559a8534 100644 --- a/src/index.ts +++ b/src/index.ts @@ -134,7 +134,7 @@ export { } from "./redeem"; export {fetchFolksLendingPool, LendingPool} from "./folks-lending-pools"; -export {FolksLendingPool} from "./folks-lending-pools/types"; +export type {FolksLendingPool} from "./folks-lending-pools/types"; export {prepareCommitTransactions, getStakingAppID} from "./stake"; export {tinymanJSSDKConfig} from "./config"; From 8fe9c5c4d180a04ea38525f857f42220a4f39081 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Wed, 10 Jan 2024 12:47:57 +0300 Subject: [PATCH 22/23] fix(lending-add-liq): Sort the assets according to fAssetIds --- dist/index.js | 2 +- .../add-liquidity/index.ts | 32 +++++++++---------- 2 files changed, 16 insertions(+), 18 deletions(-) diff --git a/dist/index.js b/dist/index.js index e15fd06c..1cb5c29c 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,F=28500,R=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,X={id:"".concat(j),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"},U="- would result negative",Q="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,Ft=ht("swap"),Rt=(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=Qt(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Xt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Vt(t){return t[t.length-1].quote.amount_out}function Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(t),assetOut:Vt(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(Ut(u).amount),Number(Vt(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),_=Xt(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:[Ft,Rt[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(Xt(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=Ut(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=Vt(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)+F*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 Re.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 Xe(){return(Xe=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 Ve(){return(Ve=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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Ve.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Xe.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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: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=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+F*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"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 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===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 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.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 Ur=(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 Xr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Vr.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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:1385499515},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,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.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=K(p.id),g=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:c.id,amount:c.amount,suggestedParams:f}),A=m?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:p.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:p.id,amount:p.amount,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(c.lendingAppId),t.encodeUint64(p.lendingAppId)],foreignAssets:[c.id,p.id,c.fAssetId,p.fAssetId],foreignApps:[c.lendingAppId,p.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(s,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[x],accounts:[o],suggestedParams:f}),T=[g,A,I,h],e.next=15,ea({client:r,network:s,assetIDs:[c.id,p.id,c.fAssetId,p.fAssetId,i]});case 15:return(y=e.sent).length&&((w=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*y.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(y.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(y),suggestedParams:f})])[1].fee=(y.length+1)*t.ALGORAND_MIN_TX_FEE,T.unshift.apply(T,w)),e.abrupt("return",t.assignGroupID(T).map((function(t){return{txn:t,signers:[l]}})));case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(e){return t.ALGORAND_MIN_TX_FEE*(4+ta)+(e?(e+1)*t.ALGORAND_MIN_TX_FEE+e*O:0)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}});function ia(t,e,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(t){return null!=t?t:Math.floor((new Date).getTime()/1e3)}(n));return Yr(t,Zr+e*r/Kr,Zr)}function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:r(r({},aa),{},{calculateDepositReturn:function(t,e,n,r){var a=ia(e,n,r);return BigInt(t)*Hr/a}}),RemoveLiquidity:r(r({},oa),{},{calculateWithdrawReturn:function(t,e,n,r){var a=ia(e,n,r);return Yr(BigInt(t),a,Hr)}}),getFolksWrapperAppOptInRequiredAssetIDs:ea};function pa(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 la(){return(la=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=pa({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=X,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.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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,F=28500,R=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,X={id:"".concat(j),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"},U="- would result negative",Q="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,Ft=ht("swap"),Rt=(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=Qt(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Xt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Vt(t){return t[t.length-1].quote.amount_out}function Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(t),assetOut:Vt(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(Ut(u).amount),Number(Vt(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),_=Xt(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:[Ft,Rt[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(Xt(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=Ut(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=Vt(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)+F*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 Re.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 Xe(){return(Xe=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 Ve(){return(Ve=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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Ve.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Xe.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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: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=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+F*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"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 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===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 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.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 Ur=(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 Xr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Vr.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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:1385499515},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=[c,p].sort((function(t,e){return e.fAssetId-t.fAssetId})),g=h(m,2),A=g[0],I=g[1],x=K(I.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),y=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),_=wt(s,C.V2),b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:f}),E=[T,y,w,b],e.next=16,ea({client:r,network:s,assetIDs:[A.id,I.id,A.fAssetId,I.fAssetId,i]});case 16:return(D=e.sent).length&&((S=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*D.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(D.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(D),suggestedParams:f})])[1].fee=(D.length+1)*t.ALGORAND_MIN_TX_FEE,E.unshift.apply(E,S)),e.abrupt("return",t.assignGroupID(E).map((function(t){return{txn:t,signers:[l]}})));case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(e){return t.ALGORAND_MIN_TX_FEE*(4+ta)+(e?(e+1)*t.ALGORAND_MIN_TX_FEE+e*O:0)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}});function ia(t,e,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(t){return null!=t?t:Math.floor((new Date).getTime()/1e3)}(n));return Yr(t,Zr+e*r/Kr,Zr)}function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:r(r({},aa),{},{calculateDepositReturn:function(t,e,n,r){var a=ia(e,n,r);return BigInt(t)*Hr/a}}),RemoveLiquidity:r(r({},oa),{},{calculateWithdrawReturn:function(t,e,n,r){var a=ia(e,n,r);return Yr(BigInt(t),a,Hr)}}),getFolksWrapperAppOptInRequiredAssetIDs:ea};function pa(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 la(){return(la=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=pa({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=X,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.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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/folks-lending-pools/add-liquidity/index.ts b/src/folks-lending-pools/add-liquidity/index.ts index 83721470..abf03d10 100644 --- a/src/folks-lending-pools/add-liquidity/index.ts +++ b/src/folks-lending-pools/add-liquidity/index.ts @@ -39,13 +39,17 @@ export async function generateTxns({ }): Promise { const wrapperAppAddress = algosdk.getApplicationAddress(FOLKS_WRAPPER_APP_ID[network]); const suggestedParams = await client.getTransactionParams().do(); - const isAlgoPool = isAlgo(asset2In.id); + + // Make sure to sort the assets according to the fAssetIds + const [asset1, asset2] = [asset1In, asset2In].sort((a, b) => b.fAssetId - a.fAssetId); + + const isAlgoPool = isAlgo(asset2.id); const asset1InTxn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: wrapperAppAddress, - assetIndex: asset1In.id, - amount: asset1In.amount, + assetIndex: asset1.id, + amount: asset1.amount, suggestedParams }); @@ -53,14 +57,14 @@ export async function generateTxns({ ? algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: wrapperAppAddress, - amount: asset2In.amount, + amount: asset2.amount, suggestedParams }) : algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: initiatorAddr, to: wrapperAppAddress, - assetIndex: asset2In.id, - amount: asset2In.amount, + assetIndex: asset2.id, + amount: asset2.amount, suggestedParams }); @@ -70,11 +74,11 @@ export async function generateTxns({ appArgs: [ encodeString("add_liquidity"), decodeAddress(poolAddress).publicKey, - encodeUint64(asset1In.lendingAppId), - encodeUint64(asset2In.lendingAppId) + encodeUint64(asset1.lendingAppId), + encodeUint64(asset2.lendingAppId) ], - foreignAssets: [asset1In.id, asset2In.id, asset1In.fAssetId, asset2In.fAssetId], - foreignApps: [asset1In.lendingAppId, asset2In.lendingAppId, lendingManagerId], + foreignAssets: [asset1.id, asset2.id, asset1.fAssetId, asset2.fAssetId], + foreignApps: [asset1.lendingAppId, asset2.lendingAppId, lendingManagerId], accounts: [poolAddress], suggestedParams }); @@ -98,13 +102,7 @@ export async function generateTxns({ const optInRequiredAssetIds = await getFolksWrapperAppOptInRequiredAssetIDs({ client, network, - assetIDs: [ - asset1In.id, - asset2In.id, - asset1In.fAssetId, - asset2In.fAssetId, - poolTokenId - ] + assetIDs: [asset1.id, asset2.id, asset1.fAssetId, asset2.fAssetId, poolTokenId] }); if (optInRequiredAssetIds.length) { From 84080f861c917aede4091f116b5856c05f18f41c Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Thu, 11 Jan 2024 11:07:58 +0300 Subject: [PATCH 23/23] fix(lending-pools): Move calculateWithdrawReturn and calculateDepositReturn outside of AddLiquidity and Remove liquidity --- dist/folks-lending-pools/index.d.ts | 34 +++++------------------------ dist/index.js | 2 +- src/folks-lending-pools/index.ts | 6 +++-- 3 files changed, 11 insertions(+), 31 deletions(-) diff --git a/dist/folks-lending-pools/index.d.ts b/dist/folks-lending-pools/index.d.ts index df3d9e39..f7668aad 100644 --- a/dist/folks-lending-pools/index.d.ts +++ b/dist/folks-lending-pools/index.d.ts @@ -1,4 +1,6 @@ import algosdk from "algosdk"; +import * as AddLiquidity from "./add-liquidity"; +import * as RemoveLiquidity from "./remove-liquidity"; import { getFolksWrapperAppOptInRequiredAssetIDs } from "./add-liquidity/utils"; import { FolksLendingPool } from "./types"; /** @@ -14,34 +16,10 @@ declare function calculateWithdrawReturn(withdrawAmount: number, depositInterest */ export declare function fetchFolksLendingPool(algod: algosdk.Algodv2, appId: number): Promise; export declare const LendingPool: { - AddLiquidity: { - calculateDepositReturn: typeof calculateDepositReturn; - generateTxns({ client, network, poolAddress, poolTokenId, lendingManagerId, asset1In, asset2In, initiatorAddr }: { - client: algosdk.Algodv2; - network: import("..").SupportedNetwork; - poolAddress: string; - poolTokenId: number; - lendingManagerId: number; - asset1In: import("./types").FolksLendingAssetInfo; - asset2In: import("./types").FolksLendingAssetInfo; - initiatorAddr: string; - }): Promise; - getAddLiquidityTotalFee(wrapperAppOptInRequiredAssetIdCount?: number | undefined): number; - }; - RemoveLiquidity: { - calculateWithdrawReturn: typeof calculateWithdrawReturn; - generateTxns({ client, pool, poolTokenIn, initiatorAddr, asset1Out, asset2Out, lendingManagerId, network }: { - client: algosdk.Algodv2; - pool: Pick; - poolTokenIn: number | bigint; - initiatorAddr: string; - asset1Out: Omit; - asset2Out: Omit; - lendingManagerId: number; - network: import("..").SupportedNetwork; - }): Promise; - getRemoveLiquidityTotalFee(): number; - }; + AddLiquidity: typeof AddLiquidity; + RemoveLiquidity: typeof RemoveLiquidity; + calculateWithdrawReturn: typeof calculateWithdrawReturn; + calculateDepositReturn: typeof calculateDepositReturn; getFolksWrapperAppOptInRequiredAssetIDs: typeof getFolksWrapperAppOptInRequiredAssetIDs; }; export {}; diff --git a/dist/index.js b/dist/index.js index 1cb5c29c..b2182c8c 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,F=28500,R=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,X={id:"".concat(j),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"},U="- would result negative",Q="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,Ft=ht("swap"),Rt=(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=Qt(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Xt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Vt(t){return t[t.length-1].quote.amount_out}function Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(t),assetOut:Vt(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(Ut(u).amount),Number(Vt(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),_=Xt(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:[Ft,Rt[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(Xt(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=Ut(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=Vt(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)+F*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 Re.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 Xe(){return(Xe=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 Ve(){return(Ve=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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Ve.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Xe.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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: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=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+F*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"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 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===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 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.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 Ur=(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 Xr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Vr.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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:1385499515},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=[c,p].sort((function(t,e){return e.fAssetId-t.fAssetId})),g=h(m,2),A=g[0],I=g[1],x=K(I.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),y=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),_=wt(s,C.V2),b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:f}),E=[T,y,w,b],e.next=16,ea({client:r,network:s,assetIDs:[A.id,I.id,A.fAssetId,I.fAssetId,i]});case 16:return(D=e.sent).length&&((S=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*D.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(D.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(D),suggestedParams:f})])[1].fee=(D.length+1)*t.ALGORAND_MIN_TX_FEE,E.unshift.apply(E,S)),e.abrupt("return",t.assignGroupID(E).map((function(t){return{txn:t,signers:[l]}})));case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var aa=Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(e){return t.ALGORAND_MIN_TX_FEE*(4+ta)+(e?(e+1)*t.ALGORAND_MIN_TX_FEE+e*O:0)}});function sa(){return(sa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var oa=Object.freeze({__proto__:null,generateTxns:function(t){return sa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}});function ia(t,e,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(t){return null!=t?t:Math.floor((new Date).getTime()/1e3)}(n));return Yr(t,Zr+e*r/Kr,Zr)}function ua(){return(ua=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ca={AddLiquidity:r(r({},aa),{},{calculateDepositReturn:function(t,e,n,r){var a=ia(e,n,r);return BigInt(t)*Hr/a}}),RemoveLiquidity:r(r({},oa),{},{calculateWithdrawReturn:function(t,e,n,r){var a=ia(e,n,r);return Yr(BigInt(t),a,Hr)}}),getFolksWrapperAppOptInRequiredAssetIDs:ea};function pa(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 la(){return(la=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=pa({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=X,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.LendingPool=ca,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 la.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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,F=28500,R=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,X={id:"".concat(j),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"},U="- would result negative",Q="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,Ft=ht("swap"),Rt=(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=Qt(t),n=e.assetIn,r=e.assetOut;return pt(r.asset.decimals,Number(r.amount))/pt(n.asset.decimals,Number(n.amount))}function Xt(t){var e=Mt[t];if(!e)throw new Error("Unknown network or network not supported: ".concat(t));return e}function Vt(t){return t[t.length-1].quote.amount_out}function Ut(t){return t[0].quote.amount_in}function Qt(t){return{assetIn:Ut(t),assetOut:Vt(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(Ut(u).amount),Number(Vt(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),_=Xt(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:[Ft,Rt[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(Xt(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=Ut(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=Vt(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)+F*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 Re.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 Xe(){return(Xe=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 Ve(){return(Ve=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 Ue=Object.freeze({__proto__:null,getPoolAssets:function(t){return Ve.apply(this,arguments)},getPoolInfo:Ce,getPoolReserves:function(t,e){return Xe.apply(this,arguments)}});function Qe(t){return Boolean(t&&!(t.asset1+t.asset2))}var qe,Ge=Object.freeze({__proto__:null,getPoolPairRatio:function(t,e){var n=Qe(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:Qe,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({},Ue),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: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=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+F*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"),Fn=(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 Rn(){return(Rn=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:Fn.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 Rn.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(R))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(R,", 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(R),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 Fr(t){return Rr.apply(this,arguments)}function Rr(){return(Rr=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,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: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:Fr};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 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===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 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.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 Ur=(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 Xr.apply(this,arguments)}(t);if(e===Ot.FixedOutput)return function(t){return Vr.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 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,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(t){var e,n={},r=w(t);try{for(r.s();!(e=r.n()).done;){var a=e.value,s=(new TextDecoder).decode(Buffer.from(a.key,"base64")),o=void 0;o=1===a.value.type?a.value.bytes:a.value.uint,n[s]=o}}catch(t){r.e(t)}finally{r.f()}return n}function Yr(t,e,n){return t*e/n}var Kr=BigInt(31536e3),Hr=BigInt(1e14),Zr=BigInt(1e16),$r={testnet:548587153,mainnet:1385499515},ta=14;function ea(t){return na.apply(this,arguments)}function na(){return(na=o(a().mark((function e(n){var r,s,o,i,u;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($r[s]),e.next=4,ve(r,i);case 4:return u=e.sent.assets.map((function(t){return t["asset-id"]})),e.abrupt("return",o.filter((function(t){return t!==j&&!u.includes(t)})));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ra(){return(ra=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,T,y,w,_,b,E,D,S;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.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=t.getApplicationAddress($r[s]),e.next=4,r.getTransactionParams().do();case 4:return f=e.sent,m=[c,p].sort((function(t,e){return e.fAssetId-t.fAssetId})),g=h(m,2),A=g[0],I=g[1],x=K(I.id),T=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:A.id,amount:A.amount,suggestedParams:f}),y=x?t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:I.amount,suggestedParams:f}):t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:I.id,amount:I.amount,suggestedParams:f}),(w=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("add_liquidity"),t.decodeAddress(o).publicKey,t.encodeUint64(A.lendingAppId),t.encodeUint64(I.lendingAppId)],foreignAssets:[A.id,I.id,A.fAssetId,I.fAssetId],foreignApps:[A.lendingAppId,I.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),_=wt(s,C.V2),b=t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("noop")],foreignAssets:[i],foreignApps:[_],accounts:[o],suggestedParams:f}),E=[T,y,w,b],e.next=16,ea({client:r,network:s,assetIDs:[A.id,I.id,A.fAssetId,I.fAssetId,i]});case 16:return(D=e.sent).length&&((S=[t.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:O*D.length,suggestedParams:f}),t.makeApplicationNoOpTxnFromObject({from:l,appIndex:$r[s],appArgs:[ht("asset_optin")].concat(v(D.map((function(e){return t.encodeUint64(e)})))),foreignAssets:v(D),suggestedParams:f})])[1].fee=(D.length+1)*t.ALGORAND_MIN_TX_FEE,E.unshift.apply(E,S)),e.abrupt("return",t.assignGroupID(E).map((function(t){return{txn:t,signers:[l]}})));case 19:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function aa(){return(aa=o(a().mark((function e(n){var r,s,o,i,u,c,p,l,d,f,m,g,A,I,x,h,v;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.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=t.getApplicationAddress($r[l]),e.next=4,r.getTransactionParams().do();case 4:if(f=e.sent,m=s.account.address(),g=s.poolTokenID){e.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return A=t.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:g,amount:o,suggestedParams:f}),(I=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("remove_liquidity"),t.decodeAddress(m).publicKey,t.encodeUint64(u.lendingAppId),t.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=t.ALGORAND_MIN_TX_FEE*(ta+1),x=wt(l,C.V2),h=t.makeApplicationNoOpTxnFromObject({from:i,appIndex:$r[l],appArgs:[ht("noop")],accounts:[m],foreignAssets:[g,u.fAssetId,c.fAssetId],foreignApps:[x],suggestedParams:f}),v=t.assignGroupID([A,I,h]),e.abrupt("return",v.map((function(t){return{txn:t,signers:[i]}})));case 16:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function sa(t,e,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(t){return null!=t?t:Math.floor((new Date).getTime()/1e3)}(n));return Yr(t,Zr+e*r/Kr,Zr)}function oa(){return(oa=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:return t.next=2,e.getApplicationByID(n).do();case 2:return r=t.sent,s=r.params["global-state"],o=Jr(s),i=Number(Buffer.from(o.pm,"base64").readBigUInt64BE(0)),u=Buffer.from(o.i,"base64"),c=u.readBigUInt64BE(32),p=u.readBigUInt64BE(40),l=Number(u.readBigUInt64BE(48)),t.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ia={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(t){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(e){return t.ALGORAND_MIN_TX_FEE*(4+ta)+(e?(e+1)*t.ALGORAND_MIN_TX_FEE+e*O:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(t){return aa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return t.ALGORAND_MIN_TX_FEE*(3+ta)}}),calculateWithdrawReturn:function(t,e,n,r){var a=sa(e,n,r);return Yr(BigInt(t),a,Hr)},calculateDepositReturn:function(t,e,n,r){var a=sa(e,n,r);return BigInt(t)*Hr/a},getFolksWrapperAppOptInRequiredAssetIDs:ea};function ua(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 ca(){return(ca=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=ua({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=X,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.LendingPool=ia,exports.MINIMUM_ADD_LIQUIDITY_AMOUNT=R,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=F,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=Ur,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 ca.apply(this,arguments)},exports.redeemAllExcessAsset=function(t){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(t){return Qr.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/folks-lending-pools/index.ts b/src/folks-lending-pools/index.ts index 243785b7..4474c4cb 100644 --- a/src/folks-lending-pools/index.ts +++ b/src/folks-lending-pools/index.ts @@ -91,7 +91,9 @@ export async function fetchFolksLendingPool( } export const LendingPool = { - AddLiquidity: {...AddLiquidity, calculateDepositReturn}, - RemoveLiquidity: {...RemoveLiquidity, calculateWithdrawReturn}, + AddLiquidity, + RemoveLiquidity, + calculateWithdrawReturn, + calculateDepositReturn, getFolksWrapperAppOptInRequiredAssetIDs };