From 7dc54a7185a377ef76492556a2fd943657f39f9d Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Thu, 19 Dec 2024 19:05:04 +0100 Subject: [PATCH 01/15] feat(liquid-stake): Get transaction recipe from the api and convert the values into correct types to generate swap router txns --- .../governance/proposal-voting/constants.d.ts | 3 +- dist/governance/utils.d.ts | 3 +- dist/index.d.ts | 46 ++-- dist/index.js | 2 +- dist/stake.d.ts | 2 +- dist/swap/common/utils.d.ts | 2 +- dist/swap/types.d.ts | 33 ++- dist/swap/v2/index.d.ts | 4 +- dist/swap/v2/router/client.d.ts | 12 + dist/swap/v2/router/constants.d.ts | 14 + dist/swap/v2/router/swap-router.d.ts | 25 +- dist/swap/v2/router/util.d.ts | 29 +- dist/util/account/accountConstants.d.ts | 2 + dist/util/account/accountUtils.d.ts | 4 +- dist/util/pool/v1_1/index.d.ts | 2 +- dist/util/util.d.ts | 4 +- src/governance/proposal-voting/constants.ts | 6 +- src/governance/proposal-voting/storage.ts | 8 +- .../proposal-voting/transactions.ts | 8 +- src/governance/rewards/transactions.ts | 11 +- src/governance/staking-voting/storage.ts | 10 +- src/governance/staking-voting/transactions.ts | 27 +- src/governance/utils.ts | 14 - src/governance/vault/storage.ts | 5 +- src/index.ts | 90 +++---- src/stake.ts | 6 +- src/swap/common/utils.ts | 20 +- src/swap/types.ts | 39 ++- src/swap/v2/index.ts | 42 +-- src/swap/v2/router/constants.ts | 17 ++ src/swap/v2/router/swap-router.ts | 248 +++++++----------- src/swap/v2/router/util.ts | 101 ++++++- src/util/account/accountConstants.ts | 4 + src/util/account/accountUtils.ts | 22 +- src/util/pool/v1_1/index.ts | 18 +- src/util/util.ts | 10 +- 36 files changed, 486 insertions(+), 407 deletions(-) create mode 100644 dist/swap/v2/router/client.d.ts create mode 100644 dist/util/account/accountConstants.d.ts create mode 100644 src/util/account/accountConstants.ts diff --git a/dist/governance/proposal-voting/constants.d.ts b/dist/governance/proposal-voting/constants.d.ts index 92d9e360..6bdcd993 100644 --- a/dist/governance/proposal-voting/constants.d.ts +++ b/dist/governance/proposal-voting/constants.d.ts @@ -11,5 +11,4 @@ declare enum ProposalVote { } declare const EXECUTION_HASH_SIZE = 34; declare const CREATE_PROPOSAL_DEFAULT_EXECUTION_HASH_ARGUMENT: Uint8Array; -declare const EXECUTOR_FALLBACK_ADDRESS = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ"; -export { PROPOSAL_BOX_PREFIX, PROPOSAL_BOX_SIZE, PROPOSAL_BOX_COST, ProposalVote, ACCOUNT_ATTENDANCE_SHEET_BOX_SIZE, ATTENDANCE_SHEET_BOX_PREFIX, ATTENDANCE_SHEET_BOX_COST, EXECUTION_HASH_SIZE, CREATE_PROPOSAL_DEFAULT_EXECUTION_HASH_ARGUMENT, EXECUTOR_FALLBACK_ADDRESS }; +export { PROPOSAL_BOX_PREFIX, PROPOSAL_BOX_SIZE, PROPOSAL_BOX_COST, ProposalVote, ACCOUNT_ATTENDANCE_SHEET_BOX_SIZE, ATTENDANCE_SHEET_BOX_PREFIX, ATTENDANCE_SHEET_BOX_COST, EXECUTION_HASH_SIZE, CREATE_PROPOSAL_DEFAULT_EXECUTION_HASH_ARGUMENT }; diff --git a/dist/governance/utils.d.ts b/dist/governance/utils.d.ts index 992da9f1..37dfc2cc 100644 --- a/dist/governance/utils.d.ts +++ b/dist/governance/utils.d.ts @@ -13,8 +13,7 @@ declare function getBias(slope: number, timeDelta: number): number; declare function calculateTinyPower(lockAmount: number, lockEndTime: number, timeStamp?: number): number; declare function getCumulativePowerDelta(bias: number, slope: number, timeDelta: number): number; declare function getGlobalState(algod: AlgodClient, appId: number): Promise>; -declare function concatUint8Arrays(...arrays: Uint8Array[]): Uint8Array; declare function generateCidFromProposalMetadata(metadata: Record): Promise; declare function combineAndRegroupTxns(...txns: Transaction[][]): Transaction[]; declare function getAllBoxNames(algod: AlgodClient, appId: number): Promise; -export { calculateTinyPower, combineAndRegroupTxns, concatUint8Arrays, doesBoxExist, generateCidFromProposalMetadata, getAllBoxNames, getBias, getCumulativePowerDelta, getGlobalState, getRawBoxValue }; +export { calculateTinyPower, combineAndRegroupTxns, doesBoxExist, generateCidFromProposalMetadata, getAllBoxNames, getBias, getCumulativePowerDelta, getGlobalState, getRawBoxValue }; diff --git a/dist/index.d.ts b/dist/index.d.ts index c4c248c2..cacb14a9 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -1,55 +1,55 @@ import SwapQuoteError from "./util/error/SwapQuoteError"; export type { InitiatorSigner, SignerTransaction, SupportedNetwork } from "./util/commonTypes"; -export { BASE_MINIMUM_BALANCE, MINIMUM_BALANCE_REQUIRED_PER_ASSET, MINIMUM_BALANCE_REQUIRED_PER_APP, MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA, MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE, MINIMUM_ADD_LIQUIDITY_AMOUNT } from "./util/constant"; +export { BASE_MINIMUM_BALANCE, MINIMUM_ADD_LIQUIDITY_AMOUNT, MINIMUM_BALANCE_REQUIRED_PER_APP, MINIMUM_BALANCE_REQUIRED_PER_ASSET, MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA, MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE } from "./util/constant"; export * from "./swap/v2/router"; export * from "./swap/common/utils"; -export { applySlippageToAmount, ASSET_OPT_IN_PROCESS_TXN_COUNT, convertFromBaseUnits, convertToBaseUnits, sendAndWaitRawTransaction, getTxnGroupID, sumUpTxnFees } from "./util/util"; +export { applySlippageToAmount, ASSET_OPT_IN_PROCESS_TXN_COUNT, convertFromBaseUnits, convertToBaseUnits, getTxnGroupID, sendAndWaitRawTransaction, sumUpTxnFees } from "./util/util"; export { generateOptIntoAssetTxns } from "./util/asset/assetUtils"; -export type { AccountAsset, TinymanAnalyticsApiAsset, IndexerAssetInformation, AssetWithIdAndAmount } from "./util/asset/assetModels"; +export type { AccountAsset, AssetWithIdAndAmount, IndexerAssetInformation, TinymanAnalyticsApiAsset } from "./util/asset/assetModels"; export { ALGO_ASSET, ALGO_ASSET_ID, POOL_TOKEN_UNIT_NAME } from "./util/asset/assetConstants"; -export { getAccountInformation, calculateAccountMinimumRequiredBalance, hasSufficientMinimumBalance, isAccountOptedIntoApp, getAccountExcessWithinPool, getAccountExcess, getMinRequiredBalanceToOptIn } from "./util/account/accountUtils"; +export { calculateAccountMinimumRequiredBalance, getAccountExcess, getAccountExcessWithinPool, getAccountInformation, getMinRequiredBalanceToOptIn, hasSufficientMinimumBalance, isAccountOptedIntoApp } from "./util/account/accountUtils"; export type { AccountInformationData } from "./util/account/accountTypes"; -export type { ContractVersionValue } from "./contract/types"; export { CONTRACT_VERSION } from "./contract/constants"; -export { tinymanContract_v2 } from "./contract/v2/contract"; +export type { ContractVersionValue } from "./contract/types"; export { tinymanContract_v1_1 } from "./contract/v1_1/contract"; -export { getValidatorAppID, generateOptIntoValidatorTxns, OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT, generateOptOutOfValidatorTxns, OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT } from "./validator"; -export type { V1PoolInfo, V2PoolInfo, PoolReserves } from "./util/pool/poolTypes"; +export { tinymanContract_v2 } from "./contract/v2/contract"; +export { generateOptIntoValidatorTxns, generateOptOutOfValidatorTxns, getValidatorAppID, OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT, OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT } from "./validator"; +export type { PoolReserves, V1PoolInfo, V2PoolInfo } from "./util/pool/poolTypes"; export { PoolStatus } from "./util/pool/poolTypes"; export { poolUtils } from "./util/pool"; export { Bootstrap } from "./bootstrap"; -export type { V1_1AddLiquidityQuote, V1_1AddLiquidityExecution } from "./add-liquidity/v1_1/types"; -export { V1_1AddLiquidityTxnIndices, V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT } from "./add-liquidity/v1_1/constants"; -export type { V2InitialAddLiquidityQuote, V2AddLiquidityInternalSwapQuote, V2FlexibleAddLiquidityQuote, V2SingleAssetInAddLiquidityQuote, V2AddLiquidityExecution } from "./add-liquidity/v2/types"; -export { V2AddLiquidityType, V2AddLiquidityTxnIndices } from "./add-liquidity/v2/constants"; -export { getAddLiquidityTotalFee } from "./add-liquidity/util"; export { AddLiquidity } from "./add-liquidity"; +export { getAddLiquidityTotalFee } from "./add-liquidity/util"; +export { V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT, V1_1AddLiquidityTxnIndices } from "./add-liquidity/v1_1/constants"; +export type { V1_1AddLiquidityExecution, V1_1AddLiquidityQuote } from "./add-liquidity/v1_1/types"; +export { V2AddLiquidityTxnIndices, V2AddLiquidityType } from "./add-liquidity/v2/constants"; +export type { V2AddLiquidityExecution, V2AddLiquidityInternalSwapQuote, V2FlexibleAddLiquidityQuote, V2InitialAddLiquidityQuote, V2SingleAssetInAddLiquidityQuote } from "./add-liquidity/v2/types"; export type { V1_1RemoveLiquidityExecution, V1_1RemoveLiquidityQuote } from "./remove-liquidity/v1_1/types"; -export type { V2RemoveLiquidityQuote, V2SingleAssetRemoveLiquidityQuote, V2RemoveLiquidityExecution } from "./remove-liquidity/v2/types"; +export type { V2RemoveLiquidityExecution, V2RemoveLiquidityQuote, V2SingleAssetRemoveLiquidityQuote } from "./remove-liquidity/v2/types"; +export { RemoveLiquidity } from "./remove-liquidity"; export { V1_1_REMOVE_LIQUIDITY_TXN_COUNT } from "./remove-liquidity/v1_1/constants"; export { V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT } from "./remove-liquidity/v2/constants"; -export { RemoveLiquidity } from "./remove-liquidity"; -export type { SwapQuote, V1SwapExecution, V2SwapExecution, DirectSwapQuote, SwapRoute, GenerateSwapTxnsParams } from "./swap/types"; -export * from "./swap/v2/util"; -export { SwapType } from "./swap/constants"; export { Swap } from "./swap"; +export { SwapType } from "./swap/constants"; +export type { DirectSwapQuote, GenerateSwapTxnsParams, SwapQuote, SwapRoute, SwapRouterResponse, V1SwapExecution, V2SwapExecution } from "./swap/types"; +export * from "./swap/v2/util"; export { SwapQuoteType } from "./swap/types"; export { SwapQuoteError }; export { SwapQuoteErrorType } from "./util/error/SwapQuoteError"; -export { redeemExcessAsset, redeemAllExcessAsset, generateRedeemTxns, REDEEM_PROCESS_TXN_COUNT } from "./redeem"; +export { generateRedeemTxns, REDEEM_PROCESS_TXN_COUNT, redeemAllExcessAsset, redeemExcessAsset } from "./redeem"; export { fetchFolksLendingPool, LendingPool } from "./folks-lending-pools"; export type { FolksLendingPool } from "./folks-lending-pools/types"; -export { prepareCommitTransactions, getStakingAppID } from "./stake"; export { tinymanJSSDKConfig } from "./config"; +export { getStakingAppID, prepareCommitTransactions } from "./stake"; export { combineAndRegroupSignerTxns, getAppCallInnerAssetData } from "./util/transaction/transactionUtils"; export { TinymanGovernanceClient } from "./governance"; export type { RawBoxCacheValue } from "./governance/types"; export { TinymanSTAlgoClient } from "./liquid-stake/stAlgoClient"; export { TinymanTAlgoClient } from "./liquid-stake/tAlgoClient"; -export { AccountState } from "./governance/vault/storage"; -export { getStartTimestampOfWeek } from "./governance/vault/utils"; export { ProposalVote } from "./governance/proposal-voting/constants"; export { generateProposalMetadata } from "./governance/proposal-voting/transactions"; export type { GenerateProposalMetadataPayload } from "./governance/proposal-voting/types"; export { intToBytes } from "./governance/util/utils"; -export { generateCidFromProposalMetadata, calculateTinyPower, combineAndRegroupTxns, concatUint8Arrays } from "./governance/utils"; +export { AccountState } from "./governance/vault/storage"; +export { getStartTimestampOfWeek } from "./governance/vault/utils"; +export { calculateTinyPower, combineAndRegroupTxns, generateCidFromProposalMetadata } from "./governance/utils"; diff --git a/dist/index.js b/dist/index.js index e2f95da3..7a2bcbac 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U={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:""},G={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},X={testnet:258703304,mainnet:22e8},q={testnet:724519992,mainnet:2537013734},Q={testnet:724676936,mainnet:2537023208},W="- would result negative",z="logic eval error:",J="exceeds schema integer count",K=/transaction \w+:/,Y=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;ir?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function ee(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function te(e){return Number(e)===j}function ne(e){return Number(e.id)}function re(e){var t,n=e.stateArray,r=void 0===n?[]:n,a=e.shouldDecodeKeys,s=void 0!==a&&a,o={},i=T(r);try{for(i.s();!(t=i.n()).done;){var u=t.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(e){i.e(e)}finally{i.f()}return o}function ae(e){var t,n=e.reduce((function(e,t){return e+t.length}),0),r=new Uint8Array(n),a=0,s=T(e);try{for(s.s();!(t=s.n()).done;){var o=t.value;r.set(o,a),a+=o.length}}catch(e){s.e(e)}finally{s.f()}return r}var se=100000n,oe=100000n,ie=100000n,ue=25000n+25000n,ce=25000n+3500n;function pe(e){var t=e["apps-total-schema"],n=0n,r=0n;t&&(t["num-byte-slice"]&&(n=t["num-byte-slice"]),t["num-uint"]&&(r=t["num-uint"]));var a=e["apps-local-state"]||[],s=e["created-apps"]||[],o=e.assets||[];return se+oe*BigInt(o.length)+ie*BigInt(s.length+a.length)+ce*r+ue*n}function le(e){return new Promise((function(t){setTimeout((function(){t(null)}),e)}))}function de(e,t){return fe.apply(this,arguments)}function fe(){return(fe=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=3,le(1e3);case 3:return r=null,e.prev=4,e.next=7,t.pendingTransactionInformation(n).do();case 7:r=e.sent,e.next=12;break;case 10:e.prev=10,e.t0=e.catch(4);case 12:if(!r){e.next=17;break}if(!r["confirmed-round"]){e.next=15;break}return e.abrupt("return",r);case 15:if(!r["pool-error"]){e.next=17;break}throw new Error("Transaction Rejected: ".concat(r["pool-error"]));case 17:e.next=0;break;case 19:case"end":return e.stop()}}),e,null,[[4,10]])})))).apply(this,arguments)}function me(e,t,n){if(t>1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function he(e,t){var n=Number(e);return ge({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function ge(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(Ae(t),2),s=a[0],o=a[1],i=v(Ae(Math.round(Number(xe(s,o+r)))),2),u=i[0],c=i[1];return Number(xe(u,c-r))}return Math.round(t)}function xe(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function Ae(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function Ie(e,t){return ve.apply(this,arguments)}function ve(){return(ve=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,de(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new Y(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function we(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ye(e){return(t=e[0].txn.group)?Buffer.from(t).toString("base64"):"";var t}function be(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function Te(e){return(new TextEncoder).encode(e)}var ke,_e=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===U.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return Te("tinyman/".concat(n,":j").concat(r))}}]),e}()),Ee=(l(ke={},U.V1_1,{testnet:62368684,mainnet:552635992}),l(ke,U.V2,{testnet:148607e3,mainnet:1002541853}),ke);function Pe(e,t){var n=Ee[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function Se(){return(Se=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:Pe(a,o),note:_e.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function De(){return(De=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:Pe(a,o),note:_e.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Oe,Ne,Be,Ce=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),Fe=.003,Le=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),Re=(l(Oe={},Ce.FixedInput,1),l(Oe,Ce.FixedOutput,2),Oe),Me=2,Ue=Te("swap"),je=(l(Ne={},Ce.FixedInput,Te("fixed-input")),l(Ne,Ce.FixedOutput,Te("fixed-output")),Ne),Ve={ASSET_OPT_IN:Te("asset_opt_in")},Ge={testnet:184778019,mainnet:1083651166},Xe=(l(Be={},Ce.FixedInput,7),l(Be,Ce.FixedOutput,8),Be);function qe(e){var t=Je(e),n=t.assetIn,r=t.assetOut;return he(r.asset.decimals,Number(r.amount))/he(n.asset.decimals,Number(n.amount))}function Qe(e){var t=Ge[e];if(!t)throw new Error("Unknown network or network not supported: ".concat(e));return t}function We(e){return e[e.length-1].quote.amount_out}function ze(e){return e[0].quote.amount_in}function Je(e){return{assetIn:ze(e),assetOut:We(e)}}function Ke(e){return Ye.apply(this,arguments)}function Ye(){return(Ye=i(s().mark((function t(n){var r,a,o,i,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.routerAppID,o=n.assetIDs,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a,appArgs:[Ve.ASSET_OPT_IN],foreignAssets:o,suggestedParams:u}),p=o.length,c.fee=e.ALGORAND_MIN_TX_FEE*(p+1),t.abrupt("return",c);case 8:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function He(e){return $e.apply(this,arguments)}function $e(){return($e=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k,_,E,P,S,D;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.initiatorAddr,a=n.client,o=n.network,i=n.swapType,u=n.route,c=n.slippage,t.next=3,a.getTransactionParams().do();case 3:return p=t.sent,l=[ne(u[0].quote.amount_in.asset),ne(u[0].quote.amount_out.asset),ne(u[1].quote.amount_out.asset)],d=l[0],f=l[1],m=l[2],h=[Number(ze(u).amount),Number(We(u).amount)],g=h[0],x=h[1],A=[u[0].pool.address,u[1].pool.address],I=A[0],v=A[1],w=i===Ce.FixedInput?g:me("positive",c,g),y=i===Ce.FixedOutput?x:me("negative",c,x),b=te(d),T=Qe(o),k=b?e.makePaymentTxnWithSuggestedParamsFromObject({from:r,to:e.getApplicationAddress(T),amount:w,suggestedParams:p}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:r,to:e.getApplicationAddress(T),amount:w,assetIndex:d,suggestedParams:p}),(_=e.makeApplicationNoOpTxnFromObject({from:r,appIndex:T,appArgs:[Ue,je[i],e.encodeUint64(y)],foreignApps:[Pe(o,U.V2)],foreignAssets:[d,f,m],accounts:[I,v],suggestedParams:p,note:_e.getAppCallTxnNoteWithClientName(U.V2)})).fee=e.ALGORAND_MIN_TX_FEE*(Xe[i]+1),E=[k,_],t.next=17,Ze({client:a,network:o,assetIDs:[d,f,m]});case 17:if(!((P=t.sent).length>0)){t.next=23;break}return t.next=21,Ke({client:a,initiatorAddr:r,assetIDs:P,routerAppID:T});case 21:S=t.sent,E.unshift(S);case 23:return D=e.assignGroupID(E),t.abrupt("return",D.map((function(e){return{txn:e,signers:[r]}})));case 25:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Ze(e){return et.apply(this,arguments)}function et(){return(et=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(Qe(a)),t.next=4,r.accountInformation(i).do();case 4:return u=t.sent,c=u.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==j&&!c.includes(e)})));case 7:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function tt(e){return nt.apply(this,arguments)}function nt(){return(nt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetInID,a=t.assetOutID,o=t.swapType,i=t.network,u={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},e.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=e.sent,e.next=7,c.json();case 7:if(p=e.sent,c.ok){e.next=14;break}if(s=p,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(!(p.route.length<2)){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",p);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var rt=function(e){return e.Direct="direct",e.Router="router",e}({}),at=4*e.ALGORAND_MIN_TX_FEE;function st(t){return(Re[t]+Me)*e.ALGORAND_MIN_TX_FEE}function ot(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:Fe;return Math.ceil(1/e)}function it(e){return e1&&void 0!==arguments[1]?arguments[1]:Fe)}function ut(t){return(Re[t]+1)*e.ALGORAND_MIN_TX_FEE}function ct(e){var t=e.assetIn,n=e.assetOut;return he(n.decimals,Number(n.amount))/he(t.decimals,Number(t.amount))}function pt(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=ct({assetIn:r,assetOut:a}),o=he(a.decimals,Number(n))/he(r.decimals,Number(t));return ge({decimalPlaces:5},Math.abs(s/o-1))}function lt(e){if(e.type===rt.Router){var t=ze(e.data.route);return{id:ne(t.asset),amount:Number(t.amount)}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function dt(e){if(e.type===rt.Router){var t=We(e.data.route);return{id:ne(t.asset),amount:Number(t.amount)}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function ft(e){return e.type===rt.Direct?e.data.pool.contractVersion:U.V2}function mt(e){return e.type===rt.Direct?e.data.quote.rate:qe(e.data.route)}function ht(e){for(var t=e[0],n=mt(t),r=1;rn&&(t=a,n=s)}return t}function gt(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}var xt={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"},At={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"},It=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),vt=function(n){d(a,It);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=Pe(n.network,U.V1_1),a=v(ee(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Pt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function Et(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?re({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Pt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var St=Te("e");function Dt(e){return Ot.apply(this,arguments)}function Ot(){return(Ot=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=re({stateArray:x}),I=t.fromByteArray(ae([e.decodeAddress(f).publicKey,St,e.encodeUint64(o.asset1ID)])),v=t.fromByteArray(ae([e.decodeAddress(f).publicKey,St,e.encodeUint64(o.asset2ID)])),w=t.fromByteArray(ae([e.decodeAddress(f).publicKey,St,e.encodeUint64(o.poolTokenID)])),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Nt(){return(Nt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=re({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fMt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Mt,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Gt(){return Gt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,_t(n,r);case 6:return i=e.sent,u=Et(i,Pe(a,U.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ft[U.V1_1].asset1],asset2ID:u[Ft[U.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Gt.apply(this,arguments)}var Xt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Gt.apply(this,arguments)},getPoolInfo:Ut,getPoolReserves:function(e,t){return Vt.apply(this,arguments)}});function qt(e){return Qt.apply(this,arguments)}function Qt(){return(Qt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=kt(U.V2),u=i.generateLogicSigAccountForPool(t),c=Pe(r,U.V2),p=u.address(),l=ee(a,o),e.next=8,_t(n,p);case 8:return d=e.sent,f=Et(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Ct.READY:Ct.NOT_CREATED,contractVersion:U.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ft.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ft.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ft.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ft.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ft.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ft.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ft.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ft.v2.totalFeeShare]),m.poolTokenID=Number(f[Ft.v2.poolTokenID]),m.asset1ID=Number(f[Ft.v2.asset1]),m.asset2ID=Number(f[Ft.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Wt(){return(Wt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,_t(t,n.account.address());case 2:return r=e.sent,a=Et(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ft.v2.asset1Reserves]),o.asset2=BigInt(a[Ft.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ft.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zt(){return(zt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,_t(n,r);case 3:return o=e.sent,i=Et(o,Pe(a,U.V2)),u=null,i&&(u={asset1ID:i[Ft[U.V2].asset1],asset2ID:i[Ft[U.V2].asset2],poolTokenID:i[Ft[U.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Jt=Object.freeze({__proto__:null,getPoolAssets:function(e){return zt.apply(this,arguments)},getPoolInfo:qt,getPoolReserves:function(e,t){return Wt.apply(this,arguments)}});function Kt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Yt,Ht=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=Kt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ut(e),qt(e)])},isPoolEmpty:Kt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Ct.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Ct.READY}}),$t=a((l(Yt={},U.V1_1,a(a({},Xt),Ht)),l(Yt,U.V2,a(a({},Jt),Ht)),Yt),Ht),Zt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),en={ASA_ALGO:96e4,ASA_ASA:859e3};function tn(){return(tn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Z(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=te(g),I=Pe(a,U.V1_1),w=wt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:_e.getAppCallTxnNoteWithClientName(U.V1_1),appArgs:[Te("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:G.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:nn(A),suggestedParams:c}),(E=[])[Zt.FUNDING_TXN]=_,E[Zt.VALIDATOR_APP_CALL]=b,E[Zt.POOL_TOKEN_CREATE]=T,E[Zt.ASSET1_OPT_IN]=k,A||(E[Zt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Zt.FUNDING_TXN],signers:[u]},{txn:P[Zt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Zt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Zt.ASSET1_OPT_IN],signers:[y]}],P[Zt.ASSET2_OPT_IN]&&S.push({txn:P[Zt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nn(e){return e?en.ASA_ALGO:en.ASA_ASA}function rn(){return(rn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=ee(i,u),f=v(d,2),m=f[0],h=f[1],g=wt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Zt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function an(e){return sn.apply(this,arguments)}function sn(){return(sn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,de(n,a[Zt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new Y(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function on(){return(on=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,an({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",$t.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var un={generateTxns:function(e){return tn.apply(this,arguments)},signTxns:function(e){return rn.apply(this,arguments)},execute:function(e){return on.apply(this,arguments)},getBootstrapFundingTxnAmount:nn};var cn=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),pn={ASA_ALGO:5,ASA_ASA:6};function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function fn(e,t){return mn.apply(this,arguments)}function mn(){return(mn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,ln(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hn(e,t){return gn.apply(this,arguments)}function gn(){return(gn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,fn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:j,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Pe(a,U.V2),l=e.getApplicationAddress(p),d=Z(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,$t.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Ct.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=Tt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=te(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[Te("bootstrap")],note:_e.getAppCallTxnNoteWithClientName(U.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=In(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:An(A),suggestedParams:c}),(y=[])[cn.FUNDING_TXN]=w,y[cn.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[cn.FUNDING_TXN]={txn:b[cn.FUNDING_TXN],signers:[u]},T[cn.VALIDATOR_APP_CALL]={txn:b[cn.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function An(e){return function(e,t){var n=kt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(U.V2,e)+In(e)+D}function In(t){return((t?pn.ASA_ALGO:pn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function vn(){return(vn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=ee(i,u),f=v(d,2),m=f[0],h=f[1],g=Tt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===cn.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function wn(){return(wn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,ln(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ft.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",$t.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new Y(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var yn,bn={generateTxns:function(e){return xn.apply(this,arguments)},signTxns:function(e){return vn.apply(this,arguments)},execute:function(e){return wn.apply(this,arguments)},getBootstrapFundingTxnAmount:An,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+An(t)}};var Tn,kn,_n,En,Pn=(l(yn={},U.V1_1,un),l(yn,U.V2,bn),l(yn,"generateTxns",(function(e){return e.contractVersion===U.V1_1?un.generateTxns(e):bn.generateTxns(e)})),l(yn,"signTxns",(function(e){return e.contractVersion===U.V1_1?un.signTxns(e):bn.signTxns(e)})),l(yn,"execute",(function(e){return e.contractVersion===U.V1_1?un.execute(e):bn.execute(e)})),l(yn,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===U.V1_1?un.getBootstrapFundingTxnAmount(n):bn.getBootstrapFundingTxnAmount(n)})),yn),Sn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),Dn=5*e.ALGORAND_MIN_TX_FEE,On=function(e){return e.SINGLE="single",e.FLEXIBLE="flexible",e.INITIAL="initial",e}({}),Nn=(l(Tn={},On.FLEXIBLE,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),l(Tn,On.SINGLE,{ASSET_IN_TXN:0,VALIDATOR_APP_CALL_TXN:1}),l(Tn,On.INITIAL,{ASSET1_IN_TXN:0,ASSET2_IN_TXN:1,VALIDATOR_APP_CALL_TXN:2}),Tn),Bn=(l(kn={},On.INITIAL,1),l(kn,On.SINGLE,2),l(kn,On.FLEXIBLE,2),kn),Cn=(l(_n={},On.INITIAL,3),l(_n,On.FLEXIBLE,3),l(_n,On.SINGLE,2),_n);function Fn(e){var t,n,r,a=e.reserves,s=e.totalFeeShare,o=e.asset1,i=e.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,h=f*p/d,g=BigInt(o.amount)-m,x=BigInt(i.amount)-h;if(g>x){var A=g;r=Rn(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Rn(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:pt({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Ln(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Lt)))}function Rn(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function Mn(t){return(Bn[t]+1)*e.ALGORAND_MIN_TX_FEE}var Un=Te("add_liquidity"),jn=(l(En={},U.V1_1,[Te("mint")]),l(En,U.V2,{INITIAL_LIQUIDITY:[Te("add_initial_liquidity")],SINGLE_ASSET_MODE:[Un,Te("single")],FLEXIBLE_MODE:[Un,Te("flexible")]}),En);function Vn(){return(Vn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=me("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:Pe(a,U.V1_1),appArgs:jn.v1_1,accounts:[l],note:_e.getAppCallTxnNoteWithClientName(U.V1_1),foreignAssets:u.id==j?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===j?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Gn(){return(Gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Sn.FEE_TXN?p:n===Sn.ASSET1_IN_TXN?l:n===Sn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Sn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,Dt({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,Ie(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=we(a),g=ye(a),e.next=17,Dt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new Y(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:function(e){return Xn.apply(this,arguments)},generateTxns:function(e){return Vn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=oo)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(it(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:he(p,Number(m))/he(u,Number(l)),priceImpact:g}}function xr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!$t.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=pt({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(it(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:rt.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:he(p,Number(l))/he(u,Number(m)),priceImpact:g}}}}function Ar(e){return Ir.apply(this,arguments)}function Ir(){return(Ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=gr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:rt.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(tt({amount:n,assetInID:r.id,assetOutID:a.id,swapType:Ce.FixedInput,network:i}).then((function(e){return{type:rt.Router,data:e}}))),e.next=6,hr(c);case 6:return p=e.sent,e.abrupt("return",ht(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(e){return wr.apply(this,arguments)}function wr(){return(wr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(xr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(tt({amount:n,assetInID:r.id,assetOutID:a.id,swapType:Ce.FixedOutput,network:i}).then((function(e){return{type:rt.Router,data:e}}))),e.next=5,hr(c);case 5:return p=e.sent,e.abrupt("return",ht(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function yr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:pt({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var br={getQuote:function(e){return mr.apply(this,arguments)},getFixedInputSwapQuote:Ar,getFixedInputDirectSwapQuote:gr,getFixedOutputDirectSwapQuote:xr,getFixedOutputSwapQuote:vr,generateTxns:function(e){return dr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return fr.apply(this,arguments)},calculateFixedInputSwap:yr};function Tr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Lt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function kr(){return(kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:_e.getAppCallTxnNoteWithClientName(U.V2),appArgs:[or,e.encodeUint64(me("negative",p,u)),e.encodeUint64(me("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(sr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[ir.ASSET_TRANSFER_TXN]=m,g[ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _r(){return(_r=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=me("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:_e.getAppCallTxnNoteWithClientName(U.V2),appArgs:[or,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(sr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[ir.ASSET_TRANSFER_TXN]=I,w[ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Er(){return(Er=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,Ie(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,hn(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Pr,Sr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=Tr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=Tr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=br.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=br.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return kr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return _r.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return Er.apply(this,arguments)}},Dr=(l(Pr={},U.V1_1,lr),l(Pr,U.V2,Sr),Pr),Or=3n,Nr=1000n,Br=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Br||{});function Cr(){return(Cr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Br.FEE_TXN_INDEX?c:n===Br.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Fr(){return(Fr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new Y({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[Te("swap"),o===Ce.FixedInput?Te("fi"):Te("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:_e.getAppCallTxnNoteWithClientName(U.V1_1),foreignAssets:c.asset2ID==j?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===Ce.FixedOutput?me("positive",i,p.assetInAmount):p.assetInAmount,I=l===j?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===Ce.FixedInput?me("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===j?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Ct.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Or/Nr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:ct(d),priceImpact:pt(a({inputSupply:i,outputSupply:u},d))};return{type:rt.Direct,data:{pool:t,quote:f}}}function Rr(e){return Mr.apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Dt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,Ie(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Dt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ur(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Ct.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Nr/(Nr-Or),l=p-c,d=he(a.assetOut,Number(u))/he(a.assetIn,Number(p)),f=he(a.assetOut,Number(i))/he(a.assetIn,Number(o)),m=ge({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:rt.Direct,data:{pool:t,quote:h}}}function jr(e){return Vr.apply(this,arguments)}function Vr(){return(Vr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Dt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,Ie(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Dt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Ct.READY){e.next=3;break}throw new Y({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Br.ASSET_IN_TXN_INDEX].txn.assetIndex||j,amount:i[Br.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Br.ASSET_OUT_TXN_INDEX].txn.assetIndex||j,amount:i[Br.ASSET_OUT_TXN_INDEX].txn.amount},o!==Ce.FixedInput){e.next=12;break}return e.next=9,Rr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,jr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ye(i),fees:we(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new Y(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Xr,qr={getQuote:function(e,t,n,r,a){return e===Ce.FixedInput?Lr({pool:t,reserves:n,assetIn:r,decimals:a}):Ur({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Lr,getFixedOutputSwapQuote:Ur,generateTxns:function(e){return Fr.apply(this,arguments)},signTxns:function(e){return Cr.apply(this,arguments)},execute:function(e){return Gr.apply(this,arguments)},executeFixedOutputSwap:jr};function Qr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(gt(n)&&!gt(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Wr(){return(Wr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===U.V1_1})))?c.push(new Promise((function(e,t){try{e(qr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===U.V2})),c.push(br.getFixedInputSwapQuote({amount:r,assetIn:{id:ne(a),decimals:a.decimals},assetOut:{id:ne(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Qr(c);case 8:return d=e.sent,e.abrupt("return",ht(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zr(){return(zr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===U.V1_1})))?c.push(new Promise((function(e,t){try{e(qr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===U.V2})),c.push(br.getFixedOutputSwapQuote({amount:r,assetIn:{id:ne(a),decimals:a.decimals},assetOut:{id:ne(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Qr(c);case 8:return d=e.sent,e.abrupt("return",ht(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Jr=(l(Xr={},U.V1_1,qr),l(Xr,U.V2,br),l(Xr,"getQuote",(function(e){var t=e.type;if(t===Ce.FixedInput)return function(e){return Wr.apply(this,arguments)}(e);if(t===Ce.FixedOutput)return function(e){return zr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Xr,"generateTxns",(function(e){return e.quote.type===rt.Direct&&ft(e.quote)===U.V1_1?qr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):br.generateTxns(e)})),l(Xr,"signTxns",(function(e){if(e.quote.type===rt.Direct&&ft(e.quote)===U.V1_1){var t=e.quote.data.pool;return qr.signTxns(a(a({},e),{},{pool:t}))}return br.signTxns(e)})),l(Xr,"execute",(function(e){return e.contractVersion===U.V1_1?qr.execute(e):br.execute(e)})),Xr);function Kr(){return(Kr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Yr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,Ie(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:we(a),confirmedRound:d,txnID:l,groupID:ye(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new Y(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Yr(e){return Hr.apply(this,arguments)}function Hr(){return(Hr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $r(){return $r=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:we(t),groupID:ye(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,Ie(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new Y(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),$r.apply(this,arguments)}function Zr(){return(Zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[Te("redeem")],note:_e.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ea(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function ta(e,t,n){return e*t/n}var na=BigInt(31536e3),ra=BigInt(1e14),aa=BigInt(1e16),sa={testnet:548587153,mainnet:1385499515},oa=14;function ia(e){return ua.apply(this,arguments)}function ua(){return(ua=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(sa[a]),t.next=4,_t(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==j&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ca(){return(ca=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(sa[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=te(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:sa[a],appArgs:[Te("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(oa+1),T=Pe(a,U.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:sa[a],appArgs:[Te("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ia({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:sa[a],appArgs:[Te("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pa(){return(pa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(sa[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:sa[l],appArgs:[Te("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(oa+1),A=Pe(l,U.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:sa[l],appArgs:[Te("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function la(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return ta(e,aa+t*r/na,aa)}function da(){return(da=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=ea(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var fa={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return ca.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+oa)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return pa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+oa)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=la(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return ta(BigInt(t),n,ra)},calculateDepositReturn:function(e){var t=e.depositAmount,n=la(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*ra/n},getFolksWrapperAppOptInRequiredAssetIDs:ia};function ma(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[Te("commit"),u],note:ae([Te("tinymanStaking/v1:b"),c,e.encodeUint64(s),u])})}function ha(){return(ha=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=ma({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[Te("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ga=604800,xa=1e3,Aa={testnet:480164661,mainnet:2200606875},Ia={testnet:360907790,mainnet:2200609638},va={testnet:336189106,mainnet:2200608153},wa={testnet:383416252,mainnet:2200608887},ya=2500,ba=400,Ta=604800,ka=Te("p"),_a=Te("a"),Ea=24,Pa=111300,Sa=ya+ba*(41+Ea),Da=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(Da||{}),Oa=new Uint8Array(34),Na="AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ";function Ba(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Ca(e,t){return 0===Buffer.compare(e,t)}function Fa(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mLa(a(e))})}function Wa({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return qa({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const za=Wa({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Wa({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Wa({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Wa({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Wa({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Wa({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Wa({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Wa({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Wa({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Ja=Qa({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});Qa({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Ka=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=$a;)n[r++]=255&t|Ya,t/=128;for(;t&Ha;)n[r++]=255&t|Ya,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},Ya=128,Ha=-128,$a=Math.pow(2,31);var Za=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&ts)<=es);return e.bytes=o-n,a},es=128,ts=127;var ns=Math.pow(2,7),rs=Math.pow(2,14),as=Math.pow(2,21),ss=Math.pow(2,28),os=Math.pow(2,35),is=Math.pow(2,42),us=Math.pow(2,49),cs=Math.pow(2,56),ps=Math.pow(2,63),ls={encode:Ka,decode:Za,encodingLength:function(e){return e{const[n,r]=ds(e.subarray(t));return t+=r,n};let r=n(),a=ws;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Ja;return[Ja.prefix,n.decode(`${Ja.prefix}${e}`)]}case Ja.prefix:{const n=t??Ja;return[Ja.prefix,n.decode(e)]}case za.prefix:{const n=t??za;return[za.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=vs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return Is(a).set(n,e),a}}const ws=112,ys=18;function bs(e,t,n){const r=ms(e),a=r+ms(t),s=new Uint8Array(a+n.byteLength);return fs(e,s,0),fs(t,s,r),s.set(n,a),s}const Ts=Symbol.for("@ipld/js-cid/CID");class ks{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?hs(this.code,t):t.then((e=>hs(this.code,e)))}throw Error("Unknown type, must be binary type")}}const _s=85,Es=function({name:e,code:t,encode:n}){return new ks(e,t,n)}({name:"sha2-256",code:18,encode:e=>La(n.createHash("sha256").update(e).digest())});var Ps=126144e3,Ss=1e7,Ds=Math.pow(2,64),Os=48,Ns=21,Bs=48,Cs=Te("tp"),Fs=Te("sc"),Ls=28100,Rs=12900,Ms=421700,Us=409700;function js(e){var t=e/Ta;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*Ta}function Vs(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=js(e)+Ta;re?r=t?e*e*Ds/(2*t):0:r=(e+(e-r))*n/2;return r}function Js(e,t){return Ks.apply(this,arguments)}function Ks(){return(Ks=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",Ys(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ys(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Hs(){for(var e=arguments.length,t=new Array(e),n=0;n0?Ns-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return xo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return xo(this.powerCount)[1]}}]),e}(),uo=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+zs(this.bias,this.slope,t)}}]),e}(),co=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),po=p((function e(t){u(this,e),this.slopeDelta=t})),lo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Ns;return e>0?Ns-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return xo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return xo(this.totalPowerCount)[1]}}]),e}();function fo(){return(fo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=ho(r),e.prev=1,e.next=4,Gs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",mo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function mo(e){var t=Buffer.from(e);return new io(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function ho(t){return e.decodeAddress(t).publicKey}function go(e){var t=Ba(e),n=new Uint8Array(Cs.length+t.length);return n.set(Cs,0),n.set(t,Cs.length),n}function xo(e){var t=e-1;return[Math.floor(t/Ns),t%Ns]}function Ao(t,n){var r=e.decodeAddress(t).publicKey,a=Ba(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function Io(e,t,n){return vo.apply(this,arguments)}function vo(){return(vo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=yo(r),e.next=3,Gs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",wo(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function wo(t){return new po(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function yo(e){return Hs(Fs,Ba(e))}function bo(e,t,n){return To.apply(this,arguments)}function To(){return(To=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Ns)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function So(e){for(var t,n=Os,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[Te("claim_rewards"),Ba(o),Ba(i),Hs.apply(void 0,w(u.map((function(e){return Ba(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?Te(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Ko({sender:s,suggestedParams:c,extraAppArgs:[Ba(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Lo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Ho=8,$o=Te("p"),Zo=Te("a"),ei=Te("v"),ti=28500,ni=12500,ri=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function ai(t,n){return Hs(Zo,e.decodeAddress(t).publicKey,Ba(n))}function si(e){return new ri(Fa(e.slice(0,8)),Fa(e.slice(8,16)),Fa(e.slice(16,24)),Fa(e.slice(24,32)),Fa(e.slice(32,40)),Fa(e.slice(40,48)),Boolean(Fa(e.slice(48,49))))}function oi(){return(oi=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=ao(r),e.next=3,Gs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",si(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ii(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Ho)throw new Error("You cannot cast vote for more than ".concat(Ho," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=Hs.apply(void 0,w(i.map((function(e){return Ba(e)})))),g=Hs.apply(void 0,w(u.map((function(e){return Ba(e)})))),x=function(e){return Hs($o,Te(e))}(s),A=ai(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Ca(e,A)})),v=ho(a),y=Math.floor(c/Ns),b=Ao(a,y),k=Ao(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return Hs(ei,Ba(e),Ba(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Ca(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[Te("cast_vote"),Te(s),h,g,Ba(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?Te(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Ko({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Ko({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*ti+_*ni;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ui(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Us),c||(b+=Rs);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:X[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[Te("create_lock"),Ba(r)],boxes:d.slice(0,8),note:p}),Ko({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ci(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Us),!n.freeAccountPowerSpaceCount){var w=Ao(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:Aa[a],name:w}),v+=Ms}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:X[a],from:s,to:e.getApplicationAddress(Aa[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:Aa[a],from:s,suggestedParams:i,appArgs:[Te("increase_lock_amount")],boxes:h,note:u}),Ko({sender:s,suggestedParams:i,index:Aa[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(Aa[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function pi(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%Ta)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=ho(o),l=Ao(o,n.lastAccountPowerBoxIndex),d=go(i.lastTotalPowerBoxIndex),f=go(i.lastTotalPowerBoxIndex+1),m=yo(n.lockEndTime),h=yo(a),g=[{appIndex:Aa[r],name:p},{appIndex:Aa[r],name:l},{appIndex:Aa[r],name:d},{appIndex:Aa[r],name:f},{appIndex:Aa[r],name:m},{appIndex:Aa[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=Ao(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:Aa[r],name:x})}var A,I=Vs(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%Ta==0){var b=yo(y);g.push({appIndex:Aa[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Rs),n.freeAccountPowerSpaceCount||(k+=Ms),v>i.freeTotalPowerSpaceCount&&(k+=Us);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:Aa[r],from:o,suggestedParams:u,appArgs:[Te("extend_lock_end_time"),Ba(a)],boxes:g.slice(0,8),note:c}),Ko({sender:o,suggestedParams:u,index:Aa[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(Aa[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function li(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=ho(a),u=Ao(a,n.lastAccountPowerBoxIndex),c=Ao(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:Aa[r],name:i},{appIndex:Aa[r],name:u},{appIndex:Aa[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:Aa[r],from:a,suggestedParams:s,appArgs:[Te("withdraw")],foreignAssets:[X[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(Aa[r]),amount:Ms});d.unshift(f)}return e.assignGroupID(d)}var di=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/xa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,_o({algodClient:this.algodClient,address:this.userAddress,appId:Aa[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=Po(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Ws(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/xa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,bo(this.algodClient,Aa[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=Po(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/Ta),u=Math.floor(t/Ta)-i,c=Array.from({length:u},(function(e,t){return(i+t)*Ta})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Ws(l,g),d=Math.max(d-x,0),e.next=28,Io(this.algodClient,Aa[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/xa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,_o({algodClient:this.algodClient,address:this.userAddress,appId:Aa[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=Po(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=zs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Js(this.algodClient,Aa[this.network]);case 3:return t=e.sent,e.abrupt("return",new lo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,Io(this.algodClient,Aa[this.network],r);case 17:return l=e.sent,e.abrupt("return",ui({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ci({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,Io(this.algodClient,Aa[this.network],n);case 19:return p=e.sent,e.abrupt("return",pi({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ci({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,Io(this.algodClient,Aa[this.network],r);case 24:return f=e.sent,l=pi({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?Zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=li({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,H({client:this.algodClient,initiatorAddr:a,assetID:X[this.network]});case 15:c=e.sent,u=Zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return fo.apply(this,arguments)}(this.algodClient,Aa[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return oi.apply(this,arguments)}(this.algodClient,Ia[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,eo(this.algodClient,Ia[this.network]);case 17:return f=e.sent,e.next=20,_o({algodClient:this.algodClient,address:c,appId:Aa[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=Po(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ii({stakingVotingAppId:Ia[this.network],vaultAppId:Aa[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Js(this.algodClient,va[this.network]);case 3:return n=t.sent,t.abrupt("return",new Ro(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,_o({algodClient:this.algodClient,address:c,appId:Aa[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*ga,h=d.firstPeriodTimestamp+(r+a)*ga,g=[],x=m;x=Math.floor(Date.now()/xa)||c.votingEndTimestamp<=Math.floor(Date.now()/xa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,_o({algodClient:this.algodClient,address:i,appId:Aa[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=Po(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*Ea)),m=ai(i,f),e.next=33,qs(this.algodClient,wa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",Oo({proposalVotingAppId:wa[this.network],vaultAppId:Aa[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Js(this.algodClient,wa[this.network]);case 2:return t=e.sent,e.abrupt("return",new ro(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),fi=2500,mi=400,hi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return Ii(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),gi=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),xi=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*Ds/Ps)),s=t-r;return s<0?0:Ws(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Ba,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=it,exports.isSwapQuoteErrorCausedByAmount=gt,exports.poolUtils=$t,exports.prepareCommitTransactions=function(e){return ha.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return $r.apply(this,arguments)},exports.redeemExcessAsset=function(e){return Kr.apply(this,arguments)},exports.sendAndWaitRawTransaction=Ie,exports.sumUpTxnFees=we,exports.tinymanContract_v1_1=wt,exports.tinymanContract_v2=Tt,exports.tinymanJSSDKConfig=_e; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({}),Ae=6;function Ie(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function ve(e,t){return 0===Buffer.compare(e,t)}function we(e){for(var t=0n,n=0;nr?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Ge(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Xe(e){return Number(e)===Be}function Qe(e){return Number(e.id)}var qe,We,ze=function(e){return e.Direct="direct",e.Router="router",e}({}),Je=4*e.ALGORAND_MIN_TX_FEE,Ke=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),Ye=(l(qe={},fe.FixedInput,1),l(qe,fe.FixedOutput,2),qe),He=2,$e=le("swap"),Ze=(l(We={},fe.FixedInput,le("fixed-input")),l(We,fe.FixedOutput,le("fixed-output")),We);function et(t){return(Ye[t]+He)*e.ALGORAND_MIN_TX_FEE}function tt(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function nt(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function rt(t){return(Ye[t]+1)*e.ALGORAND_MIN_TX_FEE}function at(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function st(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=at({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function ot(e){if(e.type===ze.Router){var t=ke(e.data);return{id:Qe(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function it(e){if(e.type===ze.Router){var t=Te(e.data);return{id:Qe(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function ut(e){return e.type===ze.Direct?e.data.pool.contractVersion:Ne.V2}function ct(e){return e.type===ze.Direct?e.data.quote.rate:be(e.data)}function pt(e){for(var t=e[0],n=ct(t),r=1;rn&&(t=a,n=s)}return t}function lt(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var dt,ft={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},mt={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"},ht=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ne.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),gt=(l(dt={},Ne.V1_1,{testnet:62368684,mainnet:552635992}),l(dt,Ne.V2,{testnet:148607e3,mainnet:1002541853}),dt);function xt(e,t){var n=gt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function At(){return(At=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:xt(a,o),note:ht.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function It(){return(It=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:xt(a,o),note:ht.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var vt=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),wt=function(n){d(a,vt);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=xt(n.network,Ne.V1_1),a=v(Ge(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:St(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function Pt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function St(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var Dt=le("e");function Ot(e){return Nt.apply(this,arguments)}function Nt(){return(Nt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,Dt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,Dt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,Dt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Bt(){return(Bt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fUt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ut,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Xt(){return Xt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,Et(n,r);case 6:return i=e.sent,u=Pt(i,xt(a,Ne.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Lt[Ne.V1_1].asset1],asset2ID:u[Lt[Ne.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Xt.apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Wt.apply(this,arguments)}function Wt(){return(Wt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=_t(Ne.V2),u=i.generateLogicSigAccountForPool(t),c=xt(r,Ne.V2),p=u.address(),l=Ge(a,o),e.next=8,Et(n,p);case 8:return d=e.sent,f=Pt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Ft.READY:Ft.NOT_CREATED,contractVersion:Ne.V2},f&&(m.asset1ProtocolFees=BigInt(f[Lt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Lt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Lt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Lt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Lt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Lt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Lt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Lt.v2.totalFeeShare]),m.poolTokenID=Number(f[Lt.v2.poolTokenID]),m.asset1ID=Number(f[Lt.v2.asset1]),m.asset2ID=Number(f[Lt.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zt(){return(zt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Et(t,n.account.address());case 2:return r=e.sent,a=Pt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Lt.v2.asset1Reserves]),o.asset2=BigInt(a[Lt.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Lt.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(){return(Jt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,Et(n,r);case 3:return o=e.sent,i=Pt(o,xt(a,Ne.V2)),u=null,i&&(u={asset1ID:i[Lt[Ne.V2].asset1],asset2ID:i[Lt[Ne.V2].asset2],poolTokenID:i[Lt[Ne.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Kt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Jt.apply(this,arguments)},getPoolInfo:qt,getPoolReserves:function(e,t){return zt.apply(this,arguments)}});function Yt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Ht,$t=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=Yt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([jt(e),qt(e)])},isPoolEmpty:Yt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Ft.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Ft.READY}}),Zt=a((l(Ht={},Ne.V1_1,a(a({},Qt),$t)),l(Ht,Ne.V2,a(a({},Kt),$t)),Ht),$t),en=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),tn={ASA_ALGO:96e4,ASA_ASA:859e3};function nn(){return(nn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Ve(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Xe(g),I=xt(a,Ne.V1_1),w=yt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:ht.getAppCallTxnNoteWithClientName(Ne.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:Fe.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:rn(A),suggestedParams:c}),(E=[])[en.FUNDING_TXN]=_,E[en.VALIDATOR_APP_CALL]=b,E[en.POOL_TOKEN_CREATE]=T,E[en.ASSET1_OPT_IN]=k,A||(E[en.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[en.FUNDING_TXN],signers:[u]},{txn:P[en.VALIDATOR_APP_CALL],signers:[y]},{txn:P[en.POOL_TOKEN_CREATE],signers:[y]},{txn:P[en.ASSET1_OPT_IN],signers:[y]}],P[en.ASSET2_OPT_IN]&&S.push({txn:P[en.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rn(e){return e?tn.ASA_ALGO:tn.ASA_ASA}function an(){return(an=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Ge(i,u),f=v(d,2),m=f[0],h=f[1],g=yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===en.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(e){return on.apply(this,arguments)}function on(){return(on=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[en.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function un(){return(un=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,sn({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Zt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var cn={generateTxns:function(e){return nn.apply(this,arguments)},signTxns:function(e){return an.apply(this,arguments)},execute:function(e){return un.apply(this,arguments)},getBootstrapFundingTxnAmount:rn};var pn=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),ln={ASA_ALGO:5,ASA_ASA:6};function dn(e,t){return fn.apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e,t){return hn.apply(this,arguments)}function hn(){return(hn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,dn(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(e,t){return xn.apply(this,arguments)}function xn(){return(xn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,mn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Be,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function An(){return(An=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=xt(a,Ne.V2),l=e.getApplicationAddress(p),d=Ve(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Zt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Ft.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=kt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Xe(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:ht.getAppCallTxnNoteWithClientName(Ne.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=vn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:In(A),suggestedParams:c}),(y=[])[pn.FUNDING_TXN]=w,y[pn.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[pn.FUNDING_TXN]={txn:b[pn.FUNDING_TXN],signers:[u]},T[pn.VALIDATOR_APP_CALL]={txn:b[pn.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function In(e){return function(e,t){var n=_t(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ne.V2,e)+vn(e)+D}function vn(t){return((t?ln.ASA_ALGO:ln.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function wn(){return(wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Ge(i,u),f=v(d,2),m=f[0],h=f[1],g=kt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===pn.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yn(){return(yn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,dn(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Lt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Zt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var bn,Tn={generateTxns:function(e){return An.apply(this,arguments)},signTxns:function(e){return wn.apply(this,arguments)},execute:function(e){return yn.apply(this,arguments)},getBootstrapFundingTxnAmount:In,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+In(t)}};var kn,_n=(l(bn={},Ne.V1_1,cn),l(bn,Ne.V2,Tn),l(bn,"generateTxns",(function(e){return e.contractVersion===Ne.V1_1?cn.generateTxns(e):Tn.generateTxns(e)})),l(bn,"signTxns",(function(e){return e.contractVersion===Ne.V1_1?cn.signTxns(e):Tn.signTxns(e)})),l(bn,"execute",(function(e){return e.contractVersion===Ne.V1_1?cn.execute(e):Tn.execute(e)})),l(bn,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ne.V1_1?cn.getBootstrapFundingTxnAmount(n):Tn.getBootstrapFundingTxnAmount(n)})),bn),En=le("add_liquidity"),Pn=(l(kn={},Ne.V1_1,[le("mint")]),l(kn,Ne.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[En,le("single")],FLEXIBLE_MODE:[En,le("flexible")]}),kn),Sn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),Dn=5*e.ALGORAND_MIN_TX_FEE;function On(){return(On=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:xt(a,Ne.V1_1),appArgs:Pn.v1_1,accounts:[l],note:ht.getAppCallTxnNoteWithClientName(Ne.V1_1),foreignAssets:u.id==Be?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Be?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(){return(Nn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Sn.FEE_TXN?p:n===Sn.ASSET1_IN_TXN?l:n===Sn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(){return(Bn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Sn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,Ot({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,Ot({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Cn,Fn,Ln,Rn=Object.freeze({__proto__:null,execute:function(e){return Bn.apply(this,arguments)},generateTxns:function(e){return On.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Qn(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Qn(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:st({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Xn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Rt)))}function Qn(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function qn(t){return(jn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Wn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function zn(e){return Jn.apply(this,arguments)}function Jn(){return(Jn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,gn(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Kn(){return(Kn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Ve(i,u),m=v(f,2),h=m[0],g=m[1],x=Xe(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:xt(a,Ne.V2),note:ht.getAppCallTxnNoteWithClientName(Ne.V2),appArgs:[].concat(w(Pn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=qn(Mn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Yn=Object.freeze({__proto__:null,execute:zn,generateTxns:function(e){return Kn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Ft.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Gn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Zt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Wn});function Hn(){return(Hn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Xe(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:xt(a,Ne.V2),note:ht.getAppCallTxnNoteWithClientName(Ne.V2),appArgs:[].concat(w(Pn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=qn(Mn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var $n=Object.freeze({__proto__:null,execute:zn,generateTxns:function(e){return Hn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Ft.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Gn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Zt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Wn});function Zn(){return(Zn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Xe(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:xt(o,Ne.V2),appArgs:Pn.v2.INITIAL_LIQUIDITY,note:ht.getAppCallTxnNoteWithClientName(Ne.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=qn(Mn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var er,tr=Object.freeze({__proto__:null,execute:zn,generateTxns:function(e){return Zn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Rt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Rt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Xn(n,r)},slippage:s}},signTxns:Wn}),nr=Object.freeze({__proto__:null,flexible:Yn,initial:tr,withSingleAsset:$n}),rr=(l(er={},Ne.V1_1,Rn),l(er,Ne.V2,nr),er);var ar=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),sr=Object.values(ar).length;function or(){return(or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Xe(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:ht.getAppCallTxnNoteWithClientName(Ne.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[ar.FEE_TXN]=w,y[ar.VALIDATOR_APP_CALL_TXN]=m,y[ar.ASSET1_OUT_TXN]=g,y[ar.ASSET2_OUT_TXN]=A,y[ar.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[ar.FEE_TXN],signers:[p]},{txn:b[ar.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[ar.ASSET1_OUT_TXN],signers:[d]},{txn:b[ar.ASSET2_OUT_TXN],signers:[d]},{txn:b[ar.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===ar.FEE_TXN?c:n===ar.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ur(){return(ur=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[ar.ASSET1_OUT_TXN].txn.amount,c=a[ar.ASSET2_OUT_TXN].txn.amount,p=a[ar.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,Ot({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,Ot({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var cr={generateTxns:function(e){return or.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return ir.apply(this,arguments)},execute:function(e){return ur.apply(this,arguments)}};function pr(){return(pr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==ze.Router){t.next=2;break}return t.abrupt("return",Ee(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Xe(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[$e,Ze[u],e.encodeUint64(v)],note:ht.getAppCallTxnNoteWithClientName(Ne.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=rt(u),(b=[])[Ke.INPUT_TXN]=w,b[Ke.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Ke.INPUT_TXN],signers:[o]},{txn:T[Ke.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function lr(){return(lr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=it(i).id,g=ot(i),e.prev=10,e.next=13,gn(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,gr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,Ar({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(lt(t)&&!lt(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function mr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Zt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=vr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(nt(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function hr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Zt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=st({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(nt(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:ze.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function gr(e){return xr.apply(this,arguments)}function xr(){return(xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=mr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:ze.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(De({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:ze.Router,data:e}}))),e.next=6,fr(c);case 6:return p=e.sent,e.abrupt("return",pt(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ar(e){return Ir.apply(this,arguments)}function Ir(){return(Ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(hr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(De({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:ze.Router,data:e}}))),e.next=5,fr(c);case 5:return p=e.sent,e.abrupt("return",pt(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:st({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var wr={getQuote:function(e){return dr.apply(this,arguments)},getFixedInputSwapQuote:gr,getFixedInputDirectSwapQuote:mr,getFixedOutputDirectSwapQuote:hr,getFixedOutputSwapQuote:Ar,generateTxns:function(e){return pr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return lr.apply(this,arguments)},calculateFixedInputSwap:vr},yr=2,br=le("remove_liquidity"),Tr=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function kr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Rt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function _r(){return(_r=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:ht.getAppCallTxnNoteWithClientName(Ne.V2),appArgs:[br,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(yr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Tr.ASSET_TRANSFER_TXN]=m,g[Tr.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Tr.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Tr.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Er(){return(Er=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:ht.getAppCallTxnNoteWithClientName(Ne.V2),appArgs:[br,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(yr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Tr.ASSET_TRANSFER_TXN]=I,w[Tr.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Tr.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Tr.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pr(){return(Pr=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,gn(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Sr,Dr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=kr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=kr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=wr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=wr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return _r.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return Er.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return Pr.apply(this,arguments)}},Or=(l(Sr={},Ne.V1_1,cr),l(Sr,Ne.V2,Dr),Sr),Nr=3n,Br=1000n,Cr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Cr||{});function Fr(){return(Fr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Cr.FEE_TXN_INDEX?c:n===Cr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(){return(Lr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:ht.getAppCallTxnNoteWithClientName(Ne.V1_1),foreignAssets:c.asset2ID==Be?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Be?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Be?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Ft.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Nr/Br,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:at(d),priceImpact:st(a({inputSupply:i,outputSupply:u},d))};return{type:ze.Direct,data:{pool:t,quote:f}}}function Mr(e){return Ur.apply(this,arguments)}function Ur(){return(Ur=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Ot({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Ot({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Ft.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Br/(Br-Nr),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:ze.Direct,data:{pool:t,quote:h}}}function Vr(e){return Gr.apply(this,arguments)}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Ot({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Ot({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Ft.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Cr.ASSET_IN_TXN_INDEX].txn.assetIndex||Be,amount:i[Cr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Cr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Be,amount:i[Cr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Mr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Vr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Qr,qr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Rr({pool:t,reserves:n,assetIn:r,decimals:a}):jr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Rr,getFixedOutputSwapQuote:jr,generateTxns:function(e){return Lr.apply(this,arguments)},signTxns:function(e){return Fr.apply(this,arguments)},execute:function(e){return Xr.apply(this,arguments)},executeFixedOutputSwap:Vr};function Wr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(lt(n)&&!lt(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function zr(){return(zr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ne.V1_1})))?c.push(new Promise((function(e,t){try{e(qr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ne.V2})),c.push(wr.getFixedInputSwapQuote({amount:r,assetIn:{id:Qe(a),decimals:a.decimals},assetOut:{id:Qe(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Wr(c);case 8:return d=e.sent,e.abrupt("return",pt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(){return(Jr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ne.V1_1})))?c.push(new Promise((function(e,t){try{e(qr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ne.V2})),c.push(wr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Qe(a),decimals:a.decimals},assetOut:{id:Qe(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Wr(c);case 8:return d=e.sent,e.abrupt("return",pt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Kr=(l(Qr={},Ne.V1_1,qr),l(Qr,Ne.V2,wr),l(Qr,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return zr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Jr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Qr,"generateTxns",(function(e){return e.quote.type===ze.Direct&&ut(e.quote)===Ne.V1_1?qr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):wr.generateTxns(e)})),l(Qr,"signTxns",(function(e){if(e.quote.type===ze.Direct&&ut(e.quote)===Ne.V1_1){var t=e.quote.data.pool;return qr.signTxns(a(a({},e),{},{pool:t}))}return wr.signTxns(e)})),l(Qr,"execute",(function(e){return e.contractVersion===Ne.V1_1?qr.execute(e):wr.execute(e)})),Qr);function Yr(){return(Yr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Hr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Hr(e){return $r.apply(this,arguments)}function $r(){return($r=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zr(){return Zr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Zr.apply(this,arguments)}function ea(){return(ea=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:ht.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ta(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function na(e,t,n){return e*t/n}var ra=BigInt(31536e3),aa=BigInt(1e14),sa=BigInt(1e16),oa={testnet:548587153,mainnet:1385499515},ia=14;function ua(e){return ca.apply(this,arguments)}function ca(){return(ca=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(oa[a]),t.next=4,Et(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Be&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pa(){return(pa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(oa[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Xe(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:oa[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ia+1),T=xt(a,Ne.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:oa[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ua({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:oa[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(oa[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:oa[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ia+1),A=xt(l,Ne.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:oa[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function da(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return na(e,sa+t*r/ra,sa)}function fa(){return(fa=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=ta(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ma={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return pa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+ia)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return la.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+ia)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=da(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return na(BigInt(t),n,aa)},calculateDepositReturn:function(e){var t=e.depositAmount,n=da(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*aa/n},getFolksWrapperAppOptInRequiredAssetIDs:ua};function ha(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function ga(){return(ga=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=ha({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var xa=604800,Aa=1e3,Ia={testnet:480164661,mainnet:2200606875},va={testnet:360907790,mainnet:2200609638},wa={testnet:336189106,mainnet:2200608153},ya={testnet:383416252,mainnet:2200608887},ba=2500,Ta=400,ka=604800,_a=le("p"),Ea=le("a"),Pa=24,Sa=111300,Da=ba+Ta*(41+Pa),Oa=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(Oa||{}),Na=new Uint8Array(34);function Ba(e){if(e instanceof Uint8Array&&"Uint8Array"===e.constructor.name)return e;if(e instanceof ArrayBuffer)return new Uint8Array(e);if(ArrayBuffer.isView(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);throw new Error("Unknown type, must be binary type")}var Ca=function(e,t){if(e.length>=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mBa(a(e))})}function Xa({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Va({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Qa=Xa({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Xa({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Xa({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Xa({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Xa({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Xa({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Xa({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Xa({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Xa({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const qa=Ga({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});Ga({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Wa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=Ka;)n[r++]=255&t|za,t/=128;for(;t&Ja;)n[r++]=255&t|za,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},za=128,Ja=-128,Ka=Math.pow(2,31);var Ya=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&$a)<=Ha);return e.bytes=o-n,a},Ha=128,$a=127;var Za=Math.pow(2,7),es=Math.pow(2,14),ts=Math.pow(2,21),ns=Math.pow(2,28),rs=Math.pow(2,35),as=Math.pow(2,42),ss=Math.pow(2,49),os=Math.pow(2,56),is=Math.pow(2,63),us={encode:Wa,decode:Ya,encodingLength:function(e){return e{const[n,r]=cs(e.subarray(t));return t+=r,n};let r=n(),a=As;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??qa;return[qa.prefix,n.decode(`${qa.prefix}${e}`)]}case qa.prefix:{const n=t??qa;return[qa.prefix,n.decode(e)]}case Qa.prefix:{const n=t??Qa;return[Qa.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=xs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return gs(a).set(n,e),a}}const As=112,Is=18;function vs(e,t,n){const r=ls(e),a=r+ls(t),s=new Uint8Array(a+n.byteLength);return ps(e,s,0),ps(t,s,r),s.set(n,a),s}const ws=Symbol.for("@ipld/js-cid/CID");class ys{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ds(this.code,t):t.then((e=>ds(this.code,e)))}throw Error("Unknown type, must be binary type")}}const bs=85,Ts=function({name:e,code:t,encode:n}){return new ys(e,t,n)}({name:"sha2-256",code:18,encode:e=>Ba(n.createHash("sha256").update(e).digest())});var ks=126144e3,_s=1e7,Es=Math.pow(2,64),Ps=48,Ss=21,Ds=48,Os=le("tp"),Ns=le("sc"),Bs=28100,Cs=12900,Fs=421700,Ls=409700;function Rs(e){var t=e/ka;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*ka}function Ms(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Rs(e)+ka;re?r=t?e*e*Es/(2*t):0:r=(e+(e-r))*n/2;return r}function qs(e,t){return Ws.apply(this,arguments)}function Ws(){return(Ws=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",zs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Js(){return(Js=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,Ts.digest(n);case 3:return r=e.sent,a=xs.createV1(bs,r),e.abrupt("return",a.toString(Qa));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ks(){for(var t=arguments.length,n=new Array(t),r=0;r0?Ss-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return fo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return fo(this.powerCount)[1]}}]),e}(),ao=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Qs(this.bias,this.slope,t)}}]),e}(),so=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),oo=p((function e(t){u(this,e),this.slopeDelta=t})),io=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Ss;return e>0?Ss-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return fo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return fo(this.totalPowerCount)[1]}}]),e}();function uo(){return(uo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=po(r),e.prev=1,e.next=4,Us(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",co(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function co(e){var t=Buffer.from(e);return new ro(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function po(t){return e.decodeAddress(t).publicKey}function lo(e){var t=Ie(e),n=new Uint8Array(Os.length+t.length);return n.set(Os,0),n.set(t,Os.length),n}function fo(e){var t=e-1;return[Math.floor(t/Ss),t%Ss]}function mo(t,n){var r=e.decodeAddress(t).publicKey,a=Ie(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function ho(e,t,n){return go.apply(this,arguments)}function go(){return(go=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=Ao(r),e.next=3,Us(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",xo(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function xo(t){return new oo(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function Ao(e){return q(Ns,Ie(e))}function Io(e,t,n){return vo.apply(this,arguments)}function vo(){return(vo=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Ss)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function ko(e){for(var t,n=Ps,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Ie(o),Ie(i),q.apply(void 0,w(u.map((function(e){return Ie(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=qo({sender:s,suggestedParams:c,extraAppArgs:[Ie(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:No,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var zo=8,Jo=le("p"),Ko=le("a"),Yo=le("v"),Ho=28500,$o=12500,Zo=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function ei(t,n){return q(Ko,e.decodeAddress(t).publicKey,Ie(n))}function ti(e){return new Zo(we(e.slice(0,8)),we(e.slice(8,16)),we(e.slice(16,24)),we(e.slice(24,32)),we(e.slice(32,40)),we(e.slice(40,48)),Boolean(we(e.slice(48,49))))}function ni(){return(ni=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=eo(r),e.next=3,Us(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ti(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ri(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>zo)throw new Error("You cannot cast vote for more than ".concat(zo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Ie(e)})))),g=q.apply(void 0,w(u.map((function(e){return Ie(e)})))),x=function(e){return q(Jo,le(e))}(s),A=ei(a,Math.floor(o.index/8192)),I=!p.some((function(e){return ve(e,A)})),v=po(a),y=Math.floor(c/Ss),b=mo(a,y),k=mo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Yo,Ie(e),Ie(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return ve(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Ie(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Ho+_*$o;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ai(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(a<_s)throw new Error("Insufficient lock amount");if(r%ka!=0)throw new Error("Invalid lock end time");var l=Ia[s],d=[{appIndex:l,name:po(o)},{appIndex:l,name:lo(i.lastTotalPowerBoxIndex)},{appIndex:l,name:lo(i.lastTotalPowerBoxIndex+1)}];if(n){var f=mo(o,n.lastAccountPowerBoxIndex);if(d.push({appIndex:l,name:f}),!n.freeAccountPowerSpaceCount){var m=mo(o,n.lastAccountPowerBoxIndex+1);d.push({appIndex:l,name:m})}}else{var h=mo(o,0);d.push({appIndex:l,name:h})}var g=Ao(r);d.push({appIndex:l,name:g});var x,A=Ms(i.lastTotalPowerTimestamp,Date.now()/1e3),I=A.length,v=T(A);try{for(v.s();!(x=v.n()).done;){var w=x.value;if(w%ka==0){var y=Ao(w);d.push({appIndex:l,name:y})}}}catch(e){v.e(e)}finally{v.f()}var b=0;n?n.freeAccountPowerSpaceCount||(b+=Fs):(b+=Bs,b+=Fs),I>i.freeTotalPowerSpaceCount&&(b+=Ls),c||(b+=Cs);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Le[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Ie(r)],boxes:d.slice(0,8),note:p}),qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function si(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(r<_s)throw new Error("Insufficient lock amount");var c,p=po(s),l=mo(s,n.lastAccountPowerBoxIndex),d=lo(o.lastTotalPowerBoxIndex),f=lo(o.lastTotalPowerBoxIndex+1),m=Ao(n.lockEndTime),h=[{appIndex:Ia[a],name:p},{appIndex:Ia[a],name:l},{appIndex:Ia[a],name:d},{appIndex:Ia[a],name:f},{appIndex:Ia[a],name:m}],g=Ms(o.lastTotalPowerTimestamp,Date.now()/1e3),x=g.length,A=T(g);try{for(A.s();!(c=A.n()).done;){var I=c.value;I%ka==0&&(m=Ao(I),h.push({appIndex:Ia[a],name:m}))}}catch(e){A.e(e)}finally{A.f()}var v=0;if(x>o.freeTotalPowerSpaceCount&&(v+=Ls),!n.freeAccountPowerSpaceCount){var w=mo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:Ia[a],name:w}),v+=Fs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Le[a],from:s,to:e.getApplicationAddress(Ia[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:Ia[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),qo({sender:s,suggestedParams:i,index:Ia[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(Ia[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function oi(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%ka)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=po(o),l=mo(o,n.lastAccountPowerBoxIndex),d=lo(i.lastTotalPowerBoxIndex),f=lo(i.lastTotalPowerBoxIndex+1),m=Ao(n.lockEndTime),h=Ao(a),g=[{appIndex:Ia[r],name:p},{appIndex:Ia[r],name:l},{appIndex:Ia[r],name:d},{appIndex:Ia[r],name:f},{appIndex:Ia[r],name:m},{appIndex:Ia[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=mo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:Ia[r],name:x})}var A,I=Ms(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%ka==0){var b=Ao(y);g.push({appIndex:Ia[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Cs),n.freeAccountPowerSpaceCount||(k+=Fs),v>i.freeTotalPowerSpaceCount&&(k+=Ls);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:Ia[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Ie(a)],boxes:g.slice(0,8),note:c}),qo({sender:o,suggestedParams:u,index:Ia[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(Ia[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function ii(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=po(a),u=mo(a,n.lastAccountPowerBoxIndex),c=mo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:Ia[r],name:i},{appIndex:Ia[r],name:u},{appIndex:Ia[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:Ia[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Le[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(Ia[r]),amount:Fs});d.unshift(f)}return e.assignGroupID(d)}var ui=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/Aa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,yo({algodClient:this.algodClient,address:this.userAddress,appId:Ia[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=To(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Xs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/Aa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io(this.algodClient,Ia[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=To(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/ka),u=Math.floor(t/ka)-i,c=Array.from({length:u},(function(e,t){return(i+t)*ka})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Xs(l,g),d=Math.max(d-x,0),e.next=28,ho(this.algodClient,Ia[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/Aa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,yo({algodClient:this.algodClient,address:this.userAddress,appId:Ia[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=To(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Qs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,qs(this.algodClient,Ia[this.network]);case 3:return t=e.sent,e.abrupt("return",new io(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,ho(this.algodClient,Ia[this.network],r);case 17:return l=e.sent,e.abrupt("return",ai({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",si({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,ho(this.algodClient,Ia[this.network],n);case 19:return p=e.sent,e.abrupt("return",oi({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=si({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,ho(this.algodClient,Ia[this.network],r);case 24:return f=e.sent,l=oi({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?Ks(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=ii({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ue({client:this.algodClient,initiatorAddr:a,assetID:Le[this.network]});case 15:c=e.sent,u=Ks([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return uo.apply(this,arguments)}(this.algodClient,Ia[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ni.apply(this,arguments)}(this.algodClient,va[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Ys(this.algodClient,va[this.network]);case 17:return f=e.sent,e.next=20,yo({algodClient:this.algodClient,address:c,appId:Ia[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=To(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ri({stakingVotingAppId:va[this.network],vaultAppId:Ia[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,qs(this.algodClient,wa[this.network]);case 3:return n=t.sent,t.abrupt("return",new Bo(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,yo({algodClient:this.algodClient,address:c,appId:Ia[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*xa,h=d.firstPeriodTimestamp+(r+a)*xa,g=[],x=m;x=Math.floor(Date.now()/Aa)||c.votingEndTimestamp<=Math.floor(Date.now()/Aa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,yo({algodClient:this.algodClient,address:i,appId:Ia[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=To(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*Pa)),m=ei(i,f),e.next=33,Vs(this.algodClient,ya[this.network],m);case 33:return h=!e.sent,e.abrupt("return",Eo({proposalVotingAppId:ya[this.network],vaultAppId:Ia[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,qs(this.algodClient,ya[this.network]);case 2:return t=e.sent,e.abrupt("return",new Zs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ci=2500,pi=400,li=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return hi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),di=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),fi=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*Es/ks)),s=t-r;return s<0?0:Xs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Ie,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=nt,exports.isSwapQuoteErrorCausedByAmount=lt,exports.poolUtils=Zt,exports.prepareCommitTransactions=function(e){return ga.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Zr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return Yr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=yt,exports.tinymanContract_v2=kt,exports.tinymanJSSDKConfig=ht; diff --git a/dist/stake.d.ts b/dist/stake.d.ts index a552aa08..083d4e6f 100644 --- a/dist/stake.d.ts +++ b/dist/stake.d.ts @@ -16,4 +16,4 @@ declare function prepareCommitTransactions({ client, stakingAppID, program, requ requiredAssetID?: number; }): Promise; declare function getStakingAppID(network: SupportedNetwork): 51948952 | 649588853; -export { prepareCommitTransactions, getStakingAppID }; +export { getStakingAppID, prepareCommitTransactions }; diff --git a/dist/swap/common/utils.d.ts b/dist/swap/common/utils.d.ts index 6574cb21..730e2754 100644 --- a/dist/swap/common/utils.d.ts +++ b/dist/swap/common/utils.d.ts @@ -40,4 +40,4 @@ declare function getSwapQuoteRate(quote: SwapQuote): number; */ declare function getBestQuote(quotes: SwapQuote[]): SwapQuote; declare function isSwapQuoteErrorCausedByAmount(error: Error): boolean; -export { calculateSwapRate, calculatePriceImpact, getSwapQuotePriceImpact, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getAssetInAndAssetOutFromSwapQuote, getSwapQuoteContractVersion, getSwapTotalFee, getSwapQuoteRate, getBestQuote, isSwapQuoteErrorCausedByAmount }; +export { calculatePriceImpact, calculateSwapRate, getAssetInAndAssetOutFromSwapQuote, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, getBestQuote, getSwapQuoteContractVersion, getSwapQuotePriceImpact, getSwapQuoteRate, getSwapTotalFee, isSwapQuoteErrorCausedByAmount }; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index f4bb3fc2..cd35509e 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -1,4 +1,4 @@ -import { Algodv2 } from "algosdk"; +import { Algodv2, TransactionType } from "algosdk"; import { AssetWithIdAndAmount, TinymanAnalyticsApiAsset } from "../util/asset/assetModels"; import { SignerTransaction, SupportedNetwork } from "../util/commonTypes"; import { PoolReserves, V1PoolInfo, V2PoolInfo } from "../util/pool/poolTypes"; @@ -41,10 +41,6 @@ export interface SwapRoutePool { asset_2: SwapRouteAsset; version: "2.0"; } -export type SwapRoute = { - quote: SwapRouterQuote; - pool: SwapRoutePool; -}[]; export interface SwapRouterQuote { swap_type: SwapType; amount_in: { @@ -68,13 +64,36 @@ export interface FetchSwapRouteQuotesPayload { amount: string; swap_type: SwapType; } -export type SwapRouterResponse = FetchSwapRouteQuotesPayload & { - route: SwapRoute; +export type SwapRouterResponse = Pick & { + asset_in: Pick; + asset_out: Pick; price_impact: string; status: { round_number: string; round_datetime: string; }; + transaction_count: number; + inner_transaction_count: number; + transactions: SwapRouterTransactionRecipe[]; + transaction_fee: string; + transaction_fee_in_input_asset: string; + output_amount: string; +}; +export interface SwapRouterTransactionRecipe { + type: TransactionType; + receiver?: string; + app_id: number; + asset_id: number; + amount: number; + args: string[] | null; + accounts?: string[]; + assets?: number[]; + apps?: number[]; +} +export type SwapRoute = { + poolAddress: string; + asset_in: number; + asset_out: number; }; export type GetSwapQuoteParams = { assetIn: Pick; diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index 5032dd0c..d6f49422 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -1,9 +1,9 @@ import { Algodv2 } from "algosdk"; +import { AssetWithIdAndDecimals } from "../../util/asset/assetModels"; import { InitiatorSigner, SignerTransaction, SupportedNetwork } from "../../util/commonTypes"; import { V2PoolInfo } from "../../util/pool/poolTypes"; -import { DirectSwapQuote, GenerateSwapTxnsParams, SwapQuote, V2SwapExecution } from "../types"; import { SwapType } from "../constants"; -import { AssetWithIdAndDecimals } from "../../util/asset/assetModels"; +import { DirectSwapQuote, GenerateSwapTxnsParams, SwapQuote, V2SwapExecution } from "../types"; declare function generateTxns(params: GenerateSwapTxnsParams): Promise; declare function signTxns({ txGroup, initiatorSigner }: { txGroup: SignerTransaction[]; diff --git a/dist/swap/v2/router/client.d.ts b/dist/swap/v2/router/client.d.ts new file mode 100644 index 00000000..88d18a9e --- /dev/null +++ b/dist/swap/v2/router/client.d.ts @@ -0,0 +1,12 @@ +import algosdk, { Algodv2 } from "algosdk"; +import TinymanBaseClient from "../../../util/client/base/baseClient"; +import { SupportedNetwork } from "../../../util/commonTypes"; +declare class SwapRouterClient extends TinymanBaseClient { + ammAppId: number; + talgoAppId: number; + tAlgoAssetId: number; + constructor(algod: Algodv2, network: SupportedNetwork); + swap(userAddress: string, inputAmount: number, outputAmount: number, route: number[], pools: string[]): Promise; + private generateGroupedReferences; +} +export default SwapRouterClient; diff --git a/dist/swap/v2/router/constants.d.ts b/dist/swap/v2/router/constants.d.ts index 1202a2df..eb536884 100644 --- a/dist/swap/v2/router/constants.d.ts +++ b/dist/swap/v2/router/constants.d.ts @@ -5,3 +5,17 @@ export declare const SWAP_ROUTER_APP_ID: Record; * Inner txn counts according to the swap type */ export declare const SWAP_ROUTER_INNER_TXN_COUNT: Record; +export declare enum V2SwapRouterSwapAppCallArgsIndices { + TxnType = 0, + InputAmount = 1, + OutputAmount = 2, + Routes = 3, + Pools = 4, + Swaps = 5 +} +export declare enum V2SwapRouterAppCallArgsTxnType { + Swap = "swap", + AssetOptIn = "asset_opt_in", + Noop = "noop" +} +export declare const SWAP_ROUTER_SWAP_APP_CALL_ARGS_LENGTH = 6; diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index 1d864361..9fc62ff5 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -1,32 +1,17 @@ -import algosdk, { Algodv2, Transaction } from "algosdk"; +import algosdk, { Algodv2, SuggestedParams } from "algosdk"; import { SupportedNetwork } from "../../../util/commonTypes"; import { SwapType } from "../../constants"; -import { SwapRouterResponse, SwapRoute } from "../../types"; -/** - * Generates txns that would opt in the Swap Router Application to the assets used in the swap router - */ -export declare function generateSwapRouterAssetOptInTransaction({ client, routerAppID, assetIDs, initiatorAddr }: { +import { SwapRouterResponse, SwapRouterTransactionRecipe } from "../../types"; +export declare function generateSwapRouterTxns({ initiatorAddr, client, route, slippage }: { client: Algodv2; - routerAppID: number; - assetIDs: number[]; initiatorAddr: string; -}): Promise; -export declare function generateSwapRouterTxns({ initiatorAddr, client, network, swapType, route, slippage }: { - client: Algodv2; - initiatorAddr: string; - swapType: SwapType; - route: SwapRoute; - network: SupportedNetwork; + route: SwapRouterResponse; slippage: number; }): Promise<{ txn: algosdk.Transaction; signers: string[]; }[]>; -export declare function getSwapRouterAppOptInRequiredAssetIDs({ client, network, assetIDs }: { - client: Algodv2; - network: SupportedNetwork; - assetIDs: number[]; -}): Promise; +export declare function generateSwapRouterTxnFromRecipe(recipe: SwapRouterTransactionRecipe, suggestedParams: SuggestedParams, userAddress: string, assetInAmount: bigint, assetOutAmount: bigint): algosdk.Transaction; export declare function getSwapRoute({ amount, assetInID, assetOutID, swapType, network }: { assetInID: number; assetOutID: number; diff --git a/dist/swap/v2/router/util.d.ts b/dist/swap/v2/router/util.d.ts index 58f63844..4650a526 100644 --- a/dist/swap/v2/router/util.d.ts +++ b/dist/swap/v2/router/util.d.ts @@ -1,23 +1,24 @@ import { SupportedNetwork } from "../../../util/commonTypes"; -import { SwapRoute } from "../../types"; -declare function getSwapRouteRate(route: SwapRoute): number; +import { SwapRoute, SwapRouterResponse } from "../../types"; +declare function getSwapRouteRate(route: SwapRouterResponse): number; declare function getSwapRouterAppID(network: SupportedNetwork): number; -declare function getAssetOutFromSwapRoute(route: SwapRoute): { - asset: import("../../types").SwapRouteAsset; - amount: string; +declare function getAssetOutFromSwapRoute(route: SwapRouterResponse): { + asset: Pick; + amount: bigint; }; -declare function getAssetInFromSwapRoute(route: SwapRoute): { - asset: import("../../types").SwapRouteAsset; - amount: string; +declare function getAssetInFromSwapRoute(route: SwapRouterResponse): { + asset: Pick; + amount: bigint; }; -declare function getAssetInAndOutFromSwapRoute(route: SwapRoute): { +declare function getAssetInAndOutFromSwapRoute(route: SwapRouterResponse): { assetIn: { - asset: import("../../types").SwapRouteAsset; - amount: string; + asset: Pick; + amount: bigint; }; assetOut: { - asset: import("../../types").SwapRouteAsset; - amount: string; + asset: Pick; + amount: bigint; }; }; -export { getSwapRouteRate, getSwapRouterAppID, getAssetOutFromSwapRoute, getAssetInFromSwapRoute, getAssetInAndOutFromSwapRoute }; +declare function getSwapRouteFromRouterResponse(routerResponse: SwapRouterResponse): SwapRoute[]; +export { getAssetInAndOutFromSwapRoute, getAssetInFromSwapRoute, getAssetOutFromSwapRoute, getSwapRouteFromRouterResponse, getSwapRouterAppID, getSwapRouteRate }; diff --git a/dist/util/account/accountConstants.d.ts b/dist/util/account/accountConstants.d.ts new file mode 100644 index 00000000..1e406900 --- /dev/null +++ b/dist/util/account/accountConstants.d.ts @@ -0,0 +1,2 @@ +declare const EXECUTOR_FALLBACK_ADDRESS = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ"; +export { EXECUTOR_FALLBACK_ADDRESS }; diff --git a/dist/util/account/accountUtils.d.ts b/dist/util/account/accountUtils.d.ts index 71d37ebc..101c766d 100644 --- a/dist/util/account/accountUtils.d.ts +++ b/dist/util/account/accountUtils.d.ts @@ -1,7 +1,7 @@ import { Algodv2, IntDecoding } from "algosdk"; -import { V1PoolInfo } from "../pool/poolTypes"; -import { AccountExcessWithinPool, AccountInformation, AccountInformationData, AccountExcess } from "./accountTypes"; import { ContractVersionValue } from "../../contract/types"; +import { V1PoolInfo } from "../pool/poolTypes"; +import { AccountExcess, AccountExcessWithinPool, AccountInformation, AccountInformationData } from "./accountTypes"; export declare function getAccountInformation(client: Algodv2, address: string, intDecoding?: IntDecoding): Promise; /** * @returns the decoded application local state object (both keys and values are decoded) diff --git a/dist/util/pool/v1_1/index.d.ts b/dist/util/pool/v1_1/index.d.ts index 967711f9..ab0dfd71 100644 --- a/dist/util/pool/v1_1/index.d.ts +++ b/dist/util/pool/v1_1/index.d.ts @@ -1,6 +1,6 @@ import { Algodv2 } from "algosdk"; -import { PoolReserves, PoolAssets, V1PoolInfo } from "../poolTypes"; import { SupportedNetwork } from "../../commonTypes"; +import { PoolAssets, PoolReserves, V1PoolInfo } from "../poolTypes"; export declare function getPoolInfo(params: { client: Algodv2; network: SupportedNetwork; diff --git a/dist/util/util.d.ts b/dist/util/util.d.ts index 76ce3287..a3b2ec72 100644 --- a/dist/util/util.d.ts +++ b/dist/util/util.d.ts @@ -1,7 +1,7 @@ /// import { Algodv2 } from "algosdk"; -import { SignerTransaction, TinymanApiErrorShape } from "./commonTypes"; import { AccountInformation } from "./account/accountTypes"; +import { SignerTransaction, TinymanApiErrorShape } from "./commonTypes"; export declare function decodeState({ stateArray, shouldDecodeKeys }: { stateArray: AccountInformation["apps-local-state"][0]["key-value"]; /** @@ -9,7 +9,7 @@ export declare function decodeState({ stateArray, shouldDecodeKeys }: { */ shouldDecodeKeys?: boolean; }): Record; -export declare function joinByteArrays(arrays: Uint8Array[]): Uint8Array; +export declare function joinByteArrays(...arrays: Uint8Array[]): Uint8Array; export declare function getMinBalanceForAccount(accountInfo: any): bigint; /** * Wait until a transaction has been confirmed or rejected by the network diff --git a/src/governance/proposal-voting/constants.ts b/src/governance/proposal-voting/constants.ts index 5a404c7b..2767e923 100644 --- a/src/governance/proposal-voting/constants.ts +++ b/src/governance/proposal-voting/constants.ts @@ -23,9 +23,6 @@ const CREATE_PROPOSAL_DEFAULT_EXECUTION_HASH_ARGUMENT = new Uint8Array( EXECUTION_HASH_SIZE ); -const EXECUTOR_FALLBACK_ADDRESS = - "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ"; - export { PROPOSAL_BOX_PREFIX, PROPOSAL_BOX_SIZE, @@ -35,6 +32,5 @@ export { ATTENDANCE_SHEET_BOX_PREFIX, ATTENDANCE_SHEET_BOX_COST, EXECUTION_HASH_SIZE, - CREATE_PROPOSAL_DEFAULT_EXECUTION_HASH_ARGUMENT, - EXECUTOR_FALLBACK_ADDRESS + CREATE_PROPOSAL_DEFAULT_EXECUTION_HASH_ARGUMENT }; diff --git a/src/governance/proposal-voting/storage.ts b/src/governance/proposal-voting/storage.ts index b1128e44..73a55f11 100644 --- a/src/governance/proposal-voting/storage.ts +++ b/src/governance/proposal-voting/storage.ts @@ -2,9 +2,9 @@ import {decodeAddress, encodeAddress} from "algosdk"; import AlgodClient from "algosdk/dist/types/client/v2/algod/algod"; import {intToBytes} from "../util/utils"; -import {concatUint8Arrays, getRawBoxValue} from "../utils"; +import {getRawBoxValue} from "../utils"; import {ATTENDANCE_SHEET_BOX_PREFIX, PROPOSAL_BOX_PREFIX} from "./constants"; -import {encodeString} from "../../util/util"; +import {encodeString, joinByteArrays} from "../../util/util"; export class Proposal { index: number; @@ -108,11 +108,11 @@ export class ProposalVotingAppGlobalState { } export function getProposalBoxName(proposalId: string) { - return concatUint8Arrays(PROPOSAL_BOX_PREFIX, encodeString(proposalId)); + return joinByteArrays(PROPOSAL_BOX_PREFIX, encodeString(proposalId)); } export function getAttendanceSheetBoxName(address: string, boxIndex: number) { - return concatUint8Arrays( + return joinByteArrays( ATTENDANCE_SHEET_BOX_PREFIX, decodeAddress(address).publicKey, intToBytes(boxIndex) diff --git a/src/governance/proposal-voting/transactions.ts b/src/governance/proposal-voting/transactions.ts index 58414cfc..3e5ba68a 100644 --- a/src/governance/proposal-voting/transactions.ts +++ b/src/governance/proposal-voting/transactions.ts @@ -1,5 +1,7 @@ import algosdk, {decodeAddress, getApplicationAddress, SuggestedParams} from "algosdk"; +import {EXECUTOR_FALLBACK_ADDRESS} from "../../util/account/accountConstants"; +import {encodeString} from "../../util/util"; import {intToBytes} from "../util/utils"; import {ACCOUNT_POWER_BOX_ARRAY_LEN} from "../vault/constants"; import { @@ -12,13 +14,11 @@ import { ACCOUNT_ATTENDANCE_SHEET_BOX_SIZE, ATTENDANCE_SHEET_BOX_COST, CREATE_PROPOSAL_DEFAULT_EXECUTION_HASH_ARGUMENT, - EXECUTOR_FALLBACK_ADDRESS, - ProposalVote, - PROPOSAL_BOX_COST + PROPOSAL_BOX_COST, + ProposalVote } from "./constants"; import {getAttendanceSheetBoxName, getProposalBoxName, Proposal} from "./storage"; import {GenerateProposalMetadataPayload} from "./types"; -import {encodeString} from "../../util/util"; export function generateProposalMetadata({ category, diff --git a/src/governance/rewards/transactions.ts b/src/governance/rewards/transactions.ts index e2ac7d13..41c3feac 100644 --- a/src/governance/rewards/transactions.ts +++ b/src/governance/rewards/transactions.ts @@ -1,5 +1,9 @@ import algosdk, {SuggestedParams, Transaction, getApplicationAddress} from "algosdk"; +import {encodeString, joinByteArrays} from "../../util/util"; +import {prepareBudgetIncreaseTxn} from "../transactions"; +import {intToBytes} from "../util/utils"; +import {ACCOUNT_POWER_BOX_ARRAY_LEN} from "../vault/constants"; import {getAccountPowerBoxName, getAccountStateBoxName} from "../vault/storage"; import { REWARD_CLAIM_SHEET_BOX_COST, @@ -7,11 +11,6 @@ import { REWARD_PERIOD_BOX_ARRAY_LEN } from "./constants"; import {getAccountRewardClaimSheetBoxName, getRewardPeriodBoxName} from "./storage"; -import {ACCOUNT_POWER_BOX_ARRAY_LEN} from "../vault/constants"; -import {intToBytes} from "../util/utils"; -import {prepareBudgetIncreaseTxn} from "../transactions"; -import {concatUint8Arrays} from "../utils"; -import {encodeString} from "../../util/util"; function prepareClaimRewardsTransactions({ rewardsAppId, @@ -117,7 +116,7 @@ function prepareClaimRewardsTransactions({ encodeString("claim_rewards"), intToBytes(periodIndexStart), intToBytes(periodCount), - concatUint8Arrays(...accountPowerIndexes.map((number) => intToBytes(number))) + joinByteArrays(...accountPowerIndexes.map((number) => intToBytes(number))) ], foreignApps: [vaultAppId], foreignAssets: [tinyAssetId], diff --git a/src/governance/staking-voting/storage.ts b/src/governance/staking-voting/storage.ts index e48f1e48..3d3881d8 100644 --- a/src/governance/staking-voting/storage.ts +++ b/src/governance/staking-voting/storage.ts @@ -1,10 +1,10 @@ import {decodeAddress} from "algosdk"; import AlgodClient from "algosdk/dist/types/client/v2/algod/algod"; -import {encodeString} from "../../util/util"; +import {encodeString, joinByteArrays} from "../../util/util"; import {getProposalBoxName} from "../proposal-voting/storage"; import {bytesToInt, intToBytes} from "../util/utils"; -import {concatUint8Arrays, getRawBoxValue} from "../utils"; +import {getRawBoxValue} from "../utils"; import { PROPOSAL_BOX_PREFIX, STAKING_ATTENDANCE_BOX_PREFIX, @@ -41,11 +41,11 @@ class StakingDistributionProposal { } function getStakingDistributionProposalBoxName(proposalId: string): Uint8Array { - return concatUint8Arrays(PROPOSAL_BOX_PREFIX, encodeString(proposalId)); + return joinByteArrays(PROPOSAL_BOX_PREFIX, encodeString(proposalId)); } function getStakingAttendanceSheetBoxName(sender: string, boxIndex: number) { - return concatUint8Arrays( + return joinByteArrays( STAKING_ATTENDANCE_BOX_PREFIX, decodeAddress(sender).publicKey, intToBytes(boxIndex) @@ -53,7 +53,7 @@ function getStakingAttendanceSheetBoxName(sender: string, boxIndex: number) { } function getStakingVoteBoxName(proposalIndex: number, assetId: number): Uint8Array { - return concatUint8Arrays( + return joinByteArrays( STAKING_VOTE_BOX_PREFIX, intToBytes(proposalIndex), intToBytes(assetId) diff --git a/src/governance/staking-voting/transactions.ts b/src/governance/staking-voting/transactions.ts index cdcf189b..2685b9e9 100644 --- a/src/governance/staking-voting/transactions.ts +++ b/src/governance/staking-voting/transactions.ts @@ -7,23 +7,22 @@ import { makePaymentTxnWithSuggestedParamsFromObject } from "algosdk"; +import {encodeString, joinByteArrays} from "../../util/util"; +import {prepareBudgetIncreaseTxn} from "../transactions"; +import {areBuffersEqual, intToBytes, sum} from "../util/utils"; +import {ACCOUNT_POWER_BOX_ARRAY_LEN} from "../vault/constants"; +import {getAccountPowerBoxName, getAccountStateBoxName} from "../vault/storage"; +import { + STAKING_ATTENDANCE_BOX_COST, + STAKING_VOTE_BOX_COST, + STAKING_VOTE_MAX_OPTION_COUNT +} from "./constants"; import { StakingDistributionProposal, getStakingAttendanceSheetBoxName, getStakingDistributionProposalBoxName, getStakingVoteBoxName } from "./storage"; -import { - STAKING_VOTE_MAX_OPTION_COUNT, - STAKING_ATTENDANCE_BOX_COST, - STAKING_VOTE_BOX_COST -} from "./constants"; -import {concatUint8Arrays} from "../utils"; -import {areBuffersEqual, intToBytes, sum} from "../util/utils"; -import {getAccountPowerBoxName, getAccountStateBoxName} from "../vault/storage"; -import {ACCOUNT_POWER_BOX_ARRAY_LEN} from "../vault/constants"; -import {prepareBudgetIncreaseTxn} from "../transactions"; -import {encodeString} from "../../util/util"; function prepareCastVoteForStakingDistributionProposalTransactions({ stakingVotingAppId, @@ -64,10 +63,8 @@ function prepareCastVoteForStakingDistributionProposalTransactions({ throw new Error("The sum of the votes must equal 100%"); } - const argVotes = concatUint8Arrays(...votes.map((vote) => intToBytes(vote))); - const argAssetIds = concatUint8Arrays( - ...assetIds.map((assetId) => intToBytes(assetId)) - ); + const argVotes = joinByteArrays(...votes.map((vote) => intToBytes(vote))); + const argAssetIds = joinByteArrays(...assetIds.map((assetId) => intToBytes(assetId))); const proposalBoxName = getStakingDistributionProposalBoxName(proposalId); diff --git a/src/governance/utils.ts b/src/governance/utils.ts index 30f1e2d0..b4569d1d 100644 --- a/src/governance/utils.ts +++ b/src/governance/utils.ts @@ -115,19 +115,6 @@ function parseGlobalStateFromApplicationInfo(applicationInfo: Record sum + arr.length, 0); - const result = new Uint8Array(totalLength); - let offset = 0; - - for (const arr of arrays) { - result.set(arr, offset); - offset += arr.length; - } - return result; -} - function serializeMetadata(metadata: Record) { return JSON.stringify(metadata, Object.keys(metadata).sort(), 0); } @@ -173,7 +160,6 @@ async function getAllBoxNames(algod: AlgodClient, appId: number) { export { calculateTinyPower, combineAndRegroupTxns, - concatUint8Arrays, doesBoxExist, generateCidFromProposalMetadata, getAllBoxNames, diff --git a/src/governance/vault/storage.ts b/src/governance/vault/storage.ts index 82c840d1..53b592f1 100644 --- a/src/governance/vault/storage.ts +++ b/src/governance/vault/storage.ts @@ -1,8 +1,9 @@ import {bytesToBigInt, decodeAddress} from "algosdk"; import AlgodClient from "algosdk/dist/types/client/v2/algod/algod"; +import {joinByteArrays} from "../../util/util"; import {intToBytes} from "../util/utils"; -import {concatUint8Arrays, getCumulativePowerDelta, getRawBoxValue} from "../utils"; +import {getCumulativePowerDelta, getRawBoxValue} from "../utils"; import { ACCOUNT_POWER_BOX_ARRAY_LEN, ACCOUNT_POWER_SIZE, @@ -207,7 +208,7 @@ function parseBoxSlopeChange(rawBox: Uint8Array) { } function getSlopeChangeBoxName(timestamp: number) { - return concatUint8Arrays(SLOPE_CHANGES, intToBytes(timestamp)); + return joinByteArrays(SLOPE_CHANGES, intToBytes(timestamp)); } async function getAllTotalPowers( diff --git a/src/index.ts b/src/index.ts index abb5a5b8..ab34ec52 100644 --- a/src/index.ts +++ b/src/index.ts @@ -8,11 +8,11 @@ export type { export { BASE_MINIMUM_BALANCE, - MINIMUM_BALANCE_REQUIRED_PER_ASSET, + MINIMUM_ADD_LIQUIDITY_AMOUNT, MINIMUM_BALANCE_REQUIRED_PER_APP, + MINIMUM_BALANCE_REQUIRED_PER_ASSET, MINIMUM_BALANCE_REQUIRED_PER_BYTE_SCHEMA, - MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE, - MINIMUM_ADD_LIQUIDITY_AMOUNT + MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE } from "./util/constant"; export * from "./swap/v2/router"; @@ -24,8 +24,8 @@ export { ASSET_OPT_IN_PROCESS_TXN_COUNT, convertFromBaseUnits, convertToBaseUnits, - sendAndWaitRawTransaction, getTxnGroupID, + sendAndWaitRawTransaction, sumUpTxnFees } from "./util/util"; @@ -33,9 +33,9 @@ export {generateOptIntoAssetTxns} from "./util/asset/assetUtils"; export type { AccountAsset, - TinymanAnalyticsApiAsset, + AssetWithIdAndAmount, IndexerAssetInformation, - AssetWithIdAndAmount + TinymanAnalyticsApiAsset } from "./util/asset/assetModels"; export { @@ -45,31 +45,31 @@ export { } from "./util/asset/assetConstants"; export { - getAccountInformation, calculateAccountMinimumRequiredBalance, - hasSufficientMinimumBalance, - isAccountOptedIntoApp, - getAccountExcessWithinPool, getAccountExcess, - getMinRequiredBalanceToOptIn + getAccountExcessWithinPool, + getAccountInformation, + getMinRequiredBalanceToOptIn, + hasSufficientMinimumBalance, + isAccountOptedIntoApp } from "./util/account/accountUtils"; export type {AccountInformationData} from "./util/account/accountTypes"; -export type {ContractVersionValue} from "./contract/types"; export {CONTRACT_VERSION} from "./contract/constants"; -export {tinymanContract_v2} from "./contract/v2/contract"; +export type {ContractVersionValue} from "./contract/types"; export {tinymanContract_v1_1} from "./contract/v1_1/contract"; +export {tinymanContract_v2} from "./contract/v2/contract"; export { - getValidatorAppID, generateOptIntoValidatorTxns, - OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT, generateOptOutOfValidatorTxns, + getValidatorAppID, + OPT_IN_VALIDATOR_APP_PROCESS_TXN_COUNT, OPT_OUT_VALIDATOR_APP_PROCESS_TXN_COUNT } from "./validator"; -export type {V1PoolInfo, V2PoolInfo, PoolReserves} from "./util/pool/poolTypes"; +export type {PoolReserves, V1PoolInfo, V2PoolInfo} from "./util/pool/poolTypes"; // eslint-disable-next-line no-duplicate-imports export {PoolStatus} from "./util/pool/poolTypes"; @@ -77,50 +77,51 @@ export {poolUtils} from "./util/pool"; export {Bootstrap} from "./bootstrap"; -export type { - V1_1AddLiquidityQuote, - V1_1AddLiquidityExecution -} from "./add-liquidity/v1_1/types"; +export {AddLiquidity} from "./add-liquidity"; +export {getAddLiquidityTotalFee} from "./add-liquidity/util"; export { - V1_1AddLiquidityTxnIndices, - V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT + V1_1_ADD_LIQUIDITY_PROCESS_TXN_COUNT, + V1_1AddLiquidityTxnIndices } from "./add-liquidity/v1_1/constants"; export type { - V2InitialAddLiquidityQuote, + V1_1AddLiquidityExecution, + V1_1AddLiquidityQuote +} from "./add-liquidity/v1_1/types"; +export {V2AddLiquidityTxnIndices, V2AddLiquidityType} from "./add-liquidity/v2/constants"; +export type { + V2AddLiquidityExecution, V2AddLiquidityInternalSwapQuote, V2FlexibleAddLiquidityQuote, - V2SingleAssetInAddLiquidityQuote, - V2AddLiquidityExecution + V2InitialAddLiquidityQuote, + V2SingleAssetInAddLiquidityQuote } from "./add-liquidity/v2/types"; -export {V2AddLiquidityType, V2AddLiquidityTxnIndices} from "./add-liquidity/v2/constants"; -export {getAddLiquidityTotalFee} from "./add-liquidity/util"; -export {AddLiquidity} from "./add-liquidity"; export type { V1_1RemoveLiquidityExecution, V1_1RemoveLiquidityQuote } from "./remove-liquidity/v1_1/types"; export type { + V2RemoveLiquidityExecution, V2RemoveLiquidityQuote, - V2SingleAssetRemoveLiquidityQuote, - V2RemoveLiquidityExecution + V2SingleAssetRemoveLiquidityQuote } from "./remove-liquidity/v2/types"; +export {RemoveLiquidity} from "./remove-liquidity"; export {V1_1_REMOVE_LIQUIDITY_TXN_COUNT} from "./remove-liquidity/v1_1/constants"; export {V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT} from "./remove-liquidity/v2/constants"; -export {RemoveLiquidity} from "./remove-liquidity"; +export {Swap} from "./swap"; +export {SwapType} from "./swap/constants"; export type { - SwapQuote, - V1SwapExecution, - V2SwapExecution, DirectSwapQuote, + GenerateSwapTxnsParams, + SwapQuote, SwapRoute, - GenerateSwapTxnsParams + SwapRouterResponse, + V1SwapExecution, + V2SwapExecution } from "./swap/types"; export * from "./swap/v2/util"; -export {SwapType} from "./swap/constants"; -export {Swap} from "./swap"; // eslint-disable-next-line no-duplicate-imports export {SwapQuoteType} from "./swap/types"; export {SwapQuoteError}; @@ -128,17 +129,17 @@ export {SwapQuoteError}; export {SwapQuoteErrorType} from "./util/error/SwapQuoteError"; export { - redeemExcessAsset, - redeemAllExcessAsset, generateRedeemTxns, - REDEEM_PROCESS_TXN_COUNT + REDEEM_PROCESS_TXN_COUNT, + redeemAllExcessAsset, + redeemExcessAsset } from "./redeem"; export {fetchFolksLendingPool, LendingPool} from "./folks-lending-pools"; export type {FolksLendingPool} from "./folks-lending-pools/types"; -export {prepareCommitTransactions, getStakingAppID} from "./stake"; export {tinymanJSSDKConfig} from "./config"; +export {getStakingAppID, prepareCommitTransactions} from "./stake"; export { combineAndRegroupSignerTxns, getAppCallInnerAssetData @@ -150,16 +151,15 @@ export type {RawBoxCacheValue} from "./governance/types"; export {TinymanSTAlgoClient} from "./liquid-stake/stAlgoClient"; export {TinymanTAlgoClient} from "./liquid-stake/tAlgoClient"; -export {AccountState} from "./governance/vault/storage"; -export {getStartTimestampOfWeek} from "./governance/vault/utils"; export {ProposalVote} from "./governance/proposal-voting/constants"; export {generateProposalMetadata} from "./governance/proposal-voting/transactions"; export type {GenerateProposalMetadataPayload} from "./governance/proposal-voting/types"; export {intToBytes} from "./governance/util/utils"; +export {AccountState} from "./governance/vault/storage"; +export {getStartTimestampOfWeek} from "./governance/vault/utils"; export { - generateCidFromProposalMetadata, calculateTinyPower, combineAndRegroupTxns, - concatUint8Arrays + generateCidFromProposalMetadata } from "./governance/utils"; diff --git a/src/stake.ts b/src/stake.ts index 8e88d45f..e98072d7 100644 --- a/src/stake.ts +++ b/src/stake.ts @@ -39,12 +39,12 @@ function createCommitTxnWithSuggestedParams({ foreignAssets: [liquidityAssetID], accounts: [program.accountAddress], appArgs: [encodeString("commit"), amountEncoded], - note: joinByteArrays([ + note: joinByteArrays( encodeString("tinymanStaking/v1:b"), programIdEncoded, encodeUint64(liquidityAssetID), amountEncoded - ]) + ) }); } @@ -109,4 +109,4 @@ function getStakingAppID(network: SupportedNetwork) { return network === "testnet" ? 51948952 : 649588853; } -export {prepareCommitTransactions, getStakingAppID}; +export {getStakingAppID, prepareCommitTransactions}; diff --git a/src/swap/common/utils.ts b/src/swap/common/utils.ts index 12ae0211..4b7b82dc 100644 --- a/src/swap/common/utils.ts +++ b/src/swap/common/utils.ts @@ -57,11 +57,11 @@ function getSwapQuotePriceImpact(quote: SwapQuote) { function getAssetInFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount { if (quote.type === SwapQuoteType.Router) { - const assetIn = getAssetInFromSwapRoute(quote.data.route); + const assetIn = getAssetInFromSwapRoute(quote.data); return { id: getAssetId(assetIn.asset), - amount: Number(assetIn.amount) + amount: assetIn.amount }; } @@ -73,11 +73,11 @@ function getAssetInFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount { function getAssetOutFromSwapQuote(quote: SwapQuote): AssetWithIdAndAmount { if (quote.type === SwapQuoteType.Router) { - const assetOut = getAssetOutFromSwapRoute(quote.data.route); + const assetOut = getAssetOutFromSwapRoute(quote.data); return { id: getAssetId(assetOut.asset), - amount: Number(assetOut.amount) + amount: assetOut.amount }; } @@ -138,7 +138,7 @@ function getSwapQuoteRate(quote: SwapQuote): number { return quote.data.quote.rate; } - return getSwapRouteRate(quote.data.route); + return getSwapRouteRate(quote.data); } /** @@ -174,15 +174,15 @@ function isSwapQuoteErrorCausedByAmount(error: Error): boolean { } export { - calculateSwapRate, calculatePriceImpact, - getSwapQuotePriceImpact, + calculateSwapRate, + getAssetInAndAssetOutFromSwapQuote, getAssetInFromSwapQuote, getAssetOutFromSwapQuote, - getAssetInAndAssetOutFromSwapQuote, + getBestQuote, getSwapQuoteContractVersion, - getSwapTotalFee, + getSwapQuotePriceImpact, getSwapQuoteRate, - getBestQuote, + getSwapTotalFee, isSwapQuoteErrorCausedByAmount }; diff --git a/src/swap/types.ts b/src/swap/types.ts index a1ff193a..705de9ed 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -1,4 +1,4 @@ -import {Algodv2} from "algosdk"; +import {Algodv2, TransactionType} from "algosdk"; import {AssetWithIdAndAmount, TinymanAnalyticsApiAsset} from "../util/asset/assetModels"; import {SignerTransaction, SupportedNetwork} from "../util/commonTypes"; @@ -48,11 +48,6 @@ export interface SwapRoutePool { version: "2.0"; } -export type SwapRoute = { - quote: SwapRouterQuote; - pool: SwapRoutePool; -}[]; - export interface SwapRouterQuote { swap_type: SwapType; amount_in: { @@ -78,13 +73,41 @@ export interface FetchSwapRouteQuotesPayload { swap_type: SwapType; } -export type SwapRouterResponse = FetchSwapRouteQuotesPayload & { - route: SwapRoute; +export type SwapRouterResponse = Pick< + FetchSwapRouteQuotesPayload, + "amount" | "swap_type" +> & { + asset_in: Pick; + asset_out: Pick; price_impact: string; status: { round_number: string; round_datetime: string; }; + transaction_count: number; + inner_transaction_count: number; + transactions: SwapRouterTransactionRecipe[]; + transaction_fee: string; + transaction_fee_in_input_asset: string; + output_amount: string; +}; + +export interface SwapRouterTransactionRecipe { + type: TransactionType; + receiver?: string; + app_id: number; + asset_id: number; + amount: number; + args: string[] | null; + accounts?: string[]; + assets?: number[]; + apps?: number[]; +} + +export type SwapRoute = { + poolAddress: string; + asset_in: number; + asset_out: number; }; export type GetSwapQuoteParams = { diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index f67a08fd..fc10140d 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -1,17 +1,32 @@ import algosdk, {Algodv2, Transaction} from "algosdk"; -import { - applySlippageToAmount, - convertFromBaseUnits, - sendAndWaitRawTransaction -} from "../../util/util"; +import {tinymanJSSDKConfig} from "../../config"; +import {CONTRACT_VERSION} from "../../contract/constants"; +import {AssetWithIdAndAmount, AssetWithIdAndDecimals} from "../../util/asset/assetModels"; +import {isAlgo} from "../../util/asset/assetUtils"; import { InitiatorSigner, SignerTransaction, SupportedNetwork } from "../../util/commonTypes"; +import SwapQuoteError, {SwapQuoteErrorType} from "../../util/error/SwapQuoteError"; import TinymanError from "../../util/error/TinymanError"; +import {poolUtils} from "../../util/pool"; import {V2PoolInfo} from "../../util/pool/poolTypes"; +import {getAppCallInnerAssetData} from "../../util/transaction/transactionUtils"; +import { + applySlippageToAmount, + convertFromBaseUnits, + sendAndWaitRawTransaction +} from "../../util/util"; +import { + calculatePriceImpact, + getAssetInFromSwapQuote, + getAssetOutFromSwapQuote, + getBestQuote, + isSwapQuoteErrorCausedByAmount +} from "../common/utils"; +import {SwapType} from "../constants"; import { DirectSwapQuote, GenerateSwapTxnsParams, @@ -19,34 +34,19 @@ import { SwapQuoteType, V2SwapExecution } from "../types"; -import {SwapType} from "../constants"; import { V2_SWAP_APP_CALL_ARG_ENCODED, V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, V2SwapTxnGroupIndices } from "./constants"; -import {isAlgo} from "../../util/asset/assetUtils"; -import { - calculatePriceImpact, - getAssetInFromSwapQuote, - getAssetOutFromSwapQuote, - getBestQuote, - isSwapQuoteErrorCausedByAmount -} from "../common/utils"; -import {getAppCallInnerAssetData} from "../../util/transaction/transactionUtils"; -import {AssetWithIdAndAmount, AssetWithIdAndDecimals} from "../../util/asset/assetModels"; -import {tinymanJSSDKConfig} from "../../config"; -import {CONTRACT_VERSION} from "../../contract/constants"; import {generateSwapRouterTxns, getSwapRoute} from "./router/swap-router"; -import {poolUtils} from "../../util/pool"; -import SwapQuoteError, {SwapQuoteErrorType} from "../../util/error/SwapQuoteError"; import {getSwapAppCallFeeAmount, isSwapAssetInAmountLow} from "./util"; async function generateTxns( params: GenerateSwapTxnsParams ): Promise { if (params.quote.type === SwapQuoteType.Router) { - return generateSwapRouterTxns({...params, route: params.quote.data.route}); + return generateSwapRouterTxns({...params, route: params.quote.data}); } const {client, initiatorAddr, slippage, swapType, quote} = params; diff --git a/src/swap/v2/router/constants.ts b/src/swap/v2/router/constants.ts index dee195ac..757cb8f8 100644 --- a/src/swap/v2/router/constants.ts +++ b/src/swap/v2/router/constants.ts @@ -13,3 +13,20 @@ export const SWAP_ROUTER_INNER_TXN_COUNT: Record = { [SwapType.FixedInput]: 7, [SwapType.FixedOutput]: 8 } as const; + +export enum V2SwapRouterSwapAppCallArgsIndices { + TxnType = 0, + InputAmount = 1, + OutputAmount = 2, + Routes = 3, + Pools = 4, + Swaps = 5 +} + +export enum V2SwapRouterAppCallArgsTxnType { + Swap = "swap", + AssetOptIn = "asset_opt_in", + Noop = "noop" +} + +export const SWAP_ROUTER_SWAP_APP_CALL_ARGS_LENGTH = 6; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index b9a15986..96278515 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -1,189 +1,139 @@ -import algosdk, { - Algodv2, - ALGORAND_MIN_TX_FEE, - getApplicationAddress, - Transaction -} from "algosdk"; - -import {CONTRACT_VERSION} from "../../../contract/constants"; -import {AccountInformation} from "../../../util/account/accountTypes"; -import {ALGO_ASSET_ID} from "../../../util/asset/assetConstants"; -import {getAssetId, isAlgo} from "../../../util/asset/assetUtils"; +import algosdk, {Algodv2, bigIntToBytes, SuggestedParams, Transaction} from "algosdk"; +import {toByteArray} from "base64-js"; + import {SupportedNetwork} from "../../../util/commonTypes"; +import {TINYMAN_ANALYTICS_API_BASE_URLS} from "../../../util/constant"; import SwapQuoteError, {SwapQuoteErrorType} from "../../../util/error/SwapQuoteError"; import {applySlippageToAmount, hasTinymanApiErrorShape} from "../../../util/util"; -import {getValidatorAppID} from "../../../validator"; import {SwapType} from "../../constants"; -import {FetchSwapRouteQuotesPayload, SwapRouterResponse, SwapRoute} from "../../types"; import { - V2_SWAP_APP_CALL_ARG_ENCODED, - V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED, - V2_SWAP_ROUTER_APP_ARGS_ENCODED -} from "../constants"; -import {SWAP_ROUTER_INNER_TXN_COUNT} from "./constants"; + FetchSwapRouteQuotesPayload, + SwapRouterResponse, + SwapRouterTransactionRecipe +} from "../../types"; import { - getAssetInFromSwapRoute, - getAssetOutFromSwapRoute, - getSwapRouterAppID -} from "./util"; -import {TINYMAN_ANALYTICS_API_BASE_URLS} from "../../../util/constant"; -import {tinymanJSSDKConfig} from "../../../config"; - -/** - * Generates txns that would opt in the Swap Router Application to the assets used in the swap router - */ -export async function generateSwapRouterAssetOptInTransaction({ - client, - routerAppID, - assetIDs, - initiatorAddr -}: { - client: Algodv2; - routerAppID: number; - assetIDs: number[]; - initiatorAddr: string; -}): Promise { - const suggestedParams = await client.getTransactionParams().do(); - // We need to create a NoOp transaction to opt-in to the assets - const assetOptInTxn = algosdk.makeApplicationNoOpTxnFromObject({ - from: initiatorAddr, - appIndex: routerAppID, - appArgs: [V2_SWAP_ROUTER_APP_ARGS_ENCODED.ASSET_OPT_IN], - foreignAssets: assetIDs, - suggestedParams - }); - // The number of inner transactions is the number of assets we're opting in to - const innerTransactionCount = assetIDs.length; - - /** - * The opt-in transaction fee should cover the total cost of inner transactions, - * and the outer transaction (thus the +1) - */ - assetOptInTxn.fee = ALGORAND_MIN_TX_FEE * (innerTransactionCount + 1); - - return assetOptInTxn; -} + V2SwapRouterAppCallArgsTxnType, + V2SwapRouterSwapAppCallArgsIndices +} from "./constants"; +import {getAssetInFromSwapRoute, getAssetOutFromSwapRoute} from "./util"; export async function generateSwapRouterTxns({ initiatorAddr, client, - network, - swapType, route, slippage }: { client: Algodv2; initiatorAddr: string; - swapType: SwapType; - route: SwapRoute; - network: SupportedNetwork; + route: SwapRouterResponse; slippage: number; }) { const suggestedParams = await client.getTransactionParams().do(); - - const [assetInID, intermediaryAssetID, assetOutID] = [ - getAssetId(route[0].quote.amount_in.asset), - getAssetId(route[0].quote.amount_out.asset), - getAssetId(route[1].quote.amount_out.asset) - ]; const [assetInAmountFromRoute, assetOutAmountFromRoute] = [ - Number(getAssetInFromSwapRoute(route).amount), - Number(getAssetOutFromSwapRoute(route).amount) + getAssetInFromSwapRoute(route).amount, + getAssetOutFromSwapRoute(route).amount ]; - const [pool1Address, pool2Address] = [route[0].pool.address, route[1].pool.address]; - const assetInAmount = - swapType === SwapType.FixedInput + route.swap_type === SwapType.FixedInput ? assetInAmountFromRoute : applySlippageToAmount("positive", slippage, assetInAmountFromRoute); const assetOutAmount = - swapType === SwapType.FixedOutput + route.swap_type === SwapType.FixedOutput ? assetOutAmountFromRoute : applySlippageToAmount("negative", slippage, assetOutAmountFromRoute); - const isAssetInAlgo = isAlgo(assetInID); + const txns: Transaction[] = []; + + route.transactions.forEach((txnRecipe) => { + txns.push( + generateSwapRouterTxnFromRecipe( + txnRecipe, + suggestedParams, + initiatorAddr, + assetInAmount, + assetOutAmount + ) + ); + }); - const routerAppID = getSwapRouterAppID(network); + txns[0].fee = Number(route.transaction_fee); + const txGroup = algosdk.assignGroupID(txns); - const inputTxn = isAssetInAlgo - ? algosdk.makePaymentTxnWithSuggestedParamsFromObject({ - from: initiatorAddr, - to: getApplicationAddress(routerAppID), - amount: assetInAmount, - suggestedParams - }) - : algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ - from: initiatorAddr, - to: getApplicationAddress(routerAppID), + return txGroup.map((txn: Transaction) => ({ + txn, + signers: [initiatorAddr] + })); +} + +export function generateSwapRouterTxnFromRecipe( + recipe: SwapRouterTransactionRecipe, + suggestedParams: SuggestedParams, + userAddress: string, + assetInAmount: bigint, + assetOutAmount: bigint +) { + let txn: Transaction; + + switch (recipe.type) { + case algosdk.TransactionType.pay: { + txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({ + from: userAddress, + to: recipe.receiver!, amount: assetInAmount, - assetIndex: assetInID, suggestedParams }); + txn.fee = 0; - const routerAppCallTxn = algosdk.makeApplicationNoOpTxnFromObject({ - from: initiatorAddr, - appIndex: routerAppID, - appArgs: [ - V2_SWAP_APP_CALL_ARG_ENCODED, - V2_SWAP_APP_CALL_SWAP_TYPE_ARGS_ENCODED[swapType], - algosdk.encodeUint64(assetOutAmount) - ], - foreignApps: [getValidatorAppID(network, CONTRACT_VERSION.V2)], - foreignAssets: [assetInID, intermediaryAssetID, assetOutID], - accounts: [pool1Address, pool2Address], - suggestedParams, - note: tinymanJSSDKConfig.getAppCallTxnNoteWithClientName(CONTRACT_VERSION.V2) - }); + return txn; + } - routerAppCallTxn.fee = - ALGORAND_MIN_TX_FEE * (SWAP_ROUTER_INNER_TXN_COUNT[swapType] + 1); + case algosdk.TransactionType.axfer: { + txn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ + from: userAddress, + to: recipe.receiver!, + amount: assetInAmount, + assetIndex: recipe.asset_id, + suggestedParams + }); + txn.fee = 0; - const txnList = [inputTxn, routerAppCallTxn]; + return txn; + } - const optInRequiredAssetIDs = await getSwapRouterAppOptInRequiredAssetIDs({ - client, - network, - assetIDs: [assetInID, intermediaryAssetID, assetOutID] - }); + case algosdk.TransactionType.appl: { + const appArgs = recipe.args?.map(toByteArray); + const textDecoder = new TextDecoder(); + + if ( + appArgs?.length && + textDecoder.decode(appArgs[V2SwapRouterSwapAppCallArgsIndices.TxnType]) === + V2SwapRouterAppCallArgsTxnType.Swap + ) { + appArgs?.splice( + V2SwapRouterSwapAppCallArgsIndices.InputAmount, + 2, + bigIntToBytes(assetInAmount, 8), + bigIntToBytes(assetOutAmount, 8) + ); + } + + txn = algosdk.makeApplicationNoOpTxnFromObject({ + from: userAddress, + appIndex: recipe.app_id, + appArgs, + accounts: recipe.accounts, + foreignApps: recipe.apps, + foreignAssets: recipe.assets, + suggestedParams + }); + txn.fee = 0; - if (optInRequiredAssetIDs.length > 0) { - const routerAppAssetOptInTxn = await generateSwapRouterAssetOptInTransaction({ - client, - initiatorAddr, - assetIDs: optInRequiredAssetIDs, - routerAppID - }); + return txn; + } - txnList.unshift(routerAppAssetOptInTxn); + default: + throw new Error(`Unknown transaction type: ${recipe.type}`); } - - const txGroup = algosdk.assignGroupID(txnList); - - return txGroup.map((txn: Transaction) => ({ - txn, - signers: [initiatorAddr] - })); -} - -export async function getSwapRouterAppOptInRequiredAssetIDs({ - client, - network, - assetIDs -}: { - client: Algodv2; - network: SupportedNetwork; - assetIDs: number[]; -}) { - const swapRouterAppAddress = getApplicationAddress(getSwapRouterAppID(network)); - const accountInfo = (await client - .accountInformation(swapRouterAppAddress) - .do()) as AccountInformation; - const appOptedInAssetIDs = accountInfo.assets.map((asset) => asset["asset-id"]); - - return assetIDs.filter( - (assetID: number) => - assetID !== ALGO_ASSET_ID && !appOptedInAssetIDs.includes(assetID) - ); } export async function getSwapRoute({ @@ -207,7 +157,7 @@ export async function getSwapRoute({ }; const response = await fetch( - `${TINYMAN_ANALYTICS_API_BASE_URLS[network].v1}/swap-router/quotes/`, + `${TINYMAN_ANALYTICS_API_BASE_URLS[network].v1}/swap-router/quotes-v2/`, { method: "POST", headers: { @@ -235,7 +185,7 @@ export async function getSwapRoute({ } } - if ((serializedResponse as SwapRouterResponse).route.length < 2) { + if (!(serializedResponse as SwapRouterResponse).transactions.length) { throw new SwapQuoteError( SwapQuoteErrorType.SwapRouterNoRouteError, "Swap router couldn't find a route for this swap." diff --git a/src/swap/v2/router/util.ts b/src/swap/v2/router/util.ts index a8c10b10..e3d1ba18 100644 --- a/src/swap/v2/router/util.ts +++ b/src/swap/v2/router/util.ts @@ -1,9 +1,20 @@ +import {encodeAddress} from "algosdk"; +import {toByteArray} from "base64-js"; + +import {bytesToInt} from "../../../governance/util/utils"; +import {EXECUTOR_FALLBACK_ADDRESS} from "../../../util/account/accountConstants"; import {SupportedNetwork} from "../../../util/commonTypes"; import {convertFromBaseUnits} from "../../../util/util"; -import {SwapRoute} from "../../types"; -import {SWAP_ROUTER_APP_ID} from "./constants"; +import {SwapType} from "../../constants"; +import {SwapRoute, SwapRouterResponse} from "../../types"; +import { + SWAP_ROUTER_APP_ID, + SWAP_ROUTER_SWAP_APP_CALL_ARGS_LENGTH, + V2SwapRouterAppCallArgsTxnType, + V2SwapRouterSwapAppCallArgsIndices +} from "./constants"; -function getSwapRouteRate(route: SwapRoute) { +function getSwapRouteRate(route: SwapRouterResponse) { const {assetIn, assetOut} = getAssetInAndOutFromSwapRoute(route); return ( @@ -22,25 +33,91 @@ function getSwapRouterAppID(network: SupportedNetwork) { return id; } -function getAssetOutFromSwapRoute(route: SwapRoute) { - return route[route.length - 1].quote.amount_out; +function getAssetOutFromSwapRoute(route: SwapRouterResponse) { + return { + asset: route.asset_out, + amount: BigInt( + route.swap_type === SwapType.FixedOutput ? route.amount : route.output_amount + ) + }; } -function getAssetInFromSwapRoute(route: SwapRoute) { - return route[0].quote.amount_in; +function getAssetInFromSwapRoute(route: SwapRouterResponse) { + return { + asset: route.asset_in, + amount: BigInt( + route.swap_type === SwapType.FixedInput ? route.amount : route.output_amount + ) + }; } -function getAssetInAndOutFromSwapRoute(route: SwapRoute) { +function getAssetInAndOutFromSwapRoute(route: SwapRouterResponse) { return { assetIn: getAssetInFromSwapRoute(route), assetOut: getAssetOutFromSwapRoute(route) }; } +function getSwapRouteFromRouterResponse(routerResponse: SwapRouterResponse): SwapRoute[] { + let swapRoute: { + poolAddress: string; + asset_in: number; + asset_out: number; + }[] = []; + const outputAssetId = Number(routerResponse.asset_out.id); + const swapAppCallArgs = routerResponse.transactions.find( + (txn) => + txn.args && + Buffer.from( + txn.args[V2SwapRouterSwapAppCallArgsIndices.TxnType], + "base64" + ).toString() === V2SwapRouterAppCallArgsTxnType.Swap + )?.args; + + if (!swapAppCallArgs || !swapAppCallArgs.length) { + return swapRoute; + } + + const routeArg = toByteArray( + swapAppCallArgs[V2SwapRouterSwapAppCallArgsIndices.Routes] + ); + const poolsArg = toByteArray(swapAppCallArgs[V2SwapRouterSwapAppCallArgsIndices.Pools]); + + if (swapAppCallArgs.length === SWAP_ROUTER_SWAP_APP_CALL_ARGS_LENGTH) { + for ( + let i = 0; + i + 8 < routeArg.length && bytesToInt(routeArg.slice(i, i + 8)) !== outputAssetId; + i = i + 8 + ) { + const asset_in = bytesToInt(routeArg.slice(i, i + 8)); + const asset_out = bytesToInt(routeArg.slice(i + 8, i + 16)); + + swapRoute.push({asset_in, asset_out, poolAddress: ""}); + } + + for ( + let i = 0; + i + 32 < poolsArg.length && + encodeAddress(poolsArg.slice(i, i + 32)) !== EXECUTOR_FALLBACK_ADDRESS; + i = i + 32 + ) { + const poolAddressArg = poolsArg.slice(i, i + 32); + const poolAddress = encodeAddress(poolAddressArg); + + if (swapRoute[i / 32]) { + swapRoute[i / 32].poolAddress = poolAddress; + } + } + } + + return swapRoute; +} + export { - getSwapRouteRate, - getSwapRouterAppID, - getAssetOutFromSwapRoute, + getAssetInAndOutFromSwapRoute, getAssetInFromSwapRoute, - getAssetInAndOutFromSwapRoute + getAssetOutFromSwapRoute, + getSwapRouteFromRouterResponse, + getSwapRouterAppID, + getSwapRouteRate }; diff --git a/src/util/account/accountConstants.ts b/src/util/account/accountConstants.ts new file mode 100644 index 00000000..faa9f931 --- /dev/null +++ b/src/util/account/accountConstants.ts @@ -0,0 +1,4 @@ +const EXECUTOR_FALLBACK_ADDRESS = + "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAY5HFKQ"; + +export {EXECUTOR_FALLBACK_ADDRESS}; diff --git a/src/util/account/accountUtils.ts b/src/util/account/accountUtils.ts index db9bd310..229f3dfe 100644 --- a/src/util/account/accountUtils.ts +++ b/src/util/account/accountUtils.ts @@ -1,7 +1,8 @@ import algosdk, {Algodv2, IntDecoding} from "algosdk"; import {fromByteArray, toByteArray} from "base64-js"; -import {V1PoolInfo} from "../pool/poolTypes"; +import {getContract} from "../../contract"; +import {ContractVersionValue} from "../../contract/types"; import { BASE_MINIMUM_BALANCE, MINIMUM_BALANCE_REQUIRED_PER_APP, @@ -11,15 +12,14 @@ import { MINIMUM_BALANCE_REQUIRED_PER_EXTRA_APP_PAGE, MINIMUM_BALANCE_REQUIRED_PER_INT_SCHEMA_VALUE } from "../constant"; +import {V1PoolInfo} from "../pool/poolTypes"; import {decodeState, encodeString, joinByteArrays} from "../util"; import { + AccountExcess, AccountExcessWithinPool, AccountInformation, - AccountInformationData, - AccountExcess + AccountInformationData } from "./accountTypes"; -import {ContractVersionValue} from "../../contract/types"; -import {getContract} from "../../contract"; export function getAccountInformation( client: Algodv2, @@ -131,25 +131,25 @@ export async function getAccountExcessWithinPool({ const state = decodeState({stateArray: keyValue}); const excessAsset1Key = fromByteArray( - joinByteArrays([ + joinByteArrays( algosdk.decodeAddress(poolAddress).publicKey, EXCESS_ENCODED, algosdk.encodeUint64(pool.asset1ID) - ]) + ) ); const excessAsset2Key = fromByteArray( - joinByteArrays([ + joinByteArrays( algosdk.decodeAddress(poolAddress).publicKey, EXCESS_ENCODED, algosdk.encodeUint64(pool.asset2ID) - ]) + ) ); const excessPoolTokenKey = fromByteArray( - joinByteArrays([ + joinByteArrays( algosdk.decodeAddress(poolAddress).publicKey, EXCESS_ENCODED, algosdk.encodeUint64(pool.poolTokenID!) - ]) + ) ); const excessAsset1Value = state[excessAsset1Key]; diff --git a/src/util/pool/v1_1/index.ts b/src/util/pool/v1_1/index.ts index e1cacfba..574f257f 100644 --- a/src/util/pool/v1_1/index.ts +++ b/src/util/pool/v1_1/index.ts @@ -1,23 +1,23 @@ import algosdk, {Algodv2, IntDecoding} from "algosdk"; import {fromByteArray} from "base64-js"; -import {PoolReserves, PoolStatus, PoolAssets, V1PoolInfo} from "../poolTypes"; import {getContract} from "../../../contract"; +import {CONTRACT_VERSION} from "../../../contract/constants"; +import {getValidatorAppID} from "../../../validator"; import { getAccountInformation, getDecodedAccountApplicationLocalState } from "../../account/accountUtils"; import {sortAssetIds} from "../../asset/assetUtils"; +import {SupportedNetwork} from "../../commonTypes"; import { decodeState, - joinByteArrays, + encodeString, getMinBalanceForAccount, - encodeString + joinByteArrays } from "../../util"; import {DECODED_APP_STATE_KEYS} from "../poolConstants"; -import {CONTRACT_VERSION} from "../../../contract/constants"; -import {SupportedNetwork} from "../../commonTypes"; -import {getValidatorAppID} from "../../../validator"; +import {PoolAssets, PoolReserves, PoolStatus, V1PoolInfo} from "../poolTypes"; const OUTSTANDING_ENCODED = encodeString("o"); const TOTAL_LIQUIDITY = 0xffffffffffffffffn; @@ -86,13 +86,13 @@ export async function getPoolReserves( const state = decodeState({stateArray: keyValue}); const outstandingAsset1Key = fromByteArray( - joinByteArrays([OUTSTANDING_ENCODED, algosdk.encodeUint64(pool.asset1ID)]) + joinByteArrays(OUTSTANDING_ENCODED, algosdk.encodeUint64(pool.asset1ID)) ); const outstandingAsset2Key = fromByteArray( - joinByteArrays([OUTSTANDING_ENCODED, algosdk.encodeUint64(pool.asset2ID)]) + joinByteArrays(OUTSTANDING_ENCODED, algosdk.encodeUint64(pool.asset2ID)) ); const outstandingPoolTokenKey = fromByteArray( - joinByteArrays([OUTSTANDING_ENCODED, algosdk.encodeUint64(pool.poolTokenID!)]) + joinByteArrays(OUTSTANDING_ENCODED, algosdk.encodeUint64(pool.poolTokenID!)) ); const outstandingAsset1Value = state[outstandingAsset1Key]; diff --git a/src/util/util.ts b/src/util/util.ts index 26fccbae..893c9118 100644 --- a/src/util/util.ts +++ b/src/util/util.ts @@ -1,7 +1,7 @@ import {Algodv2} from "algosdk"; -import {SignerTransaction, TinymanApiErrorShape} from "./commonTypes"; import {AccountInformation} from "./account/accountTypes"; +import {SignerTransaction, TinymanApiErrorShape} from "./commonTypes"; import TinymanError from "./error/TinymanError"; export function decodeState({ @@ -39,11 +39,9 @@ export function decodeState({ return state; } -export function joinByteArrays(arrays: Uint8Array[]) { +export function joinByteArrays(...arrays: Uint8Array[]): Uint8Array { let totalLength = arrays.reduce((sum, value) => sum + value.length, 0); - - let result = new Uint8Array(totalLength); - + const result = new Uint8Array(totalLength); // for each array - copy it over result // next array is copied right after the previous one let length = 0; @@ -297,7 +295,7 @@ export function sumUpTxnFees(txns: SignerTransaction[]): number { } export function getTxnGroupID(txns: SignerTransaction[]) { - return bufferToBase64(txns[0].txn.group); + return bufferToBase64(txns[0].txn.group?.buffer); } export function encodeInteger(number: bigint) { From 98037c8b236e4d2ab09348e4a0bb0a5b61db4b78 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Fri, 20 Dec 2024 13:02:03 +0100 Subject: [PATCH 02/15] chore(package): Bump up the app version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 2a3829e9..38a92585 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tinymanorg/tinyman-js-sdk", - "version": "4.1.1", + "version": "4.1.2", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@tinymanorg/tinyman-js-sdk", - "version": "4.1.1", + "version": "4.1.2", "license": "MIT", "dependencies": { "algosdk": "^2.8.0", diff --git a/package.json b/package.json index 212ef6d5..81f7029d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tinymanorg/tinyman-js-sdk", - "version": "4.1.1", + "version": "4.1.2", "description": "Tinyman JS SDK", "author": "Tinyman Core Team", "license": "MIT", From 7cfc44273f53bec4f9e9de887b46b2d254b8f9ef Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 23 Dec 2024 18:23:51 +0100 Subject: [PATCH 03/15] Add deprecation mark to swap v1_1 --- dist/swap/v1_1/index.d.ts | 14 ++++++++++++++ src/swap/v1_1/index.ts | 15 +++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/dist/swap/v1_1/index.d.ts b/dist/swap/v1_1/index.d.ts index 4117d8bc..199dea81 100644 --- a/dist/swap/v1_1/index.d.ts +++ b/dist/swap/v1_1/index.d.ts @@ -4,13 +4,20 @@ import { PoolReserves, V1PoolInfo } from "../../util/pool/poolTypes"; import { GenerateV1_1SwapTxnsParams, SwapQuote, V1SwapExecution } from "../types"; import { SwapType } from "../constants"; import { AssetWithIdAndAmount } from "../../util/asset/assetModels"; +/** + * @deprecated Use `generateTxns` in V2 instead. This will be removed soon. + */ declare function signTxns({ pool, txGroup, initiatorSigner }: { pool: V1PoolInfo; txGroup: SignerTransaction[]; initiatorSigner: InitiatorSigner; }): Promise; +/** + * @deprecated Use `generateTxns` in V2 instead. This will be removed soon. + */ declare function generateTxns({ client, quoteAndPool, swapType, slippage, initiatorAddr }: GenerateV1_1SwapTxnsParams): Promise; /** + * @deprecated Use `getQuote` in V2 instead. This will be removed soon. * * @param type - Type of the swap * @param pool - Information for the pool. @@ -26,6 +33,8 @@ declare function getQuote(type: SwapType, pool: V1PoolInfo, reserves: PoolReserv assetOut: number; }): SwapQuote; /** + * @deprecated Use `getFixedInputSwapQuote` in V2 instead. This will be removed soon. + * * Get a quote for a fixed input swap This does not execute any transactions. * * @param params.pool Information for the pool. @@ -44,6 +53,8 @@ declare function getFixedInputSwapQuote({ pool, reserves, assetIn, decimals }: { }; }): SwapQuote; /** + * @deprecated Use `getFixedOutputSwapQuote` in V2 instead. This will be removed soon. + * * Get a quote for a fixed output swap This does not execute any transactions. * * @param params.pool Information for the pool. @@ -62,6 +73,7 @@ declare function getFixedOutputSwapQuote({ pool, reserves, assetOut, decimals }: }; }): SwapQuote; /** + * @deprecated * Execute a fixed output swap with the desired quantities. * * @param params.client An Algodv2 client. @@ -87,6 +99,8 @@ declare function executeFixedOutputSwap({ client, pool, signedTxns, assetIn, ass initiatorAddr: string; }): Promise>; /** + * @deprecated Use `execute` in V2 instead. This will be removed soon. + * * Execute a swap with the desired quantities. * * @param params.client An Algodv2 client. diff --git a/src/swap/v1_1/index.ts b/src/swap/v1_1/index.ts index d9083422..1f3add75 100644 --- a/src/swap/v1_1/index.ts +++ b/src/swap/v1_1/index.ts @@ -40,6 +40,9 @@ enum SwapTxnGroupIndices { ASSET_OUT_TXN_INDEX } +/** + * @deprecated Use `generateTxns` in V2 instead. This will be removed soon. + */ async function signTxns({ pool, txGroup, @@ -66,6 +69,9 @@ async function signTxns({ return signedTxns; } +/** + * @deprecated Use `generateTxns` in V2 instead. This will be removed soon. + */ async function generateTxns({ client, quoteAndPool, @@ -179,6 +185,7 @@ async function generateTxns({ } /** + * @deprecated Use `getQuote` in V2 instead. This will be removed soon. * * @param type - Type of the swap * @param pool - Information for the pool. @@ -208,6 +215,8 @@ function getQuote( } /** + * @deprecated Use `getFixedInputSwapQuote` in V2 instead. This will be removed soon. + * * Get a quote for a fixed input swap This does not execute any transactions. * * @param params.pool Information for the pool. @@ -298,6 +307,7 @@ function getFixedInputSwapQuote({ } /** + * @deprecated * Execute a fixed input swap with the desired quantities. * * @param params.client An Algodv2 client. @@ -373,6 +383,8 @@ async function executeFixedInputSwap({ }; } /** + * @deprecated Use `getFixedOutputSwapQuote` in V2 instead. This will be removed soon. + * * Get a quote for a fixed output swap This does not execute any transactions. * * @param params.pool Information for the pool. @@ -467,6 +479,7 @@ function getFixedOutputSwapQuote({ } /** + * @deprecated * Execute a fixed output swap with the desired quantities. * * @param params.client An Algodv2 client. @@ -545,6 +558,8 @@ async function executeFixedOutputSwap({ } /** + * @deprecated Use `execute` in V2 instead. This will be removed soon. + * * Execute a swap with the desired quantities. * * @param params.client An Algodv2 client. From 577473f1c7463a723ed096858527742cab0d873e Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Fri, 27 Dec 2024 13:29:18 +0100 Subject: [PATCH 04/15] fix(swap-router): Implement types correctly, get swap route assets from api --- dist/index.js | 2 +- dist/swap/types.d.ts | 30 ++++------------- src/swap/types.ts | 31 ++++-------------- src/swap/v2/router/util.ts | 67 ++++++++------------------------------ 4 files changed, 29 insertions(+), 101 deletions(-) diff --git a/dist/index.js b/dist/index.js index 7a2bcbac..e361b5f9 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({}),Ae=6;function Ie(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function ve(e,t){return 0===Buffer.compare(e,t)}function we(e){for(var t=0n,n=0;nr?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Ge(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Xe(e){return Number(e)===Be}function Qe(e){return Number(e.id)}var qe,We,ze=function(e){return e.Direct="direct",e.Router="router",e}({}),Je=4*e.ALGORAND_MIN_TX_FEE,Ke=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),Ye=(l(qe={},fe.FixedInput,1),l(qe,fe.FixedOutput,2),qe),He=2,$e=le("swap"),Ze=(l(We={},fe.FixedInput,le("fixed-input")),l(We,fe.FixedOutput,le("fixed-output")),We);function et(t){return(Ye[t]+He)*e.ALGORAND_MIN_TX_FEE}function tt(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function nt(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function rt(t){return(Ye[t]+1)*e.ALGORAND_MIN_TX_FEE}function at(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function st(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=at({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function ot(e){if(e.type===ze.Router){var t=ke(e.data);return{id:Qe(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function it(e){if(e.type===ze.Router){var t=Te(e.data);return{id:Qe(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function ut(e){return e.type===ze.Direct?e.data.pool.contractVersion:Ne.V2}function ct(e){return e.type===ze.Direct?e.data.quote.rate:be(e.data)}function pt(e){for(var t=e[0],n=ct(t),r=1;rn&&(t=a,n=s)}return t}function lt(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var dt,ft={type:"logicsig",logic:{bytecode:"BCAIAQCBgICAgICAgPABgICAgICAgIDwAQMEBQYlJA1EMQkyAxJEMRUyAxJEMSAyAxJEMgQiDUQzAQAxABJEMwEQIQcSRDMBGIGCgICAgICAgPABEkQzARkiEjMBGyEEEhA3ARoAgAlib290c3RyYXASEEAAXDMBGSMSRDMBG4ECEjcBGgCABHN3YXASEEACOzMBGyISRDcBGgCABG1pbnQSQAE7NwEaAIAEYnVybhJAAZg3ARoAgAZyZWRlZW0SQAJbNwEaAIAEZmVlcxJAAnkAIQYhBSQjEk0yBBJENwEaARclEjcBGgIXJBIQRDMCADEAEkQzAhAhBBJEMwIhIxJEMwIiIxwSRDMCIyEHEkQzAiQjEkQzAiWACFRNUE9PTDExEkQzAiZRAA+AD1RpbnltYW5Qb29sMS4xIBJEMwIngBNodHRwczovL3RpbnltYW4ub3JnEkQzAikyAxJEMwIqMgMSRDMCKzIDEkQzAiwyAxJEMwMAMQASRDMDECEFEkQzAxElEkQzAxQxABJEMwMSIxJEJCMTQAAQMwEBMwIBCDMDAQg1AUIBsTMEADEAEkQzBBAhBRJEMwQRJBJEMwQUMQASRDMEEiMSRDMBATMCAQgzAwEIMwQBCDUBQgF8MgQhBhJENwEcATEAE0Q3ARwBMwQUEkQzAgAxABNEMwIUMQASRDMDADMCABJEMwIRJRJEMwMUMwMHMwMQIhJNMQASRDMDESMzAxAiEk0kEkQzBAAxABJEMwQUMwIAEkQzAQEzBAEINQFCAREyBCEGEkQ3ARwBMQATRDcBHAEzAhQSRDMDFDMDBzMDECISTTcBHAESRDMCADEAEkQzAhQzBAASRDMCESUSRDMDADEAEkQzAxQzAwczAxAiEk0zBAASRDMDESMzAxAiEk0kEkQzBAAxABNEMwQUMQASRDMBATMCAQgzAwEINQFCAJAyBCEFEkQ3ARwBMQATRDMCADcBHAESRDMCADEAE0QzAwAxABJEMwIUMwIHMwIQIhJNMQASRDMDFDMDBzMDECISTTMCABJEMwEBMwMBCDUBQgA+MgQhBBJENwEcATEAE0QzAhQzAgczAhAiEk03ARwBEkQzAQEzAgEINQFCABIyBCEEEkQzAQEzAgEINQFCAAAzAAAxABNEMwAHMQASRDMACDQBD0M=",address:"ABUKAXTANWR6K6ZYV75DWJEPVWWOU6SFUVRI6QHO44E4SIDLHBTD2CZ64A",size:881,variables:[{name:"TMPL_ASSET_ID_1",type:"int",index:15,length:10},{name:"TMPL_ASSET_ID_2",type:"int",index:5,length:10},{name:"TMPL_VALIDATOR_APP_ID",type:"int",index:74,length:10}],source:"https://github.com/tinymanorg/tinyman-contracts-v1/tree/dc9ab40c58b85c15d58f63a1507e18be76720dbb/contracts/pool_logicsig.teal.tmpl"},name:"pool_logicsig"},mt={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"},ht=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ne.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),gt=(l(dt={},Ne.V1_1,{testnet:62368684,mainnet:552635992}),l(dt,Ne.V2,{testnet:148607e3,mainnet:1002541853}),dt);function xt(e,t){var n=gt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function At(){return(At=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:xt(a,o),note:ht.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function It(){return(It=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:xt(a,o),note:ht.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var vt=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),wt=function(n){d(a,vt);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=xt(n.network,Ne.V1_1),a=v(Ge(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:St(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function Pt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function St(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var Dt=le("e");function Ot(e){return Nt.apply(this,arguments)}function Nt(){return(Nt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,Dt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,Dt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,Dt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Bt(){return(Bt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fUt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ut,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Xt(){return Xt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,Et(n,r);case 6:return i=e.sent,u=Pt(i,xt(a,Ne.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Lt[Ne.V1_1].asset1],asset2ID:u[Lt[Ne.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Xt.apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Wt.apply(this,arguments)}function Wt(){return(Wt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=_t(Ne.V2),u=i.generateLogicSigAccountForPool(t),c=xt(r,Ne.V2),p=u.address(),l=Ge(a,o),e.next=8,Et(n,p);case 8:return d=e.sent,f=Pt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Ft.READY:Ft.NOT_CREATED,contractVersion:Ne.V2},f&&(m.asset1ProtocolFees=BigInt(f[Lt.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Lt.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Lt.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Lt.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Lt.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Lt.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Lt.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Lt.v2.totalFeeShare]),m.poolTokenID=Number(f[Lt.v2.poolTokenID]),m.asset1ID=Number(f[Lt.v2.asset1]),m.asset2ID=Number(f[Lt.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zt(){return(zt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Et(t,n.account.address());case 2:return r=e.sent,a=Pt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Lt.v2.asset1Reserves]),o.asset2=BigInt(a[Lt.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Lt.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jt(){return(Jt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,Et(n,r);case 3:return o=e.sent,i=Pt(o,xt(a,Ne.V2)),u=null,i&&(u={asset1ID:i[Lt[Ne.V2].asset1],asset2ID:i[Lt[Ne.V2].asset2],poolTokenID:i[Lt[Ne.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Kt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Jt.apply(this,arguments)},getPoolInfo:qt,getPoolReserves:function(e,t){return zt.apply(this,arguments)}});function Yt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Ht,$t=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=Yt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([jt(e),qt(e)])},isPoolEmpty:Yt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Ft.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Ft.READY}}),Zt=a((l(Ht={},Ne.V1_1,a(a({},Qt),$t)),l(Ht,Ne.V2,a(a({},Kt),$t)),Ht),$t),en=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),tn={ASA_ALGO:96e4,ASA_ASA:859e3};function nn(){return(nn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Ve(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Xe(g),I=xt(a,Ne.V1_1),w=yt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:ht.getAppCallTxnNoteWithClientName(Ne.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:Fe.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:rn(A),suggestedParams:c}),(E=[])[en.FUNDING_TXN]=_,E[en.VALIDATOR_APP_CALL]=b,E[en.POOL_TOKEN_CREATE]=T,E[en.ASSET1_OPT_IN]=k,A||(E[en.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[en.FUNDING_TXN],signers:[u]},{txn:P[en.VALIDATOR_APP_CALL],signers:[y]},{txn:P[en.POOL_TOKEN_CREATE],signers:[y]},{txn:P[en.ASSET1_OPT_IN],signers:[y]}],P[en.ASSET2_OPT_IN]&&S.push({txn:P[en.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rn(e){return e?tn.ASA_ALGO:tn.ASA_ASA}function an(){return(an=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Ge(i,u),f=v(d,2),m=f[0],h=f[1],g=yt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===en.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sn(e){return on.apply(this,arguments)}function on(){return(on=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[en.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function un(){return(un=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,sn({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Zt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var cn={generateTxns:function(e){return nn.apply(this,arguments)},signTxns:function(e){return an.apply(this,arguments)},execute:function(e){return un.apply(this,arguments)},getBootstrapFundingTxnAmount:rn};var pn=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),ln={ASA_ALGO:5,ASA_ASA:6};function dn(e,t){return fn.apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e,t){return hn.apply(this,arguments)}function hn(){return(hn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,dn(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function gn(e,t){return xn.apply(this,arguments)}function xn(){return(xn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,mn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Be,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function An(){return(An=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=xt(a,Ne.V2),l=e.getApplicationAddress(p),d=Ve(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Zt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Ft.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=kt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Xe(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:ht.getAppCallTxnNoteWithClientName(Ne.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=vn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:In(A),suggestedParams:c}),(y=[])[pn.FUNDING_TXN]=w,y[pn.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[pn.FUNDING_TXN]={txn:b[pn.FUNDING_TXN],signers:[u]},T[pn.VALIDATOR_APP_CALL]={txn:b[pn.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function In(e){return function(e,t){var n=_t(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ne.V2,e)+vn(e)+D}function vn(t){return((t?ln.ASA_ALGO:ln.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function wn(){return(wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Ge(i,u),f=v(d,2),m=f[0],h=f[1],g=kt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===pn.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yn(){return(yn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,dn(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Lt.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Zt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var bn,Tn={generateTxns:function(e){return An.apply(this,arguments)},signTxns:function(e){return wn.apply(this,arguments)},execute:function(e){return yn.apply(this,arguments)},getBootstrapFundingTxnAmount:In,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+In(t)}};var kn,_n=(l(bn={},Ne.V1_1,cn),l(bn,Ne.V2,Tn),l(bn,"generateTxns",(function(e){return e.contractVersion===Ne.V1_1?cn.generateTxns(e):Tn.generateTxns(e)})),l(bn,"signTxns",(function(e){return e.contractVersion===Ne.V1_1?cn.signTxns(e):Tn.signTxns(e)})),l(bn,"execute",(function(e){return e.contractVersion===Ne.V1_1?cn.execute(e):Tn.execute(e)})),l(bn,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ne.V1_1?cn.getBootstrapFundingTxnAmount(n):Tn.getBootstrapFundingTxnAmount(n)})),bn),En=le("add_liquidity"),Pn=(l(kn={},Ne.V1_1,[le("mint")]),l(kn,Ne.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[En,le("single")],FLEXIBLE_MODE:[En,le("flexible")]}),kn),Sn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),Dn=5*e.ALGORAND_MIN_TX_FEE;function On(){return(On=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:xt(a,Ne.V1_1),appArgs:Pn.v1_1,accounts:[l],note:ht.getAppCallTxnNoteWithClientName(Ne.V1_1),foreignAssets:u.id==Be?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Be?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nn(){return(Nn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Sn.FEE_TXN?p:n===Sn.ASSET1_IN_TXN?l:n===Sn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Bn(){return(Bn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Sn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,Ot({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,Ot({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Cn,Fn,Ln,Rn=Object.freeze({__proto__:null,execute:function(e){return Bn.apply(this,arguments)},generateTxns:function(e){return On.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Qn(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Qn(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:st({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Xn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Rt)))}function Qn(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function qn(t){return(jn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Wn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function zn(e){return Jn.apply(this,arguments)}function Jn(){return(Jn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,gn(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Kn(){return(Kn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Ve(i,u),m=v(f,2),h=m[0],g=m[1],x=Xe(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:xt(a,Ne.V2),note:ht.getAppCallTxnNoteWithClientName(Ne.V2),appArgs:[].concat(w(Pn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=qn(Mn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Yn=Object.freeze({__proto__:null,execute:zn,generateTxns:function(e){return Kn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Ft.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Gn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Zt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Wn});function Hn(){return(Hn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Xe(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:xt(a,Ne.V2),note:ht.getAppCallTxnNoteWithClientName(Ne.V2),appArgs:[].concat(w(Pn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=qn(Mn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var $n=Object.freeze({__proto__:null,execute:zn,generateTxns:function(e){return Hn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Ft.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Gn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Zt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Wn});function Zn(){return(Zn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Xe(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:xt(o,Ne.V2),appArgs:Pn.v2.INITIAL_LIQUIDITY,note:ht.getAppCallTxnNoteWithClientName(Ne.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=qn(Mn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var er,tr=Object.freeze({__proto__:null,execute:zn,generateTxns:function(e){return Zn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Rt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Rt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Xn(n,r)},slippage:s}},signTxns:Wn}),nr=Object.freeze({__proto__:null,flexible:Yn,initial:tr,withSingleAsset:$n}),rr=(l(er={},Ne.V1_1,Rn),l(er,Ne.V2,nr),er);var ar=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),sr=Object.values(ar).length;function or(){return(or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Xe(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:ht.getAppCallTxnNoteWithClientName(Ne.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[ar.FEE_TXN]=w,y[ar.VALIDATOR_APP_CALL_TXN]=m,y[ar.ASSET1_OUT_TXN]=g,y[ar.ASSET2_OUT_TXN]=A,y[ar.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[ar.FEE_TXN],signers:[p]},{txn:b[ar.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[ar.ASSET1_OUT_TXN],signers:[d]},{txn:b[ar.ASSET2_OUT_TXN],signers:[d]},{txn:b[ar.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===ar.FEE_TXN?c:n===ar.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ur(){return(ur=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[ar.ASSET1_OUT_TXN].txn.amount,c=a[ar.ASSET2_OUT_TXN].txn.amount,p=a[ar.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,Ot({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,Ot({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var cr={generateTxns:function(e){return or.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return ir.apply(this,arguments)},execute:function(e){return ur.apply(this,arguments)}};function pr(){return(pr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==ze.Router){t.next=2;break}return t.abrupt("return",Ee(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Xe(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[$e,Ze[u],e.encodeUint64(v)],note:ht.getAppCallTxnNoteWithClientName(Ne.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=rt(u),(b=[])[Ke.INPUT_TXN]=w,b[Ke.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Ke.INPUT_TXN],signers:[o]},{txn:T[Ke.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function lr(){return(lr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=it(i).id,g=ot(i),e.prev=10,e.next=13,gn(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,gr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,Ar({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(lt(t)&&!lt(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function mr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Zt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=vr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(nt(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function hr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Zt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=st({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(nt(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:ze.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function gr(e){return xr.apply(this,arguments)}function xr(){return(xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=mr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:ze.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(De({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:ze.Router,data:e}}))),e.next=6,fr(c);case 6:return p=e.sent,e.abrupt("return",pt(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ar(e){return Ir.apply(this,arguments)}function Ir(){return(Ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(hr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(De({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:ze.Router,data:e}}))),e.next=5,fr(c);case 5:return p=e.sent,e.abrupt("return",pt(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function vr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:st({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var wr={getQuote:function(e){return dr.apply(this,arguments)},getFixedInputSwapQuote:gr,getFixedInputDirectSwapQuote:mr,getFixedOutputDirectSwapQuote:hr,getFixedOutputSwapQuote:Ar,generateTxns:function(e){return pr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return lr.apply(this,arguments)},calculateFixedInputSwap:vr},yr=2,br=le("remove_liquidity"),Tr=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function kr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Rt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function _r(){return(_r=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:ht.getAppCallTxnNoteWithClientName(Ne.V2),appArgs:[br,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(yr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Tr.ASSET_TRANSFER_TXN]=m,g[Tr.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Tr.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Tr.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Er(){return(Er=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:ht.getAppCallTxnNoteWithClientName(Ne.V2),appArgs:[br,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(yr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Tr.ASSET_TRANSFER_TXN]=I,w[Tr.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Tr.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Tr.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pr(){return(Pr=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,gn(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Sr,Dr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=kr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=kr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=wr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=wr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return _r.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return Er.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return Pr.apply(this,arguments)}},Or=(l(Sr={},Ne.V1_1,cr),l(Sr,Ne.V2,Dr),Sr),Nr=3n,Br=1000n,Cr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Cr||{});function Fr(){return(Fr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Cr.FEE_TXN_INDEX?c:n===Cr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Lr(){return(Lr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:ht.getAppCallTxnNoteWithClientName(Ne.V1_1),foreignAssets:c.asset2ID==Be?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Be?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Be?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Rr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Ft.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Nr/Br,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:at(d),priceImpact:st(a({inputSupply:i,outputSupply:u},d))};return{type:ze.Direct,data:{pool:t,quote:f}}}function Mr(e){return Ur.apply(this,arguments)}function Ur(){return(Ur=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Ot({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Ot({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Ft.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Br/(Br-Nr),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:ze.Direct,data:{pool:t,quote:h}}}function Vr(e){return Gr.apply(this,arguments)}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Ot({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Ot({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Ft.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Cr.ASSET_IN_TXN_INDEX].txn.assetIndex||Be,amount:i[Cr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Cr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Be,amount:i[Cr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Mr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Vr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Qr,qr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Rr({pool:t,reserves:n,assetIn:r,decimals:a}):jr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Rr,getFixedOutputSwapQuote:jr,generateTxns:function(e){return Lr.apply(this,arguments)},signTxns:function(e){return Fr.apply(this,arguments)},execute:function(e){return Xr.apply(this,arguments)},executeFixedOutputSwap:Vr};function Wr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(lt(n)&&!lt(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function zr(){return(zr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ne.V1_1})))?c.push(new Promise((function(e,t){try{e(qr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ne.V2})),c.push(wr.getFixedInputSwapQuote({amount:r,assetIn:{id:Qe(a),decimals:a.decimals},assetOut:{id:Qe(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Wr(c);case 8:return d=e.sent,e.abrupt("return",pt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Jr(){return(Jr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ne.V1_1})))?c.push(new Promise((function(e,t){try{e(qr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ne.V2})),c.push(wr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Qe(a),decimals:a.decimals},assetOut:{id:Qe(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Wr(c);case 8:return d=e.sent,e.abrupt("return",pt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Kr=(l(Qr={},Ne.V1_1,qr),l(Qr,Ne.V2,wr),l(Qr,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return zr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Jr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Qr,"generateTxns",(function(e){return e.quote.type===ze.Direct&&ut(e.quote)===Ne.V1_1?qr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):wr.generateTxns(e)})),l(Qr,"signTxns",(function(e){if(e.quote.type===ze.Direct&&ut(e.quote)===Ne.V1_1){var t=e.quote.data.pool;return qr.signTxns(a(a({},e),{},{pool:t}))}return wr.signTxns(e)})),l(Qr,"execute",(function(e){return e.contractVersion===Ne.V1_1?qr.execute(e):wr.execute(e)})),Qr);function Yr(){return(Yr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Hr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Hr(e){return $r.apply(this,arguments)}function $r(){return($r=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Zr(){return Zr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Zr.apply(this,arguments)}function ea(){return(ea=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:ht.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ta(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function na(e,t,n){return e*t/n}var ra=BigInt(31536e3),aa=BigInt(1e14),sa=BigInt(1e16),oa={testnet:548587153,mainnet:1385499515},ia=14;function ua(e){return ca.apply(this,arguments)}function ca(){return(ca=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(oa[a]),t.next=4,Et(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Be&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function pa(){return(pa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(oa[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Xe(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:oa[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ia+1),T=xt(a,Ne.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:oa[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ua({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:oa[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(oa[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:oa[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ia+1),A=xt(l,Ne.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:oa[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function da(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return na(e,sa+t*r/ra,sa)}function fa(){return(fa=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=ta(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ma={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return pa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+ia)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return la.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+ia)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=da(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return na(BigInt(t),n,aa)},calculateDepositReturn:function(e){var t=e.depositAmount,n=da(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*aa/n},getFolksWrapperAppOptInRequiredAssetIDs:ua};function ha(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function ga(){return(ga=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=ha({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var xa=604800,Aa=1e3,Ia={testnet:480164661,mainnet:2200606875},va={testnet:360907790,mainnet:2200609638},wa={testnet:336189106,mainnet:2200608153},ya={testnet:383416252,mainnet:2200608887},ba=2500,Ta=400,ka=604800,_a=le("p"),Ea=le("a"),Pa=24,Sa=111300,Da=ba+Ta*(41+Pa),Oa=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(Oa||{}),Na=new Uint8Array(34);function Ba(e){if(e instanceof Uint8Array&&"Uint8Array"===e.constructor.name)return e;if(e instanceof ArrayBuffer)return new Uint8Array(e);if(ArrayBuffer.isView(e))return new Uint8Array(e.buffer,e.byteOffset,e.byteLength);throw new Error("Unknown type, must be binary type")}var Ca=function(e,t){if(e.length>=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mBa(a(e))})}function Xa({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Va({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Qa=Xa({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Xa({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Xa({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Xa({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Xa({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Xa({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Xa({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Xa({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Xa({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const qa=Ga({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});Ga({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Wa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=Ka;)n[r++]=255&t|za,t/=128;for(;t&Ja;)n[r++]=255&t|za,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},za=128,Ja=-128,Ka=Math.pow(2,31);var Ya=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&$a)<=Ha);return e.bytes=o-n,a},Ha=128,$a=127;var Za=Math.pow(2,7),es=Math.pow(2,14),ts=Math.pow(2,21),ns=Math.pow(2,28),rs=Math.pow(2,35),as=Math.pow(2,42),ss=Math.pow(2,49),os=Math.pow(2,56),is=Math.pow(2,63),us={encode:Wa,decode:Ya,encodingLength:function(e){return e{const[n,r]=cs(e.subarray(t));return t+=r,n};let r=n(),a=As;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??qa;return[qa.prefix,n.decode(`${qa.prefix}${e}`)]}case qa.prefix:{const n=t??qa;return[qa.prefix,n.decode(e)]}case Qa.prefix:{const n=t??Qa;return[Qa.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=xs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return gs(a).set(n,e),a}}const As=112,Is=18;function vs(e,t,n){const r=ls(e),a=r+ls(t),s=new Uint8Array(a+n.byteLength);return ps(e,s,0),ps(t,s,r),s.set(n,a),s}const ws=Symbol.for("@ipld/js-cid/CID");class ys{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ds(this.code,t):t.then((e=>ds(this.code,e)))}throw Error("Unknown type, must be binary type")}}const bs=85,Ts=function({name:e,code:t,encode:n}){return new ys(e,t,n)}({name:"sha2-256",code:18,encode:e=>Ba(n.createHash("sha256").update(e).digest())});var ks=126144e3,_s=1e7,Es=Math.pow(2,64),Ps=48,Ss=21,Ds=48,Os=le("tp"),Ns=le("sc"),Bs=28100,Cs=12900,Fs=421700,Ls=409700;function Rs(e){var t=e/ka;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*ka}function Ms(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Rs(e)+ka;re?r=t?e*e*Es/(2*t):0:r=(e+(e-r))*n/2;return r}function qs(e,t){return Ws.apply(this,arguments)}function Ws(){return(Ws=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",zs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Js(){return(Js=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,Ts.digest(n);case 3:return r=e.sent,a=xs.createV1(bs,r),e.abrupt("return",a.toString(Qa));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Ks(){for(var t=arguments.length,n=new Array(t),r=0;r0?Ss-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return fo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return fo(this.powerCount)[1]}}]),e}(),ao=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Qs(this.bias,this.slope,t)}}]),e}(),so=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),oo=p((function e(t){u(this,e),this.slopeDelta=t})),io=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Ss;return e>0?Ss-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return fo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return fo(this.totalPowerCount)[1]}}]),e}();function uo(){return(uo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=po(r),e.prev=1,e.next=4,Us(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",co(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function co(e){var t=Buffer.from(e);return new ro(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function po(t){return e.decodeAddress(t).publicKey}function lo(e){var t=Ie(e),n=new Uint8Array(Os.length+t.length);return n.set(Os,0),n.set(t,Os.length),n}function fo(e){var t=e-1;return[Math.floor(t/Ss),t%Ss]}function mo(t,n){var r=e.decodeAddress(t).publicKey,a=Ie(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function ho(e,t,n){return go.apply(this,arguments)}function go(){return(go=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=Ao(r),e.next=3,Us(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",xo(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function xo(t){return new oo(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function Ao(e){return q(Ns,Ie(e))}function Io(e,t,n){return vo.apply(this,arguments)}function vo(){return(vo=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Ss)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function ko(e){for(var t,n=Ps,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Ie(o),Ie(i),q.apply(void 0,w(u.map((function(e){return Ie(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=qo({sender:s,suggestedParams:c,extraAppArgs:[Ie(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:No,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var zo=8,Jo=le("p"),Ko=le("a"),Yo=le("v"),Ho=28500,$o=12500,Zo=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function ei(t,n){return q(Ko,e.decodeAddress(t).publicKey,Ie(n))}function ti(e){return new Zo(we(e.slice(0,8)),we(e.slice(8,16)),we(e.slice(16,24)),we(e.slice(24,32)),we(e.slice(32,40)),we(e.slice(40,48)),Boolean(we(e.slice(48,49))))}function ni(){return(ni=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=eo(r),e.next=3,Us(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ti(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ri(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>zo)throw new Error("You cannot cast vote for more than ".concat(zo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Ie(e)})))),g=q.apply(void 0,w(u.map((function(e){return Ie(e)})))),x=function(e){return q(Jo,le(e))}(s),A=ei(a,Math.floor(o.index/8192)),I=!p.some((function(e){return ve(e,A)})),v=po(a),y=Math.floor(c/Ss),b=mo(a,y),k=mo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Yo,Ie(e),Ie(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return ve(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Ie(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Ho+_*$o;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ai(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(a<_s)throw new Error("Insufficient lock amount");if(r%ka!=0)throw new Error("Invalid lock end time");var l=Ia[s],d=[{appIndex:l,name:po(o)},{appIndex:l,name:lo(i.lastTotalPowerBoxIndex)},{appIndex:l,name:lo(i.lastTotalPowerBoxIndex+1)}];if(n){var f=mo(o,n.lastAccountPowerBoxIndex);if(d.push({appIndex:l,name:f}),!n.freeAccountPowerSpaceCount){var m=mo(o,n.lastAccountPowerBoxIndex+1);d.push({appIndex:l,name:m})}}else{var h=mo(o,0);d.push({appIndex:l,name:h})}var g=Ao(r);d.push({appIndex:l,name:g});var x,A=Ms(i.lastTotalPowerTimestamp,Date.now()/1e3),I=A.length,v=T(A);try{for(v.s();!(x=v.n()).done;){var w=x.value;if(w%ka==0){var y=Ao(w);d.push({appIndex:l,name:y})}}}catch(e){v.e(e)}finally{v.f()}var b=0;n?n.freeAccountPowerSpaceCount||(b+=Fs):(b+=Bs,b+=Fs),I>i.freeTotalPowerSpaceCount&&(b+=Ls),c||(b+=Cs);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Le[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Ie(r)],boxes:d.slice(0,8),note:p}),qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function si(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(r<_s)throw new Error("Insufficient lock amount");var c,p=po(s),l=mo(s,n.lastAccountPowerBoxIndex),d=lo(o.lastTotalPowerBoxIndex),f=lo(o.lastTotalPowerBoxIndex+1),m=Ao(n.lockEndTime),h=[{appIndex:Ia[a],name:p},{appIndex:Ia[a],name:l},{appIndex:Ia[a],name:d},{appIndex:Ia[a],name:f},{appIndex:Ia[a],name:m}],g=Ms(o.lastTotalPowerTimestamp,Date.now()/1e3),x=g.length,A=T(g);try{for(A.s();!(c=A.n()).done;){var I=c.value;I%ka==0&&(m=Ao(I),h.push({appIndex:Ia[a],name:m}))}}catch(e){A.e(e)}finally{A.f()}var v=0;if(x>o.freeTotalPowerSpaceCount&&(v+=Ls),!n.freeAccountPowerSpaceCount){var w=mo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:Ia[a],name:w}),v+=Fs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Le[a],from:s,to:e.getApplicationAddress(Ia[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:Ia[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),qo({sender:s,suggestedParams:i,index:Ia[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(Ia[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function oi(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%ka)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=po(o),l=mo(o,n.lastAccountPowerBoxIndex),d=lo(i.lastTotalPowerBoxIndex),f=lo(i.lastTotalPowerBoxIndex+1),m=Ao(n.lockEndTime),h=Ao(a),g=[{appIndex:Ia[r],name:p},{appIndex:Ia[r],name:l},{appIndex:Ia[r],name:d},{appIndex:Ia[r],name:f},{appIndex:Ia[r],name:m},{appIndex:Ia[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=mo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:Ia[r],name:x})}var A,I=Ms(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%ka==0){var b=Ao(y);g.push({appIndex:Ia[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Cs),n.freeAccountPowerSpaceCount||(k+=Fs),v>i.freeTotalPowerSpaceCount&&(k+=Ls);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:Ia[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Ie(a)],boxes:g.slice(0,8),note:c}),qo({sender:o,suggestedParams:u,index:Ia[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(Ia[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function ii(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=po(a),u=mo(a,n.lastAccountPowerBoxIndex),c=mo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:Ia[r],name:i},{appIndex:Ia[r],name:u},{appIndex:Ia[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:Ia[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Le[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(Ia[r]),amount:Fs});d.unshift(f)}return e.assignGroupID(d)}var ui=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/Aa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,yo({algodClient:this.algodClient,address:this.userAddress,appId:Ia[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=To(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Xs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/Aa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io(this.algodClient,Ia[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=To(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/ka),u=Math.floor(t/ka)-i,c=Array.from({length:u},(function(e,t){return(i+t)*ka})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Xs(l,g),d=Math.max(d-x,0),e.next=28,ho(this.algodClient,Ia[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/Aa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,yo({algodClient:this.algodClient,address:this.userAddress,appId:Ia[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=To(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Qs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,qs(this.algodClient,Ia[this.network]);case 3:return t=e.sent,e.abrupt("return",new io(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,ho(this.algodClient,Ia[this.network],r);case 17:return l=e.sent,e.abrupt("return",ai({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",si({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,ho(this.algodClient,Ia[this.network],n);case 19:return p=e.sent,e.abrupt("return",oi({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=si({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,ho(this.algodClient,Ia[this.network],r);case 24:return f=e.sent,l=oi({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?Ks(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=ii({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ue({client:this.algodClient,initiatorAddr:a,assetID:Le[this.network]});case 15:c=e.sent,u=Ks([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return uo.apply(this,arguments)}(this.algodClient,Ia[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ni.apply(this,arguments)}(this.algodClient,va[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Ys(this.algodClient,va[this.network]);case 17:return f=e.sent,e.next=20,yo({algodClient:this.algodClient,address:c,appId:Ia[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=To(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ri({stakingVotingAppId:va[this.network],vaultAppId:Ia[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,qs(this.algodClient,wa[this.network]);case 3:return n=t.sent,t.abrupt("return",new Bo(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,yo({algodClient:this.algodClient,address:c,appId:Ia[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*xa,h=d.firstPeriodTimestamp+(r+a)*xa,g=[],x=m;x=Math.floor(Date.now()/Aa)||c.votingEndTimestamp<=Math.floor(Date.now()/Aa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,yo({algodClient:this.algodClient,address:i,appId:Ia[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=To(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*Pa)),m=ei(i,f),e.next=33,Vs(this.algodClient,ya[this.network],m);case 33:return h=!e.sent,e.abrupt("return",Eo({proposalVotingAppId:ya[this.network],vaultAppId:Ia[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,qs(this.algodClient,ya[this.network]);case 2:return t=e.sent,e.abrupt("return",new Zs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ci=2500,pi=400,li=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return hi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),di=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),fi=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*Es/ks)),s=t-r;return s<0?0:Xs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Ie,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=nt,exports.isSwapQuoteErrorCausedByAmount=lt,exports.poolUtils=Zt,exports.prepareCommitTransactions=function(e){return ga.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Zr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return Yr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=yt,exports.tinymanContract_v2=kt,exports.tinymanJSSDKConfig=ht; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(e.swap_type===fe.FixedOutput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(e.swap_type===fe.FixedInput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,t.next=3,a.getTransactionParams().do();case 3:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetInID,a=t.assetOutID,o=t.swapType,i=t.network,u={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},e.next=4,fetch("".concat(M[i].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=e.sent,e.next=7,c.json();case 7:if(p=e.sent,c.ok){e.next=14;break}if(s=p,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(p.transactions.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",p);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index cd35509e..fd020ad6 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -41,23 +41,6 @@ export interface SwapRoutePool { asset_2: SwapRouteAsset; version: "2.0"; } -export interface SwapRouterQuote { - swap_type: SwapType; - amount_in: { - asset: SwapRouteAsset; - amount: string; - }; - amount_out: { - asset: SwapRouteAsset; - amount: string; - }; - swap_fees: { - amount: string; - asset: SwapRouteAsset; - }; - price: number; - price_impact: number; -} export interface FetchSwapRouteQuotesPayload { asset_in_id: string; asset_out_id: string; @@ -67,17 +50,18 @@ export interface FetchSwapRouteQuotesPayload { export type SwapRouterResponse = Pick & { asset_in: Pick; asset_out: Pick; - price_impact: string; + price_impact: string | null; status: { round_number: string; round_datetime: string; }; - transaction_count: number; - inner_transaction_count: number; + transaction_count: number | null; transactions: SwapRouterTransactionRecipe[]; - transaction_fee: string; - transaction_fee_in_input_asset: string; - output_amount: string; + transaction_fee: string | null; + transaction_fee_in_input_asset: string | null; + output_amount: string | null; + asset_ids: number[] | null; + pool_ids: string[] | null; }; export interface SwapRouterTransactionRecipe { type: TransactionType; diff --git a/src/swap/types.ts b/src/swap/types.ts index 705de9ed..ff3ac113 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -48,24 +48,6 @@ export interface SwapRoutePool { version: "2.0"; } -export interface SwapRouterQuote { - swap_type: SwapType; - amount_in: { - asset: SwapRouteAsset; - amount: string; - }; - amount_out: { - asset: SwapRouteAsset; - amount: string; - }; - swap_fees: { - amount: string; - asset: SwapRouteAsset; - }; - price: number; - price_impact: number; -} - export interface FetchSwapRouteQuotesPayload { asset_in_id: string; asset_out_id: string; @@ -79,17 +61,18 @@ export type SwapRouterResponse = Pick< > & { asset_in: Pick; asset_out: Pick; - price_impact: string; + price_impact: string | null; status: { round_number: string; round_datetime: string; }; - transaction_count: number; - inner_transaction_count: number; + transaction_count: number | null; transactions: SwapRouterTransactionRecipe[]; - transaction_fee: string; - transaction_fee_in_input_asset: string; - output_amount: string; + transaction_fee: string | null; + transaction_fee_in_input_asset: string | null; + output_amount: string | null; + asset_ids: number[] | null; + pool_ids: string[] | null; }; export interface SwapRouterTransactionRecipe { diff --git a/src/swap/v2/router/util.ts b/src/swap/v2/router/util.ts index e3d1ba18..6454dc0e 100644 --- a/src/swap/v2/router/util.ts +++ b/src/swap/v2/router/util.ts @@ -1,18 +1,8 @@ -import {encodeAddress} from "algosdk"; -import {toByteArray} from "base64-js"; - -import {bytesToInt} from "../../../governance/util/utils"; -import {EXECUTOR_FALLBACK_ADDRESS} from "../../../util/account/accountConstants"; import {SupportedNetwork} from "../../../util/commonTypes"; import {convertFromBaseUnits} from "../../../util/util"; import {SwapType} from "../../constants"; import {SwapRoute, SwapRouterResponse} from "../../types"; -import { - SWAP_ROUTER_APP_ID, - SWAP_ROUTER_SWAP_APP_CALL_ARGS_LENGTH, - V2SwapRouterAppCallArgsTxnType, - V2SwapRouterSwapAppCallArgsIndices -} from "./constants"; +import {SWAP_ROUTER_APP_ID} from "./constants"; function getSwapRouteRate(route: SwapRouterResponse) { const {assetIn, assetOut} = getAssetInAndOutFromSwapRoute(route); @@ -37,7 +27,7 @@ function getAssetOutFromSwapRoute(route: SwapRouterResponse) { return { asset: route.asset_out, amount: BigInt( - route.swap_type === SwapType.FixedOutput ? route.amount : route.output_amount + route.swap_type === SwapType.FixedOutput ? route.amount : route.output_amount ?? 0 ) }; } @@ -46,7 +36,7 @@ function getAssetInFromSwapRoute(route: SwapRouterResponse) { return { asset: route.asset_in, amount: BigInt( - route.swap_type === SwapType.FixedInput ? route.amount : route.output_amount + route.swap_type === SwapType.FixedInput ? route.amount : route.output_amount ?? 0 ) }; } @@ -64,50 +54,21 @@ function getSwapRouteFromRouterResponse(routerResponse: SwapRouterResponse): Swa asset_in: number; asset_out: number; }[] = []; - const outputAssetId = Number(routerResponse.asset_out.id); - const swapAppCallArgs = routerResponse.transactions.find( - (txn) => - txn.args && - Buffer.from( - txn.args[V2SwapRouterSwapAppCallArgsIndices.TxnType], - "base64" - ).toString() === V2SwapRouterAppCallArgsTxnType.Swap - )?.args; - if (!swapAppCallArgs || !swapAppCallArgs.length) { + if ( + !routerResponse.asset_ids || + !routerResponse.pool_ids || + routerResponse.asset_ids.length - 1 !== routerResponse.pool_ids.length + ) { return swapRoute; } - const routeArg = toByteArray( - swapAppCallArgs[V2SwapRouterSwapAppCallArgsIndices.Routes] - ); - const poolsArg = toByteArray(swapAppCallArgs[V2SwapRouterSwapAppCallArgsIndices.Pools]); - - if (swapAppCallArgs.length === SWAP_ROUTER_SWAP_APP_CALL_ARGS_LENGTH) { - for ( - let i = 0; - i + 8 < routeArg.length && bytesToInt(routeArg.slice(i, i + 8)) !== outputAssetId; - i = i + 8 - ) { - const asset_in = bytesToInt(routeArg.slice(i, i + 8)); - const asset_out = bytesToInt(routeArg.slice(i + 8, i + 16)); - - swapRoute.push({asset_in, asset_out, poolAddress: ""}); - } - - for ( - let i = 0; - i + 32 < poolsArg.length && - encodeAddress(poolsArg.slice(i, i + 32)) !== EXECUTOR_FALLBACK_ADDRESS; - i = i + 32 - ) { - const poolAddressArg = poolsArg.slice(i, i + 32); - const poolAddress = encodeAddress(poolAddressArg); - - if (swapRoute[i / 32]) { - swapRoute[i / 32].poolAddress = poolAddress; - } - } + for (let i = 0; i < routerResponse.pool_ids.length; i++) { + swapRoute.push({ + poolAddress: routerResponse.pool_ids[i], + asset_in: routerResponse.asset_ids[i], + asset_out: routerResponse.asset_ids[i + 1] + }); } return swapRoute; From 1eacc9fbeb53b7a59c69e6874d2f87dfdedfb4df Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Fri, 27 Dec 2024 14:42:30 +0100 Subject: [PATCH 05/15] fix(swap-router): Set transactions type correctly --- dist/index.js | 2 +- dist/swap/types.d.ts | 2 +- src/swap/types.ts | 2 +- src/swap/v2/router/swap-router.ts | 4 ++-- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/dist/index.js b/dist/index.js index e361b5f9..fa8e3160 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(e.swap_type===fe.FixedOutput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(e.swap_type===fe.FixedInput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,t.next=3,a.getTransactionParams().do();case 3:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetInID,a=t.assetOutID,o=t.swapType,i=t.network,u={asset_in_id:String(r),asset_out_id:String(a),swap_type:o,amount:String(n)},e.next=4,fetch("".concat(M[i].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(u)}).catch((function(){throw new Error("Network error")}));case 4:return c=e.sent,e.next=7,c.json();case 7:if(p=e.sent,c.ok){e.next=14;break}if(s=p,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(p.type,p.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(p.transactions.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",p);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(e.swap_type===fe.FixedOutput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(e.swap_type===fe.FixedInput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=n.initiatorAddr,o=n.client,i=n.route,u=n.slippage,t.next=3,o.getTransactionParams().do();case 3:return c=t.sent,p=[ve(i).amount,Ie(i).amount],l=p[0],d=p[1],f=i.swap_type===fe.FixedInput?l:te("positive",u,l),m=i.swap_type===fe.FixedOutput?d:te("negative",u,d),h=[],null===(r=i.transactions)||void 0===r||r.forEach((function(e){h.push(Te(e,c,a,f,m))})),h[0].fee=Number(i.transaction_fee),g=e.assignGroupID(h),t.abrupt("return",g.map((function(e){return{txn:e,signers:[a]}})));case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,amount:String(r)},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index fd020ad6..ffeceeac 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -56,7 +56,7 @@ export type SwapRouterResponse = Pick { + route.transactions?.forEach((txnRecipe) => { txns.push( generateSwapRouterTxnFromRecipe( txnRecipe, @@ -185,7 +185,7 @@ export async function getSwapRoute({ } } - if (!(serializedResponse as SwapRouterResponse).transactions.length) { + if (!(serializedResponse as SwapRouterResponse).transactions?.length) { throw new SwapQuoteError( SwapQuoteErrorType.SwapRouterNoRouteError, "Swap router couldn't find a route for this swap." From 300dd2db1008da4929c079205466856d19004a19 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Fri, 27 Dec 2024 15:03:48 +0100 Subject: [PATCH 06/15] fix(swap-router): Return empty array if txns don't exist --- dist/index.js | 2 +- src/swap/v2/router/swap-router.ts | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index fa8e3160..a8aca9c8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(e.swap_type===fe.FixedOutput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(e.swap_type===fe.FixedInput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return a=n.initiatorAddr,o=n.client,i=n.route,u=n.slippage,t.next=3,o.getTransactionParams().do();case 3:return c=t.sent,p=[ve(i).amount,Ie(i).amount],l=p[0],d=p[1],f=i.swap_type===fe.FixedInput?l:te("positive",u,l),m=i.swap_type===fe.FixedOutput?d:te("negative",u,d),h=[],null===(r=i.transactions)||void 0===r||r.forEach((function(e){h.push(Te(e,c,a,f,m))})),h[0].fee=Number(i.transaction_fee),g=e.assignGroupID(h),t.abrupt("return",g.map((function(e){return{txn:e,signers:[a]}})));case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,amount:String(r)},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(e.swap_type===fe.FixedOutput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(e.swap_type===fe.FixedInput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,amount:String(r)},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index ea592a69..0777fc2c 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -28,6 +28,10 @@ export async function generateSwapRouterTxns({ route: SwapRouterResponse; slippage: number; }) { + if (!route.transactions || !route.transaction_fee) { + return []; + } + const suggestedParams = await client.getTransactionParams().do(); const [assetInAmountFromRoute, assetOutAmountFromRoute] = [ getAssetInFromSwapRoute(route).amount, @@ -44,7 +48,7 @@ export async function generateSwapRouterTxns({ const txns: Transaction[] = []; - route.transactions?.forEach((txnRecipe) => { + route.transactions.forEach((txnRecipe) => { txns.push( generateSwapRouterTxnFromRecipe( txnRecipe, From 539f6a6e11d9b50e06b9e226851e6da0fe06b186 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 6 Jan 2025 14:21:07 +0100 Subject: [PATCH 07/15] fix(swap-router): Return output_amount --- dist/index.d.ts | 2 +- dist/index.js | 2 +- dist/swap/types.d.ts | 5 ---- dist/swap/v2/router/util.d.ts | 13 ++++----- src/index.ts | 1 - src/swap/types.ts | 6 ---- src/swap/v2/router/util.ts | 52 +++++++++-------------------------- 7 files changed, 21 insertions(+), 60 deletions(-) diff --git a/dist/index.d.ts b/dist/index.d.ts index cacb14a9..e9497905 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -31,7 +31,7 @@ export { V1_1_REMOVE_LIQUIDITY_TXN_COUNT } from "./remove-liquidity/v1_1/constan export { V2_REMOVE_LIQUIDITY_APP_CALL_INNER_TXN_COUNT } from "./remove-liquidity/v2/constants"; export { Swap } from "./swap"; export { SwapType } from "./swap/constants"; -export type { DirectSwapQuote, GenerateSwapTxnsParams, SwapQuote, SwapRoute, SwapRouterResponse, V1SwapExecution, V2SwapExecution } from "./swap/types"; +export type { DirectSwapQuote, GenerateSwapTxnsParams, SwapQuote, SwapRouterResponse, V1SwapExecution, V2SwapExecution } from "./swap/types"; export * from "./swap/v2/util"; export { SwapQuoteType } from "./swap/types"; export { SwapQuoteError }; diff --git a/dist/index.js b/dist/index.js index a8aca9c8..e116ce71 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(e.swap_type===fe.FixedOutput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(e.swap_type===fe.FixedInput?e.amount:null!==(t=e.output_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,amount:String(r)},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){return{asset:e.asset_in,amount:BigInt(e.amount)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,amount:String(r)},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index ffeceeac..f3ef719e 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -74,11 +74,6 @@ export interface SwapRouterTransactionRecipe { assets?: number[]; apps?: number[]; } -export type SwapRoute = { - poolAddress: string; - asset_in: number; - asset_out: number; -}; export type GetSwapQuoteParams = { assetIn: Pick; assetOut: Pick; diff --git a/dist/swap/v2/router/util.d.ts b/dist/swap/v2/router/util.d.ts index 4650a526..4bf2adeb 100644 --- a/dist/swap/v2/router/util.d.ts +++ b/dist/swap/v2/router/util.d.ts @@ -1,16 +1,16 @@ import { SupportedNetwork } from "../../../util/commonTypes"; -import { SwapRoute, SwapRouterResponse } from "../../types"; -declare function getSwapRouteRate(route: SwapRouterResponse): number; +import { SwapRouterResponse } from "../../types"; +declare function getSwapRouteRate(route: Pick): number; declare function getSwapRouterAppID(network: SupportedNetwork): number; -declare function getAssetOutFromSwapRoute(route: SwapRouterResponse): { +declare function getAssetOutFromSwapRoute(route: Pick): { asset: Pick; amount: bigint; }; -declare function getAssetInFromSwapRoute(route: SwapRouterResponse): { +declare function getAssetInFromSwapRoute(route: Pick): { asset: Pick; amount: bigint; }; -declare function getAssetInAndOutFromSwapRoute(route: SwapRouterResponse): { +declare function getAssetInAndOutFromSwapRoute(route: Pick): { assetIn: { asset: Pick; amount: bigint; @@ -20,5 +20,4 @@ declare function getAssetInAndOutFromSwapRoute(route: SwapRouterResponse): { amount: bigint; }; }; -declare function getSwapRouteFromRouterResponse(routerResponse: SwapRouterResponse): SwapRoute[]; -export { getAssetInAndOutFromSwapRoute, getAssetInFromSwapRoute, getAssetOutFromSwapRoute, getSwapRouteFromRouterResponse, getSwapRouterAppID, getSwapRouteRate }; +export { getAssetInAndOutFromSwapRoute, getAssetInFromSwapRoute, getAssetOutFromSwapRoute, getSwapRouterAppID, getSwapRouteRate }; diff --git a/src/index.ts b/src/index.ts index ab34ec52..5491475e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -116,7 +116,6 @@ export type { DirectSwapQuote, GenerateSwapTxnsParams, SwapQuote, - SwapRoute, SwapRouterResponse, V1SwapExecution, V2SwapExecution diff --git a/src/swap/types.ts b/src/swap/types.ts index 3d0a0311..da512ba6 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -87,12 +87,6 @@ export interface SwapRouterTransactionRecipe { apps?: number[]; } -export type SwapRoute = { - poolAddress: string; - asset_in: number; - asset_out: number; -}; - export type GetSwapQuoteParams = { assetIn: Pick; assetOut: Pick; diff --git a/src/swap/v2/router/util.ts b/src/swap/v2/router/util.ts index 6454dc0e..5617ccb3 100644 --- a/src/swap/v2/router/util.ts +++ b/src/swap/v2/router/util.ts @@ -1,10 +1,11 @@ import {SupportedNetwork} from "../../../util/commonTypes"; import {convertFromBaseUnits} from "../../../util/util"; -import {SwapType} from "../../constants"; -import {SwapRoute, SwapRouterResponse} from "../../types"; +import {SwapRouterResponse} from "../../types"; import {SWAP_ROUTER_APP_ID} from "./constants"; -function getSwapRouteRate(route: SwapRouterResponse) { +function getSwapRouteRate( + route: Pick +) { const {assetIn, assetOut} = getAssetInAndOutFromSwapRoute(route); return ( @@ -23,62 +24,35 @@ function getSwapRouterAppID(network: SupportedNetwork) { return id; } -function getAssetOutFromSwapRoute(route: SwapRouterResponse) { +function getAssetOutFromSwapRoute( + route: Pick +) { return { asset: route.asset_out, - amount: BigInt( - route.swap_type === SwapType.FixedOutput ? route.amount : route.output_amount ?? 0 - ) + amount: BigInt(route.output_amount ?? 0) }; } -function getAssetInFromSwapRoute(route: SwapRouterResponse) { +function getAssetInFromSwapRoute(route: Pick) { return { asset: route.asset_in, - amount: BigInt( - route.swap_type === SwapType.FixedInput ? route.amount : route.output_amount ?? 0 - ) + amount: BigInt(route.amount) }; } -function getAssetInAndOutFromSwapRoute(route: SwapRouterResponse) { +function getAssetInAndOutFromSwapRoute( + route: Pick +) { return { assetIn: getAssetInFromSwapRoute(route), assetOut: getAssetOutFromSwapRoute(route) }; } -function getSwapRouteFromRouterResponse(routerResponse: SwapRouterResponse): SwapRoute[] { - let swapRoute: { - poolAddress: string; - asset_in: number; - asset_out: number; - }[] = []; - - if ( - !routerResponse.asset_ids || - !routerResponse.pool_ids || - routerResponse.asset_ids.length - 1 !== routerResponse.pool_ids.length - ) { - return swapRoute; - } - - for (let i = 0; i < routerResponse.pool_ids.length; i++) { - swapRoute.push({ - poolAddress: routerResponse.pool_ids[i], - asset_in: routerResponse.asset_ids[i], - asset_out: routerResponse.asset_ids[i + 1] - }); - } - - return swapRoute; -} - export { getAssetInAndOutFromSwapRoute, getAssetInFromSwapRoute, getAssetOutFromSwapRoute, - getSwapRouteFromRouterResponse, getSwapRouterAppID, getSwapRouteRate }; From 4c2ba05538ba163d6b40ffdb6dae8d10b42fe55f Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 6 Jan 2025 15:03:28 +0100 Subject: [PATCH 08/15] Convert amount into input_amount --- dist/index.js | 2 +- dist/swap/types.d.ts | 3 ++- dist/swap/v2/router/util.d.ts | 6 +++--- src/swap/types.ts | 6 ++---- src/swap/v2/router/util.ts | 16 ++++++++++++---- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/dist/index.js b/dist/index.js index e116ce71..9ef73ccd 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){return{asset:e.asset_in,amount:BigInt(e.amount)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,amount:String(r)},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,amount:String(r)},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index f3ef719e..2129d83a 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -47,7 +47,7 @@ export interface FetchSwapRouteQuotesPayload { amount: string; swap_type: SwapType; } -export type SwapRouterResponse = Pick & { +export type SwapRouterResponse = Pick & { asset_in: Pick; asset_out: Pick; price_impact: string | null; @@ -59,6 +59,7 @@ export type SwapRouterResponse = Pick): number; +declare function getSwapRouteRate(route: Pick): number; declare function getSwapRouterAppID(network: SupportedNetwork): number; declare function getAssetOutFromSwapRoute(route: Pick): { asset: Pick; amount: bigint; }; -declare function getAssetInFromSwapRoute(route: Pick): { +declare function getAssetInFromSwapRoute(route: Pick): { asset: Pick; amount: bigint; }; -declare function getAssetInAndOutFromSwapRoute(route: Pick): { +declare function getAssetInAndOutFromSwapRoute(route: Pick): { assetIn: { asset: Pick; amount: bigint; diff --git a/src/swap/types.ts b/src/swap/types.ts index da512ba6..6b8e816f 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -55,10 +55,7 @@ export interface FetchSwapRouteQuotesPayload { swap_type: SwapType; } -export type SwapRouterResponse = Pick< - FetchSwapRouteQuotesPayload, - "amount" | "swap_type" -> & { +export type SwapRouterResponse = Pick & { asset_in: Pick; asset_out: Pick; price_impact: string | null; @@ -70,6 +67,7 @@ export type SwapRouterResponse = Pick< transactions: SwapRouterTransactionRecipe[] | null; transaction_fee: string | null; transaction_fee_in_input_asset: string | null; + input_amount: string | null; output_amount: string | null; asset_ids: number[] | null; pool_ids: string[] | null; diff --git a/src/swap/v2/router/util.ts b/src/swap/v2/router/util.ts index 5617ccb3..e0630e45 100644 --- a/src/swap/v2/router/util.ts +++ b/src/swap/v2/router/util.ts @@ -4,7 +4,10 @@ import {SwapRouterResponse} from "../../types"; import {SWAP_ROUTER_APP_ID} from "./constants"; function getSwapRouteRate( - route: Pick + route: Pick< + SwapRouterResponse, + "asset_in" | "asset_out" | "input_amount" | "output_amount" + > ) { const {assetIn, assetOut} = getAssetInAndOutFromSwapRoute(route); @@ -33,15 +36,20 @@ function getAssetOutFromSwapRoute( }; } -function getAssetInFromSwapRoute(route: Pick) { +function getAssetInFromSwapRoute( + route: Pick +) { return { asset: route.asset_in, - amount: BigInt(route.amount) + amount: BigInt(route.input_amount ?? 0) }; } function getAssetInAndOutFromSwapRoute( - route: Pick + route: Pick< + SwapRouterResponse, + "asset_in" | "asset_out" | "input_amount" | "output_amount" + > ) { return { assetIn: getAssetInFromSwapRoute(route), From 5cf1b8221c529516c0e2b02cb1f2953371b1c928 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 6 Jan 2025 16:24:23 +0100 Subject: [PATCH 09/15] fix(swap-router): Update payload --- dist/index.js | 2 +- dist/swap/types.d.ts | 3 ++- src/swap/types.ts | 3 ++- src/swap/v2/router/swap-router.ts | 4 +++- 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 9ef73ccd..93f17425 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,amount:String(r)},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,input_amount:i===fe.FixedInput?String(r):void 0,output_amount:i===fe.FixedOutput?String(r):void 0},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,console.log(l),p.ok){e.next=15;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=14;break}throw new E(l.type,l.fallback_message);case 14:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 15:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=17;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 17:return e.abrupt("return",l);case 18:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 2129d83a..13ce4a50 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -44,7 +44,8 @@ export interface SwapRoutePool { export interface FetchSwapRouteQuotesPayload { asset_in_id: string; asset_out_id: string; - amount: string; + input_amount?: string; + output_amount?: string; swap_type: SwapType; } export type SwapRouterResponse = Pick & { diff --git a/src/swap/types.ts b/src/swap/types.ts index 6b8e816f..c4bae0be 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -51,7 +51,8 @@ export interface SwapRoutePool { export interface FetchSwapRouteQuotesPayload { asset_in_id: string; asset_out_id: string; - amount: string; + input_amount?: string; + output_amount?: string; swap_type: SwapType; } diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 0777fc2c..c434a044 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -157,7 +157,8 @@ export async function getSwapRoute({ asset_in_id: String(assetInID), asset_out_id: String(assetOutID), swap_type: swapType, - amount: String(amount) + input_amount: swapType === SwapType.FixedInput ? String(amount) : undefined, + output_amount: swapType === SwapType.FixedOutput ? String(amount) : undefined }; const response = await fetch( @@ -175,6 +176,7 @@ export async function getSwapRoute({ const serializedResponse = await response.json(); + console.log(serializedResponse); if (!response.ok) { if (hasTinymanApiErrorShape(serializedResponse)) { throw new SwapQuoteError( From 68483be0b6120a02179d057ce6ab17c687790ba0 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 6 Jan 2025 18:21:58 +0100 Subject: [PATCH 10/15] Remove unncecessary console.log --- dist/index.js | 2 +- src/swap/v2/router/swap-router.ts | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/dist/index.js b/dist/index.js index 93f17425..f4f06eb1 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,input_amount:i===fe.FixedInput?String(r):void 0,output_amount:i===fe.FixedOutput?String(r):void 0},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,console.log(l),p.ok){e.next=15;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=14;break}throw new E(l.type,l.fallback_message);case 14:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 15:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=17;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 17:return e.abrupt("return",l);case 18:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,input_amount:i===fe.FixedInput?String(r):void 0,output_amount:i===fe.FixedOutput?String(r):void 0},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index c434a044..90c861f9 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -176,7 +176,6 @@ export async function getSwapRoute({ const serializedResponse = await response.json(); - console.log(serializedResponse); if (!response.ok) { if (hasTinymanApiErrorShape(serializedResponse)) { throw new SwapQuoteError( From b076cc3aadaa0213086828520d43fd2e33afdadb Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Wed, 8 Jan 2025 15:28:51 +0100 Subject: [PATCH 11/15] Bump up the app version --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 38a92585..0ffe4ea4 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@tinymanorg/tinyman-js-sdk", - "version": "4.1.2", + "version": "4.1.3", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "@tinymanorg/tinyman-js-sdk", - "version": "4.1.2", + "version": "4.1.3", "license": "MIT", "dependencies": { "algosdk": "^2.8.0", diff --git a/package.json b/package.json index 81f7029d..aa418299 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@tinymanorg/tinyman-js-sdk", - "version": "4.1.2", + "version": "4.1.3", "description": "Tinyman JS SDK", "author": "Tinyman Core Team", "license": "MIT", From 895329055ba6c39384978c60e81e5c5ab55246ed Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Thu, 9 Jan 2025 17:09:50 +0100 Subject: [PATCH 12/15] fix(swap-router): Get swap_fee from api response - Add extra checks for asset ids and amounts --- dist/index.js | 2 +- dist/swap/types.d.ts | 2 +- src/swap/types.ts | 2 +- src/swap/v2/router/swap-router.ts | 13 +++++++++++++ 4 files changed, 16 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index f4f06eb1..e10aac6e 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,input_amount:i===fe.FixedInput?String(r):void 0,output_amount:i===fe.FixedOutput?String(r):void 0},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:return e.abrupt("return",l);case 17:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,input_amount:i===fe.FixedInput?String(r):void 0,output_amount:i===fe.FixedOutput?String(r):void 0},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:if(!(Number(l.asset_in.id)!==a||Number(l.asset_out.id)!==o||l.swap_type===fe.FixedInput?r!==Number(l.input_amount):r!==Number(l.output_amount))){e.next=18;break}throw new E(_.UnknownError,"Swap router quote doesn't match the requested swap. Please try again.");case 18:return e.abrupt("return",l);case 19:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 13ce4a50..3d7c674f 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -59,7 +59,7 @@ export type SwapRouterResponse = Pick transaction_count: number | null; transactions: SwapRouterTransactionRecipe[] | null; transaction_fee: string | null; - transaction_fee_in_input_asset: string | null; + swap_fee: string | null; input_amount: string | null; output_amount: string | null; asset_ids: number[] | null; diff --git a/src/swap/types.ts b/src/swap/types.ts index c4bae0be..a8a1962a 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -67,7 +67,7 @@ export type SwapRouterResponse = Pick transaction_count: number | null; transactions: SwapRouterTransactionRecipe[] | null; transaction_fee: string | null; - transaction_fee_in_input_asset: string | null; + swap_fee: string | null; input_amount: string | null; output_amount: string | null; asset_ids: number[] | null; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 90c861f9..715850cd 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -197,5 +197,18 @@ export async function getSwapRoute({ ); } + if ( + Number((serializedResponse as SwapRouterResponse).asset_in.id) !== assetInID || + Number((serializedResponse as SwapRouterResponse).asset_out.id) !== assetOutID || + (serializedResponse as SwapRouterResponse).swap_type === SwapType.FixedInput + ? amount !== Number((serializedResponse as SwapRouterResponse).input_amount) + : amount !== Number((serializedResponse as SwapRouterResponse).output_amount) + ) { + throw new SwapQuoteError( + SwapQuoteErrorType.UnknownError, + "Swap router quote doesn't match the requested swap. Please try again." + ); + } + return serializedResponse; } From ecfc7843edad260a10430ebaf56e313d5228fdd9 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 13 Jan 2025 14:11:01 +0100 Subject: [PATCH 13/15] fix(swap-router): Send slippage as a parameter and use the asset amount value directly from the recipe --- dist/index.js | 2 +- dist/swap/index.d.ts | 9 ++-- dist/swap/types.d.ts | 3 ++ dist/swap/v2/index.d.ts | 9 ++-- dist/swap/v2/router/swap-router.d.ts | 8 ++-- src/swap/types.ts | 3 ++ src/swap/utils.ts | 6 ++- src/swap/v2/index.ts | 18 ++++++-- src/swap/v2/router/swap-router.ts | 64 ++++++---------------------- 9 files changed, 53 insertions(+), 69 deletions(-) diff --git a/dist/index.js b/dist/index.js index e10aac6e..c6c6f441 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003,he={testnet:184778019,mainnet:1083651166};l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ge=function(e){return e[e.TxnType=0]="TxnType",e[e.InputAmount=1]="InputAmount",e[e.OutputAmount=2]="OutputAmount",e[e.Routes=3]="Routes",e[e.Pools=4]="Pools",e[e.Swaps=5]="Swaps",e}({}),xe=function(e){return e.Swap="swap",e.AssetOptIn="asset_opt_in",e.Noop="noop",e}({});function Ae(e){var t=we(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function Ie(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function ve(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function we(e){return{assetIn:ve(e),assetOut:Ie(e)}}function ye(e){return be.apply(this,arguments)}function be(){return(be=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,o=n.route,i=n.slippage,o.transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return u=t.sent,c=[ve(o).amount,Ie(o).amount],p=c[0],l=c[1],d=o.swap_type===fe.FixedInput?p:te("positive",i,p),f=o.swap_type===fe.FixedOutput?l:te("negative",i,l),m=[],o.transactions.forEach((function(e){m.push(Te(e,u,r,d,f))})),m[0].fee=Number(o.transaction_fee),h=e.assignGroupID(m),t.abrupt("return",h.map((function(e){return{txn:e,signers:[r]}})));case 14:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Te(n,r,a,s,o){var i;switch(n.type){case e.TransactionType.pay:return(i=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,suggestedParams:r})).fee=0,i;case e.TransactionType.axfer:return(i=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:s,assetIndex:n.asset_id,suggestedParams:r})).fee=0,i;case e.TransactionType.appl:var u,c=null===(u=n.args)||void 0===u?void 0:u.map(t.toByteArray),p=new TextDecoder;return null!=c&&c.length&&p.decode(c[ge.TxnType])===xe.Swap&&(null==c||c.splice(ge.InputAmount,2,e.bigIntToBytes(s,8),e.bigIntToBytes(o,8))),(i=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:c,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,i;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function ke(e){return _e.apply(this,arguments)}function _e(){return(_e=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,input_amount:i===fe.FixedInput?String(r):void 0,output_amount:i===fe.FixedOutput?String(r):void 0},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(c)}).catch((function(){throw new Error("Network error")}));case 4:return p=e.sent,e.next=7,p.json();case 7:if(l=e.sent,p.ok){e.next=14;break}if(s=l,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(l.type,l.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=l.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:if(!(Number(l.asset_in.id)!==a||Number(l.asset_out.id)!==o||l.swap_type===fe.FixedInput?r!==Number(l.input_amount):r!==Number(l.output_amount))){e.next=18;break}throw new E(_.UnknownError,"Swap router quote doesn't match the requested swap. Please try again.");case 18:return e.abrupt("return",l);case 19:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var Ee={V1_1:"v1_1",V2:"v2"},Pe=0,Se={id:"".concat(Pe),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},De={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Oe={testnet:258703304,mainnet:22e8},Ne={testnet:724519992,mainnet:2537013734},Be={testnet:724676936,mainnet:2537023208};function Ce(e){return Fe.apply(this,arguments)}function Fe(){return(Fe=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Le(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Re(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Me(e){return Number(e)===Pe}function Ue(e){return Number(e.id)}var je,Ve,Ge=function(e){return e.Direct="direct",e.Router="router",e}({}),Xe=4*e.ALGORAND_MIN_TX_FEE,Qe=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),qe=(l(je={},fe.FixedInput,1),l(je,fe.FixedOutput,2),je),We=2,ze=le("swap"),Je=(l(Ve={},fe.FixedInput,le("fixed-input")),l(Ve,fe.FixedOutput,le("fixed-output")),Ve);function Ke(t){return(qe[t]+We)*e.ALGORAND_MIN_TX_FEE}function Ye(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function He(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function $e(t){return(qe[t]+1)*e.ALGORAND_MIN_TX_FEE}function Ze(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function et(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=Ze({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function tt(e){if(e.type===Ge.Router){var t=ve(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function nt(e){if(e.type===Ge.Router){var t=Ie(e.data);return{id:Ue(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function rt(e){return e.type===Ge.Direct?e.data.pool.contractVersion:Ee.V2}function at(e){return e.type===Ge.Direct?e.data.quote.rate:Ae(e.data)}function st(e){for(var t=e[0],n=at(t),r=1;rn&&(t=a,n=s)}return t}function ot(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var it,ut={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"},ct={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"},pt=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===Ee.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),lt=(l(it={},Ee.V1_1,{testnet:62368684,mainnet:552635992}),l(it,Ee.V2,{testnet:148607e3,mainnet:1002541853}),it);function dt(e,t){var n=lt[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function ft(){return(ft=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mt(){return(mt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:dt(a,o),note:pt.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ht=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),gt=function(n){d(a,ht);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=dt(n.network,Ee.V1_1),a=v(Re(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:Tt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function bt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function Tt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var kt=le("e");function _t(e){return Et.apply(this,arguments)}function Et(){return(Et=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,kt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function Pt(){return(Pt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fCt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Ct,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Mt(){return Mt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,yt(n,r);case 6:return i=e.sent,u=bt(i,dt(a,Ee.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[Ot[Ee.V1_1].asset1],asset2ID:u[Ot[Ee.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Mt.apply(this,arguments)}var Ut=Object.freeze({__proto__:null,getPoolAssets:function(e){return Mt.apply(this,arguments)},getPoolInfo:Ft,getPoolReserves:function(e,t){return Rt.apply(this,arguments)}});function jt(e){return Vt.apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=wt(Ee.V2),u=i.generateLogicSigAccountForPool(t),c=dt(r,Ee.V2),p=u.address(),l=Re(a,o),e.next=8,yt(n,p);case 8:return d=e.sent,f=bt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Dt.READY:Dt.NOT_CREATED,contractVersion:Ee.V2},f&&(m.asset1ProtocolFees=BigInt(f[Ot.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[Ot.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[Ot.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[Ot.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[Ot.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[Ot.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[Ot.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[Ot.v2.totalFeeShare]),m.poolTokenID=Number(f[Ot.v2.poolTokenID]),m.asset1ID=Number(f[Ot.v2.asset1]),m.asset2ID=Number(f[Ot.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Gt(){return(Gt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,yt(t,n.account.address());case 2:return r=e.sent,a=bt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[Ot.v2.asset1Reserves]),o.asset2=BigInt(a[Ot.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[Ot.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xt(){return(Xt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,yt(n,r);case 3:return o=e.sent,i=bt(o,dt(a,Ee.V2)),u=null,i&&(u={asset1ID:i[Ot[Ee.V2].asset1],asset2ID:i[Ot[Ee.V2].asset2],poolTokenID:i[Ot[Ee.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Xt.apply(this,arguments)},getPoolInfo:jt,getPoolReserves:function(e,t){return Gt.apply(this,arguments)}});function qt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Wt,zt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=qt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Ft(e),jt(e)])},isPoolEmpty:qt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Dt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Dt.READY}}),Jt=a((l(Wt={},Ee.V1_1,a(a({},Ut),zt)),l(Wt,Ee.V2,a(a({},Qt),zt)),Wt),zt),Kt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Yt={ASA_ALGO:96e4,ASA_ASA:859e3};function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Le(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Me(g),I=dt(a,Ee.V1_1),w=xt.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:De.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:$t(A),suggestedParams:c}),(E=[])[Kt.FUNDING_TXN]=_,E[Kt.VALIDATOR_APP_CALL]=b,E[Kt.POOL_TOKEN_CREATE]=T,E[Kt.ASSET1_OPT_IN]=k,A||(E[Kt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[Kt.FUNDING_TXN],signers:[u]},{txn:P[Kt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[Kt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[Kt.ASSET1_OPT_IN],signers:[y]}],P[Kt.ASSET2_OPT_IN]&&S.push({txn:P[Kt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return e?Yt.ASA_ALGO:Yt.ASA_ASA}function Zt(){return(Zt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=xt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===Kt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function en(e){return tn.apply(this,arguments)}function tn(){return(tn=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[Kt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function nn(){return(nn=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,en({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Jt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var rn={generateTxns:function(e){return Ht.apply(this,arguments)},signTxns:function(e){return Zt.apply(this,arguments)},execute:function(e){return nn.apply(this,arguments)},getBootstrapFundingTxnAmount:$t};var an=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),sn={ASA_ALGO:5,ASA_ASA:6};function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,on(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ln(e,t){return dn.apply(this,arguments)}function dn(){return(dn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,cn(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:Pe,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function fn(){return(fn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=dt(a,Ee.V2),l=e.getApplicationAddress(p),d=Le(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Jt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Dt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Me(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:pt.getAppCallTxnNoteWithClientName(Ee.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=hn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:mn(A),suggestedParams:c}),(y=[])[an.FUNDING_TXN]=w,y[an.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[an.FUNDING_TXN]={txn:b[an.FUNDING_TXN],signers:[u]},T[an.VALIDATOR_APP_CALL]={txn:b[an.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function mn(e){return function(e,t){var n=wt(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(Ee.V2,e)+hn(e)+D}function hn(t){return((t?sn.ASA_ALGO:sn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function gn(){return(gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Re(i,u),f=v(d,2),m=f[0],h=f[1],g=vt.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===an.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xn(){return(xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,on(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(Ot.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Jt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var An,In={generateTxns:function(e){return fn.apply(this,arguments)},signTxns:function(e){return gn.apply(this,arguments)},execute:function(e){return xn.apply(this,arguments)},getBootstrapFundingTxnAmount:mn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+mn(t)}};var vn,wn=(l(An={},Ee.V1_1,rn),l(An,Ee.V2,In),l(An,"generateTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.generateTxns(e):In.generateTxns(e)})),l(An,"signTxns",(function(e){return e.contractVersion===Ee.V1_1?rn.signTxns(e):In.signTxns(e)})),l(An,"execute",(function(e){return e.contractVersion===Ee.V1_1?rn.execute(e):In.execute(e)})),l(An,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===Ee.V1_1?rn.getBootstrapFundingTxnAmount(n):In.getBootstrapFundingTxnAmount(n)})),An),yn=le("add_liquidity"),bn=(l(vn={},Ee.V1_1,[le("mint")]),l(vn,Ee.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[yn,le("single")],FLEXIBLE_MODE:[yn,le("flexible")]}),vn),Tn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),kn=5*e.ALGORAND_MIN_TX_FEE;function _n(){return(_n=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:dt(a,Ee.V1_1),appArgs:bn.v1_1,accounts:[l],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:u.id==Pe?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function En(){return(En=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===Tn.FEE_TXN?p:n===Tn.ASSET1_IN_TXN?l:n===Tn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Pn(){return(Pn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[Tn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,_t({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var Sn,Dn,On,Nn=Object.freeze({__proto__:null,execute:function(e){return Pn.apply(this,arguments)},generateTxns:function(e){return _n.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Un(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Un(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:et({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Mn(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Nt)))}function Un(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function jn(t){return(Fn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Vn(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function Gn(e){return Xn.apply(this,arguments)}function Xn(){return(Xn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,ln(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Le(i,u),m=v(f,2),h=m[0],g=m[1],x=Me(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=jn(Bn.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Rn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Jt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Vn});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Me(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:dt(a,Ee.V2),note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[].concat(w(bn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=jn(Bn.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Dt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Rn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Jt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Vn});function Jn(){return(Jn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Me(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:dt(o,Ee.V2),appArgs:bn.v2.INITIAL_LIQUIDITY,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=jn(Bn.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Kn,Yn=Object.freeze({__proto__:null,execute:Gn,generateTxns:function(e){return Jn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Nt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Nt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Mn(n,r)},slippage:s}},signTxns:Vn}),Hn=Object.freeze({__proto__:null,flexible:qn,initial:Yn,withSingleAsset:zn}),$n=(l(Kn={},Ee.V1_1,Nn),l(Kn,Ee.V2,Hn),Kn);var Zn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),er=Object.values(Zn).length;function tr(){return(tr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Me(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Zn.FEE_TXN]=w,y[Zn.VALIDATOR_APP_CALL_TXN]=m,y[Zn.ASSET1_OUT_TXN]=g,y[Zn.ASSET2_OUT_TXN]=A,y[Zn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Zn.FEE_TXN],signers:[p]},{txn:b[Zn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Zn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Zn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Zn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function nr(){return(nr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Zn.FEE_TXN?c:n===Zn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function rr(){return(rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Zn.ASSET1_OUT_TXN].txn.amount,c=a[Zn.ASSET2_OUT_TXN].txn.amount,p=a[Zn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,_t({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,_t({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var ar={generateTxns:function(e){return tr.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return nr.apply(this,arguments)},execute:function(e){return rr.apply(this,arguments)}};function sr(){return(sr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==Ge.Router){t.next=2;break}return t.abrupt("return",ye(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Me(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[ze,Je[u],e.encodeUint64(v)],note:pt.getAppCallTxnNoteWithClientName(Ee.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=$e(u),(b=[])[Qe.INPUT_TXN]=w,b[Qe.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Qe.INPUT_TXN],signers:[o]},{txn:T[Qe.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function or(){return(or=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=nt(i).id,g=tt(i),e.prev=10,e.next=13,ln(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function ir(){return(ir=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.isSwapRouterEnabled,c=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,lr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:u,network:i,pool:c});case 4:p=e.sent,e.next=10;break;case 7:return e.next=9,fr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:u,network:i,pool:c});case 9:p=e.sent;case 10:return e.abrupt("return",p);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(ot(t)&&!ot(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function cr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=hr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function pr(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Jt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=et({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(He(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:Ge.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[],u?c.push(new Promise((function(e,t){try{var s=cr({amount:n,assetIn:r,assetOut:a,pool:u});e({type:Ge.Direct,data:{pool:u,quote:s}})}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=6,ur(c);case 6:return p=e.sent,e.abrupt("return",st(p));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){return mr.apply(this,arguments)}function mr(){return(mr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.pool,c=[new Promise((function(e,t){try{e(pr({amount:n,assetIn:r,assetOut:a,pool:u}))}catch(e){t(e)}}))],o&&c.push(ke({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i}).then((function(e){return{type:Ge.Router,data:e}}))),e.next=5,ur(c);case 5:return p=e.sent,e.abrupt("return",st(p));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function hr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:et({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var gr={getQuote:function(e){return ir.apply(this,arguments)},getFixedInputSwapQuote:lr,getFixedInputDirectSwapQuote:cr,getFixedOutputDirectSwapQuote:pr,getFixedOutputSwapQuote:fr,generateTxns:function(e){return sr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return or.apply(this,arguments)},calculateFixedInputSwap:hr},xr=2,Ar=le("remove_liquidity"),Ir=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function vr(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Nt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function wr(){return(wr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[Ir.ASSET_TRANSFER_TXN]=m,g[Ir.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[Ir.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[Ir.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function yr(){return(yr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:pt.getAppCallTxnNoteWithClientName(Ee.V2),appArgs:[Ar,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(xr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[Ir.ASSET_TRANSFER_TXN]=I,w[Ir.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[Ir.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[Ir.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function br(){return(br=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,ln(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var Tr,kr={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=vr(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=vr(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=gr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=gr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return wr.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return yr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return br.apply(this,arguments)}},_r=(l(Tr={},Ee.V1_1,ar),l(Tr,Ee.V2,kr),Tr),Er=3n,Pr=1000n,Sr=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Sr||{});function Dr(){return(Dr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Sr.FEE_TXN_INDEX?c:n===Sr.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Or(){return(Or=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:pt.getAppCallTxnNoteWithClientName(Ee.V1_1),foreignAssets:c.asset2ID==Pe?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===Pe?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Nr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*Er/Pr,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:Ze(d),priceImpact:et(a({inputSupply:i,outputSupply:u},d))};return{type:Ge.Direct,data:{pool:t,quote:f}}}function Br(e){return Cr.apply(this,arguments)}function Cr(){return(Cr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Fr(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Dt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*Pr/(Pr-Er),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:Ge.Direct,data:{pool:t,quote:h}}}function Lr(e){return Rr.apply(this,arguments)}function Rr(){return(Rr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,_t({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,_t({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Mr(){return(Mr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Dt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Sr.ASSET_IN_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Sr.ASSET_OUT_TXN_INDEX].txn.assetIndex||Pe,amount:i[Sr.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Br({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Lr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Ur,jr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Nr({pool:t,reserves:n,assetIn:r,decimals:a}):Fr({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Nr,getFixedOutputSwapQuote:Fr,generateTxns:function(e){return Or.apply(this,arguments)},signTxns:function(e){return Dr.apply(this,arguments)},execute:function(e){return Mr.apply(this,arguments)},executeFixedOutputSwap:Lr};function Vr(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(ot(n)&&!ot(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function Gr(){return(Gr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedInputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===Ee.V1_1})))?c.push(new Promise((function(e,t){try{e(jr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===Ee.V2})),c.push(gr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Ue(a),decimals:a.decimals},assetOut:{id:Ue(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network})),e.next=8,Vr(c);case 8:return d=e.sent,e.abrupt("return",st(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Qr=(l(Ur={},Ee.V1_1,jr),l(Ur,Ee.V2,gr),l(Ur,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return Gr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Xr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Ur,"generateTxns",(function(e){return e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1?jr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):gr.generateTxns(e)})),l(Ur,"signTxns",(function(e){if(e.quote.type===Ge.Direct&&rt(e.quote)===Ee.V1_1){var t=e.quote.data.pool;return jr.signTxns(a(a({},e),{},{pool:t}))}return gr.signTxns(e)})),l(Ur,"execute",(function(e){return e.contractVersion===Ee.V1_1?jr.execute(e):gr.execute(e)})),Ur);function qr(){return(qr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Wr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Wr(e){return zr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(){return Jr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Jr.apply(this,arguments)}function Kr(){return(Kr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:pt.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Hr(e,t,n){return e*t/n}var $r=BigInt(31536e3),Zr=BigInt(1e14),ea=BigInt(1e16),ta={testnet:548587153,mainnet:1385499515},na=14;function ra(e){return aa.apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(ta[a]),t.next=4,yt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==Pe&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(){return(sa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(ta[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Me(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),T=dt(a,Ee.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ra({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:ta[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function oa(){return(oa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(ta[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(na+1),A=dt(l,Ee.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:ta[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ia(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Hr(e,ea+t*r/$r,ea)}function ua(){return(ua=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Yr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ca={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return sa.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+na)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return oa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+na)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Hr(BigInt(t),n,Zr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=ia(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Zr/n},getFolksWrapperAppOptInRequiredAssetIDs:ra};function pa(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function la(){return(la=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=pa({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var da=604800,fa=1e3,ma={testnet:480164661,mainnet:2200606875},ha={testnet:360907790,mainnet:2200609638},ga={testnet:336189106,mainnet:2200608153},xa={testnet:383416252,mainnet:2200608887},Aa=2500,Ia=400,va=604800,wa=le("p"),ya=le("a"),ba=24,Ta=111300,ka=Aa+Ia*(41+ba),_a=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(_a||{}),Ea=new Uint8Array(34);function Pa(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Sa(e,t){return 0===Buffer.compare(e,t)}function Da(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mOa(a(e))})}function Va({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ua({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const Ga=Va({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Va({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Va({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Va({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Va({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Va({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Va({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Va({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Va({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Xa=ja({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});ja({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Qa=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=za;)n[r++]=255&t|qa,t/=128;for(;t&Wa;)n[r++]=255&t|qa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},qa=128,Wa=-128,za=Math.pow(2,31);var Ja=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ya)<=Ka);return e.bytes=o-n,a},Ka=128,Ya=127;var Ha=Math.pow(2,7),$a=Math.pow(2,14),Za=Math.pow(2,21),es=Math.pow(2,28),ts=Math.pow(2,35),ns=Math.pow(2,42),rs=Math.pow(2,49),as=Math.pow(2,56),ss=Math.pow(2,63),os={encode:Qa,decode:Ja,encodingLength:function(e){return e{const[n,r]=is(e.subarray(t));return t+=r,n};let r=n(),a=gs;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Xa;return[Xa.prefix,n.decode(`${Xa.prefix}${e}`)]}case Xa.prefix:{const n=t??Xa;return[Xa.prefix,n.decode(e)]}case Ga.prefix:{const n=t??Ga;return[Ga.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=hs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ms(a).set(n,e),a}}const gs=112,xs=18;function As(e,t,n){const r=cs(e),a=r+cs(t),s=new Uint8Array(a+n.byteLength);return us(e,s,0),us(t,s,r),s.set(n,a),s}const Is=Symbol.for("@ipld/js-cid/CID");class vs{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?ps(this.code,t):t.then((e=>ps(this.code,e)))}throw Error("Unknown type, must be binary type")}}const ws=85,ys=function({name:e,code:t,encode:n}){return new vs(e,t,n)}({name:"sha2-256",code:18,encode:e=>Oa(n.createHash("sha256").update(e).digest())});var bs=126144e3,Ts=1e7,ks=Math.pow(2,64),_s=48,Es=21,Ps=48,Ss=le("tp"),Ds=le("sc"),Os=28100,Ns=12900,Bs=421700,Cs=409700;function Fs(e){var t=e/va;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*va}function Ls(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Fs(e)+va;re?r=t?e*e*ks/(2*t):0:r=(e+(e-r))*n/2;return r}function Xs(e,t){return Qs.apply(this,arguments)}function Qs(){return(Qs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",qs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Ws(){return(Ws=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,ys.digest(n);case 3:return r=e.sent,a=hs.createV1(ws,r),e.abrupt("return",a.toString(Ga));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function zs(){for(var t=arguments.length,n=new Array(t),r=0;r0?Es-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return lo(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return lo(this.powerCount)[1]}}]),e}(),ro=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+Gs(this.bias,this.slope,t)}}]),e}(),ao=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),so=p((function e(t){u(this,e),this.slopeDelta=t})),oo=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%Es;return e>0?Es-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return lo(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return lo(this.totalPowerCount)[1]}}]),e}();function io(){return(io=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=co(r),e.prev=1,e.next=4,Rs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",uo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function uo(e){var t=Buffer.from(e);return new no(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function co(t){return e.decodeAddress(t).publicKey}function po(e){var t=Pa(e),n=new Uint8Array(Ss.length+t.length);return n.set(Ss,0),n.set(t,Ss.length),n}function lo(e){var t=e-1;return[Math.floor(t/Es),t%Es]}function fo(t,n){var r=e.decodeAddress(t).publicKey,a=Pa(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function mo(e,t,n){return ho.apply(this,arguments)}function ho(){return(ho=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=xo(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",go(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function go(t){return new so(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function xo(e){return q(Ds,Pa(e))}function Ao(e,t,n){return Io.apply(this,arguments)}function Io(){return(Io=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/Es)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function To(e){for(var t,n=_s,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),Pa(o),Pa(i),q.apply(void 0,w(u.map((function(e){return Pa(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Qo({sender:s,suggestedParams:c,extraAppArgs:[Pa(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:Oo,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Wo=8,zo=le("p"),Jo=le("a"),Ko=le("v"),Yo=28500,Ho=12500,$o=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Zo(t,n){return q(Jo,e.decodeAddress(t).publicKey,Pa(n))}function ei(e){return new $o(Da(e.slice(0,8)),Da(e.slice(8,16)),Da(e.slice(16,24)),Da(e.slice(24,32)),Da(e.slice(32,40)),Da(e.slice(40,48)),Boolean(Da(e.slice(48,49))))}function ti(){return(ti=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=$s(r),e.next=3,Rs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",ei(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ni(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Wo)throw new Error("You cannot cast vote for more than ".concat(Wo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return Pa(e)})))),g=q.apply(void 0,w(u.map((function(e){return Pa(e)})))),x=function(e){return q(zo,le(e))}(s),A=Zo(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Sa(e,A)})),v=co(a),y=Math.floor(c/Es),b=fo(a,y),k=fo(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(Ko,Pa(e),Pa(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Sa(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,Pa(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Qo({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Yo+_*Ho;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ri(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Cs),c||(b+=Ns);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Oe[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),Pa(r)],boxes:d.slice(0,8),note:p}),Qo({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ai(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Cs),!n.freeAccountPowerSpaceCount){var w=fo(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:ma[a],name:w}),v+=Bs}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Oe[a],from:s,to:e.getApplicationAddress(ma[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:ma[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Qo({sender:s,suggestedParams:i,index:ma[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(ma[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function si(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%va)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=co(o),l=fo(o,n.lastAccountPowerBoxIndex),d=po(i.lastTotalPowerBoxIndex),f=po(i.lastTotalPowerBoxIndex+1),m=xo(n.lockEndTime),h=xo(a),g=[{appIndex:ma[r],name:p},{appIndex:ma[r],name:l},{appIndex:ma[r],name:d},{appIndex:ma[r],name:f},{appIndex:ma[r],name:m},{appIndex:ma[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=fo(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:ma[r],name:x})}var A,I=Ls(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%va==0){var b=xo(y);g.push({appIndex:ma[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ns),n.freeAccountPowerSpaceCount||(k+=Bs),v>i.freeTotalPowerSpaceCount&&(k+=Cs);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),Pa(a)],boxes:g.slice(0,8),note:c}),Qo({sender:o,suggestedParams:u,index:ma[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(ma[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function oi(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=co(a),u=fo(a,n.lastAccountPowerBoxIndex),c=fo(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:ma[r],name:i},{appIndex:ma[r],name:u},{appIndex:ma[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:ma[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Oe[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(ma[r]),amount:Bs});d.unshift(f)}return e.assignGroupID(d)}var ii=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Vs(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Ao(this.algodClient,ma[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=bo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/va),u=Math.floor(t/va)-i,c=Array.from({length:u},(function(e,t){return(i+t)*va})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Vs(l,g),d=Math.max(d-x,0),e.next=28,mo(this.algodClient,ma[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/fa),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,wo({algodClient:this.algodClient,address:this.userAddress,appId:ma[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=bo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Gs(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Xs(this.algodClient,ma[this.network]);case 3:return t=e.sent,e.abrupt("return",new oo(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,mo(this.algodClient,ma[this.network],r);case 17:return l=e.sent,e.abrupt("return",ri({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ai({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,mo(this.algodClient,ma[this.network],n);case 19:return p=e.sent,e.abrupt("return",si({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ai({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,mo(this.algodClient,ma[this.network],r);case 24:return f=e.sent,l=si({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?zs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=oi({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ce({client:this.algodClient,initiatorAddr:a,assetID:Oe[this.network]});case 15:c=e.sent,u=zs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return io.apply(this,arguments)}(this.algodClient,ma[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return ti.apply(this,arguments)}(this.algodClient,ha[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Js(this.algodClient,ha[this.network]);case 17:return f=e.sent,e.next=20,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=bo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ni({stakingVotingAppId:ha[this.network],vaultAppId:ma[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Xs(this.algodClient,ga[this.network]);case 3:return n=t.sent,t.abrupt("return",new No(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,wo({algodClient:this.algodClient,address:c,appId:ma[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*da,h=d.firstPeriodTimestamp+(r+a)*da,g=[],x=m;x=Math.floor(Date.now()/fa)||c.votingEndTimestamp<=Math.floor(Date.now()/fa))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,wo({algodClient:this.algodClient,address:i,appId:ma[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=bo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*ba)),m=Zo(i,f),e.next=33,Us(this.algodClient,xa[this.network],m);case 33:return h=!e.sent,e.abrupt("return",_o({proposalVotingAppId:xa[this.network],vaultAppId:ma[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Xs(this.algodClient,xa[this.network]);case 2:return t=e.sent,e.abrupt("return",new Hs(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),ui=2500,ci=400,pi=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return mi(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),li=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),di=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*ks/bs)),s=t-r;return s<0?0:Vs(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=Pa,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=He,exports.isSwapQuoteErrorCausedByAmount=ot,exports.poolUtils=Jt,exports.prepareCommitTransactions=function(e){return la.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Jr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return qr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=xt,exports.tinymanContract_v2=vt,exports.tinymanJSSDKConfig=pt; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003;function he(e){return ge.apply(this,arguments)}function ge(){return(ge=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,(o=n.route).transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return i=t.sent,u=[],o.transactions.forEach((function(e){u.push(xe(e,i,r))})),u[0].fee=Number(o.transaction_fee),c=e.assignGroupID(u),t.abrupt("return",c.map((function(e){return{txn:e,signers:[r]}})));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xe(n,r,a){var s;switch(n.type){case e.TransactionType.pay:return(s=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,suggestedParams:r})).fee=0,s;case e.TransactionType.axfer:return(s=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,assetIndex:n.asset_id,suggestedParams:r})).fee=0,s;case e.TransactionType.appl:var o,i=null===(o=n.args)||void 0===o?void 0:o.map(t.toByteArray);return(s=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:i,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,s;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function Ae(e){return Ie.apply(this,arguments)}function Ie(){return(Ie=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c=t.slippage,p={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,input_amount:i===fe.FixedInput?String(r):void 0,output_amount:i===fe.FixedOutput?String(r):void 0,slippage:c},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(p)}).catch((function(){throw new Error("Network error")}));case 4:return l=e.sent,e.next=7,l.json();case 7:if(d=e.sent,l.ok){e.next=14;break}if(s=d,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(d.type,d.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=d.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:if(!(Number(d.asset_in.id)!==a||Number(d.asset_out.id)!==o||d.swap_type===fe.FixedInput?r!==Number(d.input_amount):r!==Number(d.output_amount))){e.next=18;break}throw new E(_.UnknownError,"Swap router quote doesn't match the requested swap. Please try again.");case 18:return e.abrupt("return",d);case 19:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var ve={testnet:184778019,mainnet:1083651166};function we(e){var t=Te(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function ye(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function be(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function Te(e){return{assetIn:be(e),assetOut:ye(e)}}l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ke={V1_1:"v1_1",V2:"v2"},_e=0,Ee={id:"".concat(_e),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},Pe={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Se={testnet:258703304,mainnet:22e8},De={testnet:724519992,mainnet:2537013734},Oe={testnet:724676936,mainnet:2537023208};function Ne(e){return Be.apply(this,arguments)}function Be(){return(Be=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Ce(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Fe(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Le(e){return Number(e)===_e}function Re(e){return Number(e.id)}var Me,Ue,je=function(e){return e.Direct="direct",e.Router="router",e}({}),Ve=4*e.ALGORAND_MIN_TX_FEE,Ge=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),Xe=(l(Me={},fe.FixedInput,1),l(Me,fe.FixedOutput,2),Me),Qe=2,qe=le("swap"),We=(l(Ue={},fe.FixedInput,le("fixed-input")),l(Ue,fe.FixedOutput,le("fixed-output")),Ue);function ze(t){return(Xe[t]+Qe)*e.ALGORAND_MIN_TX_FEE}function Je(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function Ke(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function Ye(t){return(Xe[t]+1)*e.ALGORAND_MIN_TX_FEE}function He(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function $e(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=He({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function Ze(e){if(e.type===je.Router){var t=be(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function et(e){if(e.type===je.Router){var t=ye(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function tt(e){return e.type===je.Direct?e.data.pool.contractVersion:ke.V2}function nt(e){return e.type===je.Direct?e.data.quote.rate:we(e.data)}function rt(e){for(var t=e[0],n=nt(t),r=1;rn&&(t=a,n=s)}return t}function at(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var st,ot={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"},it={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"},ut=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===ke.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),ct=(l(st={},ke.V1_1,{testnet:62368684,mainnet:552635992}),l(st,ke.V2,{testnet:148607e3,mainnet:1002541853}),st);function pt(e,t){var n=ct[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function lt(){return(lt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dt(){return(dt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ft=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),mt=function(n){d(a,ft);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=pt(n.network,ke.V1_1),a=v(Fe(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:yt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function wt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function yt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var bt=le("e");function Tt(e){return kt.apply(this,arguments)}function kt(){return(kt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function _t(){return(_t=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fNt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Nt,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Lt(){return Lt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,vt(n,r);case 6:return i=e.sent,u=wt(i,pt(a,ke.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[St[ke.V1_1].asset1],asset2ID:u[St[ke.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Lt.apply(this,arguments)}var Rt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Lt.apply(this,arguments)},getPoolInfo:Bt,getPoolReserves:function(e,t){return Ft.apply(this,arguments)}});function Mt(e){return Ut.apply(this,arguments)}function Ut(){return(Ut=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=It(ke.V2),u=i.generateLogicSigAccountForPool(t),c=pt(r,ke.V2),p=u.address(),l=Fe(a,o),e.next=8,vt(n,p);case 8:return d=e.sent,f=wt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Pt.READY:Pt.NOT_CREATED,contractVersion:ke.V2},f&&(m.asset1ProtocolFees=BigInt(f[St.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[St.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[St.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[St.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[St.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[St.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[St.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[St.v2.totalFeeShare]),m.poolTokenID=Number(f[St.v2.poolTokenID]),m.asset1ID=Number(f[St.v2.asset1]),m.asset2ID=Number(f[St.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(){return(jt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,vt(t,n.account.address());case 2:return r=e.sent,a=wt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[St.v2.asset1Reserves]),o.asset2=BigInt(a[St.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[St.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,vt(n,r);case 3:return o=e.sent,i=wt(o,pt(a,ke.V2)),u=null,i&&(u={asset1ID:i[St[ke.V2].asset1],asset2ID:i[St[ke.V2].asset2],poolTokenID:i[St[ke.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Vt.apply(this,arguments)},getPoolInfo:Mt,getPoolReserves:function(e,t){return jt.apply(this,arguments)}});function Xt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Qt,qt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=Xt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Bt(e),Mt(e)])},isPoolEmpty:Xt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Pt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Pt.READY}}),Wt=a((l(Qt={},ke.V1_1,a(a({},Rt),qt)),l(Qt,ke.V2,a(a({},Gt),qt)),Qt),qt),zt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Jt={ASA_ALGO:96e4,ASA_ASA:859e3};function Kt(){return(Kt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Ce(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Le(g),I=pt(a,ke.V1_1),w=ht.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:Pe.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Yt(A),suggestedParams:c}),(E=[])[zt.FUNDING_TXN]=_,E[zt.VALIDATOR_APP_CALL]=b,E[zt.POOL_TOKEN_CREATE]=T,E[zt.ASSET1_OPT_IN]=k,A||(E[zt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[zt.FUNDING_TXN],signers:[u]},{txn:P[zt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[zt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[zt.ASSET1_OPT_IN],signers:[y]}],P[zt.ASSET2_OPT_IN]&&S.push({txn:P[zt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yt(e){return e?Jt.ASA_ALGO:Jt.ASA_ASA}function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=ht.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===zt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return Zt.apply(this,arguments)}function Zt(){return(Zt=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[zt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function en(){return(en=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,$t({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Wt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var tn={generateTxns:function(e){return Kt.apply(this,arguments)},signTxns:function(e){return Ht.apply(this,arguments)},execute:function(e){return en.apply(this,arguments)},getBootstrapFundingTxnAmount:Yt};var nn=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(e,t){return sn.apply(this,arguments)}function sn(){return(sn=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,an(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,on(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:_e,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function ln(){return(ln=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=pt(a,ke.V2),l=e.getApplicationAddress(p),d=Ce(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Wt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Pt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Le(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:ut.getAppCallTxnNoteWithClientName(ke.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=fn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:dn(A),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=w,y[nn.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[nn.FUNDING_TXN]={txn:b[nn.FUNDING_TXN],signers:[u]},T[nn.VALIDATOR_APP_CALL]={txn:b[nn.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dn(e){return function(e,t){var n=It(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(ke.V2,e)+fn(e)+D}function fn(t){return((t?rn.ASA_ALGO:rn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function mn(){return(mn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===nn.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,an(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(St.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Wt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var gn,xn={generateTxns:function(e){return ln.apply(this,arguments)},signTxns:function(e){return mn.apply(this,arguments)},execute:function(e){return hn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+dn(t)}};var An,In=(l(gn={},ke.V1_1,tn),l(gn,ke.V2,xn),l(gn,"generateTxns",(function(e){return e.contractVersion===ke.V1_1?tn.generateTxns(e):xn.generateTxns(e)})),l(gn,"signTxns",(function(e){return e.contractVersion===ke.V1_1?tn.signTxns(e):xn.signTxns(e)})),l(gn,"execute",(function(e){return e.contractVersion===ke.V1_1?tn.execute(e):xn.execute(e)})),l(gn,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===ke.V1_1?tn.getBootstrapFundingTxnAmount(n):xn.getBootstrapFundingTxnAmount(n)})),gn),vn=le("add_liquidity"),wn=(l(An={},ke.V1_1,[le("mint")]),l(An,ke.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[vn,le("single")],FLEXIBLE_MODE:[vn,le("flexible")]}),An),yn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),bn=5*e.ALGORAND_MIN_TX_FEE;function Tn(){return(Tn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:pt(a,ke.V1_1),appArgs:wn.v1_1,accounts:[l],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:u.id==_e?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kn(){return(kn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===yn.FEE_TXN?p:n===yn.ASSET1_IN_TXN?l:n===yn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _n(){return(_n=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[yn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,Tt({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var En,Pn,Sn,Dn=Object.freeze({__proto__:null,execute:function(e){return _n.apply(this,arguments)},generateTxns:function(e){return Tn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Rn(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Rn(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:$e({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Ln(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Dt)))}function Rn(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function Mn(t){return(Bn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Un(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function jn(e){return Vn.apply(this,arguments)}function Vn(){return(Vn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,cn(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Gn(){return(Gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Ce(i,u),m=v(f,2),h=m[0],g=m[1],x=Le(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=Mn(On.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Xn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Gn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Fn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Wt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Un});function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Le(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=Mn(On.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Fn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Wt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Un});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Le(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:pt(o,ke.V2),appArgs:wn.v2.INITIAL_LIQUIDITY,note:ut.getAppCallTxnNoteWithClientName(ke.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=Mn(On.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn,Jn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Dt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Dt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Ln(n,r)},slippage:s}},signTxns:Un}),Kn=Object.freeze({__proto__:null,flexible:Xn,initial:Jn,withSingleAsset:qn}),Yn=(l(zn={},ke.V1_1,Dn),l(zn,ke.V2,Kn),zn);var Hn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),$n=Object.values(Hn).length;function Zn(){return(Zn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Le(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Hn.FEE_TXN]=w,y[Hn.VALIDATOR_APP_CALL_TXN]=m,y[Hn.ASSET1_OUT_TXN]=g,y[Hn.ASSET2_OUT_TXN]=A,y[Hn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Hn.FEE_TXN],signers:[p]},{txn:b[Hn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Hn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Hn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Hn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function er(){return(er=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Hn.FEE_TXN?c:n===Hn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function tr(){return(tr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Hn.ASSET1_OUT_TXN].txn.amount,c=a[Hn.ASSET2_OUT_TXN].txn.amount,p=a[Hn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,Tt({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var nr={generateTxns:function(e){return Zn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return er.apply(this,arguments)},execute:function(e){return tr.apply(this,arguments)}};function rr(){return(rr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==je.Router){t.next=2;break}return t.abrupt("return",he(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Le(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[qe,We[u],e.encodeUint64(v)],note:ut.getAppCallTxnNoteWithClientName(ke.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=Ye(u),(b=[])[Ge.INPUT_TXN]=w,b[Ge.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Ge.INPUT_TXN],signers:[o]},{txn:T[Ge.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=et(i).id,g=Ze(i),e.prev=10,e.next=13,cn(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function sr(){return(sr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.slippage,c=t.isSwapRouterEnabled,p=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,cr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 4:l=e.sent,e.next=10;break;case 7:return e.next=9,lr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 9:l=e.sent;case 10:return e.abrupt("return",l);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function or(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(at(t)&&!at(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function ir(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=fr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function ur(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=$e({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:je.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function cr(e){return pr.apply(this,arguments)}function pr(){return(pr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[],c?p.push(new Promise((function(e,t){try{var s=ir({amount:n,assetIn:r,assetOut:a,pool:c});e({type:je.Direct,data:{pool:c,quote:s}})}catch(e){t(e)}}))):p.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=6,or(p);case 6:return l=e.sent,e.abrupt("return",rt(l));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[new Promise((function(e,t){try{e(ur({amount:n,assetIn:r,assetOut:a,pool:c}))}catch(e){t(e)}}))],o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=5,or(p);case 5:return l=e.sent,e.abrupt("return",rt(l));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:$e({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var mr={getQuote:function(e){return sr.apply(this,arguments)},getFixedInputSwapQuote:cr,getFixedInputDirectSwapQuote:ir,getFixedOutputDirectSwapQuote:ur,getFixedOutputSwapQuote:lr,generateTxns:function(e){return rr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return ar.apply(this,arguments)},calculateFixedInputSwap:fr},hr=2,gr=le("remove_liquidity"),xr=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function Ar(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Dt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Ir(){return(Ir=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[xr.ASSET_TRANSFER_TXN]=m,g[xr.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[xr.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[xr.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function vr(){return(vr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[xr.ASSET_TRANSFER_TXN]=I,w[xr.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[xr.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[xr.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function wr(){return(wr=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,cn(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var yr,br={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=Ar(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=Ar(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=mr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=mr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return Ir.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return vr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return wr.apply(this,arguments)}},Tr=(l(yr={},ke.V1_1,nr),l(yr,ke.V2,br),yr),kr=3n,_r=1000n,Er=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Er||{});function Pr(){return(Pr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Er.FEE_TXN_INDEX?c:n===Er.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sr(){return(Sr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:c.asset2ID==_e?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*kr/_r,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:He(d),priceImpact:$e(a({inputSupply:i,outputSupply:u},d))};return{type:je.Direct,data:{pool:t,quote:f}}}function Or(e){return Nr.apply(this,arguments)}function Nr(){return(Nr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Br(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*_r/(_r-kr),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:je.Direct,data:{pool:t,quote:h}}}function Cr(e){return Fr.apply(this,arguments)}function Fr(){return(Fr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lr(){return(Lr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Pt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Er.ASSET_IN_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Er.ASSET_OUT_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Or({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Cr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Rr,Mr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Dr({pool:t,reserves:n,assetIn:r,decimals:a}):Br({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Dr,getFixedOutputSwapQuote:Br,generateTxns:function(e){return Sr.apply(this,arguments)},signTxns:function(e){return Pr.apply(this,arguments)},execute:function(e){return Lr.apply(this,arguments)},executeFixedOutputSwap:Cr};function Ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(at(n)&&!at(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function jr(){return(jr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedInputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vr(){return(Vr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gr=(l(Rr={},ke.V1_1,Mr),l(Rr,ke.V2,mr),l(Rr,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return jr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Vr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Rr,"generateTxns",(function(e){return e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1?Mr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):mr.generateTxns(e)})),l(Rr,"signTxns",(function(e){if(e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1){var t=e.quote.data.pool;return Mr.signTxns(a(a({},e),{},{pool:t}))}return mr.signTxns(e)})),l(Rr,"execute",(function(e){return e.contractVersion===ke.V1_1?Mr.execute(e):mr.execute(e)})),Rr);function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Qr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Qr(e){return qr.apply(this,arguments)}function qr(){return(qr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wr(){return Wr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:ut.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Kr(e,t,n){return e*t/n}var Yr=BigInt(31536e3),Hr=BigInt(1e14),$r=BigInt(1e16),Zr={testnet:548587153,mainnet:1385499515},ea=14;function ta(e){return na.apply(this,arguments)}function na(){return(na=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(Zr[a]),t.next=4,vt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==_e&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ra(){return(ra=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(Zr[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Le(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),T=pt(a,ke.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ta({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(Zr[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),A=pt(l,ke.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Kr(e,$r+t*r/Yr,$r)}function oa(){return(oa=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Jr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ia={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+ea)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return aa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+ea)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Kr(BigInt(t),n,Hr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Hr/n},getFolksWrapperAppOptInRequiredAssetIDs:ta};function ua(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function ca(){return(ca=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=ua({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pa=604800,la=1e3,da={testnet:480164661,mainnet:2200606875},fa={testnet:360907790,mainnet:2200609638},ma={testnet:336189106,mainnet:2200608153},ha={testnet:383416252,mainnet:2200608887},ga=2500,xa=400,Aa=604800,Ia=le("p"),va=le("a"),wa=24,ya=111300,ba=ga+xa*(41+wa),Ta=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(Ta||{}),ka=new Uint8Array(34);function _a(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Ea(e,t){return 0===Buffer.compare(e,t)}function Pa(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mSa(a(e))})}function Ua({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ra({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const ja=Ua({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Ua({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Ua({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Ua({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Ua({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Ua({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Ua({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ua({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Ua({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Va=Ma({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});Ma({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Ga=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=qa;)n[r++]=255&t|Xa,t/=128;for(;t&Qa;)n[r++]=255&t|Xa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},Xa=128,Qa=-128,qa=Math.pow(2,31);var Wa=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ja)<=za);return e.bytes=o-n,a},za=128,Ja=127;var Ka=Math.pow(2,7),Ya=Math.pow(2,14),Ha=Math.pow(2,21),$a=Math.pow(2,28),Za=Math.pow(2,35),es=Math.pow(2,42),ts=Math.pow(2,49),ns=Math.pow(2,56),rs=Math.pow(2,63),as={encode:Ga,decode:Wa,encodingLength:function(e){return e{const[n,r]=ss(e.subarray(t));return t+=r,n};let r=n(),a=ms;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Va;return[Va.prefix,n.decode(`${Va.prefix}${e}`)]}case Va.prefix:{const n=t??Va;return[Va.prefix,n.decode(e)]}case ja.prefix:{const n=t??ja;return[ja.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=fs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ds(a).set(n,e),a}}const ms=112,hs=18;function gs(e,t,n){const r=is(e),a=r+is(t),s=new Uint8Array(a+n.byteLength);return os(e,s,0),os(t,s,r),s.set(n,a),s}const xs=Symbol.for("@ipld/js-cid/CID");class As{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?us(this.code,t):t.then((e=>us(this.code,e)))}throw Error("Unknown type, must be binary type")}}const Is=85,vs=function({name:e,code:t,encode:n}){return new As(e,t,n)}({name:"sha2-256",code:18,encode:e=>Sa(n.createHash("sha256").update(e).digest())});var ws=126144e3,ys=1e7,bs=Math.pow(2,64),Ts=48,ks=21,_s=48,Es=le("tp"),Ps=le("sc"),Ss=28100,Ds=12900,Os=421700,Ns=409700;function Bs(e){var t=e/Aa;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*Aa}function Cs(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Bs(e)+Aa;re?r=t?e*e*bs/(2*t):0:r=(e+(e-r))*n/2;return r}function Vs(e,t){return Gs.apply(this,arguments)}function Gs(){return(Gs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",Xs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Qs(){return(Qs=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,vs.digest(n);case 3:return r=e.sent,a=fs.createV1(Is,r),e.abrupt("return",a.toString(ja));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(){for(var t=arguments.length,n=new Array(t),r=0;r0?ks-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return co(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return co(this.powerCount)[1]}}]),e}(),to=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+js(this.bias,this.slope,t)}}]),e}(),no=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),ro=p((function e(t){u(this,e),this.slopeDelta=t})),ao=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%ks;return e>0?ks-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return co(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return co(this.totalPowerCount)[1]}}]),e}();function so(){return(so=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=io(r),e.prev=1,e.next=4,Fs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",oo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function oo(e){var t=Buffer.from(e);return new eo(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function io(t){return e.decodeAddress(t).publicKey}function uo(e){var t=_a(e),n=new Uint8Array(Es.length+t.length);return n.set(Es,0),n.set(t,Es.length),n}function co(e){var t=e-1;return[Math.floor(t/ks),t%ks]}function po(t,n){var r=e.decodeAddress(t).publicKey,a=_a(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function lo(e,t,n){return fo.apply(this,arguments)}function fo(){return(fo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=ho(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",mo(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function mo(t){return new ro(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function ho(e){return q(Ps,_a(e))}function go(e,t,n){return xo.apply(this,arguments)}function xo(){return(xo=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/ks)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function yo(e){for(var t,n=Ts,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),_a(o),_a(i),q.apply(void 0,w(u.map((function(e){return _a(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Go({sender:s,suggestedParams:c,extraAppArgs:[_a(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:So,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Qo=8,qo=le("p"),Wo=le("a"),zo=le("v"),Jo=28500,Ko=12500,Yo=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Ho(t,n){return q(Wo,e.decodeAddress(t).publicKey,_a(n))}function $o(e){return new Yo(Pa(e.slice(0,8)),Pa(e.slice(8,16)),Pa(e.slice(16,24)),Pa(e.slice(24,32)),Pa(e.slice(32,40)),Pa(e.slice(40,48)),Boolean(Pa(e.slice(48,49))))}function Zo(){return(Zo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=Ys(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",$o(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ei(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Qo)throw new Error("You cannot cast vote for more than ".concat(Qo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return _a(e)})))),g=q.apply(void 0,w(u.map((function(e){return _a(e)})))),x=function(e){return q(qo,le(e))}(s),A=Ho(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Ea(e,A)})),v=io(a),y=Math.floor(c/ks),b=po(a,y),k=po(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(zo,_a(e),_a(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Ea(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,_a(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Jo+_*Ko;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ti(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Ns),c||(b+=Ds);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Se[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),_a(r)],boxes:d.slice(0,8),note:p}),Go({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ni(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Ns),!n.freeAccountPowerSpaceCount){var w=po(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:da[a],name:w}),v+=Os}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Se[a],from:s,to:e.getApplicationAddress(da[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:da[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Go({sender:s,suggestedParams:i,index:da[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(da[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function ri(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%Aa)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=io(o),l=po(o,n.lastAccountPowerBoxIndex),d=uo(i.lastTotalPowerBoxIndex),f=uo(i.lastTotalPowerBoxIndex+1),m=ho(n.lockEndTime),h=ho(a),g=[{appIndex:da[r],name:p},{appIndex:da[r],name:l},{appIndex:da[r],name:d},{appIndex:da[r],name:f},{appIndex:da[r],name:m},{appIndex:da[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=po(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:da[r],name:x})}var A,I=Cs(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%Aa==0){var b=ho(y);g.push({appIndex:da[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ds),n.freeAccountPowerSpaceCount||(k+=Os),v>i.freeTotalPowerSpaceCount&&(k+=Ns);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),_a(a)],boxes:g.slice(0,8),note:c}),Go({sender:o,suggestedParams:u,index:da[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(da[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function ai(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=io(a),u=po(a,n.lastAccountPowerBoxIndex),c=po(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:da[r],name:i},{appIndex:da[r],name:u},{appIndex:da[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Se[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(da[r]),amount:Os});d.unshift(f)}return e.assignGroupID(d)}var si=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Us(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/la),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,go(this.algodClient,da[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=wo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/Aa),u=Math.floor(t/Aa)-i,c=Array.from({length:u},(function(e,t){return(i+t)*Aa})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Us(l,g),d=Math.max(d-x,0),e.next=28,lo(this.algodClient,da[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=js(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Vs(this.algodClient,da[this.network]);case 3:return t=e.sent,e.abrupt("return",new ao(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,lo(this.algodClient,da[this.network],r);case 17:return l=e.sent,e.abrupt("return",ti({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ni({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,lo(this.algodClient,da[this.network],n);case 19:return p=e.sent,e.abrupt("return",ri({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ni({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,lo(this.algodClient,da[this.network],r);case 24:return f=e.sent,l=ri({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?qs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=ai({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ne({client:this.algodClient,initiatorAddr:a,assetID:Se[this.network]});case 15:c=e.sent,u=qs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return so.apply(this,arguments)}(this.algodClient,da[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return Zo.apply(this,arguments)}(this.algodClient,fa[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Ws(this.algodClient,fa[this.network]);case 17:return f=e.sent,e.next=20,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=wo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ei({stakingVotingAppId:fa[this.network],vaultAppId:da[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Vs(this.algodClient,ma[this.network]);case 3:return n=t.sent,t.abrupt("return",new Do(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*pa,h=d.firstPeriodTimestamp+(r+a)*pa,g=[],x=m;x=Math.floor(Date.now()/la)||c.votingEndTimestamp<=Math.floor(Date.now()/la))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,Io({algodClient:this.algodClient,address:i,appId:da[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=wo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*wa)),m=Ho(i,f),e.next=33,Rs(this.algodClient,ha[this.network],m);case 33:return h=!e.sent,e.abrupt("return",To({proposalVotingAppId:ha[this.network],vaultAppId:da[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Vs(this.algodClient,ha[this.network]);case 2:return t=e.sent,e.abrupt("return",new Ks(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),oi=2500,ii=400,ui=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return di(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),ci=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),pi=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*bs/ws)),s=t-r;return s<0?0:Us(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=_a,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=Ke,exports.isSwapQuoteErrorCausedByAmount=at,exports.poolUtils=Wt,exports.prepareCommitTransactions=function(e){return ca.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return Xr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=ht,exports.tinymanContract_v2=At,exports.tinymanJSSDKConfig=ut; diff --git a/dist/swap/index.d.ts b/dist/swap/index.d.ts index 77b01ee2..6ddc4a9e 100644 --- a/dist/swap/index.d.ts +++ b/dist/swap/index.d.ts @@ -47,21 +47,23 @@ export declare const Swap: { }) => Promise>; }; v2: { - getQuote: ({ type, amount, assetIn, assetOut, network, isSwapRouterEnabled, pool }: { + getQuote: ({ type, amount, assetIn, assetOut, network, slippage, isSwapRouterEnabled, pool }: { type: import("./constants").SwapType; amount: number | bigint; assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals; assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals; pool: import("..").V2PoolInfo | null; network: import("..").SupportedNetwork; + slippage: number; isSwapRouterEnabled?: boolean | undefined; }) => Promise; - getFixedInputSwapQuote: ({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: { + getFixedInputSwapQuote: ({ amount, assetIn, assetOut, isSwapRouterEnabled, network, slippage, pool }: { amount: number | bigint; assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals; assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals; network: import("..").SupportedNetwork; pool: import("..").V2PoolInfo | null; + slippage: number; isSwapRouterEnabled?: boolean | undefined; }) => Promise; getFixedInputDirectSwapQuote: ({ amount, assetIn, assetOut, pool }: { @@ -76,12 +78,13 @@ export declare const Swap: { assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals; assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals; }) => import("./types").SwapQuote; - getFixedOutputSwapQuote: ({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: { + getFixedOutputSwapQuote: ({ amount, assetIn, assetOut, isSwapRouterEnabled, network, slippage, pool }: { amount: number | bigint; assetIn: import("../util/asset/assetModels").AssetWithIdAndDecimals; assetOut: import("../util/asset/assetModels").AssetWithIdAndDecimals; pool: import("..").V2PoolInfo | null; network: import("..").SupportedNetwork; + slippage: number; isSwapRouterEnabled?: boolean | undefined; }) => Promise; generateTxns: (params: import("./types").GenerateSwapTxnsParams) => Promise; diff --git a/dist/swap/types.d.ts b/dist/swap/types.d.ts index 3d7c674f..af8b1866 100644 --- a/dist/swap/types.d.ts +++ b/dist/swap/types.d.ts @@ -47,6 +47,7 @@ export interface FetchSwapRouteQuotesPayload { input_amount?: string; output_amount?: string; swap_type: SwapType; + slippage: number; } export type SwapRouterResponse = Pick & { asset_in: Pick; @@ -88,6 +89,8 @@ export type GetSwapQuoteParams = { network: SupportedNetwork; /** If `true`, the function will also check the quotes that use swap route */ isSwapRouterEnabled?: boolean; + /** Slippage rate. Should be given as 0.1% -> 0.001. */ + slippage: number; }; export type SwapQuote = { data: DirectSwapQuoteAndPool; diff --git a/dist/swap/v2/index.d.ts b/dist/swap/v2/index.d.ts index d6f49422..99b9c23f 100644 --- a/dist/swap/v2/index.d.ts +++ b/dist/swap/v2/index.d.ts @@ -28,13 +28,14 @@ declare function execute({ client, quote, txGroup, signedTxns }: { * @param isSwapRouterEnabled - Whether the swap router is enabled * @returns A promise for the Swap quote */ -declare function getQuote({ type, amount, assetIn, assetOut, network, isSwapRouterEnabled, pool }: { +declare function getQuote({ type, amount, assetIn, assetOut, network, slippage, isSwapRouterEnabled, pool }: { type: SwapType; amount: number | bigint; assetIn: AssetWithIdAndDecimals; assetOut: AssetWithIdAndDecimals; pool: V2PoolInfo | null; network: SupportedNetwork; + slippage: number; isSwapRouterEnabled?: boolean; }): Promise; declare function getFixedInputDirectSwapQuote({ amount, assetIn, assetOut, pool }: { @@ -52,23 +53,25 @@ declare function getFixedOutputDirectSwapQuote({ amount, assetIn, assetOut, pool /** * @returns A quote for a fixed input swap. Does NOT execute any transactions. */ -declare function getFixedInputSwapQuote({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: { +declare function getFixedInputSwapQuote({ amount, assetIn, assetOut, isSwapRouterEnabled, network, slippage, pool }: { amount: number | bigint; assetIn: AssetWithIdAndDecimals; assetOut: AssetWithIdAndDecimals; network: SupportedNetwork; pool: V2PoolInfo | null; + slippage: number; isSwapRouterEnabled?: boolean; }): Promise; /** * @returns A quote for a fixed output swap. Does NOT execute any transactions. */ -declare function getFixedOutputSwapQuote({ amount, assetIn, assetOut, isSwapRouterEnabled, network, pool }: { +declare function getFixedOutputSwapQuote({ amount, assetIn, assetOut, isSwapRouterEnabled, network, slippage, pool }: { amount: number | bigint; assetIn: AssetWithIdAndDecimals; assetOut: AssetWithIdAndDecimals; pool: V2PoolInfo | null; network: SupportedNetwork; + slippage: number; isSwapRouterEnabled?: boolean; }): Promise; declare function calculateFixedInputSwap({ inputSupply, outputSupply, swapInputAmount, totalFeeShare, decimals }: { diff --git a/dist/swap/v2/router/swap-router.d.ts b/dist/swap/v2/router/swap-router.d.ts index 9fc62ff5..e25fa813 100644 --- a/dist/swap/v2/router/swap-router.d.ts +++ b/dist/swap/v2/router/swap-router.d.ts @@ -2,20 +2,20 @@ import algosdk, { Algodv2, SuggestedParams } from "algosdk"; import { SupportedNetwork } from "../../../util/commonTypes"; import { SwapType } from "../../constants"; import { SwapRouterResponse, SwapRouterTransactionRecipe } from "../../types"; -export declare function generateSwapRouterTxns({ initiatorAddr, client, route, slippage }: { +export declare function generateSwapRouterTxns({ initiatorAddr, client, route }: { client: Algodv2; initiatorAddr: string; route: SwapRouterResponse; - slippage: number; }): Promise<{ txn: algosdk.Transaction; signers: string[]; }[]>; -export declare function generateSwapRouterTxnFromRecipe(recipe: SwapRouterTransactionRecipe, suggestedParams: SuggestedParams, userAddress: string, assetInAmount: bigint, assetOutAmount: bigint): algosdk.Transaction; -export declare function getSwapRoute({ amount, assetInID, assetOutID, swapType, network }: { +export declare function generateSwapRouterTxnFromRecipe(recipe: SwapRouterTransactionRecipe, suggestedParams: SuggestedParams, userAddress: string): algosdk.Transaction; +export declare function getSwapRoute({ amount, assetInID, assetOutID, swapType, network, slippage }: { assetInID: number; assetOutID: number; swapType: SwapType; amount: number | bigint; network: SupportedNetwork; + slippage: number; }): Promise; diff --git a/src/swap/types.ts b/src/swap/types.ts index a8a1962a..06e31137 100644 --- a/src/swap/types.ts +++ b/src/swap/types.ts @@ -54,6 +54,7 @@ export interface FetchSwapRouteQuotesPayload { input_amount?: string; output_amount?: string; swap_type: SwapType; + slippage: number; } export type SwapRouterResponse = Pick & { @@ -95,6 +96,8 @@ export type GetSwapQuoteParams = { network: SupportedNetwork; /** If `true`, the function will also check the quotes that use swap route */ isSwapRouterEnabled?: boolean; + /** Slippage rate. Should be given as 0.1% -> 0.001. */ + slippage: number; }; export type SwapQuote = diff --git a/src/swap/utils.ts b/src/swap/utils.ts index 602aadd9..b02a8c75 100644 --- a/src/swap/utils.ts +++ b/src/swap/utils.ts @@ -125,7 +125,8 @@ export async function getFixedInputSwapQuote( }, pool: v2Pool?.info ?? null, isSwapRouterEnabled, - network: params.network + network: params.network, + slippage: params.slippage }) ); @@ -192,7 +193,8 @@ export async function getFixedOutputSwapQuote( }, pool: v2Pool?.info ?? null, isSwapRouterEnabled, - network: params.network + network: params.network, + slippage: params.slippage }) ); diff --git a/src/swap/v2/index.ts b/src/swap/v2/index.ts index fc10140d..d2ff86ba 100644 --- a/src/swap/v2/index.ts +++ b/src/swap/v2/index.ts @@ -213,6 +213,7 @@ async function getQuote({ assetIn, assetOut, network, + slippage, isSwapRouterEnabled, pool }: { @@ -222,6 +223,7 @@ async function getQuote({ assetOut: AssetWithIdAndDecimals; pool: V2PoolInfo | null; network: SupportedNetwork; + slippage: number; isSwapRouterEnabled?: boolean; }): Promise { let quote: SwapQuote; @@ -233,7 +235,8 @@ async function getQuote({ amount, isSwapRouterEnabled, network, - pool + pool, + slippage }); } else { quote = await getFixedOutputSwapQuote({ @@ -242,7 +245,8 @@ async function getQuote({ assetOut, isSwapRouterEnabled, network, - pool + pool, + slippage }); } @@ -458,6 +462,7 @@ async function getFixedInputSwapQuote({ assetOut, isSwapRouterEnabled, network, + slippage, pool }: { amount: number | bigint; @@ -465,6 +470,7 @@ async function getFixedInputSwapQuote({ assetOut: AssetWithIdAndDecimals; network: SupportedNetwork; pool: V2PoolInfo | null; + slippage: number; isSwapRouterEnabled?: boolean; }): Promise { const quotePromises: Promise[] = []; @@ -510,7 +516,8 @@ async function getFixedInputSwapQuote({ assetInID: assetIn.id, assetOutID: assetOut.id, swapType: SwapType.FixedInput, - network + network, + slippage }).then((data) => ({type: SwapQuoteType.Router, data})) ); } @@ -529,6 +536,7 @@ async function getFixedOutputSwapQuote({ assetOut, isSwapRouterEnabled, network, + slippage, pool }: { amount: number | bigint; @@ -536,6 +544,7 @@ async function getFixedOutputSwapQuote({ assetOut: AssetWithIdAndDecimals; pool: V2PoolInfo | null; network: SupportedNetwork; + slippage: number; isSwapRouterEnabled?: boolean; }): Promise { const quotePromises: Promise[] = [ @@ -562,7 +571,8 @@ async function getFixedOutputSwapQuote({ assetInID: assetIn.id, assetOutID: assetOut.id, swapType: SwapType.FixedOutput, - network + network, + slippage }).then((data) => ({type: SwapQuoteType.Router, data})) ); } diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 715850cd..7121cada 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -1,63 +1,36 @@ -import algosdk, {Algodv2, bigIntToBytes, SuggestedParams, Transaction} from "algosdk"; +import algosdk, {Algodv2, SuggestedParams, Transaction} from "algosdk"; import {toByteArray} from "base64-js"; import {SupportedNetwork} from "../../../util/commonTypes"; import {TINYMAN_ANALYTICS_API_BASE_URLS} from "../../../util/constant"; import SwapQuoteError, {SwapQuoteErrorType} from "../../../util/error/SwapQuoteError"; -import {applySlippageToAmount, hasTinymanApiErrorShape} from "../../../util/util"; +import {hasTinymanApiErrorShape} from "../../../util/util"; import {SwapType} from "../../constants"; import { FetchSwapRouteQuotesPayload, SwapRouterResponse, SwapRouterTransactionRecipe } from "../../types"; -import { - V2SwapRouterAppCallArgsTxnType, - V2SwapRouterSwapAppCallArgsIndices -} from "./constants"; -import {getAssetInFromSwapRoute, getAssetOutFromSwapRoute} from "./util"; export async function generateSwapRouterTxns({ initiatorAddr, client, - route, - slippage + route }: { client: Algodv2; initiatorAddr: string; route: SwapRouterResponse; - slippage: number; }) { if (!route.transactions || !route.transaction_fee) { return []; } const suggestedParams = await client.getTransactionParams().do(); - const [assetInAmountFromRoute, assetOutAmountFromRoute] = [ - getAssetInFromSwapRoute(route).amount, - getAssetOutFromSwapRoute(route).amount - ]; - const assetInAmount = - route.swap_type === SwapType.FixedInput - ? assetInAmountFromRoute - : applySlippageToAmount("positive", slippage, assetInAmountFromRoute); - const assetOutAmount = - route.swap_type === SwapType.FixedOutput - ? assetOutAmountFromRoute - : applySlippageToAmount("negative", slippage, assetOutAmountFromRoute); const txns: Transaction[] = []; route.transactions.forEach((txnRecipe) => { - txns.push( - generateSwapRouterTxnFromRecipe( - txnRecipe, - suggestedParams, - initiatorAddr, - assetInAmount, - assetOutAmount - ) - ); + txns.push(generateSwapRouterTxnFromRecipe(txnRecipe, suggestedParams, initiatorAddr)); }); txns[0].fee = Number(route.transaction_fee); @@ -72,9 +45,7 @@ export async function generateSwapRouterTxns({ export function generateSwapRouterTxnFromRecipe( recipe: SwapRouterTransactionRecipe, suggestedParams: SuggestedParams, - userAddress: string, - assetInAmount: bigint, - assetOutAmount: bigint + userAddress: string ) { let txn: Transaction; @@ -83,7 +54,7 @@ export function generateSwapRouterTxnFromRecipe( txn = algosdk.makePaymentTxnWithSuggestedParamsFromObject({ from: userAddress, to: recipe.receiver!, - amount: assetInAmount, + amount: recipe.amount, suggestedParams }); txn.fee = 0; @@ -95,7 +66,7 @@ export function generateSwapRouterTxnFromRecipe( txn = algosdk.makeAssetTransferTxnWithSuggestedParamsFromObject({ from: userAddress, to: recipe.receiver!, - amount: assetInAmount, + amount: recipe.amount, assetIndex: recipe.asset_id, suggestedParams }); @@ -106,20 +77,6 @@ export function generateSwapRouterTxnFromRecipe( case algosdk.TransactionType.appl: { const appArgs = recipe.args?.map(toByteArray); - const textDecoder = new TextDecoder(); - - if ( - appArgs?.length && - textDecoder.decode(appArgs[V2SwapRouterSwapAppCallArgsIndices.TxnType]) === - V2SwapRouterAppCallArgsTxnType.Swap - ) { - appArgs?.splice( - V2SwapRouterSwapAppCallArgsIndices.InputAmount, - 2, - bigIntToBytes(assetInAmount, 8), - bigIntToBytes(assetOutAmount, 8) - ); - } txn = algosdk.makeApplicationNoOpTxnFromObject({ from: userAddress, @@ -145,20 +102,23 @@ export async function getSwapRoute({ assetInID, assetOutID, swapType, - network + network, + slippage }: { assetInID: number; assetOutID: number; swapType: SwapType; amount: number | bigint; network: SupportedNetwork; + slippage: number; }): Promise { const payload: FetchSwapRouteQuotesPayload = { asset_in_id: String(assetInID), asset_out_id: String(assetOutID), swap_type: swapType, input_amount: swapType === SwapType.FixedInput ? String(amount) : undefined, - output_amount: swapType === SwapType.FixedOutput ? String(amount) : undefined + output_amount: swapType === SwapType.FixedOutput ? String(amount) : undefined, + slippage }; const response = await fetch( From e3c1d31655160b147a547a32bb0b603f266e16e8 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 13 Jan 2025 15:47:59 +0100 Subject: [PATCH 14/15] make sure the asset amounts are in BigInt type --- dist/index.js | 2 +- src/swap/v2/router/swap-router.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/dist/index.js b/dist/index.js index c6c6f441..116cacd3 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003;function he(e){return ge.apply(this,arguments)}function ge(){return(ge=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,(o=n.route).transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return i=t.sent,u=[],o.transactions.forEach((function(e){u.push(xe(e,i,r))})),u[0].fee=Number(o.transaction_fee),c=e.assignGroupID(u),t.abrupt("return",c.map((function(e){return{txn:e,signers:[r]}})));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xe(n,r,a){var s;switch(n.type){case e.TransactionType.pay:return(s=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,suggestedParams:r})).fee=0,s;case e.TransactionType.axfer:return(s=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,assetIndex:n.asset_id,suggestedParams:r})).fee=0,s;case e.TransactionType.appl:var o,i=null===(o=n.args)||void 0===o?void 0:o.map(t.toByteArray);return(s=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:i,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,s;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function Ae(e){return Ie.apply(this,arguments)}function Ie(){return(Ie=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetInID,o=t.assetOutID,i=t.swapType,u=t.network,c=t.slippage,p={asset_in_id:String(a),asset_out_id:String(o),swap_type:i,input_amount:i===fe.FixedInput?String(r):void 0,output_amount:i===fe.FixedOutput?String(r):void 0,slippage:c},e.next=4,fetch("".concat(M[u].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(p)}).catch((function(){throw new Error("Network error")}));case 4:return l=e.sent,e.next=7,l.json();case 7:if(d=e.sent,l.ok){e.next=14;break}if(s=d,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(d.type,d.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=d.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:if(!(Number(d.asset_in.id)!==a||Number(d.asset_out.id)!==o||d.swap_type===fe.FixedInput?r!==Number(d.input_amount):r!==Number(d.output_amount))){e.next=18;break}throw new E(_.UnknownError,"Swap router quote doesn't match the requested swap. Please try again.");case 18:return e.abrupt("return",d);case 19:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var ve={testnet:184778019,mainnet:1083651166};function we(e){var t=Te(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function ye(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function be(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function Te(e){return{assetIn:be(e),assetOut:ye(e)}}l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ke={V1_1:"v1_1",V2:"v2"},_e=0,Ee={id:"".concat(_e),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},Pe={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Se={testnet:258703304,mainnet:22e8},De={testnet:724519992,mainnet:2537013734},Oe={testnet:724676936,mainnet:2537023208};function Ne(e){return Be.apply(this,arguments)}function Be(){return(Be=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Ce(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Fe(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Le(e){return Number(e)===_e}function Re(e){return Number(e.id)}var Me,Ue,je=function(e){return e.Direct="direct",e.Router="router",e}({}),Ve=4*e.ALGORAND_MIN_TX_FEE,Ge=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),Xe=(l(Me={},fe.FixedInput,1),l(Me,fe.FixedOutput,2),Me),Qe=2,qe=le("swap"),We=(l(Ue={},fe.FixedInput,le("fixed-input")),l(Ue,fe.FixedOutput,le("fixed-output")),Ue);function ze(t){return(Xe[t]+Qe)*e.ALGORAND_MIN_TX_FEE}function Je(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function Ke(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function Ye(t){return(Xe[t]+1)*e.ALGORAND_MIN_TX_FEE}function He(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function $e(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=He({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function Ze(e){if(e.type===je.Router){var t=be(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function et(e){if(e.type===je.Router){var t=ye(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function tt(e){return e.type===je.Direct?e.data.pool.contractVersion:ke.V2}function nt(e){return e.type===je.Direct?e.data.quote.rate:we(e.data)}function rt(e){for(var t=e[0],n=nt(t),r=1;rn&&(t=a,n=s)}return t}function at(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var st,ot={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"},it={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"},ut=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===ke.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),ct=(l(st={},ke.V1_1,{testnet:62368684,mainnet:552635992}),l(st,ke.V2,{testnet:148607e3,mainnet:1002541853}),st);function pt(e,t){var n=ct[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function lt(){return(lt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dt(){return(dt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ft=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),mt=function(n){d(a,ft);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=pt(n.network,ke.V1_1),a=v(Fe(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:yt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function wt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function yt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var bt=le("e");function Tt(e){return kt.apply(this,arguments)}function kt(){return(kt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function _t(){return(_t=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fNt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Nt,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Lt(){return Lt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,vt(n,r);case 6:return i=e.sent,u=wt(i,pt(a,ke.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[St[ke.V1_1].asset1],asset2ID:u[St[ke.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Lt.apply(this,arguments)}var Rt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Lt.apply(this,arguments)},getPoolInfo:Bt,getPoolReserves:function(e,t){return Ft.apply(this,arguments)}});function Mt(e){return Ut.apply(this,arguments)}function Ut(){return(Ut=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=It(ke.V2),u=i.generateLogicSigAccountForPool(t),c=pt(r,ke.V2),p=u.address(),l=Fe(a,o),e.next=8,vt(n,p);case 8:return d=e.sent,f=wt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Pt.READY:Pt.NOT_CREATED,contractVersion:ke.V2},f&&(m.asset1ProtocolFees=BigInt(f[St.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[St.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[St.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[St.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[St.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[St.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[St.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[St.v2.totalFeeShare]),m.poolTokenID=Number(f[St.v2.poolTokenID]),m.asset1ID=Number(f[St.v2.asset1]),m.asset2ID=Number(f[St.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(){return(jt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,vt(t,n.account.address());case 2:return r=e.sent,a=wt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[St.v2.asset1Reserves]),o.asset2=BigInt(a[St.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[St.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,vt(n,r);case 3:return o=e.sent,i=wt(o,pt(a,ke.V2)),u=null,i&&(u={asset1ID:i[St[ke.V2].asset1],asset2ID:i[St[ke.V2].asset2],poolTokenID:i[St[ke.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Vt.apply(this,arguments)},getPoolInfo:Mt,getPoolReserves:function(e,t){return jt.apply(this,arguments)}});function Xt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Qt,qt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=Xt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Bt(e),Mt(e)])},isPoolEmpty:Xt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Pt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Pt.READY}}),Wt=a((l(Qt={},ke.V1_1,a(a({},Rt),qt)),l(Qt,ke.V2,a(a({},Gt),qt)),Qt),qt),zt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Jt={ASA_ALGO:96e4,ASA_ASA:859e3};function Kt(){return(Kt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Ce(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Le(g),I=pt(a,ke.V1_1),w=ht.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:Pe.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Yt(A),suggestedParams:c}),(E=[])[zt.FUNDING_TXN]=_,E[zt.VALIDATOR_APP_CALL]=b,E[zt.POOL_TOKEN_CREATE]=T,E[zt.ASSET1_OPT_IN]=k,A||(E[zt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[zt.FUNDING_TXN],signers:[u]},{txn:P[zt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[zt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[zt.ASSET1_OPT_IN],signers:[y]}],P[zt.ASSET2_OPT_IN]&&S.push({txn:P[zt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yt(e){return e?Jt.ASA_ALGO:Jt.ASA_ASA}function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=ht.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===zt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return Zt.apply(this,arguments)}function Zt(){return(Zt=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[zt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function en(){return(en=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,$t({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Wt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var tn={generateTxns:function(e){return Kt.apply(this,arguments)},signTxns:function(e){return Ht.apply(this,arguments)},execute:function(e){return en.apply(this,arguments)},getBootstrapFundingTxnAmount:Yt};var nn=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(e,t){return sn.apply(this,arguments)}function sn(){return(sn=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,an(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,on(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:_e,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function ln(){return(ln=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=pt(a,ke.V2),l=e.getApplicationAddress(p),d=Ce(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Wt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Pt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Le(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:ut.getAppCallTxnNoteWithClientName(ke.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=fn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:dn(A),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=w,y[nn.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[nn.FUNDING_TXN]={txn:b[nn.FUNDING_TXN],signers:[u]},T[nn.VALIDATOR_APP_CALL]={txn:b[nn.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dn(e){return function(e,t){var n=It(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(ke.V2,e)+fn(e)+D}function fn(t){return((t?rn.ASA_ALGO:rn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function mn(){return(mn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===nn.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,an(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(St.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Wt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var gn,xn={generateTxns:function(e){return ln.apply(this,arguments)},signTxns:function(e){return mn.apply(this,arguments)},execute:function(e){return hn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+dn(t)}};var An,In=(l(gn={},ke.V1_1,tn),l(gn,ke.V2,xn),l(gn,"generateTxns",(function(e){return e.contractVersion===ke.V1_1?tn.generateTxns(e):xn.generateTxns(e)})),l(gn,"signTxns",(function(e){return e.contractVersion===ke.V1_1?tn.signTxns(e):xn.signTxns(e)})),l(gn,"execute",(function(e){return e.contractVersion===ke.V1_1?tn.execute(e):xn.execute(e)})),l(gn,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===ke.V1_1?tn.getBootstrapFundingTxnAmount(n):xn.getBootstrapFundingTxnAmount(n)})),gn),vn=le("add_liquidity"),wn=(l(An={},ke.V1_1,[le("mint")]),l(An,ke.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[vn,le("single")],FLEXIBLE_MODE:[vn,le("flexible")]}),An),yn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),bn=5*e.ALGORAND_MIN_TX_FEE;function Tn(){return(Tn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:pt(a,ke.V1_1),appArgs:wn.v1_1,accounts:[l],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:u.id==_e?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kn(){return(kn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===yn.FEE_TXN?p:n===yn.ASSET1_IN_TXN?l:n===yn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _n(){return(_n=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[yn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,Tt({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var En,Pn,Sn,Dn=Object.freeze({__proto__:null,execute:function(e){return _n.apply(this,arguments)},generateTxns:function(e){return Tn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Rn(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Rn(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:$e({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Ln(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Dt)))}function Rn(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function Mn(t){return(Bn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Un(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function jn(e){return Vn.apply(this,arguments)}function Vn(){return(Vn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,cn(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Gn(){return(Gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Ce(i,u),m=v(f,2),h=m[0],g=m[1],x=Le(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=Mn(On.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Xn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Gn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Fn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Wt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Un});function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Le(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=Mn(On.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Fn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Wt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Un});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Le(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:pt(o,ke.V2),appArgs:wn.v2.INITIAL_LIQUIDITY,note:ut.getAppCallTxnNoteWithClientName(ke.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=Mn(On.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn,Jn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Dt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Dt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Ln(n,r)},slippage:s}},signTxns:Un}),Kn=Object.freeze({__proto__:null,flexible:Xn,initial:Jn,withSingleAsset:qn}),Yn=(l(zn={},ke.V1_1,Dn),l(zn,ke.V2,Kn),zn);var Hn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),$n=Object.values(Hn).length;function Zn(){return(Zn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Le(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Hn.FEE_TXN]=w,y[Hn.VALIDATOR_APP_CALL_TXN]=m,y[Hn.ASSET1_OUT_TXN]=g,y[Hn.ASSET2_OUT_TXN]=A,y[Hn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Hn.FEE_TXN],signers:[p]},{txn:b[Hn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Hn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Hn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Hn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function er(){return(er=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Hn.FEE_TXN?c:n===Hn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function tr(){return(tr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Hn.ASSET1_OUT_TXN].txn.amount,c=a[Hn.ASSET2_OUT_TXN].txn.amount,p=a[Hn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,Tt({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var nr={generateTxns:function(e){return Zn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return er.apply(this,arguments)},execute:function(e){return tr.apply(this,arguments)}};function rr(){return(rr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==je.Router){t.next=2;break}return t.abrupt("return",he(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Le(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[qe,We[u],e.encodeUint64(v)],note:ut.getAppCallTxnNoteWithClientName(ke.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=Ye(u),(b=[])[Ge.INPUT_TXN]=w,b[Ge.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Ge.INPUT_TXN],signers:[o]},{txn:T[Ge.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=et(i).id,g=Ze(i),e.prev=10,e.next=13,cn(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function sr(){return(sr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.slippage,c=t.isSwapRouterEnabled,p=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,cr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 4:l=e.sent,e.next=10;break;case 7:return e.next=9,lr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 9:l=e.sent;case 10:return e.abrupt("return",l);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function or(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(at(t)&&!at(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function ir(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=fr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function ur(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=$e({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:je.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function cr(e){return pr.apply(this,arguments)}function pr(){return(pr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[],c?p.push(new Promise((function(e,t){try{var s=ir({amount:n,assetIn:r,assetOut:a,pool:c});e({type:je.Direct,data:{pool:c,quote:s}})}catch(e){t(e)}}))):p.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=6,or(p);case 6:return l=e.sent,e.abrupt("return",rt(l));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[new Promise((function(e,t){try{e(ur({amount:n,assetIn:r,assetOut:a,pool:c}))}catch(e){t(e)}}))],o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=5,or(p);case 5:return l=e.sent,e.abrupt("return",rt(l));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:$e({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var mr={getQuote:function(e){return sr.apply(this,arguments)},getFixedInputSwapQuote:cr,getFixedInputDirectSwapQuote:ir,getFixedOutputDirectSwapQuote:ur,getFixedOutputSwapQuote:lr,generateTxns:function(e){return rr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return ar.apply(this,arguments)},calculateFixedInputSwap:fr},hr=2,gr=le("remove_liquidity"),xr=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function Ar(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Dt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Ir(){return(Ir=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[xr.ASSET_TRANSFER_TXN]=m,g[xr.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[xr.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[xr.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function vr(){return(vr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[xr.ASSET_TRANSFER_TXN]=I,w[xr.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[xr.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[xr.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function wr(){return(wr=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,cn(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var yr,br={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=Ar(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=Ar(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=mr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=mr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return Ir.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return vr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return wr.apply(this,arguments)}},Tr=(l(yr={},ke.V1_1,nr),l(yr,ke.V2,br),yr),kr=3n,_r=1000n,Er=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Er||{});function Pr(){return(Pr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Er.FEE_TXN_INDEX?c:n===Er.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sr(){return(Sr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:c.asset2ID==_e?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*kr/_r,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:He(d),priceImpact:$e(a({inputSupply:i,outputSupply:u},d))};return{type:je.Direct,data:{pool:t,quote:f}}}function Or(e){return Nr.apply(this,arguments)}function Nr(){return(Nr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Br(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*_r/(_r-kr),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:je.Direct,data:{pool:t,quote:h}}}function Cr(e){return Fr.apply(this,arguments)}function Fr(){return(Fr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lr(){return(Lr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Pt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Er.ASSET_IN_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Er.ASSET_OUT_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Or({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Cr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Rr,Mr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Dr({pool:t,reserves:n,assetIn:r,decimals:a}):Br({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Dr,getFixedOutputSwapQuote:Br,generateTxns:function(e){return Sr.apply(this,arguments)},signTxns:function(e){return Pr.apply(this,arguments)},execute:function(e){return Lr.apply(this,arguments)},executeFixedOutputSwap:Cr};function Ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(at(n)&&!at(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function jr(){return(jr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedInputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vr(){return(Vr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gr=(l(Rr={},ke.V1_1,Mr),l(Rr,ke.V2,mr),l(Rr,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return jr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Vr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Rr,"generateTxns",(function(e){return e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1?Mr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):mr.generateTxns(e)})),l(Rr,"signTxns",(function(e){if(e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1){var t=e.quote.data.pool;return Mr.signTxns(a(a({},e),{},{pool:t}))}return mr.signTxns(e)})),l(Rr,"execute",(function(e){return e.contractVersion===ke.V1_1?Mr.execute(e):mr.execute(e)})),Rr);function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Qr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Qr(e){return qr.apply(this,arguments)}function qr(){return(qr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wr(){return Wr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:ut.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Kr(e,t,n){return e*t/n}var Yr=BigInt(31536e3),Hr=BigInt(1e14),$r=BigInt(1e16),Zr={testnet:548587153,mainnet:1385499515},ea=14;function ta(e){return na.apply(this,arguments)}function na(){return(na=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(Zr[a]),t.next=4,vt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==_e&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ra(){return(ra=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(Zr[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Le(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),T=pt(a,ke.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ta({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(Zr[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),A=pt(l,ke.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Kr(e,$r+t*r/Yr,$r)}function oa(){return(oa=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Jr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ia={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+ea)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return aa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+ea)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Kr(BigInt(t),n,Hr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Hr/n},getFolksWrapperAppOptInRequiredAssetIDs:ta};function ua(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function ca(){return(ca=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=ua({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pa=604800,la=1e3,da={testnet:480164661,mainnet:2200606875},fa={testnet:360907790,mainnet:2200609638},ma={testnet:336189106,mainnet:2200608153},ha={testnet:383416252,mainnet:2200608887},ga=2500,xa=400,Aa=604800,Ia=le("p"),va=le("a"),wa=24,ya=111300,ba=ga+xa*(41+wa),Ta=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(Ta||{}),ka=new Uint8Array(34);function _a(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Ea(e,t){return 0===Buffer.compare(e,t)}function Pa(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mSa(a(e))})}function Ua({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ra({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const ja=Ua({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Ua({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Ua({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Ua({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Ua({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Ua({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Ua({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ua({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Ua({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Va=Ma({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});Ma({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Ga=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=qa;)n[r++]=255&t|Xa,t/=128;for(;t&Qa;)n[r++]=255&t|Xa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},Xa=128,Qa=-128,qa=Math.pow(2,31);var Wa=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ja)<=za);return e.bytes=o-n,a},za=128,Ja=127;var Ka=Math.pow(2,7),Ya=Math.pow(2,14),Ha=Math.pow(2,21),$a=Math.pow(2,28),Za=Math.pow(2,35),es=Math.pow(2,42),ts=Math.pow(2,49),ns=Math.pow(2,56),rs=Math.pow(2,63),as={encode:Ga,decode:Wa,encodingLength:function(e){return e{const[n,r]=ss(e.subarray(t));return t+=r,n};let r=n(),a=ms;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Va;return[Va.prefix,n.decode(`${Va.prefix}${e}`)]}case Va.prefix:{const n=t??Va;return[Va.prefix,n.decode(e)]}case ja.prefix:{const n=t??ja;return[ja.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=fs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ds(a).set(n,e),a}}const ms=112,hs=18;function gs(e,t,n){const r=is(e),a=r+is(t),s=new Uint8Array(a+n.byteLength);return os(e,s,0),os(t,s,r),s.set(n,a),s}const xs=Symbol.for("@ipld/js-cid/CID");class As{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?us(this.code,t):t.then((e=>us(this.code,e)))}throw Error("Unknown type, must be binary type")}}const Is=85,vs=function({name:e,code:t,encode:n}){return new As(e,t,n)}({name:"sha2-256",code:18,encode:e=>Sa(n.createHash("sha256").update(e).digest())});var ws=126144e3,ys=1e7,bs=Math.pow(2,64),Ts=48,ks=21,_s=48,Es=le("tp"),Ps=le("sc"),Ss=28100,Ds=12900,Os=421700,Ns=409700;function Bs(e){var t=e/Aa;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*Aa}function Cs(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Bs(e)+Aa;re?r=t?e*e*bs/(2*t):0:r=(e+(e-r))*n/2;return r}function Vs(e,t){return Gs.apply(this,arguments)}function Gs(){return(Gs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",Xs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Qs(){return(Qs=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,vs.digest(n);case 3:return r=e.sent,a=fs.createV1(Is,r),e.abrupt("return",a.toString(ja));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(){for(var t=arguments.length,n=new Array(t),r=0;r0?ks-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return co(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return co(this.powerCount)[1]}}]),e}(),to=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+js(this.bias,this.slope,t)}}]),e}(),no=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),ro=p((function e(t){u(this,e),this.slopeDelta=t})),ao=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%ks;return e>0?ks-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return co(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return co(this.totalPowerCount)[1]}}]),e}();function so(){return(so=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=io(r),e.prev=1,e.next=4,Fs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",oo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function oo(e){var t=Buffer.from(e);return new eo(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function io(t){return e.decodeAddress(t).publicKey}function uo(e){var t=_a(e),n=new Uint8Array(Es.length+t.length);return n.set(Es,0),n.set(t,Es.length),n}function co(e){var t=e-1;return[Math.floor(t/ks),t%ks]}function po(t,n){var r=e.decodeAddress(t).publicKey,a=_a(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function lo(e,t,n){return fo.apply(this,arguments)}function fo(){return(fo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=ho(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",mo(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function mo(t){return new ro(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function ho(e){return q(Ps,_a(e))}function go(e,t,n){return xo.apply(this,arguments)}function xo(){return(xo=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/ks)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function yo(e){for(var t,n=Ts,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),_a(o),_a(i),q.apply(void 0,w(u.map((function(e){return _a(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Go({sender:s,suggestedParams:c,extraAppArgs:[_a(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:So,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Qo=8,qo=le("p"),Wo=le("a"),zo=le("v"),Jo=28500,Ko=12500,Yo=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Ho(t,n){return q(Wo,e.decodeAddress(t).publicKey,_a(n))}function $o(e){return new Yo(Pa(e.slice(0,8)),Pa(e.slice(8,16)),Pa(e.slice(16,24)),Pa(e.slice(24,32)),Pa(e.slice(32,40)),Pa(e.slice(40,48)),Boolean(Pa(e.slice(48,49))))}function Zo(){return(Zo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=Ys(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",$o(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ei(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Qo)throw new Error("You cannot cast vote for more than ".concat(Qo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return _a(e)})))),g=q.apply(void 0,w(u.map((function(e){return _a(e)})))),x=function(e){return q(qo,le(e))}(s),A=Ho(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Ea(e,A)})),v=io(a),y=Math.floor(c/ks),b=po(a,y),k=po(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(zo,_a(e),_a(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Ea(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,_a(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Jo+_*Ko;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ti(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Ns),c||(b+=Ds);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Se[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),_a(r)],boxes:d.slice(0,8),note:p}),Go({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ni(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Ns),!n.freeAccountPowerSpaceCount){var w=po(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:da[a],name:w}),v+=Os}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Se[a],from:s,to:e.getApplicationAddress(da[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:da[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Go({sender:s,suggestedParams:i,index:da[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(da[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function ri(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%Aa)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=io(o),l=po(o,n.lastAccountPowerBoxIndex),d=uo(i.lastTotalPowerBoxIndex),f=uo(i.lastTotalPowerBoxIndex+1),m=ho(n.lockEndTime),h=ho(a),g=[{appIndex:da[r],name:p},{appIndex:da[r],name:l},{appIndex:da[r],name:d},{appIndex:da[r],name:f},{appIndex:da[r],name:m},{appIndex:da[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=po(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:da[r],name:x})}var A,I=Cs(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%Aa==0){var b=ho(y);g.push({appIndex:da[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ds),n.freeAccountPowerSpaceCount||(k+=Os),v>i.freeTotalPowerSpaceCount&&(k+=Ns);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),_a(a)],boxes:g.slice(0,8),note:c}),Go({sender:o,suggestedParams:u,index:da[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(da[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function ai(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=io(a),u=po(a,n.lastAccountPowerBoxIndex),c=po(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:da[r],name:i},{appIndex:da[r],name:u},{appIndex:da[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Se[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(da[r]),amount:Os});d.unshift(f)}return e.assignGroupID(d)}var si=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Us(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/la),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,go(this.algodClient,da[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=wo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/Aa),u=Math.floor(t/Aa)-i,c=Array.from({length:u},(function(e,t){return(i+t)*Aa})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Us(l,g),d=Math.max(d-x,0),e.next=28,lo(this.algodClient,da[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=js(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Vs(this.algodClient,da[this.network]);case 3:return t=e.sent,e.abrupt("return",new ao(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,lo(this.algodClient,da[this.network],r);case 17:return l=e.sent,e.abrupt("return",ti({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ni({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,lo(this.algodClient,da[this.network],n);case 19:return p=e.sent,e.abrupt("return",ri({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ni({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,lo(this.algodClient,da[this.network],r);case 24:return f=e.sent,l=ri({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?qs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=ai({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ne({client:this.algodClient,initiatorAddr:a,assetID:Se[this.network]});case 15:c=e.sent,u=qs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return so.apply(this,arguments)}(this.algodClient,da[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return Zo.apply(this,arguments)}(this.algodClient,fa[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Ws(this.algodClient,fa[this.network]);case 17:return f=e.sent,e.next=20,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=wo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ei({stakingVotingAppId:fa[this.network],vaultAppId:da[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Vs(this.algodClient,ma[this.network]);case 3:return n=t.sent,t.abrupt("return",new Do(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*pa,h=d.firstPeriodTimestamp+(r+a)*pa,g=[],x=m;x=Math.floor(Date.now()/la)||c.votingEndTimestamp<=Math.floor(Date.now()/la))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,Io({algodClient:this.algodClient,address:i,appId:da[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=wo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*wa)),m=Ho(i,f),e.next=33,Rs(this.algodClient,ha[this.network],m);case 33:return h=!e.sent,e.abrupt("return",To({proposalVotingAppId:ha[this.network],vaultAppId:da[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Vs(this.algodClient,ha[this.network]);case 2:return t=e.sent,e.abrupt("return",new Ks(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),oi=2500,ii=400,ui=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return di(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),ci=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),pi=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*bs/ws)),s=t-r;return s<0?0:Us(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=_a,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=Ke,exports.isSwapQuoteErrorCausedByAmount=at,exports.poolUtils=Wt,exports.prepareCommitTransactions=function(e){return ca.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return Xr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=ht,exports.tinymanContract_v2=At,exports.tinymanJSSDKConfig=ut; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003;function he(e){return ge.apply(this,arguments)}function ge(){return(ge=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,(o=n.route).transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return i=t.sent,u=[],o.transactions.forEach((function(e){u.push(xe(e,i,r))})),u[0].fee=Number(o.transaction_fee),c=e.assignGroupID(u),t.abrupt("return",c.map((function(e){return{txn:e,signers:[r]}})));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xe(n,r,a){var s;switch(n.type){case e.TransactionType.pay:return(s=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,suggestedParams:r})).fee=0,s;case e.TransactionType.axfer:return(s=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,assetIndex:n.asset_id,suggestedParams:r})).fee=0,s;case e.TransactionType.appl:var o,i=null===(o=n.args)||void 0===o?void 0:o.map(t.toByteArray);return(s=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:i,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,s;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function Ae(e){return Ie.apply(this,arguments)}function Ie(){return(Ie=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.amount,i=t.assetInID,u=t.assetOutID,c=t.swapType,p=t.network,l=t.slippage,d={asset_in_id:String(i),asset_out_id:String(u),swap_type:c,input_amount:c===fe.FixedInput?String(o):void 0,output_amount:c===fe.FixedOutput?String(o):void 0,slippage:l},e.next=4,fetch("".concat(M[p].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(d)}).catch((function(){throw new Error("Network error")}));case 4:return f=e.sent,e.next=7,f.json();case 7:if(m=e.sent,f.ok){e.next=14;break}if(s=m,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(m.type,m.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=m.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:if(!(Number(m.asset_in.id)!==i||Number(m.asset_out.id)!==u||m.swap_type===fe.FixedInput?o!==BigInt(null!==(r=m.input_amount)&&void 0!==r?r:0):o!==BigInt(null!==(a=m.output_amount)&&void 0!==a?a:0))){e.next=18;break}throw new E(_.UnknownError,"Swap router quote doesn't match the requested swap. Please try again.");case 18:return e.abrupt("return",m);case 19:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var ve={testnet:184778019,mainnet:1083651166};function we(e){var t=Te(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function ye(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function be(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function Te(e){return{assetIn:be(e),assetOut:ye(e)}}l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ke={V1_1:"v1_1",V2:"v2"},_e=0,Ee={id:"".concat(_e),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},Pe={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Se={testnet:258703304,mainnet:22e8},De={testnet:724519992,mainnet:2537013734},Oe={testnet:724676936,mainnet:2537023208};function Ne(e){return Be.apply(this,arguments)}function Be(){return(Be=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Ce(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Fe(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Le(e){return Number(e)===_e}function Re(e){return Number(e.id)}var Me,Ue,je=function(e){return e.Direct="direct",e.Router="router",e}({}),Ve=4*e.ALGORAND_MIN_TX_FEE,Ge=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),Xe=(l(Me={},fe.FixedInput,1),l(Me,fe.FixedOutput,2),Me),Qe=2,qe=le("swap"),We=(l(Ue={},fe.FixedInput,le("fixed-input")),l(Ue,fe.FixedOutput,le("fixed-output")),Ue);function ze(t){return(Xe[t]+Qe)*e.ALGORAND_MIN_TX_FEE}function Je(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function Ke(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function Ye(t){return(Xe[t]+1)*e.ALGORAND_MIN_TX_FEE}function He(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function $e(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=He({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function Ze(e){if(e.type===je.Router){var t=be(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function et(e){if(e.type===je.Router){var t=ye(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function tt(e){return e.type===je.Direct?e.data.pool.contractVersion:ke.V2}function nt(e){return e.type===je.Direct?e.data.quote.rate:we(e.data)}function rt(e){for(var t=e[0],n=nt(t),r=1;rn&&(t=a,n=s)}return t}function at(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var st,ot={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"},it={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"},ut=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===ke.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),ct=(l(st={},ke.V1_1,{testnet:62368684,mainnet:552635992}),l(st,ke.V2,{testnet:148607e3,mainnet:1002541853}),st);function pt(e,t){var n=ct[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function lt(){return(lt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dt(){return(dt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ft=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),mt=function(n){d(a,ft);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=pt(n.network,ke.V1_1),a=v(Fe(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:yt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function wt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function yt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var bt=le("e");function Tt(e){return kt.apply(this,arguments)}function kt(){return(kt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function _t(){return(_t=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fNt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Nt,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Lt(){return Lt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,vt(n,r);case 6:return i=e.sent,u=wt(i,pt(a,ke.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[St[ke.V1_1].asset1],asset2ID:u[St[ke.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Lt.apply(this,arguments)}var Rt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Lt.apply(this,arguments)},getPoolInfo:Bt,getPoolReserves:function(e,t){return Ft.apply(this,arguments)}});function Mt(e){return Ut.apply(this,arguments)}function Ut(){return(Ut=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=It(ke.V2),u=i.generateLogicSigAccountForPool(t),c=pt(r,ke.V2),p=u.address(),l=Fe(a,o),e.next=8,vt(n,p);case 8:return d=e.sent,f=wt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Pt.READY:Pt.NOT_CREATED,contractVersion:ke.V2},f&&(m.asset1ProtocolFees=BigInt(f[St.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[St.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[St.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[St.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[St.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[St.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[St.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[St.v2.totalFeeShare]),m.poolTokenID=Number(f[St.v2.poolTokenID]),m.asset1ID=Number(f[St.v2.asset1]),m.asset2ID=Number(f[St.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(){return(jt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,vt(t,n.account.address());case 2:return r=e.sent,a=wt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[St.v2.asset1Reserves]),o.asset2=BigInt(a[St.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[St.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,vt(n,r);case 3:return o=e.sent,i=wt(o,pt(a,ke.V2)),u=null,i&&(u={asset1ID:i[St[ke.V2].asset1],asset2ID:i[St[ke.V2].asset2],poolTokenID:i[St[ke.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Vt.apply(this,arguments)},getPoolInfo:Mt,getPoolReserves:function(e,t){return jt.apply(this,arguments)}});function Xt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Qt,qt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=Xt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Bt(e),Mt(e)])},isPoolEmpty:Xt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Pt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Pt.READY}}),Wt=a((l(Qt={},ke.V1_1,a(a({},Rt),qt)),l(Qt,ke.V2,a(a({},Gt),qt)),Qt),qt),zt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Jt={ASA_ALGO:96e4,ASA_ASA:859e3};function Kt(){return(Kt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Ce(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Le(g),I=pt(a,ke.V1_1),w=ht.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:Pe.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Yt(A),suggestedParams:c}),(E=[])[zt.FUNDING_TXN]=_,E[zt.VALIDATOR_APP_CALL]=b,E[zt.POOL_TOKEN_CREATE]=T,E[zt.ASSET1_OPT_IN]=k,A||(E[zt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[zt.FUNDING_TXN],signers:[u]},{txn:P[zt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[zt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[zt.ASSET1_OPT_IN],signers:[y]}],P[zt.ASSET2_OPT_IN]&&S.push({txn:P[zt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yt(e){return e?Jt.ASA_ALGO:Jt.ASA_ASA}function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=ht.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===zt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return Zt.apply(this,arguments)}function Zt(){return(Zt=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[zt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function en(){return(en=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,$t({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Wt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var tn={generateTxns:function(e){return Kt.apply(this,arguments)},signTxns:function(e){return Ht.apply(this,arguments)},execute:function(e){return en.apply(this,arguments)},getBootstrapFundingTxnAmount:Yt};var nn=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(e,t){return sn.apply(this,arguments)}function sn(){return(sn=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,an(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,on(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:_e,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function ln(){return(ln=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=pt(a,ke.V2),l=e.getApplicationAddress(p),d=Ce(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Wt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Pt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Le(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:ut.getAppCallTxnNoteWithClientName(ke.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=fn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:dn(A),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=w,y[nn.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[nn.FUNDING_TXN]={txn:b[nn.FUNDING_TXN],signers:[u]},T[nn.VALIDATOR_APP_CALL]={txn:b[nn.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dn(e){return function(e,t){var n=It(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(ke.V2,e)+fn(e)+D}function fn(t){return((t?rn.ASA_ALGO:rn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function mn(){return(mn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===nn.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,an(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(St.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Wt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var gn,xn={generateTxns:function(e){return ln.apply(this,arguments)},signTxns:function(e){return mn.apply(this,arguments)},execute:function(e){return hn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+dn(t)}};var An,In=(l(gn={},ke.V1_1,tn),l(gn,ke.V2,xn),l(gn,"generateTxns",(function(e){return e.contractVersion===ke.V1_1?tn.generateTxns(e):xn.generateTxns(e)})),l(gn,"signTxns",(function(e){return e.contractVersion===ke.V1_1?tn.signTxns(e):xn.signTxns(e)})),l(gn,"execute",(function(e){return e.contractVersion===ke.V1_1?tn.execute(e):xn.execute(e)})),l(gn,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===ke.V1_1?tn.getBootstrapFundingTxnAmount(n):xn.getBootstrapFundingTxnAmount(n)})),gn),vn=le("add_liquidity"),wn=(l(An={},ke.V1_1,[le("mint")]),l(An,ke.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[vn,le("single")],FLEXIBLE_MODE:[vn,le("flexible")]}),An),yn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),bn=5*e.ALGORAND_MIN_TX_FEE;function Tn(){return(Tn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:pt(a,ke.V1_1),appArgs:wn.v1_1,accounts:[l],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:u.id==_e?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kn(){return(kn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===yn.FEE_TXN?p:n===yn.ASSET1_IN_TXN?l:n===yn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _n(){return(_n=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[yn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,Tt({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var En,Pn,Sn,Dn=Object.freeze({__proto__:null,execute:function(e){return _n.apply(this,arguments)},generateTxns:function(e){return Tn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Rn(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Rn(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:$e({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Ln(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Dt)))}function Rn(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function Mn(t){return(Bn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Un(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function jn(e){return Vn.apply(this,arguments)}function Vn(){return(Vn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,cn(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Gn(){return(Gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Ce(i,u),m=v(f,2),h=m[0],g=m[1],x=Le(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=Mn(On.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Xn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Gn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Fn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Wt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Un});function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Le(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=Mn(On.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Fn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Wt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Un});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Le(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:pt(o,ke.V2),appArgs:wn.v2.INITIAL_LIQUIDITY,note:ut.getAppCallTxnNoteWithClientName(ke.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=Mn(On.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn,Jn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Dt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Dt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Ln(n,r)},slippage:s}},signTxns:Un}),Kn=Object.freeze({__proto__:null,flexible:Xn,initial:Jn,withSingleAsset:qn}),Yn=(l(zn={},ke.V1_1,Dn),l(zn,ke.V2,Kn),zn);var Hn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),$n=Object.values(Hn).length;function Zn(){return(Zn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Le(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Hn.FEE_TXN]=w,y[Hn.VALIDATOR_APP_CALL_TXN]=m,y[Hn.ASSET1_OUT_TXN]=g,y[Hn.ASSET2_OUT_TXN]=A,y[Hn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Hn.FEE_TXN],signers:[p]},{txn:b[Hn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Hn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Hn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Hn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function er(){return(er=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Hn.FEE_TXN?c:n===Hn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function tr(){return(tr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Hn.ASSET1_OUT_TXN].txn.amount,c=a[Hn.ASSET2_OUT_TXN].txn.amount,p=a[Hn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,Tt({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var nr={generateTxns:function(e){return Zn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return er.apply(this,arguments)},execute:function(e){return tr.apply(this,arguments)}};function rr(){return(rr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==je.Router){t.next=2;break}return t.abrupt("return",he(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Le(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[qe,We[u],e.encodeUint64(v)],note:ut.getAppCallTxnNoteWithClientName(ke.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=Ye(u),(b=[])[Ge.INPUT_TXN]=w,b[Ge.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Ge.INPUT_TXN],signers:[o]},{txn:T[Ge.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=et(i).id,g=Ze(i),e.prev=10,e.next=13,cn(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function sr(){return(sr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.slippage,c=t.isSwapRouterEnabled,p=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,cr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 4:l=e.sent,e.next=10;break;case 7:return e.next=9,lr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 9:l=e.sent;case 10:return e.abrupt("return",l);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function or(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(at(t)&&!at(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function ir(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=fr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function ur(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=$e({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:je.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function cr(e){return pr.apply(this,arguments)}function pr(){return(pr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[],c?p.push(new Promise((function(e,t){try{var s=ir({amount:n,assetIn:r,assetOut:a,pool:c});e({type:je.Direct,data:{pool:c,quote:s}})}catch(e){t(e)}}))):p.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=6,or(p);case 6:return l=e.sent,e.abrupt("return",rt(l));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[new Promise((function(e,t){try{e(ur({amount:n,assetIn:r,assetOut:a,pool:c}))}catch(e){t(e)}}))],o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=5,or(p);case 5:return l=e.sent,e.abrupt("return",rt(l));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:$e({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var mr={getQuote:function(e){return sr.apply(this,arguments)},getFixedInputSwapQuote:cr,getFixedInputDirectSwapQuote:ir,getFixedOutputDirectSwapQuote:ur,getFixedOutputSwapQuote:lr,generateTxns:function(e){return rr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return ar.apply(this,arguments)},calculateFixedInputSwap:fr},hr=2,gr=le("remove_liquidity"),xr=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function Ar(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Dt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Ir(){return(Ir=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[xr.ASSET_TRANSFER_TXN]=m,g[xr.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[xr.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[xr.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function vr(){return(vr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[xr.ASSET_TRANSFER_TXN]=I,w[xr.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[xr.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[xr.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function wr(){return(wr=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,cn(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var yr,br={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=Ar(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=Ar(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=mr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=mr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return Ir.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return vr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return wr.apply(this,arguments)}},Tr=(l(yr={},ke.V1_1,nr),l(yr,ke.V2,br),yr),kr=3n,_r=1000n,Er=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Er||{});function Pr(){return(Pr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Er.FEE_TXN_INDEX?c:n===Er.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sr(){return(Sr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:c.asset2ID==_e?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*kr/_r,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:He(d),priceImpact:$e(a({inputSupply:i,outputSupply:u},d))};return{type:je.Direct,data:{pool:t,quote:f}}}function Or(e){return Nr.apply(this,arguments)}function Nr(){return(Nr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Br(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*_r/(_r-kr),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:je.Direct,data:{pool:t,quote:h}}}function Cr(e){return Fr.apply(this,arguments)}function Fr(){return(Fr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lr(){return(Lr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Pt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Er.ASSET_IN_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Er.ASSET_OUT_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Or({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Cr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Rr,Mr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Dr({pool:t,reserves:n,assetIn:r,decimals:a}):Br({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Dr,getFixedOutputSwapQuote:Br,generateTxns:function(e){return Sr.apply(this,arguments)},signTxns:function(e){return Pr.apply(this,arguments)},execute:function(e){return Lr.apply(this,arguments)},executeFixedOutputSwap:Cr};function Ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(at(n)&&!at(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function jr(){return(jr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedInputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vr(){return(Vr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gr=(l(Rr={},ke.V1_1,Mr),l(Rr,ke.V2,mr),l(Rr,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return jr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Vr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Rr,"generateTxns",(function(e){return e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1?Mr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):mr.generateTxns(e)})),l(Rr,"signTxns",(function(e){if(e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1){var t=e.quote.data.pool;return Mr.signTxns(a(a({},e),{},{pool:t}))}return mr.signTxns(e)})),l(Rr,"execute",(function(e){return e.contractVersion===ke.V1_1?Mr.execute(e):mr.execute(e)})),Rr);function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Qr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Qr(e){return qr.apply(this,arguments)}function qr(){return(qr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wr(){return Wr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:ut.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Kr(e,t,n){return e*t/n}var Yr=BigInt(31536e3),Hr=BigInt(1e14),$r=BigInt(1e16),Zr={testnet:548587153,mainnet:1385499515},ea=14;function ta(e){return na.apply(this,arguments)}function na(){return(na=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(Zr[a]),t.next=4,vt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==_e&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ra(){return(ra=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(Zr[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Le(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),T=pt(a,ke.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ta({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(Zr[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),A=pt(l,ke.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Kr(e,$r+t*r/Yr,$r)}function oa(){return(oa=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Jr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ia={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+ea)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return aa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+ea)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Kr(BigInt(t),n,Hr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Hr/n},getFolksWrapperAppOptInRequiredAssetIDs:ta};function ua(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function ca(){return(ca=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=ua({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pa=604800,la=1e3,da={testnet:480164661,mainnet:2200606875},fa={testnet:360907790,mainnet:2200609638},ma={testnet:336189106,mainnet:2200608153},ha={testnet:383416252,mainnet:2200608887},ga=2500,xa=400,Aa=604800,Ia=le("p"),va=le("a"),wa=24,ya=111300,ba=ga+xa*(41+wa),Ta=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(Ta||{}),ka=new Uint8Array(34);function _a(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Ea(e,t){return 0===Buffer.compare(e,t)}function Pa(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mSa(a(e))})}function Ua({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ra({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const ja=Ua({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Ua({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Ua({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Ua({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Ua({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Ua({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Ua({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ua({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Ua({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Va=Ma({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});Ma({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Ga=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=qa;)n[r++]=255&t|Xa,t/=128;for(;t&Qa;)n[r++]=255&t|Xa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},Xa=128,Qa=-128,qa=Math.pow(2,31);var Wa=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ja)<=za);return e.bytes=o-n,a},za=128,Ja=127;var Ka=Math.pow(2,7),Ya=Math.pow(2,14),Ha=Math.pow(2,21),$a=Math.pow(2,28),Za=Math.pow(2,35),es=Math.pow(2,42),ts=Math.pow(2,49),ns=Math.pow(2,56),rs=Math.pow(2,63),as={encode:Ga,decode:Wa,encodingLength:function(e){return e{const[n,r]=ss(e.subarray(t));return t+=r,n};let r=n(),a=ms;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Va;return[Va.prefix,n.decode(`${Va.prefix}${e}`)]}case Va.prefix:{const n=t??Va;return[Va.prefix,n.decode(e)]}case ja.prefix:{const n=t??ja;return[ja.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=fs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ds(a).set(n,e),a}}const ms=112,hs=18;function gs(e,t,n){const r=is(e),a=r+is(t),s=new Uint8Array(a+n.byteLength);return os(e,s,0),os(t,s,r),s.set(n,a),s}const xs=Symbol.for("@ipld/js-cid/CID");class As{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?us(this.code,t):t.then((e=>us(this.code,e)))}throw Error("Unknown type, must be binary type")}}const Is=85,vs=function({name:e,code:t,encode:n}){return new As(e,t,n)}({name:"sha2-256",code:18,encode:e=>Sa(n.createHash("sha256").update(e).digest())});var ws=126144e3,ys=1e7,bs=Math.pow(2,64),Ts=48,ks=21,_s=48,Es=le("tp"),Ps=le("sc"),Ss=28100,Ds=12900,Os=421700,Ns=409700;function Bs(e){var t=e/Aa;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*Aa}function Cs(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Bs(e)+Aa;re?r=t?e*e*bs/(2*t):0:r=(e+(e-r))*n/2;return r}function Vs(e,t){return Gs.apply(this,arguments)}function Gs(){return(Gs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",Xs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Qs(){return(Qs=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,vs.digest(n);case 3:return r=e.sent,a=fs.createV1(Is,r),e.abrupt("return",a.toString(ja));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(){for(var t=arguments.length,n=new Array(t),r=0;r0?ks-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return co(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return co(this.powerCount)[1]}}]),e}(),to=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+js(this.bias,this.slope,t)}}]),e}(),no=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),ro=p((function e(t){u(this,e),this.slopeDelta=t})),ao=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%ks;return e>0?ks-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return co(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return co(this.totalPowerCount)[1]}}]),e}();function so(){return(so=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=io(r),e.prev=1,e.next=4,Fs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",oo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function oo(e){var t=Buffer.from(e);return new eo(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function io(t){return e.decodeAddress(t).publicKey}function uo(e){var t=_a(e),n=new Uint8Array(Es.length+t.length);return n.set(Es,0),n.set(t,Es.length),n}function co(e){var t=e-1;return[Math.floor(t/ks),t%ks]}function po(t,n){var r=e.decodeAddress(t).publicKey,a=_a(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function lo(e,t,n){return fo.apply(this,arguments)}function fo(){return(fo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=ho(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",mo(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function mo(t){return new ro(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function ho(e){return q(Ps,_a(e))}function go(e,t,n){return xo.apply(this,arguments)}function xo(){return(xo=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/ks)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function yo(e){for(var t,n=Ts,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),_a(o),_a(i),q.apply(void 0,w(u.map((function(e){return _a(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Go({sender:s,suggestedParams:c,extraAppArgs:[_a(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:So,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Qo=8,qo=le("p"),Wo=le("a"),zo=le("v"),Jo=28500,Ko=12500,Yo=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Ho(t,n){return q(Wo,e.decodeAddress(t).publicKey,_a(n))}function $o(e){return new Yo(Pa(e.slice(0,8)),Pa(e.slice(8,16)),Pa(e.slice(16,24)),Pa(e.slice(24,32)),Pa(e.slice(32,40)),Pa(e.slice(40,48)),Boolean(Pa(e.slice(48,49))))}function Zo(){return(Zo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=Ys(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",$o(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ei(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Qo)throw new Error("You cannot cast vote for more than ".concat(Qo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return _a(e)})))),g=q.apply(void 0,w(u.map((function(e){return _a(e)})))),x=function(e){return q(qo,le(e))}(s),A=Ho(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Ea(e,A)})),v=io(a),y=Math.floor(c/ks),b=po(a,y),k=po(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(zo,_a(e),_a(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Ea(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,_a(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Jo+_*Ko;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ti(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Ns),c||(b+=Ds);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Se[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),_a(r)],boxes:d.slice(0,8),note:p}),Go({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ni(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Ns),!n.freeAccountPowerSpaceCount){var w=po(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:da[a],name:w}),v+=Os}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Se[a],from:s,to:e.getApplicationAddress(da[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:da[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Go({sender:s,suggestedParams:i,index:da[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(da[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function ri(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%Aa)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=io(o),l=po(o,n.lastAccountPowerBoxIndex),d=uo(i.lastTotalPowerBoxIndex),f=uo(i.lastTotalPowerBoxIndex+1),m=ho(n.lockEndTime),h=ho(a),g=[{appIndex:da[r],name:p},{appIndex:da[r],name:l},{appIndex:da[r],name:d},{appIndex:da[r],name:f},{appIndex:da[r],name:m},{appIndex:da[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=po(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:da[r],name:x})}var A,I=Cs(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%Aa==0){var b=ho(y);g.push({appIndex:da[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ds),n.freeAccountPowerSpaceCount||(k+=Os),v>i.freeTotalPowerSpaceCount&&(k+=Ns);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),_a(a)],boxes:g.slice(0,8),note:c}),Go({sender:o,suggestedParams:u,index:da[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(da[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function ai(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=io(a),u=po(a,n.lastAccountPowerBoxIndex),c=po(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:da[r],name:i},{appIndex:da[r],name:u},{appIndex:da[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Se[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(da[r]),amount:Os});d.unshift(f)}return e.assignGroupID(d)}var si=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Us(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/la),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,go(this.algodClient,da[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=wo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/Aa),u=Math.floor(t/Aa)-i,c=Array.from({length:u},(function(e,t){return(i+t)*Aa})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Us(l,g),d=Math.max(d-x,0),e.next=28,lo(this.algodClient,da[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=js(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Vs(this.algodClient,da[this.network]);case 3:return t=e.sent,e.abrupt("return",new ao(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,lo(this.algodClient,da[this.network],r);case 17:return l=e.sent,e.abrupt("return",ti({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ni({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,lo(this.algodClient,da[this.network],n);case 19:return p=e.sent,e.abrupt("return",ri({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ni({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,lo(this.algodClient,da[this.network],r);case 24:return f=e.sent,l=ri({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?qs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=ai({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ne({client:this.algodClient,initiatorAddr:a,assetID:Se[this.network]});case 15:c=e.sent,u=qs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return so.apply(this,arguments)}(this.algodClient,da[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return Zo.apply(this,arguments)}(this.algodClient,fa[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Ws(this.algodClient,fa[this.network]);case 17:return f=e.sent,e.next=20,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=wo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ei({stakingVotingAppId:fa[this.network],vaultAppId:da[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Vs(this.algodClient,ma[this.network]);case 3:return n=t.sent,t.abrupt("return",new Do(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*pa,h=d.firstPeriodTimestamp+(r+a)*pa,g=[],x=m;x=Math.floor(Date.now()/la)||c.votingEndTimestamp<=Math.floor(Date.now()/la))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,Io({algodClient:this.algodClient,address:i,appId:da[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=wo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*wa)),m=Ho(i,f),e.next=33,Rs(this.algodClient,ha[this.network],m);case 33:return h=!e.sent,e.abrupt("return",To({proposalVotingAppId:ha[this.network],vaultAppId:da[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Vs(this.algodClient,ha[this.network]);case 2:return t=e.sent,e.abrupt("return",new Ks(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),oi=2500,ii=400,ui=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return di(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),ci=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),pi=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*bs/ws)),s=t-r;return s<0?0:Us(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=_a,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=Ke,exports.isSwapQuoteErrorCausedByAmount=at,exports.poolUtils=Wt,exports.prepareCommitTransactions=function(e){return ca.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return Xr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=ht,exports.tinymanContract_v2=At,exports.tinymanJSSDKConfig=ut; diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index 7121cada..f6da5529 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -161,8 +161,8 @@ export async function getSwapRoute({ Number((serializedResponse as SwapRouterResponse).asset_in.id) !== assetInID || Number((serializedResponse as SwapRouterResponse).asset_out.id) !== assetOutID || (serializedResponse as SwapRouterResponse).swap_type === SwapType.FixedInput - ? amount !== Number((serializedResponse as SwapRouterResponse).input_amount) - : amount !== Number((serializedResponse as SwapRouterResponse).output_amount) + ? amount !== BigInt((serializedResponse as SwapRouterResponse).input_amount ?? 0) + : amount !== BigInt((serializedResponse as SwapRouterResponse).output_amount ?? 0) ) { throw new SwapQuoteError( SwapQuoteErrorType.UnknownError, From 29795f6c6c22df90d5f5d9d339a1ba297827bba1 Mon Sep 17 00:00:00 2001 From: gulcinuras Date: Mon, 13 Jan 2025 16:17:18 +0100 Subject: [PATCH 15/15] fix(swap-router): Make sure amount is in bigint type as well --- dist/index.js | 2 +- src/liquid-stake/tAlgoClient.test.ts | 2 +- src/swap/v2/router/swap-router.ts | 6 ++++-- 3 files changed, 6 insertions(+), 4 deletions(-) diff --git a/dist/index.js b/dist/index.js index 116cacd3..d0f0ebe5 100644 --- a/dist/index.js +++ b/dist/index.js @@ -1 +1 @@ -"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003;function he(e){return ge.apply(this,arguments)}function ge(){return(ge=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,(o=n.route).transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return i=t.sent,u=[],o.transactions.forEach((function(e){u.push(xe(e,i,r))})),u[0].fee=Number(o.transaction_fee),c=e.assignGroupID(u),t.abrupt("return",c.map((function(e){return{txn:e,signers:[r]}})));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xe(n,r,a){var s;switch(n.type){case e.TransactionType.pay:return(s=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,suggestedParams:r})).fee=0,s;case e.TransactionType.axfer:return(s=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,assetIndex:n.asset_id,suggestedParams:r})).fee=0,s;case e.TransactionType.appl:var o,i=null===(o=n.args)||void 0===o?void 0:o.map(t.toByteArray);return(s=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:i,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,s;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function Ae(e){return Ie.apply(this,arguments)}function Ie(){return(Ie=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.amount,i=t.assetInID,u=t.assetOutID,c=t.swapType,p=t.network,l=t.slippage,d={asset_in_id:String(i),asset_out_id:String(u),swap_type:c,input_amount:c===fe.FixedInput?String(o):void 0,output_amount:c===fe.FixedOutput?String(o):void 0,slippage:l},e.next=4,fetch("".concat(M[p].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(d)}).catch((function(){throw new Error("Network error")}));case 4:return f=e.sent,e.next=7,f.json();case 7:if(m=e.sent,f.ok){e.next=14;break}if(s=m,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(m.type,m.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=m.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:if(!(Number(m.asset_in.id)!==i||Number(m.asset_out.id)!==u||m.swap_type===fe.FixedInput?o!==BigInt(null!==(r=m.input_amount)&&void 0!==r?r:0):o!==BigInt(null!==(a=m.output_amount)&&void 0!==a?a:0))){e.next=18;break}throw new E(_.UnknownError,"Swap router quote doesn't match the requested swap. Please try again.");case 18:return e.abrupt("return",m);case 19:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var ve={testnet:184778019,mainnet:1083651166};function we(e){var t=Te(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function ye(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function be(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function Te(e){return{assetIn:be(e),assetOut:ye(e)}}l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ke={V1_1:"v1_1",V2:"v2"},_e=0,Ee={id:"".concat(_e),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},Pe={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Se={testnet:258703304,mainnet:22e8},De={testnet:724519992,mainnet:2537013734},Oe={testnet:724676936,mainnet:2537023208};function Ne(e){return Be.apply(this,arguments)}function Be(){return(Be=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Ce(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Fe(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Le(e){return Number(e)===_e}function Re(e){return Number(e.id)}var Me,Ue,je=function(e){return e.Direct="direct",e.Router="router",e}({}),Ve=4*e.ALGORAND_MIN_TX_FEE,Ge=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),Xe=(l(Me={},fe.FixedInput,1),l(Me,fe.FixedOutput,2),Me),Qe=2,qe=le("swap"),We=(l(Ue={},fe.FixedInput,le("fixed-input")),l(Ue,fe.FixedOutput,le("fixed-output")),Ue);function ze(t){return(Xe[t]+Qe)*e.ALGORAND_MIN_TX_FEE}function Je(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function Ke(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function Ye(t){return(Xe[t]+1)*e.ALGORAND_MIN_TX_FEE}function He(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function $e(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=He({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function Ze(e){if(e.type===je.Router){var t=be(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function et(e){if(e.type===je.Router){var t=ye(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function tt(e){return e.type===je.Direct?e.data.pool.contractVersion:ke.V2}function nt(e){return e.type===je.Direct?e.data.quote.rate:we(e.data)}function rt(e){for(var t=e[0],n=nt(t),r=1;rn&&(t=a,n=s)}return t}function at(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var st,ot={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"},it={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"},ut=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===ke.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),ct=(l(st={},ke.V1_1,{testnet:62368684,mainnet:552635992}),l(st,ke.V2,{testnet:148607e3,mainnet:1002541853}),st);function pt(e,t){var n=ct[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function lt(){return(lt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dt(){return(dt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ft=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),mt=function(n){d(a,ft);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=pt(n.network,ke.V1_1),a=v(Fe(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:yt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function wt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function yt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var bt=le("e");function Tt(e){return kt.apply(this,arguments)}function kt(){return(kt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function _t(){return(_t=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fNt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Nt,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Lt(){return Lt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,vt(n,r);case 6:return i=e.sent,u=wt(i,pt(a,ke.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[St[ke.V1_1].asset1],asset2ID:u[St[ke.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Lt.apply(this,arguments)}var Rt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Lt.apply(this,arguments)},getPoolInfo:Bt,getPoolReserves:function(e,t){return Ft.apply(this,arguments)}});function Mt(e){return Ut.apply(this,arguments)}function Ut(){return(Ut=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=It(ke.V2),u=i.generateLogicSigAccountForPool(t),c=pt(r,ke.V2),p=u.address(),l=Fe(a,o),e.next=8,vt(n,p);case 8:return d=e.sent,f=wt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Pt.READY:Pt.NOT_CREATED,contractVersion:ke.V2},f&&(m.asset1ProtocolFees=BigInt(f[St.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[St.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[St.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[St.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[St.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[St.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[St.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[St.v2.totalFeeShare]),m.poolTokenID=Number(f[St.v2.poolTokenID]),m.asset1ID=Number(f[St.v2.asset1]),m.asset2ID=Number(f[St.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(){return(jt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,vt(t,n.account.address());case 2:return r=e.sent,a=wt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[St.v2.asset1Reserves]),o.asset2=BigInt(a[St.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[St.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,vt(n,r);case 3:return o=e.sent,i=wt(o,pt(a,ke.V2)),u=null,i&&(u={asset1ID:i[St[ke.V2].asset1],asset2ID:i[St[ke.V2].asset2],poolTokenID:i[St[ke.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Vt.apply(this,arguments)},getPoolInfo:Mt,getPoolReserves:function(e,t){return jt.apply(this,arguments)}});function Xt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Qt,qt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=Xt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Bt(e),Mt(e)])},isPoolEmpty:Xt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Pt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Pt.READY}}),Wt=a((l(Qt={},ke.V1_1,a(a({},Rt),qt)),l(Qt,ke.V2,a(a({},Gt),qt)),Qt),qt),zt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Jt={ASA_ALGO:96e4,ASA_ASA:859e3};function Kt(){return(Kt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Ce(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Le(g),I=pt(a,ke.V1_1),w=ht.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:Pe.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Yt(A),suggestedParams:c}),(E=[])[zt.FUNDING_TXN]=_,E[zt.VALIDATOR_APP_CALL]=b,E[zt.POOL_TOKEN_CREATE]=T,E[zt.ASSET1_OPT_IN]=k,A||(E[zt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[zt.FUNDING_TXN],signers:[u]},{txn:P[zt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[zt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[zt.ASSET1_OPT_IN],signers:[y]}],P[zt.ASSET2_OPT_IN]&&S.push({txn:P[zt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yt(e){return e?Jt.ASA_ALGO:Jt.ASA_ASA}function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=ht.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===zt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return Zt.apply(this,arguments)}function Zt(){return(Zt=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[zt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function en(){return(en=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,$t({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Wt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var tn={generateTxns:function(e){return Kt.apply(this,arguments)},signTxns:function(e){return Ht.apply(this,arguments)},execute:function(e){return en.apply(this,arguments)},getBootstrapFundingTxnAmount:Yt};var nn=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(e,t){return sn.apply(this,arguments)}function sn(){return(sn=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,an(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,on(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:_e,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function ln(){return(ln=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=pt(a,ke.V2),l=e.getApplicationAddress(p),d=Ce(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Wt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Pt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Le(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:ut.getAppCallTxnNoteWithClientName(ke.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=fn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:dn(A),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=w,y[nn.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[nn.FUNDING_TXN]={txn:b[nn.FUNDING_TXN],signers:[u]},T[nn.VALIDATOR_APP_CALL]={txn:b[nn.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dn(e){return function(e,t){var n=It(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(ke.V2,e)+fn(e)+D}function fn(t){return((t?rn.ASA_ALGO:rn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function mn(){return(mn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===nn.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,an(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(St.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Wt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var gn,xn={generateTxns:function(e){return ln.apply(this,arguments)},signTxns:function(e){return mn.apply(this,arguments)},execute:function(e){return hn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+dn(t)}};var An,In=(l(gn={},ke.V1_1,tn),l(gn,ke.V2,xn),l(gn,"generateTxns",(function(e){return e.contractVersion===ke.V1_1?tn.generateTxns(e):xn.generateTxns(e)})),l(gn,"signTxns",(function(e){return e.contractVersion===ke.V1_1?tn.signTxns(e):xn.signTxns(e)})),l(gn,"execute",(function(e){return e.contractVersion===ke.V1_1?tn.execute(e):xn.execute(e)})),l(gn,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===ke.V1_1?tn.getBootstrapFundingTxnAmount(n):xn.getBootstrapFundingTxnAmount(n)})),gn),vn=le("add_liquidity"),wn=(l(An={},ke.V1_1,[le("mint")]),l(An,ke.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[vn,le("single")],FLEXIBLE_MODE:[vn,le("flexible")]}),An),yn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),bn=5*e.ALGORAND_MIN_TX_FEE;function Tn(){return(Tn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:pt(a,ke.V1_1),appArgs:wn.v1_1,accounts:[l],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:u.id==_e?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kn(){return(kn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===yn.FEE_TXN?p:n===yn.ASSET1_IN_TXN?l:n===yn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _n(){return(_n=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[yn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,Tt({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var En,Pn,Sn,Dn=Object.freeze({__proto__:null,execute:function(e){return _n.apply(this,arguments)},generateTxns:function(e){return Tn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Rn(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Rn(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:$e({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Ln(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Dt)))}function Rn(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function Mn(t){return(Bn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Un(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function jn(e){return Vn.apply(this,arguments)}function Vn(){return(Vn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,cn(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Gn(){return(Gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Ce(i,u),m=v(f,2),h=m[0],g=m[1],x=Le(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=Mn(On.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Xn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Gn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Fn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Wt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Un});function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Le(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=Mn(On.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Fn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Wt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Un});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Le(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:pt(o,ke.V2),appArgs:wn.v2.INITIAL_LIQUIDITY,note:ut.getAppCallTxnNoteWithClientName(ke.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=Mn(On.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn,Jn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Dt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Dt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Ln(n,r)},slippage:s}},signTxns:Un}),Kn=Object.freeze({__proto__:null,flexible:Xn,initial:Jn,withSingleAsset:qn}),Yn=(l(zn={},ke.V1_1,Dn),l(zn,ke.V2,Kn),zn);var Hn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),$n=Object.values(Hn).length;function Zn(){return(Zn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Le(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Hn.FEE_TXN]=w,y[Hn.VALIDATOR_APP_CALL_TXN]=m,y[Hn.ASSET1_OUT_TXN]=g,y[Hn.ASSET2_OUT_TXN]=A,y[Hn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Hn.FEE_TXN],signers:[p]},{txn:b[Hn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Hn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Hn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Hn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function er(){return(er=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Hn.FEE_TXN?c:n===Hn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function tr(){return(tr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Hn.ASSET1_OUT_TXN].txn.amount,c=a[Hn.ASSET2_OUT_TXN].txn.amount,p=a[Hn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,Tt({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var nr={generateTxns:function(e){return Zn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return er.apply(this,arguments)},execute:function(e){return tr.apply(this,arguments)}};function rr(){return(rr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==je.Router){t.next=2;break}return t.abrupt("return",he(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Le(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[qe,We[u],e.encodeUint64(v)],note:ut.getAppCallTxnNoteWithClientName(ke.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=Ye(u),(b=[])[Ge.INPUT_TXN]=w,b[Ge.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Ge.INPUT_TXN],signers:[o]},{txn:T[Ge.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=et(i).id,g=Ze(i),e.prev=10,e.next=13,cn(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function sr(){return(sr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.slippage,c=t.isSwapRouterEnabled,p=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,cr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 4:l=e.sent,e.next=10;break;case 7:return e.next=9,lr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 9:l=e.sent;case 10:return e.abrupt("return",l);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function or(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(at(t)&&!at(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function ir(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=fr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function ur(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=$e({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:je.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function cr(e){return pr.apply(this,arguments)}function pr(){return(pr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[],c?p.push(new Promise((function(e,t){try{var s=ir({amount:n,assetIn:r,assetOut:a,pool:c});e({type:je.Direct,data:{pool:c,quote:s}})}catch(e){t(e)}}))):p.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=6,or(p);case 6:return l=e.sent,e.abrupt("return",rt(l));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[new Promise((function(e,t){try{e(ur({amount:n,assetIn:r,assetOut:a,pool:c}))}catch(e){t(e)}}))],o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=5,or(p);case 5:return l=e.sent,e.abrupt("return",rt(l));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:$e({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var mr={getQuote:function(e){return sr.apply(this,arguments)},getFixedInputSwapQuote:cr,getFixedInputDirectSwapQuote:ir,getFixedOutputDirectSwapQuote:ur,getFixedOutputSwapQuote:lr,generateTxns:function(e){return rr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return ar.apply(this,arguments)},calculateFixedInputSwap:fr},hr=2,gr=le("remove_liquidity"),xr=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function Ar(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Dt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Ir(){return(Ir=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[xr.ASSET_TRANSFER_TXN]=m,g[xr.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[xr.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[xr.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function vr(){return(vr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[xr.ASSET_TRANSFER_TXN]=I,w[xr.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[xr.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[xr.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function wr(){return(wr=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,cn(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var yr,br={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=Ar(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=Ar(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=mr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=mr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return Ir.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return vr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return wr.apply(this,arguments)}},Tr=(l(yr={},ke.V1_1,nr),l(yr,ke.V2,br),yr),kr=3n,_r=1000n,Er=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Er||{});function Pr(){return(Pr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Er.FEE_TXN_INDEX?c:n===Er.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sr(){return(Sr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:c.asset2ID==_e?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*kr/_r,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:He(d),priceImpact:$e(a({inputSupply:i,outputSupply:u},d))};return{type:je.Direct,data:{pool:t,quote:f}}}function Or(e){return Nr.apply(this,arguments)}function Nr(){return(Nr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Br(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*_r/(_r-kr),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:je.Direct,data:{pool:t,quote:h}}}function Cr(e){return Fr.apply(this,arguments)}function Fr(){return(Fr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lr(){return(Lr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Pt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Er.ASSET_IN_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Er.ASSET_OUT_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Or({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Cr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Rr,Mr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Dr({pool:t,reserves:n,assetIn:r,decimals:a}):Br({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Dr,getFixedOutputSwapQuote:Br,generateTxns:function(e){return Sr.apply(this,arguments)},signTxns:function(e){return Pr.apply(this,arguments)},execute:function(e){return Lr.apply(this,arguments)},executeFixedOutputSwap:Cr};function Ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(at(n)&&!at(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function jr(){return(jr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedInputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vr(){return(Vr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gr=(l(Rr={},ke.V1_1,Mr),l(Rr,ke.V2,mr),l(Rr,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return jr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Vr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Rr,"generateTxns",(function(e){return e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1?Mr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):mr.generateTxns(e)})),l(Rr,"signTxns",(function(e){if(e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1){var t=e.quote.data.pool;return Mr.signTxns(a(a({},e),{},{pool:t}))}return mr.signTxns(e)})),l(Rr,"execute",(function(e){return e.contractVersion===ke.V1_1?Mr.execute(e):mr.execute(e)})),Rr);function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Qr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Qr(e){return qr.apply(this,arguments)}function qr(){return(qr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wr(){return Wr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:ut.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Kr(e,t,n){return e*t/n}var Yr=BigInt(31536e3),Hr=BigInt(1e14),$r=BigInt(1e16),Zr={testnet:548587153,mainnet:1385499515},ea=14;function ta(e){return na.apply(this,arguments)}function na(){return(na=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(Zr[a]),t.next=4,vt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==_e&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ra(){return(ra=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(Zr[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Le(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),T=pt(a,ke.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ta({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(Zr[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),A=pt(l,ke.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Kr(e,$r+t*r/Yr,$r)}function oa(){return(oa=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Jr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ia={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+ea)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return aa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+ea)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Kr(BigInt(t),n,Hr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Hr/n},getFolksWrapperAppOptInRequiredAssetIDs:ta};function ua(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function ca(){return(ca=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=ua({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pa=604800,la=1e3,da={testnet:480164661,mainnet:2200606875},fa={testnet:360907790,mainnet:2200609638},ma={testnet:336189106,mainnet:2200608153},ha={testnet:383416252,mainnet:2200608887},ga=2500,xa=400,Aa=604800,Ia=le("p"),va=le("a"),wa=24,ya=111300,ba=ga+xa*(41+wa),Ta=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(Ta||{}),ka=new Uint8Array(34);function _a(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Ea(e,t){return 0===Buffer.compare(e,t)}function Pa(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mSa(a(e))})}function Ua({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ra({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const ja=Ua({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Ua({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Ua({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Ua({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Ua({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Ua({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Ua({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ua({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Ua({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Va=Ma({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});Ma({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Ga=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=qa;)n[r++]=255&t|Xa,t/=128;for(;t&Qa;)n[r++]=255&t|Xa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},Xa=128,Qa=-128,qa=Math.pow(2,31);var Wa=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ja)<=za);return e.bytes=o-n,a},za=128,Ja=127;var Ka=Math.pow(2,7),Ya=Math.pow(2,14),Ha=Math.pow(2,21),$a=Math.pow(2,28),Za=Math.pow(2,35),es=Math.pow(2,42),ts=Math.pow(2,49),ns=Math.pow(2,56),rs=Math.pow(2,63),as={encode:Ga,decode:Wa,encodingLength:function(e){return e{const[n,r]=ss(e.subarray(t));return t+=r,n};let r=n(),a=ms;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Va;return[Va.prefix,n.decode(`${Va.prefix}${e}`)]}case Va.prefix:{const n=t??Va;return[Va.prefix,n.decode(e)]}case ja.prefix:{const n=t??ja;return[ja.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=fs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ds(a).set(n,e),a}}const ms=112,hs=18;function gs(e,t,n){const r=is(e),a=r+is(t),s=new Uint8Array(a+n.byteLength);return os(e,s,0),os(t,s,r),s.set(n,a),s}const xs=Symbol.for("@ipld/js-cid/CID");class As{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?us(this.code,t):t.then((e=>us(this.code,e)))}throw Error("Unknown type, must be binary type")}}const Is=85,vs=function({name:e,code:t,encode:n}){return new As(e,t,n)}({name:"sha2-256",code:18,encode:e=>Sa(n.createHash("sha256").update(e).digest())});var ws=126144e3,ys=1e7,bs=Math.pow(2,64),Ts=48,ks=21,_s=48,Es=le("tp"),Ps=le("sc"),Ss=28100,Ds=12900,Os=421700,Ns=409700;function Bs(e){var t=e/Aa;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*Aa}function Cs(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Bs(e)+Aa;re?r=t?e*e*bs/(2*t):0:r=(e+(e-r))*n/2;return r}function Vs(e,t){return Gs.apply(this,arguments)}function Gs(){return(Gs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",Xs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Qs(){return(Qs=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,vs.digest(n);case 3:return r=e.sent,a=fs.createV1(Is,r),e.abrupt("return",a.toString(ja));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(){for(var t=arguments.length,n=new Array(t),r=0;r0?ks-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return co(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return co(this.powerCount)[1]}}]),e}(),to=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+js(this.bias,this.slope,t)}}]),e}(),no=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),ro=p((function e(t){u(this,e),this.slopeDelta=t})),ao=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%ks;return e>0?ks-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return co(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return co(this.totalPowerCount)[1]}}]),e}();function so(){return(so=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=io(r),e.prev=1,e.next=4,Fs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",oo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function oo(e){var t=Buffer.from(e);return new eo(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function io(t){return e.decodeAddress(t).publicKey}function uo(e){var t=_a(e),n=new Uint8Array(Es.length+t.length);return n.set(Es,0),n.set(t,Es.length),n}function co(e){var t=e-1;return[Math.floor(t/ks),t%ks]}function po(t,n){var r=e.decodeAddress(t).publicKey,a=_a(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function lo(e,t,n){return fo.apply(this,arguments)}function fo(){return(fo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=ho(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",mo(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function mo(t){return new ro(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function ho(e){return q(Ps,_a(e))}function go(e,t,n){return xo.apply(this,arguments)}function xo(){return(xo=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/ks)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function yo(e){for(var t,n=Ts,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),_a(o),_a(i),q.apply(void 0,w(u.map((function(e){return _a(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Go({sender:s,suggestedParams:c,extraAppArgs:[_a(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:So,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Qo=8,qo=le("p"),Wo=le("a"),zo=le("v"),Jo=28500,Ko=12500,Yo=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Ho(t,n){return q(Wo,e.decodeAddress(t).publicKey,_a(n))}function $o(e){return new Yo(Pa(e.slice(0,8)),Pa(e.slice(8,16)),Pa(e.slice(16,24)),Pa(e.slice(24,32)),Pa(e.slice(32,40)),Pa(e.slice(40,48)),Boolean(Pa(e.slice(48,49))))}function Zo(){return(Zo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=Ys(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",$o(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ei(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Qo)throw new Error("You cannot cast vote for more than ".concat(Qo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return _a(e)})))),g=q.apply(void 0,w(u.map((function(e){return _a(e)})))),x=function(e){return q(qo,le(e))}(s),A=Ho(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Ea(e,A)})),v=io(a),y=Math.floor(c/ks),b=po(a,y),k=po(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(zo,_a(e),_a(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Ea(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,_a(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Jo+_*Ko;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ti(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Ns),c||(b+=Ds);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Se[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),_a(r)],boxes:d.slice(0,8),note:p}),Go({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ni(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Ns),!n.freeAccountPowerSpaceCount){var w=po(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:da[a],name:w}),v+=Os}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Se[a],from:s,to:e.getApplicationAddress(da[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:da[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Go({sender:s,suggestedParams:i,index:da[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(da[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function ri(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%Aa)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=io(o),l=po(o,n.lastAccountPowerBoxIndex),d=uo(i.lastTotalPowerBoxIndex),f=uo(i.lastTotalPowerBoxIndex+1),m=ho(n.lockEndTime),h=ho(a),g=[{appIndex:da[r],name:p},{appIndex:da[r],name:l},{appIndex:da[r],name:d},{appIndex:da[r],name:f},{appIndex:da[r],name:m},{appIndex:da[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=po(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:da[r],name:x})}var A,I=Cs(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%Aa==0){var b=ho(y);g.push({appIndex:da[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ds),n.freeAccountPowerSpaceCount||(k+=Os),v>i.freeTotalPowerSpaceCount&&(k+=Ns);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),_a(a)],boxes:g.slice(0,8),note:c}),Go({sender:o,suggestedParams:u,index:da[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(da[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function ai(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=io(a),u=po(a,n.lastAccountPowerBoxIndex),c=po(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:da[r],name:i},{appIndex:da[r],name:u},{appIndex:da[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Se[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(da[r]),amount:Os});d.unshift(f)}return e.assignGroupID(d)}var si=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Us(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/la),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,go(this.algodClient,da[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=wo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/Aa),u=Math.floor(t/Aa)-i,c=Array.from({length:u},(function(e,t){return(i+t)*Aa})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Us(l,g),d=Math.max(d-x,0),e.next=28,lo(this.algodClient,da[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=js(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Vs(this.algodClient,da[this.network]);case 3:return t=e.sent,e.abrupt("return",new ao(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,lo(this.algodClient,da[this.network],r);case 17:return l=e.sent,e.abrupt("return",ti({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ni({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,lo(this.algodClient,da[this.network],n);case 19:return p=e.sent,e.abrupt("return",ri({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ni({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,lo(this.algodClient,da[this.network],r);case 24:return f=e.sent,l=ri({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?qs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=ai({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ne({client:this.algodClient,initiatorAddr:a,assetID:Se[this.network]});case 15:c=e.sent,u=qs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return so.apply(this,arguments)}(this.algodClient,da[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return Zo.apply(this,arguments)}(this.algodClient,fa[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Ws(this.algodClient,fa[this.network]);case 17:return f=e.sent,e.next=20,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=wo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ei({stakingVotingAppId:fa[this.network],vaultAppId:da[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Vs(this.algodClient,ma[this.network]);case 3:return n=t.sent,t.abrupt("return",new Do(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*pa,h=d.firstPeriodTimestamp+(r+a)*pa,g=[],x=m;x=Math.floor(Date.now()/la)||c.votingEndTimestamp<=Math.floor(Date.now()/la))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,Io({algodClient:this.algodClient,address:i,appId:da[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=wo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*wa)),m=Ho(i,f),e.next=33,Rs(this.algodClient,ha[this.network],m);case 33:return h=!e.sent,e.abrupt("return",To({proposalVotingAppId:ha[this.network],vaultAppId:da[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Vs(this.algodClient,ha[this.network]);case 2:return t=e.sent,e.abrupt("return",new Ks(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),oi=2500,ii=400,ui=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return di(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),ci=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),pi=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*bs/ws)),s=t-r;return s<0?0:Us(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=_a,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=Ke,exports.isSwapQuoteErrorCausedByAmount=at,exports.poolUtils=Wt,exports.prepareCommitTransactions=function(e){return ca.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return Xr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=ht,exports.tinymanContract_v2=At,exports.tinymanJSSDKConfig=ut; +"use strict";var e=require("algosdk"),t=require("base64-js"),n=require("crypto");function r(e,t){var n=Object.keys(e);if(Object.getOwnPropertySymbols){var r=Object.getOwnPropertySymbols(e);t&&(r=r.filter((function(t){return Object.getOwnPropertyDescriptor(e,t).enumerable}))),n.push.apply(n,r)}return n}function a(e){for(var t=1;t=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;--t){var n=this.tryEntries[t];if(n.finallyLoc===e)return this.complete(n.completion,n.afterLoc),k(n),d}},catch:function(e){for(var t=this.tryEntries.length-1;t>=0;--t){var n=this.tryEntries[t];if(n.tryLoc===e){var r=n.completion;if("throw"===r.type){var a=r.arg;k(n)}return a}}throw new Error("illegal catch attempt")},delegateYield:function(e,t,n){return this.delegate={iterator:E(e),resultName:t,nextLoc:n},"next"===this.method&&(this.arg=void 0),d}},e}function o(e,t,n,r,a,s,o){try{var i=e[s](o),u=i.value}catch(e){return void n(e)}i.done?t(u):Promise.resolve(u).then(r,a)}function i(e){return function(){var t=this,n=arguments;return new Promise((function(r,a){var s=e.apply(t,n);function i(e){o(s,r,a,i,u,"next",e)}function u(e){o(s,r,a,i,u,"throw",e)}i(void 0)}))}}function u(e,t){if(!(e instanceof t))throw new TypeError("Cannot call a class as a function")}function c(e,t){for(var n=0;ne.length)&&(t=e.length);for(var n=0,r=new Array(t);n=e.length?{done:!0}:{done:!1,value:e[r++]}},e:function(e){throw e},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(e)},n:function(){var e=n.next();return o=e.done,e},e:function(e){i=!0,s=e},f:function(){try{o||null==n.return||n.return()}finally{if(i)throw s}}}}function k(e){var t=function(e,t){if("object"!=typeof e||null===e)return e;var n=e[Symbol.toPrimitive];if(void 0!==n){var r=n.call(e,t||"default");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return("string"===t?String:Number)(e)}(e,"string");return"symbol"==typeof t?t:String(t)}var _=function(e){return e.SwapRouterStaleDataError="SwapRouterStaleDataError",e.SwapRouterNoRouteError="SwapRouterNoRouteError",e.SwapRouterLowSwapAmountError="SwapRouterLowSwapAmountError",e.SwapRouterInsufficientReservesError="SwapRouterInsufficientReservesError",e.SwapRouterPoolHasNoLiquidityError="SwapRouterPoolHasNoLiquidityError",e.NoAvailablePoolError="NoAvailablePoolError",e.OutputAmountExceedsAvailableLiquidityError="OutputAmountExceedsAvailableLiquidityError",e.UnknownError="UnknownError",e.LowSwapAmountError="LowSwapAmountError",e.AssetDoesNotBelongToPoolError="AssetDoesNotBelongToPoolError",e.InvalidSwapTypeError="InvalidSwapTypeError",e}({}),E=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;return u(this,n),(a=t.call(this,r)).type=e,a.message=r,Error.captureStackTrace&&Error.captureStackTrace(A(a),n),a}return p(n)}(),P=Uint8Array.from([1]),S=1e5,D=1e5,O=1e5,N=1e5,B=1e5,C=5e4,F=28500,L=1e3,R=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"}},U="- would result negative",j="logic eval error:",V="exceeds schema integer count",G=/transaction \w+:/,X=function(e){d(n,x(Error));var t=I(n);function n(e,r){var a;u(this,n);for(var s=arguments.length,o=new Array(s>2?s-2:0),i=2;i1||t<0)throw new Error("Invalid slippage value. Must be between 0 and 1, got ".concat(t));var r;try{var a="negative"===e?1-t:1+t;r=BigInt(Math.floor(Number(n)*a))}catch(e){throw new Error(e.message)}return r}function ne(e,t){var n=Number(e);return re({decimalPlaces:n},Math.pow(10,-n)*Number(t))}function re(e,t){var n=e.decimalPlaces,r=void 0===n?0:n;if(r>0){var a=v(se(t),2),s=a[0],o=a[1],i=v(se(Math.round(Number(ae(s,o+r)))),2),u=i[0],c=i[1];return Number(ae(u,c-r))}return Math.round(t)}function ae(e,t){return e+(t<0?"e".concat(t):"e+".concat(t))}function se(e){if(e.toString().includes("e")){var t=e.toString().split("e");return[parseFloat(t[0]),parseFloat(t[1])]}return[e,0]}function oe(e,t){return ie.apply(this,arguments)}function ie(){return(ie=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:e.prev=0,r=[],a=T(n),e.prev=3,a.s();case 5:if((o=a.n()).done){e.next=18;break}return i=o.value,e.next=9,t.sendRawTransaction(i).do();case 9:return u=e.sent,c=u.txId,e.next=13,Z(t,c);case 13:p=e.sent,l=p["confirmed-round"],r.push({confirmedRound:l,txnID:c});case 16:e.next=5;break;case 18:e.next=23;break;case 20:e.prev=20,e.t0=e.catch(3),a.e(e.t0);case 23:return e.prev=23,a.f(),e.finish(23);case 26:return e.abrupt("return",r);case 29:throw e.prev=29,e.t1=e.catch(0),new X(e.t1,"We encountered an error while processing this transaction. Try again later.");case 32:case"end":return e.stop()}}),e,null,[[0,29],[3,20,23,26]])})))).apply(this,arguments)}function ue(e){return e.reduce((function(e,t){return e+t.txn.fee}),0)}function ce(e){var t,n;return(n=null===(t=e[0].txn.group)||void 0===t?void 0:t.buffer)?Buffer.from(n).toString("base64"):""}function pe(e){for(var t=[];;){var n=Number(e&BigInt(127));if(!(e>>=BigInt(7))){t.push(n);break}t.push(128|n)}return t}function le(e){return(new TextEncoder).encode(e)}var de,fe=function(e){return e.FixedInput="fixed-input",e.FixedOutput="fixed-output",e}({}),me=.003;function he(e){return ge.apply(this,arguments)}function ge(){return(ge=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.initiatorAddr,a=n.client,(o=n.route).transactions&&o.transaction_fee){t.next=3;break}return t.abrupt("return",[]);case 3:return t.next=5,a.getTransactionParams().do();case 5:return i=t.sent,u=[],o.transactions.forEach((function(e){u.push(xe(e,i,r))})),u[0].fee=Number(o.transaction_fee),c=e.assignGroupID(u),t.abrupt("return",c.map((function(e){return{txn:e,signers:[r]}})));case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function xe(n,r,a){var s;switch(n.type){case e.TransactionType.pay:return(s=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,suggestedParams:r})).fee=0,s;case e.TransactionType.axfer:return(s=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:a,to:n.receiver,amount:n.amount,assetIndex:n.asset_id,suggestedParams:r})).fee=0,s;case e.TransactionType.appl:var o,i=null===(o=n.args)||void 0===o?void 0:o.map(t.toByteArray);return(s=e.makeApplicationNoOpTxnFromObject({from:a,appIndex:n.app_id,appArgs:i,accounts:n.accounts,foreignApps:n.apps,foreignAssets:n.assets,suggestedParams:r})).fee=0,s;default:throw new Error("Unknown transaction type: ".concat(n.type))}}function Ae(e){return Ie.apply(this,arguments)}function Ie(){return(Ie=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.amount,i=t.assetInID,u=t.assetOutID,c=t.swapType,p=t.network,l=t.slippage,d={asset_in_id:String(i),asset_out_id:String(u),swap_type:c,input_amount:c===fe.FixedInput?String(o):void 0,output_amount:c===fe.FixedOutput?String(o):void 0,slippage:l},e.next=4,fetch("".concat(M[p].v1,"/swap-router/quotes-v2/"),{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify(d)}).catch((function(){throw new Error("Network error")}));case 4:return f=e.sent,e.next=7,f.json();case 7:if(m=e.sent,f.ok){e.next=14;break}if(s=m,!Boolean(s)||void 0===s.fallback_message){e.next=13;break}throw new E(m.type,m.fallback_message);case 13:throw new E(_.UnknownError,"There was an error while getting a quote from Swap Router");case 14:if(null!==(n=m.transactions)&&void 0!==n&&n.length){e.next=16;break}throw new E(_.SwapRouterNoRouteError,"Swap router couldn't find a route for this swap.");case 16:if(!(Number(m.asset_in.id)!==i||Number(m.asset_out.id)!==u||m.swap_type===fe.FixedInput?BigInt(o)!==BigInt(null!==(r=m.input_amount)&&void 0!==r?r:0):BigInt(o)!==BigInt(null!==(a=m.output_amount)&&void 0!==a?a:0))){e.next=18;break}throw new E(_.UnknownError,"Swap router quote doesn't match the requested swap. Please try again.");case 18:return e.abrupt("return",m);case 19:case"end":return e.stop()}var s}),e)})))).apply(this,arguments)}var ve={testnet:184778019,mainnet:1083651166};function we(e){var t=Te(e),n=t.assetIn,r=t.assetOut;return ne(r.asset.decimals,Number(r.amount))/ne(n.asset.decimals,Number(n.amount))}function ye(e){var t;return{asset:e.asset_out,amount:BigInt(null!==(t=e.output_amount)&&void 0!==t?t:0)}}function be(e){var t;return{asset:e.asset_in,amount:BigInt(null!==(t=e.input_amount)&&void 0!==t?t:0)}}function Te(e){return{assetIn:be(e),assetOut:ye(e)}}l(de={},fe.FixedInput,7),l(de,fe.FixedOutput,8);var ke={V1_1:"v1_1",V2:"v2"},_e=0,Ee={id:"".concat(_e),name:"Algorand",unit_name:"ALGO",decimals:6,url:"https://algorand.org",is_liquidity_token:!1,total_amount:"6615503326932151",clawback_address:""},Pe={V1:"TM1POOL",V1_1:"TMPOOL11",V2:"TMPOOL2"},Se={testnet:258703304,mainnet:22e8},De={testnet:724519992,mainnet:2537013734},Oe={testnet:724676936,mainnet:2537023208};function Ne(e){return Be.apply(this,arguments)}function Be(){return(Be=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.assetID,o=n.initiatorAddr,t.prev=1,t.next=4,r.getTransactionParams().do();case 4:return i=t.sent,u=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:o,assetIndex:a,amount:0,suggestedParams:i}),t.abrupt("return",[{txn:u,signers:[o]}]);case 9:throw t.prev=9,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while opting into this asset. Try again later.");case 12:case"end":return t.stop()}}),t,null,[[1,9]])})))).apply(this,arguments)}function Ce(e,t){var n=Number(e.id),r=Number(t.id);return n>r?[a(a({},e),{},{id:n}),a(a({},t),{},{id:r})]:[a(a({},t),{},{id:r}),a(a({},e),{},{id:n})]}function Fe(e,t){var n=[e,t];return[Math.max.apply(Math,n),Math.min.apply(Math,n)]}function Le(e){return Number(e)===_e}function Re(e){return Number(e.id)}var Me,Ue,je=function(e){return e.Direct="direct",e.Router="router",e}({}),Ve=4*e.ALGORAND_MIN_TX_FEE,Ge=function(e){return e[e.INPUT_TXN=0]="INPUT_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({}),Xe=(l(Me={},fe.FixedInput,1),l(Me,fe.FixedOutput,2),Me),Qe=2,qe=le("swap"),We=(l(Ue={},fe.FixedInput,le("fixed-input")),l(Ue,fe.FixedOutput,le("fixed-output")),Ue);function ze(t){return(Xe[t]+Qe)*e.ALGORAND_MIN_TX_FEE}function Je(){var e=arguments.length>0&&void 0!==arguments[0]?arguments[0]:me;return Math.ceil(1/e)}function Ke(e){return e1&&void 0!==arguments[1]?arguments[1]:me)}function Ye(t){return(Xe[t]+1)*e.ALGORAND_MIN_TX_FEE}function He(e){var t=e.assetIn,n=e.assetOut;return ne(n.decimals,Number(n.amount))/ne(t.decimals,Number(t.amount))}function $e(e){var t=e.inputSupply,n=e.outputSupply,r=e.assetIn,a=e.assetOut,s=He({assetIn:r,assetOut:a}),o=ne(a.decimals,Number(n))/ne(r.decimals,Number(t));return re({decimalPlaces:5},Math.abs(s/o-1))}function Ze(e){if(e.type===je.Router){var t=be(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetInID,amount:e.data.quote.assetInAmount}}function et(e){if(e.type===je.Router){var t=ye(e.data);return{id:Re(t.asset),amount:t.amount}}return{id:e.data.quote.assetOutID,amount:e.data.quote.assetOutAmount}}function tt(e){return e.type===je.Direct?e.data.pool.contractVersion:ke.V2}function nt(e){return e.type===je.Direct?e.data.quote.rate:we(e.data)}function rt(e){for(var t=e[0],n=nt(t),r=1;rn&&(t=a,n=s)}return t}function at(e){return e instanceof E&&[_.SwapRouterInsufficientReservesError,_.SwapRouterLowSwapAmountError,_.OutputAmountExceedsAvailableLiquidityError,_.LowSwapAmountError].includes(e.type)}le("asset_opt_in");var st,ot={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"},it={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"},ut=new(function(){function e(){u(this,e),this.clientName="tinyman-js-sdk"}return p(e,[{key:"getClientName",value:function(){return this.clientName}},{key:"setClientName",value:function(e){this.clientName=e}},{key:"getAppCallTxnNoteWithClientName",value:function(e,t){var n=e===ke.V1_1?"v1":e,r=JSON.stringify(a({origin:this.clientName},t));return le("tinyman/".concat(n,":j").concat(r))}}]),e}()),ct=(l(st={},ke.V1_1,{testnet:62368684,mainnet:552635992}),l(st,ke.V2,{testnet:148607e3,mainnet:1002541853}),st);function pt(e,t){var n=ct[t][e];if(!n)throw new Error("No Validator App exists for ".concat(e," network with ").concat(t," contract version"));return n}function lt(){return(lt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationOptInTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dt(){return(dt=i(s().mark((function t(n){var r,a,o,i,u,c;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.contractVersion,i=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return u=t.sent,c=e.makeApplicationClearStateTxnFromObject({from:i,appIndex:pt(a,o),note:ut.getAppCallTxnNoteWithClientName(o),suggestedParams:u}),t.abrupt("return",[{txn:c,signers:[i]}]);case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var ft=p((function e(t){u(this,e),this.schema={numLocalInts:t.local_state_schema.num_uints,numLocalByteSlices:t.local_state_schema.num_byte_slices,numGlobalInts:t.global_state_schema.num_uints,numGlobalByteSlices:t.global_state_schema.num_byte_slices}})),mt=function(n){d(a,ft);var r=I(a);function a(e,t){var n;return u(this,a),(n=r.call(this,e)).poolLogicSigContractTemplate=t.logic.bytecode,n.templateVariables=t.logic.variables,n}return p(a,[{key:"generateLogicSigAccountForPool",value:function(n){if(n.asset1ID===n.asset2ID)throw new Error("Assets are the same");var r=pt(n.network,ke.V1_1),a=v(Fe(n.asset1ID,n.asset2ID),2),s=a[0],o=a[1],i=Array.from(t.toByteArray(this.poolLogicSigContractTemplate)),u={asset_id_1:s,asset_id_2:o,validator_app_id:r},c=0;this.templateVariables.sort((function(e,t){return e.index-t.index}));for(var p=0;p2&&void 0!==arguments[2]?arguments[2]:e.IntDecoding.DEFAULT;return new Promise(function(){var e=i(s().mark((function e(o,i){var u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,t.accountInformation(n).setIntDecoding(r).do();case 3:u=e.sent,o(a(a({},u),{},{minimum_required_balance:yt(u)})),e.next=10;break;case 7:e.prev=7,e.t0=e.catch(0),i(new Error(e.t0.message||"Failed to fetch account information"));case 10:case"end":return e.stop()}}),e,null,[[0,7]])})));return function(t,n){return e.apply(this,arguments)}}())}function wt(e,t){var n=e["apps-local-state"].find((function(e){return e.id===t}));return n?Q({stateArray:n["key-value"],shouldDecodeKeys:!0}):null}function yt(e){var t=e["apps-total-schema"];return S+D*(e.assets||[]).length+N*(e["created-apps"]||[]).length+O*(e["apps-local-state"]||[]).length+C*Number(t&&t["num-byte-slice"]||0)+F*Number(t&&t["num-uint"]||0)+B*(e["apps-total-extra-pages"]||0)}var bt=le("e");function Tt(e){return kt.apply(this,arguments)}function kt(){return(kt=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,k,_;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.pool,i=r.accountAddr,n.next=3,a.accountInformation(i).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:u=n.sent,c=u["apps-local-state"]||[],p=0n,l=0n,d=0n,f=o.account.address(),m=T(c),n.prev=10,m.s();case 12:if((h=m.n()).done){n.next=31;break}if((g=h.value).id==o.validatorAppID){n.next=16;break}return n.abrupt("continue",29);case 16:if(x=g["key-value"]){n.next=19;break}return n.abrupt("break",31);case 19:A=Q({stateArray:x}),I=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset1ID))),v=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.asset2ID))),w=t.fromByteArray(q(e.decodeAddress(f).publicKey,bt,e.encodeUint64(o.poolTokenID))),y=A[I],b=A[v],k=A[w],"bigint"==typeof y&&(p=y),"bigint"==typeof b&&(l=b),"bigint"==typeof k&&(d=k);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(!((_={excessAsset1:p,excessAsset2:l,excessPoolTokens:d}).excessAsset1<0n||_.excessAsset2<0n||_.excessPoolTokens<0n)){n.next=42;break}throw new Error("Invalid account excess: ".concat(_));case 42:return n.abrupt("return",_);case 43:case"end":return n.stop()}}),n,null,[[10,33,36,39]])})))).apply(this,arguments)}function _t(){return(_t=i(s().mark((function n(r){var a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(n){for(;;)switch(n.prev=n.next){case 0:return a=r.client,o=r.accountAddr,i=r.validatorAppID,n.next=3,a.accountInformation(o).setIntDecoding(e.IntDecoding.BIGINT).do();case 3:if(u=n.sent,c=u["apps-local-state"]||[],p=c.find((function(e){return e.id==i})),l=[],p&&p["key-value"])for(d=Q({stateArray:p["key-value"]}),f=0,m=Object.entries(d);fNt)){n.next=48;break}throw new Error("Issued liquidity value is out of the expected range ([0n, ".concat(Nt,"]): ").concat(N.issuedLiquidity));case 48:return n.abrupt("return",N);case 49:case"end":return n.stop()}}),n,null,[[8,31,34,37]])})))).apply(this,arguments)}function Lt(){return Lt=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.address,a=t.network,!(o=d.length>1&&void 0!==d[1]?d[1]:{})[r]){e.next=4;break}return e.abrupt("return",o[r]);case 4:return e.next=6,vt(n,r);case 6:return i=e.sent,u=wt(i,pt(a,ke.V1_1)),c=null,u&&(l=i["created-assets"][0],p=l.index,c={asset1ID:u[St[ke.V1_1].asset1],asset2ID:u[St[ke.V1_1].asset2],poolTokenID:p},o[r]=c),e.abrupt("return",c);case 11:case"end":return e.stop()}}),e)}))),Lt.apply(this,arguments)}var Rt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Lt.apply(this,arguments)},getPoolInfo:Bt,getPoolReserves:function(e,t){return Ft.apply(this,arguments)}});function Mt(e){return Ut.apply(this,arguments)}function Ut(){return(Ut=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.asset1ID,o=t.asset2ID,i=It(ke.V2),u=i.generateLogicSigAccountForPool(t),c=pt(r,ke.V2),p=u.address(),l=Fe(a,o),e.next=8,vt(n,p);case 8:return d=e.sent,f=wt(d,c),m={account:u,validatorAppID:c,asset1ID:l[0],asset2ID:l[1],status:f?Pt.READY:Pt.NOT_CREATED,contractVersion:ke.V2},f&&(m.asset1ProtocolFees=BigInt(f[St.v2.asset1ProtocolFees]),m.asset2ProtocolFees=BigInt(f[St.v2.asset2ProtocolFees]),m.asset1Reserves=BigInt(f[St.v2.asset1Reserves]),m.asset2Reserves=BigInt(f[St.v2.asset2Reserves]),m.issuedPoolTokens=BigInt(f[St.v2.issuedPoolTokens]),m.cumulativePriceUpdateTimeStamp=Number(f[St.v2.cumulativePriceUpdateTimeStamp]),m.protocolFeeRatio=Number(f[St.v2.protocolFeeRatio]),m.totalFeeShare=BigInt(f[St.v2.totalFeeShare]),m.poolTokenID=Number(f[St.v2.poolTokenID]),m.asset1ID=Number(f[St.v2.asset1]),m.asset2ID=Number(f[St.v2.asset2])),e.abrupt("return",m);case 13:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function jt(){return(jt=i(s().mark((function e(t,n){var r,a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,vt(t,n.account.address());case 2:return r=e.sent,a=wt(r,n.validatorAppID),o={asset1:0n,asset2:0n,issuedLiquidity:0n,round:r.round},a&&(o.asset1=BigInt(a[St.v2.asset1Reserves]),o.asset2=BigInt(a[St.v2.asset2Reserves]),o.issuedLiquidity=BigInt(a[St.v2.issuedPoolTokens])),e.abrupt("return",o);case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vt(){return(Vt=i(s().mark((function e(t){var n,r,a,o,i,u;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.address,a=t.network,e.next=3,vt(n,r);case 3:return o=e.sent,i=wt(o,pt(a,ke.V2)),u=null,i&&(u={asset1ID:i[St[ke.V2].asset1],asset2ID:i[St[ke.V2].asset2],poolTokenID:i[St[ke.V2].poolTokenID]}),e.abrupt("return",u);case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gt=Object.freeze({__proto__:null,getPoolAssets:function(e){return Vt.apply(this,arguments)},getPoolInfo:Mt,getPoolReserves:function(e,t){return jt.apply(this,arguments)}});function Xt(e){return Boolean(e&&!(e.asset1+e.asset2))}var Qt,qt=Object.freeze({__proto__:null,getPoolPairRatio:function(e){var t=Xt(e),n=null;return e&&!t&&e.asset1&&e.asset2&&(n=Number(e.asset1)/Number(e.asset2)),n},getPoolShare:function(e,t){var n=Number(t)/Number(e);return Number.isFinite(n)||(n=0),n},getPoolsForPair:function(e){return Promise.all([Bt(e),Mt(e)])},isPoolEmpty:Xt,isPoolNotCreated:function(e){return(null==e?void 0:e.status)===Pt.NOT_CREATED},isPoolReady:function(e){return(null==e?void 0:e.status)===Pt.READY}}),Wt=a((l(Qt={},ke.V1_1,a(a({},Rt),qt)),l(Qt,ke.V2,a(a({},Gt),qt)),Qt),qt),zt=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e[e.POOL_TOKEN_CREATE=2]="POOL_TOKEN_CREATE",e[e.ASSET1_OPT_IN=3]="ASSET1_OPT_IN",e[e.ASSET2_OPT_IN=4]="ASSET2_OPT_IN",e}({}),Jt={ASA_ALGO:96e4,ASA_ASA:859e3};function Kt(){return(Kt=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T,k,_,E,P,S;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=Ce(o,i),l=v(p,2),d=l[0],f=d.id,m=d.unit_name,h=l[1],g=h.id,x=h.unit_name,A=Le(g),I=pt(a,ke.V1_1),w=ht.generateLogicSigAccountForPool({network:a,asset1ID:f,asset2ID:g}),y=w.address(),b=e.makeApplicationOptInTxnFromObject({from:y,appIndex:I,note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),appArgs:[le("bootstrap"),e.encodeUint64(f),e.encodeUint64(g)],foreignAssets:A?[f]:[g],suggestedParams:c}),T=e.makeAssetCreateTxnWithSuggestedParamsFromObject({from:y,total:0xffffffffffffffffn,decimals:6,defaultFrozen:!1,unitName:Pe.V1_1,assetName:"TinymanPool1.1 ".concat(m,"-").concat(x),assetURL:"https://tinyman.org",suggestedParams:c}),k=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:f,amount:0,suggestedParams:c}),_=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:y,amount:Yt(A),suggestedParams:c}),(E=[])[zt.FUNDING_TXN]=_,E[zt.VALIDATOR_APP_CALL]=b,E[zt.POOL_TOKEN_CREATE]=T,E[zt.ASSET1_OPT_IN]=k,A||(E[zt.ASSET2_OPT_IN]=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:y,to:y,assetIndex:g,amount:0,suggestedParams:c})),P=e.assignGroupID(E),S=[{txn:P[zt.FUNDING_TXN],signers:[u]},{txn:P[zt.VALIDATOR_APP_CALL],signers:[y]},{txn:P[zt.POOL_TOKEN_CREATE],signers:[y]},{txn:P[zt.ASSET1_OPT_IN],signers:[y]}],P[zt.ASSET2_OPT_IN]&&S.push({txn:P[zt.ASSET2_OPT_IN],signers:[y]}),t.abrupt("return",S);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Yt(e){return e?Jt.ASA_ALGO:Jt.ASA_ASA}function Ht(){return(Ht=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=ht.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===zt.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function $t(e){return Zt.apply(this,arguments)}function Zt(){return(Zt=i(s().mark((function e(t){var n,r,a,o,i;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.signedTxns,a=t.txnIDs,e.prev=1,e.next=4,n.sendRawTransaction(r).do();case 4:return e.next=6,Z(n,a[zt.POOL_TOKEN_CREATE]);case 6:if(o=e.sent,"number"==typeof(i=o["asset-index"])){e.next=10;break}throw new Error("Generated ID is not valid: got ".concat(i));case 10:return e.abrupt("return",{poolTokenID:i});case 13:throw e.prev=13,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while bootstraping the pool. Try again later.");case 16:case"end":return e.stop()}}),e,null,[[1,13]])})))).apply(this,arguments)}function en(){return(en=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.signedTxns,c=t.txnIDs,e.next=3,$t({client:n,signedTxns:u,txnIDs:c});case 3:return e.abrupt("return",Wt.v1_1.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var tn={generateTxns:function(e){return Kt.apply(this,arguments)},signTxns:function(e){return Ht.apply(this,arguments)},execute:function(e){return en.apply(this,arguments)},getBootstrapFundingTxnAmount:Yt};var nn=function(e){return e[e.FUNDING_TXN=0]="FUNDING_TXN",e[e.VALIDATOR_APP_CALL=1]="VALIDATOR_APP_CALL",e}({}),rn={ASA_ALGO:5,ASA_ASA:6};function an(e,t){return sn.apply(this,arguments)}function sn(){return(sn=i(s().mark((function t(n,r){var a,o,i;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(!(o=null===(a=r.find((function(e){return"appl"===e.txn.type})))||void 0===a?void 0:a.txn.txID())){t.next=7;break}return t.next=4,e.waitForConfirmation(n,o,R);case 4:t.t0=t.sent,t.next=8;break;case 7:t.t0=void 0;case 8:return i=t.t0,t.abrupt("return",i);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function on(e,t){return un.apply(this,arguments)}function un(){return(un=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,an(t,n);case 2:return r=e.sent,e.abrupt("return",null==r?void 0:r["inner-txns"]);case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function cn(e,t){return pn.apply(this,arguments)}function pn(){return(pn=i(s().mark((function t(n,r){var a,o;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.next=2,on(n,r);case 2:return a=t.sent,s=r[0].txn,o=e.encodeAddress(s.from.publicKey),t.abrupt("return",null==a?void 0:a.reduce((function(t,n){var r=t,a=n.txn.txn;return a.type===e.TransactionType.axfer&&e.encodeAddress(a.arcv)===o?r.push({id:a.xaid,amount:a.aamt}):a.type===e.TransactionType.pay&&e.encodeAddress(a.rcv)===o&&r.push({id:_e,amount:a.amt}),r}),[]));case 5:case"end":return t.stop()}var s}),t)})))).apply(this,arguments)}function ln(){return(ln=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.asset_1,i=n.asset_2,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=pt(a,ke.V2),l=e.getApplicationAddress(p),d=Ce(o,i),f=v(d,2),m=f[0].id,h=f[1].id,t.next=9,Wt.v2.getPoolInfo({client:r,network:a,asset1ID:m,asset2ID:h});case 9:if(t.sent.status!==Pt.READY){t.next=12;break}throw new Error("Pool for ".concat(o.unit_name,"-").concat(i.unit_name," already exists"));case 12:return g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=g.address(),A=Le(h),(I=e.makeApplicationOptInTxnFromObject({from:x,appIndex:p,appArgs:[le("bootstrap")],note:ut.getAppCallTxnNoteWithClientName(ke.V2),foreignAssets:[m,h],rekeyTo:l,suggestedParams:c})).fee=fn(A),w=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:x,amount:dn(A),suggestedParams:c}),(y=[])[nn.FUNDING_TXN]=w,y[nn.VALIDATOR_APP_CALL]=I,b=e.assignGroupID(y),(T=[])[nn.FUNDING_TXN]={txn:b[nn.FUNDING_TXN],signers:[u]},T[nn.VALIDATOR_APP_CALL]={txn:b[nn.VALIDATOR_APP_CALL],signers:[x]},t.abrupt("return",T);case 26:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function dn(e){return function(e,t){var n=It(e).schema,r=n.numLocalInts,a=n.numLocalByteSlices,s=S+D+D+O+F*r+C*a;return t||(s+=D),s}(ke.V2,e)+fn(e)+D}function fn(t){return((t?rn.ASA_ALGO:rn.ASA_ASA)+1)*e.ALGORAND_MIN_TX_FEE}function mn(){return(mn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.network,o=n.initiatorSigner,i=n.asset1ID,u=n.asset2ID,t.next=3,o([r]);case 3:return c=t.sent,p=v(c,1),l=p[0],d=Fe(i,u),f=v(d,2),m=f[0],h=f[1],g=At.generateLogicSigAccountForPool({network:a,asset1ID:m,asset2ID:h}),x=[],A=r.map((function(t,n){if(n===nn.FUNDING_TXN)return x.push(t.txn.txID().toString()),l;var r=e.signLogicSigTransactionObject(t.txn,g),a=r.txID,s=r.blob;return x.push(a),s})),t.abrupt("return",{signedTxns:A,txnIDs:x});case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function hn(){return(hn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.network,a=t.pool,o=a.asset1ID,i=a.asset2ID,u=t.txGroup,c=t.signedTxns,e.prev=1,e.next=4,n.sendRawTransaction(c).do();case 4:return e.next=6,an(n,u);case 6:if(e.t1=p=e.sent,e.t0=null===e.t1,e.t0){e.next=10;break}e.t0=void 0===p;case 10:if(!e.t0){e.next=14;break}e.t2=void 0,e.next=15;break;case 14:e.t2=null===(l=p["local-state-delta"][0].delta)||void 0===l||null===(d=l.find((function(e){return e.key===btoa(St.v2.poolTokenID)})))||void 0===d?void 0:d.value.uint;case 15:if("number"==typeof(f=e.t2)){e.next=18;break}throw new Error("Generated ID is not valid: got ".concat(f));case 18:return e.abrupt("return",Wt.v2.getPoolInfo({client:n,network:r,asset1ID:o,asset2ID:i}));case 21:throw e.prev=21,e.t3=e.catch(1),new X(e.t3,"We encountered something unexpected while bootstraping the pool. Try again later.");case 24:case"end":return e.stop()}}),e,null,[[1,21]])})))).apply(this,arguments)}var gn,xn={generateTxns:function(e){return ln.apply(this,arguments)},signTxns:function(e){return mn.apply(this,arguments)},execute:function(e){return hn.apply(this,arguments)},getBootstrapFundingTxnAmount:dn,getTotalCost:function(t){return e.ALGORAND_MIN_TX_FEE+dn(t)}};var An,In=(l(gn={},ke.V1_1,tn),l(gn,ke.V2,xn),l(gn,"generateTxns",(function(e){return e.contractVersion===ke.V1_1?tn.generateTxns(e):xn.generateTxns(e)})),l(gn,"signTxns",(function(e){return e.contractVersion===ke.V1_1?tn.signTxns(e):xn.signTxns(e)})),l(gn,"execute",(function(e){return e.contractVersion===ke.V1_1?tn.execute(e):xn.execute(e)})),l(gn,"calculateBootstrapFundingTxnAmount",(function(e){var t=e.contractVersion,n=e.isAlgoPool;return t===ke.V1_1?tn.getBootstrapFundingTxnAmount(n):xn.getBootstrapFundingTxnAmount(n)})),gn),vn=le("add_liquidity"),wn=(l(An={},ke.V1_1,[le("mint")]),l(An,ke.V2,{INITIAL_LIQUIDITY:[le("add_initial_liquidity")],SINGLE_ASSET_MODE:[vn,le("single")],FLEXIBLE_MODE:[vn,le("flexible")]}),An),yn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_IN_TXN=2]="ASSET1_IN_TXN",e[e.ASSET2_IN_TXN=3]="ASSET2_IN_TXN",e[e.LIQUDITY_OUT_TXN=4]="LIQUDITY_OUT_TXN",e}({}),bn=5*e.ALGORAND_MIN_TX_FEE;function Tn(){return(Tn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.slippage,l=n.initiatorAddr,d=te("negative",p,c.amount),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:pt(a,ke.V1_1),appArgs:wn.v1_1,accounts:[l],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:u.id==_e?[i.id,c.id]:[i.id,u.id,c.id],suggestedParams:f}),h=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:f}),g=u.id===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:u.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:o,assetIndex:u.id,amount:u.amount,suggestedParams:f}),x=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:l,assetIndex:c.id,amount:d,suggestedParams:f}),A=e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:o,amount:m.fee+x.fee,note:P,suggestedParams:f}),I=e.assignGroupID([A,m,h,g,x]),t.abrupt("return",[{txn:I[0],signers:[l]},{txn:I[1],signers:[o]},{txn:I[2],signers:[l]},{txn:I[3],signers:[l]},{txn:I[4],signers:[o]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function kn(){return(kn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,i=r.account,t.next=4,o([a]);case 4:return u=t.sent,c=v(u,3),p=c[0],l=c[1],d=c[2],f=a.map((function(t,n){return n===yn.FEE_TXN?p:n===yn.ASSET1_IN_TXN?l:n===yn.ASSET2_IN_TXN?d:e.signLogicSigTransactionObject(t.txn,i).blob})),t.abrupt("return",f);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function _n(){return(_n=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=BigInt(a[yn.LIQUDITY_OUT_TXN].txn.amount),e.next=5,Tt({client:n,pool:r,accountAddr:i});case 5:return c=e.sent,e.next=8,oe(n,[o]);case 8:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=ue(a),g=ce(a),e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessPoolTokens-c.excessPoolTokens)<0n&&(A=0n),e.abrupt("return",{round:f,fees:h,poolTokenID:r.poolTokenID,poolTokenOut:u+A,excessAmount:{excessAmountForAddingLiquidity:A,totalExcessAmount:x.excessPoolTokens},txnID:m,groupID:g});case 23:throw e.prev=23,e.t0=e.catch(1),"SlippageTolerance"===(I=new X(e.t0,"We encountered something unexpected while adding liquidity. Try again later.")).type&&I.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),I;case 28:case"end":return e.stop()}}),e,null,[[1,23]])})))).apply(this,arguments)}var En,Pn,Sn,Dn=Object.freeze({__proto__:null,execute:function(e){return _n.apply(this,arguments)},generateTxns:function(e){return Tn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.asset1In,a=e.asset2In;if(0n===n.issuedLiquidity){var s=BigInt(Math.floor(Math.sqrt(Number(r)*Number(a))));if(s<=BigInt(L))throw new Error("Initial liquidity amount is too small. The amount must be greater than ".concat(L,", this quote is for ").concat(s,"."));return{round:n.round,asset1ID:t.asset1ID,asset1In:BigInt(r),asset2ID:t.asset2ID,asset2In:BigInt(a),poolTokenID:t.poolTokenID,poolTokenOut:s-BigInt(L),share:1}}var o=BigInt(r)*n.issuedLiquidity/n.asset1,i=BigInt(a)*n.issuedLiquidity/n.asset2,u=ox){var A=g;r=Rn(A,s),t={id:o.id,amount:A+r,decimals:o.decimals,reserves:a.asset1},n={id:i.id,amount:BigInt(Math.abs(Math.min(Number(x),0))),decimals:i.decimals,reserves:a.asset2},f-=r*d/(c*BigInt(2))}else{var I=x;r=Rn(I,s),t={id:i.id,amount:I+r,decimals:i.decimals,reserves:a.asset2},n={id:o.id,amount:BigInt(Math.abs(Math.min(Number(g),0))),decimals:o.decimals,reserves:a.asset1},f-=r*d/(p*BigInt(2))}return{poolTokenOutAmount:f,internalSwapQuote:{assetIn:t,assetOut:n,swapFees:r,priceImpact:$e({inputSupply:t.reserves,outputSupply:n.reserves,assetIn:t,assetOut:n})}}}function Ln(e,t){if(!e.amount||!t.amount)throw new Error("Both assets are required for the initial add liquidity");return BigInt(Math.floor(Math.abs(Math.sqrt(Number(e.amount)*Number(t.amount))-Dt)))}function Rn(e,t){return e*BigInt(t)/(BigInt(1e4)-BigInt(t))}function Mn(t){return(Bn[t]+1)*e.ALGORAND_MIN_TX_FEE}function Un(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])}function jn(e){return Vn.apply(this,arguments)}function Vn(){return(Vn=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,e.prev=1,e.next=4,oe(n,[o]);case 4:return i=e.sent,u=v(i,1),c=u[0],p=c.confirmedRound,l=c.txnID,e.prev=9,e.next=12,cn(n,a);case 12:if(e.t1=f=e.sent,e.t0=null===e.t1,e.t0){e.next=16;break}e.t0=void 0===f;case 16:if(!e.t0){e.next=20;break}e.t2=void 0,e.next=21;break;case 20:e.t2=f.find((function(e){return e.id===r.poolTokenID}));case 21:d=e.t2,e.next=26;break;case 24:e.prev=24,e.t3=e.catch(9);case 26:return e.abrupt("return",{round:p,assetOut:d,fees:ue(a),poolTokenID:r.poolTokenID,txnID:l,groupID:ce(a)});case 29:throw e.prev=29,e.t4=e.catch(1),"SlippageTolerance"===(m=new X(e.t4,"We encountered something unexpected while adding liquidity. Try again later.")).type&&m.setMessage("Adding liquidity failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),m;case 34:case"end":return e.stop()}}),e,null,[[1,29],[9,24]])})))).apply(this,arguments)}function Gn(){return(Gn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.asset1In,u=n.asset2In,c=n.poolTokenOut,p=n.initiatorAddr,l=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return d=t.sent,f=Ce(i,u),m=v(f,2),h=m[0],g=m[1],x=Le(g.id),A=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:h.id,amount:h.amount,suggestedParams:d}),I=x?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:o,amount:g.amount,suggestedParams:d}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:o,assetIndex:g.id,amount:g.amount,suggestedParams:d}),(y=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.FLEXIBLE_MODE),[e.encodeUint64(l)]),accounts:[o],foreignAssets:[c.id],suggestedParams:d})).fee=Mn(On.FLEXIBLE),b=e.assignGroupID([A,I,y]),t.abrupt("return",[{txn:b[0],signers:[p]},{txn:b[1],signers:[p]},{txn:b[2],signers:[p]}]);case 12:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var Xn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Gn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.slippage,r=void 0===n?.05:n,a=e.asset1,s=e.asset2;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity at the moment. To be able to do Flexible Swap, you should first add initial liquidity.");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},i=Fn({reserves:o,totalFeeShare:t.totalFeeShare,asset1:{id:t.asset1ID,amount:a.amount,decimals:a.decimals},asset2:{id:t.asset2ID,amount:s.amount,decimals:s.decimals}}),u=i.poolTokenOutAmount,c=i.internalSwapQuote,p=u-BigInt(Math.ceil(Number(u)*r));return{asset1In:{id:t.asset1ID,amount:BigInt(a.amount)},asset2In:{id:t.asset2ID,amount:BigInt(s.amount)},poolTokenOut:{id:t.poolTokenID,amount:u},share:Wt.getPoolShare(o.issuedLiquidity+u,u),slippage:r,internalSwapQuote:c,minPoolTokenAssetAmountWithSlippage:p}},signTxns:Un});function Qn(){return(Qn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.assetIn,u=n.poolTokenId,c=n.initiatorAddr,p=n.minPoolTokenAssetAmount,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=Le(i.id),f=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:c,to:o,amount:i.amount,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:c,to:o,assetIndex:i.id,amount:i.amount,suggestedParams:l}),(m=e.makeApplicationNoOpTxnFromObject({from:c,appIndex:pt(a,ke.V2),note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[].concat(w(wn.v2.SINGLE_ASSET_MODE),[e.encodeUint64(p)]),accounts:[o],foreignAssets:[u],suggestedParams:l})).fee=Mn(On.SINGLE),h=e.assignGroupID([f,m]),t.abrupt("return",[{txn:h[0],signers:[c]},{txn:h[1],signers:[c]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var qn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Qn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.assetIn,r=e.slippage,a=void 0===r?.05:r,s=e.decimals;if(0n===t.issuedPoolTokens)throw new Error("Pool has no liquidity");if(t.status!==Pt.READY)throw new Error("Pool is not ready");if(!t.poolTokenID)throw new Error("Pool token ID is not available");var o=n.id===t.asset1ID,i=n.id===t.asset2ID;if(!o&&!i)throw new Error("Provided input asset id didn't match any asset of the pool");var u={id:t.asset1ID,amount:o?n.amount:0,decimals:s.asset1},c={id:t.asset2ID,amount:i?n.amount:0,decimals:s.asset2},p={asset1:t.asset1Reserves||0n,asset2:t.asset2Reserves||0n,issuedLiquidity:t.issuedPoolTokens||0n},l=Fn({reserves:p,totalFeeShare:t.totalFeeShare||0,asset1:u,asset2:c}),d=l.poolTokenOutAmount,f=l.internalSwapQuote,m=d-BigInt(Math.ceil(Number(d)*a));return{assetIn:{id:o?t.asset1ID:t.asset2ID,amount:BigInt(n.amount)},poolTokenOut:{id:t.poolTokenID,amount:d},share:Wt.getPoolShare(p.issuedLiquidity+d,d),slippage:a,internalSwapQuote:f,minPoolTokenAssetAmountWithSlippage:m}},signTxns:Un});function Wn(){return(Wn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.network,i=n.poolAddress,u=n.asset1In,c=n.asset2In,p=n.poolTokenId,l=n.initiatorAddr,d=Le(a.asset2ID),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset1ID,amount:u.amount,suggestedParams:f}),h=d?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:i,amount:c.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:i,assetIndex:a.asset2ID,amount:c.amount,suggestedParams:f}),(g=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:pt(o,ke.V2),appArgs:wn.v2.INITIAL_LIQUIDITY,note:ut.getAppCallTxnNoteWithClientName(ke.V2),accounts:[i],foreignAssets:[p],suggestedParams:f})).fee=Mn(On.INITIAL),t.abrupt("return",e.assignGroupID([m,h,g]).map((function(e){return{txn:e,signers:[l]}})));case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var zn,Jn=Object.freeze({__proto__:null,execute:jn,generateTxns:function(e){return Wn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.asset1,r=e.asset2,a=e.slippage,s=void 0===a?.05:a;if(0n!==t.issuedPoolTokens)throw new Error("Pool already has liquidity");var o=BigInt(Math.floor(Math.sqrt(Number(n.amount)*Number(r.amount))));if(o<=BigInt(Dt))throw new Error("Initial liquidity amount is too small. Liquidity amount must be greater than ".concat(Dt,", this quote is for ").concat(o,"."));return{asset1In:{id:t.asset1ID,amount:BigInt(n.amount)},asset2In:{id:t.asset2ID,amount:BigInt(r.amount)},poolTokenOut:{id:t.poolTokenID,amount:Ln(n,r)},slippage:s}},signTxns:Un}),Kn=Object.freeze({__proto__:null,flexible:Xn,initial:Jn,withSingleAsset:qn}),Yn=(l(zn={},ke.V1_1,Dn),l(zn,ke.V2,Kn),zn);var Hn=function(e){return e[e.FEE_TXN=0]="FEE_TXN",e[e.VALIDATOR_APP_CALL_TXN=1]="VALIDATOR_APP_CALL_TXN",e[e.ASSET1_OUT_TXN=2]="ASSET1_OUT_TXN",e[e.ASSET2_OUT_TXN=3]="ASSET2_OUT_TXN",e[e.POOL_TOKEN_IN_TXN=4]="POOL_TOKEN_IN_TXN",e}({}),$n=Object.values(Hn).length;function Zn(){return(Zn=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.asset1Out,u=n.asset2Out,c=n.slippage,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return l=t.sent,d=a.account.address(),f=Le(a.asset2ID),m=e.makeApplicationNoOpTxnFromObject({from:d,appIndex:a.validatorAppID,appArgs:[le("burn")],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),accounts:[p],foreignAssets:f?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:l}),h=te("negative",c,i),g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset1ID,amount:h,suggestedParams:l}),x=te("negative",c,u),A=f?e.makePaymentTxnWithSuggestedParamsFromObject({from:d,to:p,amount:x,suggestedParams:l}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:d,to:p,assetIndex:a.asset2ID,amount:x,suggestedParams:l}),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:d,assetIndex:a.poolTokenID,amount:o,suggestedParams:l}),v=m.fee+g.fee+A.fee,w=e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:d,amount:v,note:P,suggestedParams:l}),(y=[])[Hn.FEE_TXN]=w,y[Hn.VALIDATOR_APP_CALL_TXN]=m,y[Hn.ASSET1_OUT_TXN]=g,y[Hn.ASSET2_OUT_TXN]=A,y[Hn.POOL_TOKEN_IN_TXN]=I,b=e.assignGroupID(y),t.abrupt("return",[{txn:b[Hn.FEE_TXN],signers:[p]},{txn:b[Hn.VALIDATOR_APP_CALL_TXN],signers:[d]},{txn:b[Hn.ASSET1_OUT_TXN],signers:[d]},{txn:b[Hn.ASSET2_OUT_TXN],signers:[d]},{txn:b[Hn.POOL_TOKEN_IN_TXN],signers:[p]}]);case 22:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function er(){return(er=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=r.account,d=a.map((function(t,n){return n===Hn.FEE_TXN?c:n===Hn.POOL_TOKEN_IN_TXN?p:e.signLogicSigTransactionObject(t.txn,l).blob})),t.abrupt("return",d);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function tr(){return(tr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,w;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.signedTxns,i=t.initiatorAddr,e.prev=1,u=a[Hn.ASSET1_OUT_TXN].txn.amount,c=a[Hn.ASSET2_OUT_TXN].txn.amount,p=a[Hn.POOL_TOKEN_IN_TXN].txn.amount,e.next=7,Tt({client:n,pool:r,accountAddr:i});case 7:return l=e.sent,e.next=10,oe(n,[o]);case 10:return d=e.sent,f=v(d,1),m=f[0],h=m.confirmedRound,g=m.txnID,e.next=17,Tt({client:n,pool:r,accountAddr:i});case 17:return x=e.sent,(A=x.excessAsset1-l.excessAsset1)<0n&&(A=0n),(I=x.excessAsset2-l.excessAsset2)<0n&&(I=0n),e.abrupt("return",{round:h,fees:ue(a),asset1ID:r.asset1ID,asset1Out:BigInt(u)+A,asset2ID:r.asset2ID,asset2Out:BigInt(c)+I,poolTokenID:r.poolTokenID,poolTokenIn:BigInt(p),excessAmounts:[{assetID:r.asset1ID,excessAmountForBurning:A,totalExcessAmount:x.excessAsset1},{assetID:r.asset2ID,excessAmountForBurning:I,totalExcessAmount:x.excessAsset2}],txnID:g,groupID:ce(a)});case 25:throw e.prev=25,e.t0=e.catch(1),"SlippageTolerance"===(w=new X(e.t0,"We encountered something unexpected while burning liquidity. Try again later.")).type&&w.setMessage("The burn failed due to too much slippage in the price. Please adjust the slippage tolerance and try again."),w;case 30:case"end":return e.stop()}}),e,null,[[1,25]])})))).apply(this,arguments)}var nr={generateTxns:function(e){return Zn.apply(this,arguments)},getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=n.issuedLiquidity&&a*n.asset1/n.issuedLiquidity,o=n.issuedLiquidity&&a*n.asset2/n.issuedLiquidity;return{round:n.round,poolTokenID:t.poolTokenID,poolTokenIn:a,asset1ID:t.asset1ID,asset1Out:s,asset2ID:t.asset2ID,asset2Out:o}},signTxns:function(e){return er.apply(this,arguments)},execute:function(e){return tr.apply(this,arguments)}};function rr(){return(rr=i(s().mark((function t(n){var r,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(n.quote.type!==je.Router){t.next=2;break}return t.abrupt("return",he(a(a({},n),{},{route:n.quote.data})));case 2:if(r=n.client,o=n.initiatorAddr,i=n.slippage,u=n.swapType,c=n.quote,p=c.data,l=p.pool,d=p.quote,f=d.assetInID,m=d.assetOutID,h=l.account.address(),(g=[l.asset1ID,l.asset2ID]).includes(f)&&g.includes(m)&&f!==m){t.next=9;break}throw new X({pool:l,quote:c},"Input asset (#".concat(f,") and output asset (#").concat(m,") provided to generate transactions do not belong to the pool ").concat(h,"."));case 9:return t.next=11,r.getTransactionParams().do();case 11:return x=t.sent,A=Le(f),I=u===fe.FixedInput?d.assetInAmount:te("positive",i,d.assetInAmount),v=u===fe.FixedOutput?d.assetOutAmount:te("negative",i,d.assetOutAmount),w=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,suggestedParams:x}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:h,amount:I,assetIndex:f,suggestedParams:x}),(y=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:l.validatorAppID,appArgs:[qe,We[u],e.encodeUint64(v)],note:ut.getAppCallTxnNoteWithClientName(ke.V2,n.appCallNoteExtraData),accounts:[h],foreignAssets:[l.asset1ID,l.asset2ID],suggestedParams:x})).fee=Ye(u),(b=[])[Ge.INPUT_TXN]=w,b[Ge.APP_CALL_TXN]=y,T=e.assignGroupID(b),t.abrupt("return",[{txn:T[Ge.INPUT_TXN],signers:[o]},{txn:T[Ge.APP_CALL_TXN],signers:[o]}]);case 23:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ar(){return(ar=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return o=t.client,i=t.quote,u=t.txGroup,c=t.signedTxns,e.next=3,oe(o,[c]);case 3:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,h=et(i).id,g=Ze(i),e.prev=10,e.next=13,cn(o,u);case 13:x=e.sent,e.next=18;break;case 16:e.prev=16,e.t0=e.catch(10);case 18:return A=null===(n=x)||void 0===n||null===(r=n.find((function(e){return e.id===g.id})))||void 0===r?void 0:r.amount,I=null===(a=x)||void 0===a?void 0:a.find((function(e){return e.id===h})),e.abrupt("return",{round:f,assetIn:{amount:BigInt(g.amount)-BigInt(A||0),id:g.id},assetOut:I,quote:i,txnID:m});case 21:case"end":return e.stop()}}),e,null,[[10,16]])})))).apply(this,arguments)}function sr(){return(sr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.type,r=t.amount,a=t.assetIn,o=t.assetOut,i=t.network,u=t.slippage,c=t.isSwapRouterEnabled,p=t.pool,n!==fe.FixedInput){e.next=7;break}return e.next=4,cr({assetIn:a,assetOut:o,amount:r,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 4:l=e.sent,e.next=10;break;case 7:return e.next=9,lr({amount:r,assetIn:a,assetOut:o,isSwapRouterEnabled:c,network:i,pool:p,slippage:u});case 9:l=e.sent;case 10:return e.abrupt("return",l);case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function or(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=e[0].reason;if(2===e.length){var n=e[1].reason;if(at(t)&&!at(n))throw t;throw n}throw t}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function ir(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(i===a.asset1ID)s=a.asset1Reserves,o=a.asset2Reserves;else{if(i!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(i,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset2Reserves,o=a.asset1Reserves}var f=fr({inputSupply:s,outputSupply:o,swapInputAmount:l,totalFeeShare:d,decimals:{assetIn:u,assetOut:p}}),m=f.swapOutputAmount,h=f.totalFeeAmount,g=f.priceImpact;if(m>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(t)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{assetInID:i,assetInAmount:l,assetOutID:c,assetOutAmount:m,swapFee:Number(h),rate:ne(p,Number(m))/ne(u,Number(l)),priceImpact:g}}function ur(e){var t=e.amount,n=e.assetIn,r=e.assetOut,a=e.pool;if(!a||!Wt.isPoolReady(a))throw new E(_.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(t),d=a.totalFeeShare;if(c===a.asset1ID)s=a.asset2Reserves,o=a.asset1Reserves;else{if(c!==a.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(c,") doesn't belong to the pool ").concat(a.account.address(),"."));s=a.asset1Reserves,o=a.asset2Reserves}var f=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapOutputAmount,a=e.totalFeeShare,s=e.decimals,o=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.outputAmount,a=t*n,s=BigInt(a/(n-r))-t;return s+=BigInt(1),s}({inputSupply:t,outputSupply:n,outputAmount:r}),i=function(e){var t=e.swapAmount,n=e.totalFeeShare,r=Math.floor(Number(t*BigInt(1e4)/(BigInt(1e4)-BigInt(n)))),a=BigInt(r)-t;return a}({swapAmount:o,totalFeeShare:a}),u=o+i,c=$e({inputSupply:t,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,h=f.totalFeeAmount,g=f.priceImpact;if(l>o)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");if(Ke(Number(m)))throw new E(_.LowSwapAmountError,"Swap amount is too low.");return{type:je.Direct,data:{pool:a,quote:{assetInID:i,assetInAmount:m,assetOutID:c,assetOutAmount:l,swapFee:Number(h),rate:ne(p,Number(l))/ne(u,Number(m)),priceImpact:g}}}}function cr(e){return pr.apply(this,arguments)}function pr(){return(pr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[],c?p.push(new Promise((function(e,t){try{var s=ir({amount:n,assetIn:r,assetOut:a,pool:c});e({type:je.Direct,data:{pool:c,quote:s}})}catch(e){t(e)}}))):p.push(Promise.reject(new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair"))),o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedInput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=6,or(p);case 6:return l=e.sent,e.abrupt("return",rt(l));case 8:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function lr(e){return dr.apply(this,arguments)}function dr(){return(dr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.amount,r=t.assetIn,a=t.assetOut,o=t.isSwapRouterEnabled,i=t.network,u=t.slippage,c=t.pool,p=[new Promise((function(e,t){try{e(ur({amount:n,assetIn:r,assetOut:a,pool:c}))}catch(e){t(e)}}))],o&&p.push(Ae({amount:n,assetInID:r.id,assetOutID:a.id,swapType:fe.FixedOutput,network:i,slippage:u}).then((function(e){return{type:je.Router,data:e}}))),e.next=5,or(p);case 5:return l=e.sent,e.abrupt("return",rt(l));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function fr(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapInputAmount,a=e.totalFeeShare,s=e.decimals,o=BigInt(function(e){var t=e.inputAmount,n=e.totalFeeShare;return Math.floor(Number(t*BigInt(n))/1e4)}({inputAmount:r,totalFeeShare:a})),i=function(e){var t=e.inputSupply,n=e.outputSupply,r=e.swapAmount,a=t*n,s=n-BigInt(a/(t+BigInt(r)));return s-=BigInt(1)}({inputSupply:t,outputSupply:n,swapAmount:r-o});return{swapOutputAmount:i,totalFeeAmount:o,priceImpact:$e({inputSupply:t,outputSupply:n,assetIn:{amount:r,decimals:s.assetIn},assetOut:{amount:i,decimals:s.assetOut}})}}var mr={getQuote:function(e){return sr.apply(this,arguments)},getFixedInputSwapQuote:cr,getFixedInputDirectSwapQuote:ir,getFixedOutputDirectSwapQuote:ur,getFixedOutputSwapQuote:lr,generateTxns:function(e){return rr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return ar.apply(this,arguments)},calculateFixedInputSwap:fr},hr=2,gr=le("remove_liquidity"),xr=function(e){return e[e.ASSET_TRANSFER_TXN=0]="ASSET_TRANSFER_TXN",e[e.APP_CALL_TXN=1]="APP_CALL_TXN",e}({});function Ar(e,t){var n,r,a=BigInt(e),s=t.issuedLiquidity;return s>a+BigInt(Dt)?(n=a*t.asset1/s,r=a*t.asset2/s):(n=t.asset1,r=t.asset2),{asset1OutputAmount:n,asset2OutputAmount:r}}function Ir(){return(Ir=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.minAsset1Amount,c=n.minAsset2Amount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.account.address(),f=a.poolTokenID){t.next=8;break}throw new Error("Pool token asset ID is missing");case 8:return m=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:f,amount:o,suggestedParams:l}),(h=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(te("negative",p,u)),e.encodeUint64(te("negative",p,c))],accounts:[d],foreignAssets:[a.asset1ID,a.asset2ID],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(g=[])[xr.ASSET_TRANSFER_TXN]=m,g[xr.APP_CALL_TXN]=h,x=e.assignGroupID(g),t.abrupt("return",[{txn:x[xr.ASSET_TRANSFER_TXN],signers:[i]},{txn:x[xr.APP_CALL_TXN],signers:[i]}]);case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function vr(){return(vr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.initiatorAddr,i=n.poolTokenIn,u=n.outputAssetId,c=n.minOutputAssetAmount,p=n.slippage,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=a.asset1ID,f=a.asset2ID,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:if(g=0,x=0,A=te("negative",p,c),u!==d){t.next=17;break}g=A,x=0,t.next=23;break;case 17:if(u!==f){t.next=22;break}g=0,x=A,t.next=23;break;case 22:throw new Error("Invalid output asset id. It doesn't match with pool assets");case 23:return I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:o,to:m,assetIndex:h,amount:i,suggestedParams:l}),(v=e.makeApplicationNoOpTxnFromObject({from:o,appIndex:a.validatorAppID,note:ut.getAppCallTxnNoteWithClientName(ke.V2),appArgs:[gr,e.encodeUint64(g),e.encodeUint64(x)],accounts:[m],foreignAssets:[u],suggestedParams:l})).fee=(hr+1)*e.ALGORAND_MIN_TX_FEE,(w=[])[xr.ASSET_TRANSFER_TXN]=I,w[xr.APP_CALL_TXN]=v,y=e.assignGroupID(w),t.abrupt("return",[{txn:y[xr.ASSET_TRANSFER_TXN],signers:[o]},{txn:y[xr.APP_CALL_TXN],signers:[o]}]);case 31:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function wr(){return(wr=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.txGroup,a=t.signedTxns,e.next=3,oe(n,[a]);case 3:return o=e.sent,i=v(o,1),u=i[0].txnID,e.prev=6,e.next=9,cn(n,r);case 9:c=e.sent,e.next=14;break;case 12:e.prev=12,e.t0=e.catch(6);case 14:return e.abrupt("return",{outputAssets:c,txnID:u});case 15:case"end":return e.stop()}}),e,null,[[6,12]])})))).apply(this,arguments)}var yr,br={getQuote:function(e){var t=e.pool,n=e.reserves,r=e.poolTokenIn,a=BigInt(r),s=Ar(a,n),o=s.asset1OutputAmount,i=s.asset2OutputAmount;return{round:n.round,asset1Out:{assetId:t.asset1ID,amount:o},asset2Out:{assetId:t.asset2ID,amount:i},poolTokenIn:{assetId:t.poolTokenID,amount:a}}},getSingleAssetRemoveLiquidityQuote:function(e){var t,n=e.pool,r=e.reserves,a=e.poolTokenIn,s=e.assetOutID,o=e.decimals,i=BigInt(a),u=Ar(i,r),c=u.asset1OutputAmount,p=u.asset2OutputAmount,l=n.totalFeeShare;if(s===n.asset1ID){var d=mr.calculateFixedInputSwap({inputSupply:r.asset2-p,outputSupply:r.asset1-c,swapInputAmount:p,totalFeeShare:l,decimals:o}),f=d.swapOutputAmount,m=d.totalFeeAmount,h=d.priceImpact;t={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:h}}}else{if(s!==n.asset2ID)throw new Error("assetOutID must be one of the pool assets");var g=mr.calculateFixedInputSwap({inputSupply:r.asset1-c,outputSupply:r.asset2-p,swapInputAmount:c,totalFeeShare:l,decimals:o}),x=g.swapOutputAmount,A=g.totalFeeAmount,I=g.priceImpact;t={round:r.round,assetOut:{assetId:s,amount:p+x},poolTokenIn:{assetId:n.poolTokenID,amount:i},internalSwapQuote:{amountIn:{assetId:n.asset2ID,amount:p},amountOut:{assetId:n.asset1ID,amount:x},swapFees:{assetId:n.asset2ID,amount:A},priceImpact:I}}}return t},generateTxns:function(e){return Ir.apply(this,arguments)},generateSingleAssetOutTxns:function(e){return vr.apply(this,arguments)},signTxns:function(e){var t=e.txGroup;return(0,e.initiatorSigner)([t])},execute:function(e){return wr.apply(this,arguments)}},Tr=(l(yr={},ke.V1_1,nr),l(yr,ke.V2,br),yr),kr=3n,_r=1000n,Er=function(e){return e[e.FEE_TXN_INDEX=0]="FEE_TXN_INDEX",e[e.VALIDATOR_APP_CALL_TXN_INDEX=1]="VALIDATOR_APP_CALL_TXN_INDEX",e[e.ASSET_IN_TXN_INDEX=2]="ASSET_IN_TXN_INDEX",e[e.ASSET_OUT_TXN_INDEX=3]="ASSET_OUT_TXN_INDEX",e}(Er||{});function Pr(){return(Pr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.pool,a=n.txGroup,o=n.initiatorSigner,t.next=3,o([a]);case 3:return i=t.sent,u=v(i,2),c=u[0],p=u[1],l=a.map((function(t,n){return n===Er.FEE_TXN_INDEX?c:n===Er.ASSET_IN_TXN_INDEX?p:e.signLogicSigTransactionObject(t.txn,r.account.lsig).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Sr(){return(Sr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:if(r=n.client,a=n.quoteAndPool,o=n.swapType,i=n.slippage,u=n.initiatorAddr,c=a.pool,p=a.quote,l=p.assetInID,d=p.assetOutID,f=c.account.address(),(m=[c.asset1ID,c.asset2ID]).includes(l)&&m.includes(d)&&l!==d){t.next=7;break}throw new X({pool:c,quote:p},"Input asset (#".concat(l,") and output asset (#").concat(d,") provided to generate transactions do not belong to the pool ").concat(f,"."));case 7:return t.next=9,r.getTransactionParams().do();case 9:return h=t.sent,g=[le("swap"),o===fe.FixedInput?le("fi"):le("fo")],x=e.makeApplicationNoOpTxnFromObject({from:f,appIndex:c.validatorAppID,appArgs:g,accounts:[u],note:ut.getAppCallTxnNoteWithClientName(ke.V1_1),foreignAssets:c.asset2ID==_e?[c.asset1ID,c.poolTokenID]:[c.asset1ID,c.asset2ID,c.poolTokenID],suggestedParams:h}),A=o===fe.FixedOutput?te("positive",i,p.assetInAmount):p.assetInAmount,I=l===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:A,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:u,to:f,assetIndex:l,amount:A,suggestedParams:h}),v=o===fe.FixedInput?te("negative",i,p.assetOutAmount):p.assetOutAmount,w=d===_e?e.makePaymentTxnWithSuggestedParamsFromObject({from:f,to:u,amount:v,suggestedParams:h}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:f,to:u,assetIndex:d,amount:v,suggestedParams:h}),y=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:f,amount:x.fee+w.fee,note:P,suggestedParams:h}),b=e.assignGroupID([y,x,I,w]),t.abrupt("return",[{txn:b[0],signers:[u]},{txn:b[1],signers:[f]},{txn:b[2],signers:[u]},{txn:b[3],signers:[f]}]);case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Dr(e){var t=e.pool,n=e.reserves,r=e.assetIn,s=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var o,i,u,c=BigInt(r.amount);if(r.id===t.asset1ID)o=t.asset2ID,i=n.asset1,u=n.asset2;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Input asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));o=t.asset1ID,i=n.asset2,u=n.asset1}var p=c*kr/_r,l=u-i*u/(i+(c-p));if(l>u)throw new E(_.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:r.id,assetInAmount:c,assetOutID:o,assetOutAmount:l,swapFee:Number(p),rate:He(d),priceImpact:$e(a({inputSupply:i,outputSupply:u},d))};return{type:je.Direct,data:{pool:t,quote:f}}}function Or(e){return Nr.apply(this,arguments)}function Nr(){return(Nr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,i.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount),assetOutID:i.id,assetOutAmount:BigInt(i.amount)+A,excessAmount:{assetID:i.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Br(e){var t=e.pool,n=e.reserves,r=e.assetOut,a=e.decimals;if(t.status!==Pt.READY)throw new E(_.NoAvailablePoolError,"There is not an available pool for this asset pair");var s,o,i,u=BigInt(r.amount);if(r.id===t.asset1ID)s=t.asset2ID,o=n.asset2,i=n.asset1;else{if(r.id!==t.asset2ID)throw new E(_.AssetDoesNotBelongToPoolError,"Output asset (#".concat(r.id,") doesn't belong to the pool ").concat(t.account.address(),"."));s=t.asset1ID,o=n.asset1,i=n.asset2}if(u>i)throw new E(_.OutputAmountExceedsAvailableLiquidityError,"Output amount exceeds available liquidity.");var c=o*i/(i-u)-o,p=c*_r/(_r-kr),l=p-c,d=ne(a.assetOut,Number(u))/ne(a.assetIn,Number(p)),f=ne(a.assetOut,Number(i))/ne(a.assetIn,Number(o)),m=re({decimalPlaces:5},Math.abs(d/f-1)),h={round:n.round,assetInID:s,assetInAmount:p,assetOutID:r.id,assetOutAmount:u,swapFee:Number(l),rate:d,priceImpact:m};return{type:je.Direct,data:{pool:t,quote:h}}}function Cr(e){return Fr.apply(this,arguments)}function Fr(){return(Fr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.signedTxns,o=t.assetIn,i=t.assetOut,u=t.initiatorAddr,e.next=3,Tt({client:n,pool:r,accountAddr:u});case 3:return c=e.sent,e.next=6,oe(n,[a]);case 6:return p=e.sent,l=v(p,1),d=l[0],f=d.confirmedRound,m=d.txnID,e.next=13,Tt({client:n,pool:r,accountAddr:u});case 13:return h=e.sent,o.id===r.asset1ID?(g=c.excessAsset1,x=h.excessAsset1):(g=c.excessAsset2,x=h.excessAsset2),(A=x-g)<0n&&(A=0n),e.abrupt("return",{round:f,assetInID:o.id,assetInAmount:BigInt(o.amount)-A,assetOutID:i.id,assetOutAmount:BigInt(i.amount),excessAmount:{assetID:o.id,excessAmountForSwap:A,totalExcessAmount:x},txnID:m});case 18:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Lr(){return(Lr=i(s().mark((function e(t){var n,r,o,i,u,c,p,l,d,f;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(n=t.client,r=t.pool,o=t.swapType,i=t.txGroup,u=t.signedTxns,c=t.initiatorAddr,r.status===Pt.READY){e.next=3;break}throw new X({pool:r,swapType:o,txGroup:i},"There is not an available pool for this asset pair");case 3:if(e.prev=3,p={id:i[Er.ASSET_IN_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_IN_TXN_INDEX].txn.amount},l={id:i[Er.ASSET_OUT_TXN_INDEX].txn.assetIndex||_e,amount:i[Er.ASSET_OUT_TXN_INDEX].txn.amount},o!==fe.FixedInput){e.next=12;break}return e.next=9,Or({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 9:d=e.sent,e.next=15;break;case 12:return e.next=14,Cr({client:n,pool:r,signedTxns:u,assetIn:p,assetOut:l,initiatorAddr:c});case 14:d=e.sent;case 15:return e.abrupt("return",a(a({},d),{},{groupID:ce(i),fees:ue(i)}));case 18:throw e.prev=18,e.t0=e.catch(3),"SlippageTolerance"===(f=new X(e.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 e.stop()}}),e,null,[[3,18]])})))).apply(this,arguments)}var Rr,Mr={getQuote:function(e,t,n,r,a){return e===fe.FixedInput?Dr({pool:t,reserves:n,assetIn:r,decimals:a}):Br({pool:t,reserves:n,assetOut:r,decimals:a})},getFixedInputSwapQuote:Dr,getFixedOutputSwapQuote:Br,generateTxns:function(e){return Sr.apply(this,arguments)},signTxns:function(e){return Pr.apply(this,arguments)},execute:function(e){return Lr.apply(this,arguments)},executeFixedOutputSwap:Cr};function Ur(e){return Promise.allSettled(e).then((function(e){if(e.every((function(e){return"rejected"===e.status}))){var t=v(e.map((function(e){return e.reason})),2),n=t[0],r=t[1];if(at(n)&&!at(r))throw n;throw r}return e.filter((function(e){return"fulfilled"===e.status&&e.value})).map((function(e){return e.value}))}))}function jr(){return(jr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.isSwapRouterEnabled,u=t.pools,c=[],(p=u.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedInputSwapQuote({pool:p.info,assetIn:{amount:r,id:Number(a.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=u.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedInputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:i,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Vr(){return(Vr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.amount,a=t.assetIn,o=t.assetOut,i=t.pools,u=t.isSwapRouterEnabled,c=[],(p=i.find((function(e){return e.info.contractVersion===ke.V1_1})))?c.push(new Promise((function(e,t){try{e(Mr.getFixedOutputSwapQuote({pool:p.info,assetOut:{amount:r,id:Number(o.id)},decimals:{assetIn:a.decimals,assetOut:o.decimals},reserves:p.reserves}))}catch(e){t(e)}}))):c.push(Promise.reject(new E(_.NoAvailablePoolError,"Trying to swap from non-existent pool"))),l=i.find((function(e){return e.info.contractVersion===ke.V2})),c.push(mr.getFixedOutputSwapQuote({amount:r,assetIn:{id:Re(a),decimals:a.decimals},assetOut:{id:Re(o),decimals:o.decimals},pool:null!==(n=null==l?void 0:l.info)&&void 0!==n?n:null,isSwapRouterEnabled:u,network:t.network,slippage:t.slippage})),e.next=8,Ur(c);case 8:return d=e.sent,e.abrupt("return",rt(d));case 10:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var Gr=(l(Rr={},ke.V1_1,Mr),l(Rr,ke.V2,mr),l(Rr,"getQuote",(function(e){var t=e.type;if(t===fe.FixedInput)return function(e){return jr.apply(this,arguments)}(e);if(t===fe.FixedOutput)return function(e){return Vr.apply(this,arguments)}(e);throw new E(_.InvalidSwapTypeError,"Invalid swap type")})),l(Rr,"generateTxns",(function(e){return e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1?Mr.generateTxns(a(a({},e),{},{quoteAndPool:e.quote.data})):mr.generateTxns(e)})),l(Rr,"signTxns",(function(e){if(e.quote.type===je.Direct&&tt(e.quote)===ke.V1_1){var t=e.quote.data.pool;return Mr.signTxns(a(a({},e),{},{pool:t}))}return mr.signTxns(e)})),l(Rr,"execute",(function(e){return e.contractVersion===ke.V1_1?Mr.execute(e):mr.execute(e)})),Rr);function Xr(){return(Xr=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.client,r=t.pool,a=t.txGroup,o=t.initiatorSigner,e.prev=1,e.next=4,Qr({txGroup:a,pool:r,initiatorSigner:o});case 4:return i=e.sent,e.next=7,oe(n,[i]);case 7:return u=e.sent,c=v(u,1),p=c[0],l=p.txnID,d=p.confirmedRound,e.abrupt("return",{fees:ue(a),confirmedRound:d,txnID:l,groupID:ce(a)});case 15:throw e.prev=15,e.t0=e.catch(1),new X(e.t0,"We encountered something unexpected while redeeming. Try again later.");case 18:case"end":return e.stop()}}),e,null,[[1,15]])})))).apply(this,arguments)}function Qr(e){return qr.apply(this,arguments)}function qr(){return(qr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.txGroup,a=n.pool,o=n.initiatorSigner,t.next=3,o([r]);case 3:return i=t.sent,u=v(i,1),c=u[0],p=a.account.lsig,l=r.map((function(t,n){return 0===n?c:e.signLogicSigTransactionObject(t.txn,p).blob})),t.abrupt("return",l);case 9:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Wr(){return Wr=i(s().mark((function t(n){var r,a,o,u,c,p;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.data,o=n.initiatorSigner,t.prev=1,u=a.map((function(e){var t=e.txGroup,n=e.pool;return{txns:t,txnFees:ue(t),groupID:ce(t),lsig:n.account.lsig}})),t.next=5,o(u.map((function(e){return e.txns})));case 5:return c=t.sent,p=Promise.all(u.map((function(t,n){return new Promise(function(){var a=i(s().mark((function a(o,i){var u,p,l,d,f,m;return s().wrap((function(a){for(;;)switch(a.prev=a.next){case 0:return a.prev=0,u=t.txns.map((function(r,a){return 0===a?c[n]:e.signLogicSigTransactionObject(r.txn,t.lsig).blob})),a.next=4,oe(r,[u]);case 4:p=a.sent,l=v(p,1),d=l[0],f=d.txnID,m=d.confirmedRound,o({fees:t.txnFees,groupID:t.groupID,txnID:f,confirmedRound:m}),a.next=15;break;case 12:a.prev=12,a.t0=a.catch(0),i(a.t0);case 15:case"end":return a.stop()}}),a,null,[[0,12]])})));return function(e,t){return a.apply(this,arguments)}}())}))),t.abrupt("return",p);case 10:throw t.prev=10,t.t0=t.catch(1),new X(t.t0,"We encountered something unexpected while redeeming. Try again later.");case 13:case"end":return t.stop()}}),t,null,[[1,10]])}))),Wr.apply(this,arguments)}function zr(){return(zr=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.assetID,i=n.assetOut,u=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:return c=t.sent,p=a.account.address(),l=e.makeApplicationNoOpTxnFromObject({from:p,appIndex:a.validatorAppID,appArgs:[le("redeem")],note:ut.getAppCallTxnNoteWithClientName(a.contractVersion),accounts:[u],foreignAssets:0==a.asset2ID?[a.asset1ID,a.poolTokenID]:[a.asset1ID,a.asset2ID,a.poolTokenID],suggestedParams:c}),d=0===o?e.makePaymentTxnWithSuggestedParamsFromObject({from:p,to:u,amount:BigInt(i),suggestedParams:c}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:p,to:u,assetIndex:o,amount:BigInt(i),suggestedParams:c}),f=e.makePaymentTxnWithSuggestedParamsFromObject({from:u,to:p,amount:l.fee+d.fee,note:P,suggestedParams:c}),m=e.assignGroupID([f,l,d]),t.abrupt("return",[{txn:m[0],signers:[u]},{txn:m[1],signers:[p]},{txn:m[2],signers:[p]}]);case 10:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function Jr(e){var t,n={},r=T(e);try{for(r.s();!(t=r.n()).done;){var a=t.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(e){r.e(e)}finally{r.f()}return n}function Kr(e,t,n){return e*t/n}var Yr=BigInt(31536e3),Hr=BigInt(1e14),$r=BigInt(1e16),Zr={testnet:548587153,mainnet:1385499515},ea=14;function ta(e){return na.apply(this,arguments)}function na(){return(na=i(s().mark((function t(n){var r,a,o,i,u;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.assetIDs,i=e.getApplicationAddress(Zr[a]),t.next=4,vt(r,i);case 4:return u=t.sent.assets.map((function(e){return e["asset-id"]})),t.abrupt("return",o.filter((function(e){return e!==_e&&!u.includes(e)})));case 6:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function ra(){return(ra=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,y,b,T,k,_,E,P;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.network,o=n.poolAddress,i=n.poolTokenId,u=n.lendingManagerId,c=n.asset1In,p=n.asset2In,l=n.initiatorAddr,d=e.getApplicationAddress(Zr[a]),t.next=4,r.getTransactionParams().do();case 4:return f=t.sent,m=[c,p].sort((function(e,t){return t.fAssetId-e.fAssetId})),h=v(m,2),g=h[0],x=h[1],A=Le(x.id),I=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:g.id,amount:g.amount,suggestedParams:f}),y=A?e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:x.amount,suggestedParams:f}):e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:l,to:d,assetIndex:x.id,amount:x.amount,suggestedParams:f}),(b=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("add_liquidity"),e.decodeAddress(o).publicKey,e.encodeUint64(g.lendingAppId),e.encodeUint64(x.lendingAppId)],foreignAssets:[g.id,x.id,g.fAssetId,x.fAssetId],foreignApps:[g.lendingAppId,x.lendingAppId,u],accounts:[o],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),T=pt(a,ke.V2),k=e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("noop")],foreignAssets:[i],foreignApps:[T],accounts:[o],suggestedParams:f}),_=[I,y,b,k],t.next=16,ta({client:r,network:a,assetIDs:[g.id,x.id,g.fAssetId,x.fAssetId,i]});case 16:return(E=t.sent).length&&((P=[e.makePaymentTxnWithSuggestedParamsFromObject({from:l,to:d,amount:D*E.length,suggestedParams:f}),e.makeApplicationNoOpTxnFromObject({from:l,appIndex:Zr[a],appArgs:[le("asset_optin")].concat(w(E.map((function(t){return e.encodeUint64(t)})))),foreignAssets:w(E),suggestedParams:f})])[1].fee=(E.length+1)*e.ALGORAND_MIN_TX_FEE,_.unshift.apply(_,P)),t.abrupt("return",e.assignGroupID(_).map((function(e){return{txn:e,signers:[l]}})));case 19:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function aa(){return(aa=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.pool,o=n.poolTokenIn,i=n.initiatorAddr,u=n.asset1Out,c=n.asset2Out,p=n.lendingManagerId,l=n.network,d=e.getApplicationAddress(Zr[l]),t.next=4,r.getTransactionParams().do();case 4:if(f=t.sent,m=a.account.address(),h=a.poolTokenID){t.next=9;break}throw new Error("Pool token asset ID is missing");case 9:return g=e.makeAssetTransferTxnWithSuggestedParamsFromObject({from:i,to:d,assetIndex:h,amount:o,suggestedParams:f}),(x=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("remove_liquidity"),e.decodeAddress(m).publicKey,e.encodeUint64(u.lendingAppId),e.encodeUint64(c.lendingAppId)],accounts:[m],foreignAssets:[u.id,c.id,u.fAssetId,c.fAssetId],foreignApps:[u.lendingAppId,c.lendingAppId,p],suggestedParams:f})).fee=e.ALGORAND_MIN_TX_FEE*(ea+1),A=pt(l,ke.V2),I=e.makeApplicationNoOpTxnFromObject({from:i,appIndex:Zr[l],appArgs:[le("noop")],accounts:[m],foreignAssets:[h,u.fAssetId,c.fAssetId],foreignApps:[A],suggestedParams:f}),v=e.assignGroupID([g,x,I]),t.abrupt("return",v.map((function(e){return{txn:e,signers:[i]}})));case 16:case"end":return t.stop()}}),t)})))).apply(this,arguments)}function sa(e,t,n){var r=BigInt(Math.floor((new Date).getTime()/1e3)-function(e){return null!=e?e:Math.floor((new Date).getTime()/1e3)}(n));return Kr(e,$r+t*r/Yr,$r)}function oa(){return(oa=i(s().mark((function e(t,n){var r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,a=r.params["global-state"],o=Jr(a),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)),e.abrupt("return",{appId:n,managerAppId:i,depositInterestRate:c,depositInterestIndex:p,lastUpdate:l});case 11:case"end":return e.stop()}}),e)})))).apply(this,arguments)}var ia={AddLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return ra.apply(this,arguments)},getAddLiquidityTotalFee:function(t){return e.ALGORAND_MIN_TX_FEE*(4+ea)+(t?(t+1)*e.ALGORAND_MIN_TX_FEE+t*D:0)}}),RemoveLiquidity:Object.freeze({__proto__:null,generateTxns:function(e){return aa.apply(this,arguments)},getRemoveLiquidityTotalFee:function(){return e.ALGORAND_MIN_TX_FEE*(3+ea)}}),calculateWithdrawReturn:function(e){var t=e.withdrawAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return Kr(BigInt(t),n,Hr)},calculateDepositReturn:function(e){var t=e.depositAmount,n=sa(e.depositInterestIndex,e.depositInterestRate,e.lastUpdate);return BigInt(t)*Hr/n},getFolksWrapperAppOptInRequiredAssetIDs:ta};function ua(t){var n=t.suggestedParams,r=t.stakingAppID,a=t.initiatorAddr,s=t.liquidityAssetID,o=t.program,i=t.amount,u=e.encodeUint64(i),c=e.encodeUint64(o.id);return e.makeApplicationNoOpTxnFromObject({appIndex:r,from:a,suggestedParams:n,foreignAssets:[s],accounts:[o.accountAddress],appArgs:[le("commit"),u],note:q(le("tinymanStaking/v1:b"),c,e.encodeUint64(s),u)})}function ca(){return(ca=i(s().mark((function t(n){var r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return r=n.client,a=n.stakingAppID,o=n.program,i=n.requiredAssetID,u=n.liquidityAssetID,c=n.amount,p=n.initiatorAddr,t.next=3,r.getTransactionParams().do();case 3:if(l=t.sent,d=ua({suggestedParams:l,stakingAppID:a,program:o,liquidityAssetID:u,initiatorAddr:p,amount:c}),f=[d],"number"!=typeof i){t.next=10;break}return m=e.makeApplicationNoOpTxnFromObject({appIndex:a,from:p,suggestedParams:l,foreignAssets:[i],accounts:[o.accountAddress],appArgs:[le("log_balance")]}),f=e.assignGroupID([d,m]),t.abrupt("return",[{txn:f[0],signers:[p]},{txn:f[1],signers:[p]}]);case 10:return t.abrupt("return",[{txn:f[0],signers:[p]}]);case 11:case"end":return t.stop()}}),t)})))).apply(this,arguments)}var pa=604800,la=1e3,da={testnet:480164661,mainnet:2200606875},fa={testnet:360907790,mainnet:2200609638},ma={testnet:336189106,mainnet:2200608153},ha={testnet:383416252,mainnet:2200608887},ga=2500,xa=400,Aa=604800,Ia=le("p"),va=le("a"),wa=24,ya=111300,ba=ga+xa*(41+wa),Ta=function(e){return e[e.Against=0]="Against",e[e.For=1]="For",e[e.Abstain=2]="Abstain",e}(Ta||{}),ka=new Uint8Array(34);function _a(e){for(var t=arguments.length>1&&void 0!==arguments[1]?arguments[1]:8,n=new Uint8Array(t),r=BigInt(e),a=t-1;a>=0;a--)n[a]=Number(r&BigInt(255)),r>>=BigInt(8);return n}function Ea(e,t){return 0===Buffer.compare(e,t)}function Pa(e){for(var t=0n,n=0;n=255)throw new TypeError("Alphabet too long");for(var n=new Uint8Array(256),r=0;r>>0,o=new Uint8Array(s);e[t];){var p=n[e.charCodeAt(t)];if(255===p)return;for(var l=0,d=s-1;(0!==p||l>>0,o[d]=p%256>>>0,p=p/256>>>0;if(0!==p)throw new Error("Non-zero carry");a=l,t++}if(" "!==e[t]){for(var f=s-a;f!==s&&0===o[f];)f++;for(var m=new Uint8Array(r+(s-f)),h=r;f!==s;)m[h++]=o[f++];return m}}}return{encode:function(t){if(t instanceof Uint8Array||(ArrayBuffer.isView(t)?t=new Uint8Array(t.buffer,t.byteOffset,t.byteLength):Array.isArray(t)&&(t=Uint8Array.from(t))),!(t instanceof Uint8Array))throw new TypeError("Expected Uint8Array");if(0===t.length)return"";for(var n=0,r=0,a=0,s=t.length;a!==s&&0===t[a];)a++,n++;for(var o=(s-a)*p+1>>>0,c=new Uint8Array(o);a!==s;){for(var l=t[a],d=0,f=o-1;(0!==l||d>>0,c[f]=l%i>>>0,l=l/i>>>0;if(0!==l)throw new Error("Non-zero carry");r=d,a++}for(var m=o-r;m!==o&&0===c[m];)m++;for(var h=u.repeat(n);mSa(a(e))})}function Ua({name:e,prefix:t,bitsPerChar:n,alphabet:r}){return Ra({prefix:t,name:e,encode:e=>function(e,t,n){const r="="===t[t.length-1],a=(1<n;)o-=n,s+=t[a&i>>o];if(0!==o&&(s+=t[a&i<function(e,t,n,r){const a={};for(let e=0;e=8&&(i-=8,o[c++]=255&u>>i)}if(i>=n||0!=(255&u<<8-i))throw new SyntaxError("Unexpected end of data");return o}(t,r,n,e)})}const ja=Ua({prefix:"b",name:"base32",alphabet:"abcdefghijklmnopqrstuvwxyz234567",bitsPerChar:5});Ua({prefix:"B",name:"base32upper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",bitsPerChar:5}),Ua({prefix:"c",name:"base32pad",alphabet:"abcdefghijklmnopqrstuvwxyz234567=",bitsPerChar:5}),Ua({prefix:"C",name:"base32padupper",alphabet:"ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=",bitsPerChar:5}),Ua({prefix:"v",name:"base32hex",alphabet:"0123456789abcdefghijklmnopqrstuv",bitsPerChar:5}),Ua({prefix:"V",name:"base32hexupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV",bitsPerChar:5}),Ua({prefix:"t",name:"base32hexpad",alphabet:"0123456789abcdefghijklmnopqrstuv=",bitsPerChar:5}),Ua({prefix:"T",name:"base32hexpadupper",alphabet:"0123456789ABCDEFGHIJKLMNOPQRSTUV=",bitsPerChar:5}),Ua({prefix:"h",name:"base32z",alphabet:"ybndrfg8ejkmcpqxot1uwisza345h769",bitsPerChar:5});const Va=Ma({name:"base58btc",prefix:"z",alphabet:"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"});Ma({name:"base58flickr",prefix:"Z",alphabet:"123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ"});var Ga=function e(t,n,r){n=n||[];var a=r=r||0;for(;t>=qa;)n[r++]=255&t|Xa,t/=128;for(;t&Qa;)n[r++]=255&t|Xa,t>>>=7;return n[r]=0|t,e.bytes=r-a+1,n},Xa=128,Qa=-128,qa=Math.pow(2,31);var Wa=function e(t,n){var r,a=0,s=0,o=n=n||0,i=t.length;do{if(o>=i)throw e.bytes=0,new RangeError("Could not decode varint");r=t[o++],a+=s<28?(r&Ja)<=za);return e.bytes=o-n,a},za=128,Ja=127;var Ka=Math.pow(2,7),Ya=Math.pow(2,14),Ha=Math.pow(2,21),$a=Math.pow(2,28),Za=Math.pow(2,35),es=Math.pow(2,42),ts=Math.pow(2,49),ns=Math.pow(2,56),rs=Math.pow(2,63),as={encode:Ga,decode:Wa,encodingLength:function(e){return e{const[n,r]=ss(e.subarray(t));return t+=r,n};let r=n(),a=ms;if(18===r?(r=0,t=0):a=n(),0!==r&&1!==r)throw new RangeError(`Invalid CID version ${r}`);const s=t,o=n(),i=n(),u=t+i;return{version:r,codec:a,multihashCode:o,digestSize:i,multihashSize:u-s,size:u}}static parse(e,t){const[n,r]=function(e,t){switch(e[0]){case"Q":{const n=t??Va;return[Va.prefix,n.decode(`${Va.prefix}${e}`)]}case Va.prefix:{const n=t??Va;return[Va.prefix,n.decode(e)]}case ja.prefix:{const n=t??ja;return[ja.prefix,n.decode(e)]}default:if(null==t)throw Error("To parse non base32 or base58btc encoded CID multibase decoder must be provided");return[e[0],t.decode(e)]}}(e,t),a=fs.decode(r);if(0===a.version&&"Q"!==e[0])throw Error("Version 0 CID string must not include multibase prefix");return ds(a).set(n,e),a}}const ms=112,hs=18;function gs(e,t,n){const r=is(e),a=r+is(t),s=new Uint8Array(a+n.byteLength);return os(e,s,0),os(t,s,r),s.set(n,a),s}const xs=Symbol.for("@ipld/js-cid/CID");class As{name;code;encode;constructor(e,t,n){this.name=e,this.code=t,this.encode=n}digest(e){if(e instanceof Uint8Array){const t=this.encode(e);return t instanceof Uint8Array?us(this.code,t):t.then((e=>us(this.code,e)))}throw Error("Unknown type, must be binary type")}}const Is=85,vs=function({name:e,code:t,encode:n}){return new As(e,t,n)}({name:"sha2-256",code:18,encode:e=>Sa(n.createHash("sha256").update(e).digest())});var ws=126144e3,ys=1e7,bs=Math.pow(2,64),Ts=48,ks=21,_s=48,Es=le("tp"),Ps=le("sc"),Ss=28100,Ds=12900,Os=421700,Ns=409700;function Bs(e){var t=e/Aa;return(arguments.length>1&&void 0!==arguments[1]&&arguments[1]?Math.floor(t):Math.ceil(t))*Aa}function Cs(e,t){if(e>t)throw new Error("Old timestamp must be less than or equal to new timestamp");for(var n=[],r=Bs(e)+Aa;re?r=t?e*e*bs/(2*t):0:r=(e+(e-r))*n/2;return r}function Vs(e,t){return Gs.apply(this,arguments)}function Gs(){return(Gs=i(s().mark((function e(t,n){var r;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,t.getApplicationByID(n).do();case 2:return r=e.sent,e.abrupt("return",Xs(r));case 4:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function Xs(e){var t,n={},r=T(e.params["global-state"]);try{for(r.s();!(t=r.n()).done;){var a=t.value,s=Buffer.from(a.key,"base64").toString(),o=void 0;o=1===a.value.type?Buffer.from(a.value.bytes||"","base64"):a.value.uint||0,n[s]=o}}catch(e){r.e(e)}finally{r.f()}return n}function Qs(){return(Qs=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=(new TextEncoder).encode(t),e.next=3,vs.digest(n);case 3:return r=e.sent,a=fs.createV1(Is,r),e.abrupt("return",a.toString(ja));case 6:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function qs(){for(var t=arguments.length,n=new Array(t),r=0;r0?ks-e:0}},{key:"lastAccountPowerBoxIndex",get:function(){return co(this.powerCount)[0]}},{key:"lastAccountPowerArrayIndex",get:function(){return co(this.powerCount)[1]}}]),e}(),to=function(){function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a}return p(e,[{key:"lockEndTimestamp",get:function(){var e=this.bias*Math.pow(2,64)/this.slope;return this.timestamp+e}},{key:"cumulativePowerAt",value:function(e){var t=e-this.timestamp;if(t<0)throw new Error("Time delta must be greater than or equal to 0");return this.cumulativePower+js(this.bias,this.slope,t)}}]),e}(),no=p((function e(t,n,r,a){u(this,e),this.bias=t,this.timestamp=n,this.slope=r,this.cumulativePower=a})),ro=p((function e(t){u(this,e),this.slopeDelta=t})),ao=function(){function e(t,n,r,a){u(this,e),this.tinyAssetId=t,this.totalLockedAmount=n,this.totalPowerCount=r,this.lastTotalPowerTimestamp=a}return p(e,[{key:"freeTotalPowerSpaceCount",get:function(){var e=this.totalPowerCount%ks;return e>0?ks-e:0}},{key:"lastTotalPowerBoxIndex",get:function(){return co(this.totalPowerCount)[0]}},{key:"lastTotalPowerArrayIndex",get:function(){return co(this.totalPowerCount)[1]}}]),e}();function so(){return(so=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=io(r),e.prev=1,e.next=4,Fs(t,n,a);case 4:if(!(o=e.sent)){e.next=7;break}return e.abrupt("return",oo(o));case 7:return e.abrupt("return",null);case 10:return e.prev=10,e.t0=e.catch(1),console.log(e.t0),e.abrupt("return",null);case 14:case"end":return e.stop()}}),e,null,[[1,10]])})))).apply(this,arguments)}function oo(e){var t=Buffer.from(e);return new eo(t.readUIntBE(0,8),t.readUIntBE(8,8),t.readUIntBE(16,8),t.readUIntBE(24,8))}function io(t){return e.decodeAddress(t).publicKey}function uo(e){var t=_a(e),n=new Uint8Array(Es.length+t.length);return n.set(Es,0),n.set(t,Es.length),n}function co(e){var t=e-1;return[Math.floor(t/ks),t%ks]}function po(t,n){var r=e.decodeAddress(t).publicKey,a=_a(n),s=new Uint8Array(r.length+a.length);return s.set(r,0),s.set(a,r.length),s}function lo(e,t,n){return fo.apply(this,arguments)}function fo(){return(fo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=ho(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",mo(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function mo(t){return new ro(Number(e.bytesToBigInt(t.slice(t.length-16,t.length))))}function ho(e){return q(Ps,_a(e))}function go(e,t,n){return xo.apply(this,arguments)}function xo(){return(xo=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=0,r&&(a=Math.ceil(r/ks)),o=[],i=0;case 4:if(!(i=e[r].timestamp))break;n=r}return n}function yo(e){for(var t,n=Ts,r=[],a=0;a=11)throw new Error("Boxes' length cannot be larger than 10");var y=d.slice(0,6),b=[e.makeApplicationNoOpTxnFromObject({from:s,suggestedParams:c,appIndex:n,appArgs:[le("claim_rewards"),_a(o),_a(i),q.apply(void 0,w(u.map((function(e){return _a(e)}))))],foreignApps:[r],foreignAssets:[a],boxes:y,note:l?le(l):void 0})];b[0].fee=b[0].fee*(i+2);var k=0,_=92+865*i,E=700+700*i;if(_>E&&(k=Math.floor((_-E)/666)+1),k||d.length>6){var P=d.slice(6),S=Go({sender:s,suggestedParams:c,extraAppArgs:[_a(Math.max(k-1,0))],index:n,foreignApps:[r],boxes:P});S.fee*=Math.max(k,1),b.unshift(S)}if(p){var D=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,to:e.getApplicationAddress(n),amount:So,suggestedParams:c});b.unshift(D)}return e.assignGroupID(b)}var Qo=8,qo=le("p"),Wo=le("a"),zo=le("v"),Jo=28500,Ko=12500,Yo=p((function e(t,n,r,a,s,o,i){u(this,e),this.index=t,this.creationTimestamp=n,this.votingStartTimestamp=r,this.votingEndTimestamp=a,this.votingPower=s,this.voteCount=o,this.isCancelled=i}));function Ho(t,n){return q(Wo,e.decodeAddress(t).publicKey,_a(n))}function $o(e){return new Yo(Pa(e.slice(0,8)),Pa(e.slice(8,16)),Pa(e.slice(16,24)),Pa(e.slice(24,32)),Pa(e.slice(32,40)),Pa(e.slice(40,48)),Boolean(Pa(e.slice(48,49))))}function Zo(){return(Zo=i(s().mark((function e(t,n,r){var a,o;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=Ys(r),e.next=3,Fs(t,n,a);case 3:if(o=e.sent){e.next=6;break}return e.abrupt("return",null);case 6:return e.abrupt("return",$o(o));case 7:case"end":return e.stop()}}),e)})))).apply(this,arguments)}function ei(t){var n=t.stakingVotingAppId,r=t.vaultAppId,a=t.sender,s=t.proposalId,o=t.proposal,i=t.votes,u=t.assetIds,c=t.accountPowerIndex,p=t.appBoxNames,l=t.suggestedParams,d=t.appCallNote,f=void 0===d?null:d;if(i.length!==u.length)throw new Error("The number of votes must be equal to the number of asset ids");if(u.length>Qo)throw new Error("You cannot cast vote for more than ".concat(Qo," different pools"));if(100!==i.reduce((function(e,t){return e+t}),0))throw new Error("The sum of the votes must equal 100%");var m,h=q.apply(void 0,w(i.map((function(e){return _a(e)})))),g=q.apply(void 0,w(u.map((function(e){return _a(e)})))),x=function(e){return q(qo,le(e))}(s),A=Ho(a,Math.floor(o.index/8192)),I=!p.some((function(e){return Ea(e,A)})),v=io(a),y=Math.floor(c/ks),b=po(a,y),k=po(a,y+1),_=0,E=[],P=T(u);try{var S=function(){var e=m.value,t=function(e,t){return q(zo,_a(e),_a(t))}(o.index,e);E.push({appIndex:n,name:t}),p.some((function(e){return Ea(e,t)}))||(_+=1)};for(P.s();!(m=P.n()).done;)S()}catch(e){P.e(e)}finally{P.f()}var D=[{appIndex:n,name:x},{appIndex:n,name:A}].concat(E,[{appIndex:r,name:v},{appIndex:r,name:b},{appIndex:r,name:k}]),O=[e.makeApplicationNoOpTxnFromObject({from:a,suggestedParams:l,appIndex:n,appArgs:[le("cast_vote"),le(s),h,g,_a(c)],foreignApps:[r],boxes:D.slice(0,7),note:f?le(f):void 0})];O[0].fee*=2,D.length>=7&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(7,14)})),D.length>=14&&O.push(Go({sender:a,suggestedParams:l,index:r,foreignApps:[n],boxes:D.slice(14)}));var N=Number(I)*Jo+_*Ko;return N&&(O=[e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:l,to:e.getApplicationAddress(n),amount:N})].concat(w(O))),e.assignGroupID(O)}function ti(t){var n=t.accountState,r=t.lockEndTime,a=t.lockedAmount,s=t.network,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.slopeChangeAtLockEndTime,p=t.appCallNote;if(ai.freeTotalPowerSpaceCount&&(b+=Ns),c||(b+=Ds);var k=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({amount:a,assetIndex:Se[s],from:o,suggestedParams:u,to:e.getApplicationAddress(l)}),e.makeApplicationNoOpTxnFromObject({appIndex:l,from:o,suggestedParams:u,appArgs:[le("create_lock"),_a(r)],boxes:d.slice(0,8),note:p}),Go({sender:o,suggestedParams:u,index:l,boxes:d.slice(8)})];if(b){var _=e.makePaymentTxnWithSuggestedParamsFromObject({amount:b,from:o,suggestedParams:u,to:e.getApplicationAddress(l)});k.unshift(_)}return e.assignGroupID(k)}function ni(t){var n=t.accountState,r=t.lockedAmount,a=t.network,s=t.sender,o=t.vaultAppGlobalState,i=t.suggestedParams,u=t.appCallNote;if(ro.freeTotalPowerSpaceCount&&(v+=Ns),!n.freeAccountPowerSpaceCount){var w=po(s,n.lastAccountPowerBoxIndex+1);h.push({appIndex:da[a],name:w}),v+=Os}var y=[e.makeAssetTransferTxnWithSuggestedParamsFromObject({assetIndex:Se[a],from:s,to:e.getApplicationAddress(da[a]),amount:r,suggestedParams:i}),e.makeApplicationNoOpTxnFromObject({appIndex:da[a],from:s,suggestedParams:i,appArgs:[le("increase_lock_amount")],boxes:h,note:u}),Go({sender:s,suggestedParams:i,index:da[a]})];if(v){var b=e.makePaymentTxnWithSuggestedParamsFromObject({from:s,suggestedParams:i,to:e.getApplicationAddress(da[a]),amount:v});y.unshift(b)}return e.assignGroupID(y)}function ri(t){var n=t.accountState,r=t.network,a=t.newLockEndTime,s=t.slopeChangeAtNewLockEndTime,o=t.sender,i=t.vaultAppGlobalState,u=t.suggestedParams,c=t.appCallNote;if(a%Aa)throw new Error("Invalid lock end time");if(a<=n.lockEndTime)throw new Error("New lock end time must be greater than current lock end time");var p=io(o),l=po(o,n.lastAccountPowerBoxIndex),d=uo(i.lastTotalPowerBoxIndex),f=uo(i.lastTotalPowerBoxIndex+1),m=ho(n.lockEndTime),h=ho(a),g=[{appIndex:da[r],name:p},{appIndex:da[r],name:l},{appIndex:da[r],name:d},{appIndex:da[r],name:f},{appIndex:da[r],name:m},{appIndex:da[r],name:h}];if(!n.freeAccountPowerSpaceCount){var x=po(o,n.lastAccountPowerBoxIndex+1);g.push({appIndex:da[r],name:x})}var A,I=Cs(i.lastTotalPowerTimestamp,Math.floor(Date.now()/1e3)),v=I.length,w=T(I);try{for(w.s();!(A=w.n()).done;){var y=A.value;if(y%Aa==0){var b=ho(y);g.push({appIndex:da[r],name:b})}}}catch(e){w.e(e)}finally{w.f()}var k=0;s||(k+=Ds),n.freeAccountPowerSpaceCount||(k+=Os),v>i.freeTotalPowerSpaceCount&&(k+=Ns);var _=[e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:o,suggestedParams:u,appArgs:[le("extend_lock_end_time"),_a(a)],boxes:g.slice(0,8),note:c}),Go({sender:o,suggestedParams:u,index:da[r],boxes:g.slice(8)})];if(k){var E=e.makePaymentTxnWithSuggestedParamsFromObject({from:o,suggestedParams:u,to:e.getApplicationAddress(da[r]),amount:k});_.unshift(E)}return e.assignGroupID(_)}function ai(t){var n=t.accountState,r=t.network,a=t.sender,s=t.suggestedParams,o=t.appCallNote,i=io(a),u=po(a,n.lastAccountPowerBoxIndex),c=po(a,n.lastAccountPowerBoxIndex+1),p=[{appIndex:da[r],name:i},{appIndex:da[r],name:u},{appIndex:da[r],name:c}],l=e.makeApplicationNoOpTxnFromObject({appIndex:da[r],from:a,suggestedParams:s,appArgs:[le("withdraw")],foreignAssets:[Se[r]],boxes:p,note:o});l.fee*=2;var d=[l];if(!n.freeAccountPowerSpaceCount){var f=e.makePaymentTxnWithSuggestedParamsFromObject({from:a,suggestedParams:s,to:e.getApplicationAddress(da[r]),amount:Os});d.unshift(f)}return e.assignGroupID(d)}var si=function(){function t(e,n,r){u(this,t),this.algodClient=e,this.userAddress=n,this.network=r}var n,r,a,o,c,l,d,f,m,h,g,x,A,I,v,y;return p(t,[{key:"getTinyPower",value:(y=i(s().mark((function e(){var t,n,r,a,o,i,u,c=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=c.length>0&&void 0!==c[0]?c[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=Math.max(o.bias-Us(o.slope,i),0),e.abrupt("return",u);case 16:case"end":return e.stop()}}),e,this)}))),function(){return y.apply(this,arguments)})},{key:"getTotalTinyPower",value:(v=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=v.length>0&&void 0!==v[0]?v[0]:Math.floor(Date.now()/la),e.next=3,this.fetchVaultAppGlobalState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,go(this.algodClient,da[this.network],n.totalPowerCount);case 8:if(r=e.sent,a=wo(r,t)){e.next=12;break}return e.abrupt("return",0);case 12:o=r[a],i=Math.floor(o.timestamp/Aa),u=Math.floor(t/Aa)-i,c=Array.from({length:u},(function(e,t){return(i+t)*Aa})),p=c.map((function(e,t){return[0===t?o.timestamp:c[t-1],e]})),l=o.slope,d=o.bias,f=T(p),e.prev=19,f.s();case 21:if((m=f.n()).done){e.next=34;break}return h=m.value,g=h[1]-h[0],x=Us(l,g),d=Math.max(d-x,0),e.next=28,lo(this.algodClient,da[this.network],h[1]);case 28:A=e.sent,I=(null==A?void 0:A.slopeDelta)||0,l=Math.max(l-I,0),0!==d&&0!==l||(d=0,l=0);case 32:e.next=21;break;case 34:e.next=39;break;case 36:e.prev=36,e.t0=e.catch(19),f.e(e.t0);case 39:return e.prev=39,f.f(),e.finish(39);case 42:return e.abrupt("return",d);case 43:case"end":return e.stop()}}),e,this,[[19,36,39,42]])}))),function(){return v.apply(this,arguments)})},{key:"getCumulativeTinyPower",value:(I=i(s().mark((function e(){var t,n,r,a,o,i,u,c,p=arguments;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return t=p.length>0&&void 0!==p[0]?p[0]:Math.floor(Date.now()/la),e.next=3,this.fetchAccountState();case 3:if(n=e.sent){e.next=6;break}return e.abrupt("return",0);case 6:return e.next=8,Io({algodClient:this.algodClient,address:this.userAddress,appId:da[this.network],powerCount:n.powerCount});case 8:if(r=e.sent,null!==(a=wo(r,t))){e.next=12;break}return e.abrupt("return",0);case 12:return o=r[a],i=t-o.timestamp,u=js(o.bias,o.slope,i),c=o.cumulativePower-u,e.abrupt("return",c);case 17:case"end":return e.stop()}}),e,this)}))),function(){return I.apply(this,arguments)})},{key:"fetchVaultAppGlobalState",value:(A=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.prev=0,e.next=3,Vs(this.algodClient,da[this.network]);case 3:return t=e.sent,e.abrupt("return",new ao(t.tiny_asset_id,t.total_locked_amount,t.total_power_count,t.last_total_power_timestamp));case 7:return e.prev=7,e.t0=e.catch(0),console.error(e.t0),e.abrupt("return",null);case 11:case"end":return e.stop()}}),e,this,[[0,7]])}))),function(){return A.apply(this,arguments)})},{key:"generateCreateLockTransactions",value:(x=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=o,c){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(p){e.next=15;break}return e.next=14,this.algodClient.getTransactionParams().do();case 14:p=e.sent;case 15:return e.next=17,lo(this.algodClient,da[this.network],r);case 17:return l=e.sent,e.abrupt("return",ti({lockedAmount:n,lockEndTime:r,vaultAppGlobalState:c,slopeChangeAtLockEndTime:l,sender:i,network:this.network,accountState:u,suggestedParams:p}));case 19:case"end":return e.stop()}}),e,this)}))),function(e){return x.apply(this,arguments)})},{key:"generateIncreaseLockAmountTransactions",value:(g=i(s().mark((function e(t){var n,r,a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockedAmount,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.abrupt("return",ni({accountState:i,lockedAmount:n,network:this.network,sender:o,vaultAppGlobalState:u,suggestedParams:c}));case 18:case"end":return e.stop()}}),e,this)}))),function(e){return g.apply(this,arguments)})},{key:"generateExtendLockTimeTransactions",value:(h=i(s().mark((function e(t){var n,r,a,o,i,u,c,p;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.newLockEndTime,r=t.userAddress,a=t.suggestedParams,o=r||this.userAddress,e.next=4,this.fetchAccountState();case 4:return i=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(u=e.sent,c=a,u){e.next=11;break}throw new Error("There was an error while fetching vault app global state");case 11:if(i){e.next=13;break}throw new Error("There was an error while fetcing the account state");case 13:if(c){e.next=17;break}return e.next=16,this.algodClient.getTransactionParams().do();case 16:c=e.sent;case 17:return e.next=19,lo(this.algodClient,da[this.network],n);case 19:return p=e.sent,e.abrupt("return",ri({accountState:i,network:this.network,newLockEndTime:n,sender:o,vaultAppGlobalState:u,suggestedParams:c,slopeChangeAtNewLockEndTime:null==p?void 0:p.slopeDelta}));case 21:case"end":return e.stop()}}),e,this)}))),function(e){return h.apply(this,arguments)})},{key:"generateIncreaseLockAmountAndExtendLockEndTimeTransactions",value:(m=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return n=t.lockAmount,r=t.lockEndTime,a=t.userAddress,o=t.suggestedParams,i=a||this.userAddress,e.next=4,this.fetchAccountState();case 4:return u=e.sent,e.next=7,this.fetchVaultAppGlobalState();case 7:if(c=e.sent,p=[],l=[],d=o,c){e.next=13;break}throw new Error("There was an error while fetching vault app global state");case 13:if(u){e.next=15;break}throw new Error("There was an error while fetcing the account state");case 15:if(d){e.next=19;break}return e.next=18,this.algodClient.getTransactionParams().do();case 18:d=e.sent;case 19:return p=ni({accountState:u,lockedAmount:n,network:this.network,sender:i,vaultAppGlobalState:c,suggestedParams:d}),u.powerCount+=1,c.totalPowerCount+=1,e.next=24,lo(this.algodClient,da[this.network],r);case 24:return f=e.sent,l=ri({accountState:u,network:this.network,newLockEndTime:r,sender:i,vaultAppGlobalState:c,suggestedParams:d,slopeChangeAtNewLockEndTime:null==f?void 0:f.slopeDelta}),m=[].concat(w(p),w(l)),e.abrupt("return",Boolean(p.length)&&Boolean(l.length)?qs(p,l):m);case 28:case"end":return e.stop()}}),e,this)}))),function(e){return m.apply(this,arguments)})},{key:"generateWithdrawTransactions",value:(f=i(s().mark((function e(t,n,r){var a,o,i,u,c;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return a=t||this.userAddress,e.next=3,this.fetchAccountState();case 3:if(o=e.sent,i=r,o){e.next=7;break}throw new Error("There was an error while fetcing the account state");case 7:if(i){e.next=11;break}return e.next=10,this.algodClient.getTransactionParams().do();case 10:i=e.sent;case 11:if(u=ai({accountState:o,network:this.network,sender:a,suggestedParams:i,client:this.algodClient}),!n){e.next=17;break}return e.next=15,Ne({client:this.algodClient,initiatorAddr:a,assetID:Se[this.network]});case 15:c=e.sent,u=qs([c[0].txn],u);case 17:return e.abrupt("return",u);case 18:case"end":return e.stop()}}),e,this)}))),function(e,t,n){return f.apply(this,arguments)})},{key:"fetchAccountState",value:function(){return function(e,t,n){return so.apply(this,arguments)}(this.algodClient,da[this.network],this.userAddress)}},{key:"fetchStakingDistributionProposal",value:function(e){return function(e,t,n){return Zo.apply(this,arguments)}(this.algodClient,fa[this.network],e)}},{key:"generateCastVoteForStakingDistributionProposalTransactions",value:(d=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return r=t.proposalId,a=t.votes,o=t.assetIds,i=t.userAddress,u=t.suggestedParams,c=null!=i?i:this.userAddress,p=u,e.next=5,this.fetchStakingDistributionProposal(r);case 5:if(l=e.sent){e.next=8;break}throw new Error("There was an error while fetching staking distribution proposal");case 8:if(p){e.next=12;break}return e.next=11,this.algodClient.getTransactionParams().do();case 11:p=e.sent;case 12:return e.next=14,this.fetchAccountState();case 14:return d=e.sent,e.next=17,Ws(this.algodClient,fa[this.network]);case 17:return f=e.sent,e.next=20,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==d?void 0:d.powerCount)&&void 0!==n?n:null});case 20:if(m=e.sent,null!==(h=wo(m,l.creationTimestamp))){e.next=24;break}throw new Error("It is required to have an account power at the staking distribution proposal creation timestamp");case 24:return e.abrupt("return",ei({stakingVotingAppId:fa[this.network],vaultAppId:da[this.network],sender:c,proposalId:r,proposal:l,votes:a,assetIds:o,accountPowerIndex:h,appBoxNames:f,suggestedParams:p,appCallNote:null}));case 25:case"end":return e.stop()}}),e,this)}))),function(e){return d.apply(this,arguments)})},{key:"fetchRewardsAppGlobalState",value:(l=i(s().mark((function t(){var n;return s().wrap((function(t){for(;;)switch(t.prev=t.next){case 0:return t.prev=0,t.next=3,Vs(this.algodClient,ma[this.network]);case 3:return n=t.sent,t.abrupt("return",new Do(n.tiny_asset_id,n.vault_app_id,n.reward_history_count,n.first_period_timestamp,n.reward_period_count,e.encodeAddress(n.manager),e.encodeAddress(n.rewards_manager)));case 7:return t.prev=7,t.t0=t.catch(0),console.error(t.t0),t.abrupt("return",null);case 11:case"end":return t.stop()}}),t,this,[[0,7]])}))),function(){return l.apply(this,arguments)})},{key:"generateClaimRewardTransactions",value:(c=i(s().mark((function e(t){var n,r,a,o,i,u,c,p,l,d,f,m,h,g,x,A,I,v,w,y,b,T,k;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(r=t.periodIndexStart,a=t.periodCount,o=t.userAddress,i=t.suggestedParams,u=t.shouldOptIntoTINY,c=null!=o?o:this.userAddress,p=i){e.next=7;break}return e.next=6,this.algodClient.getTransactionParams().do();case 6:p=e.sent;case 7:return e.next=9,this.fetchAccountState();case 9:return l=e.sent,e.next=12,this.fetchRewardsAppGlobalState();case 12:if(d=e.sent){e.next=15;break}throw new Error("There was an error while fetching rewards app global state");case 15:if(!(r+a>d.rewardPeriodCount)){e.next=17;break}throw new Error("The reward period index to be claimed cannot be larger than the latest reward period index");case 17:return e.next=19,Io({algodClient:this.algodClient,address:c,appId:da[this.network],powerCount:null!==(n=null==l?void 0:l.powerCount)&&void 0!==n?n:null});case 19:for(f=e.sent,m=d.firstPeriodTimestamp+r*pa,h=d.firstPeriodTimestamp+(r+a)*pa,g=[],x=m;x=Math.floor(Date.now()/la)||c.votingEndTimestamp<=Math.floor(Date.now()/la))){e.next=23;break}throw new Error("Voting period is not active");case 23:return e.next=25,Io({algodClient:this.algodClient,address:i,appId:da[this.network],powerCount:p.powerCount});case 25:if(l=e.sent,null!==(d=wo(l,c.creationTimestamp))){e.next=29;break}throw new Error("Account power index not found");case 29:return f=Math.floor(c.index/(8*wa)),m=Ho(i,f),e.next=33,Rs(this.algodClient,ha[this.network],m);case 33:return h=!e.sent,e.abrupt("return",To({proposalVotingAppId:ha[this.network],vaultAppId:da[this.network],sender:i,proposalId:n,proposal:c,vote:a,accountPowerIndex:d,createAttendanceSheetBox:h,suggestedParams:u}));case 35:case"end":return e.stop()}}),e,this)}))),function(e){return a.apply(this,arguments)})},{key:"fetchProposalVotingAppGlobalState",value:(r=i(s().mark((function e(){var t;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:return e.next=2,Vs(this.algodClient,ha[this.network]);case 2:return t=e.sent,e.abrupt("return",new Ks(t.vault_app_id,t.proposal_index_counter,t.voting_delay,t.voting_duration,t.proposal_threshold,t.proposal_threshold_numerator,t.quorum_threshold,t.approval_requirement,t.manager,t.proposal_manager));case 4:case"end":return e.stop()}}),e,this)}))),function(){return r.apply(this,arguments)})},{key:"getRequiredTinyPowerToCreateProposal",value:(n=i(s().mark((function e(t){var n,r,a;return s().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(null==t){e.next=4;break}e.t0=t,e.next=7;break;case 4:return e.next=6,this.getTotalTinyPower();case 6:e.t0=e.sent;case 7:return n=e.t0,e.next=10,this.fetchProposalVotingAppGlobalState();case 10:return r=e.sent,a=r.proposalThreshold,r.proposalThresholdNumerator&&(a=Math.max(a,Math.floor(n*r.proposalThresholdNumerator/100)+1)),e.abrupt("return",a);case 14:case"end":return e.stop()}}),e,this)}))),function(e){return n.apply(this,arguments)})}]),t}(),oi=2500,ii=400,ui=function(){function e(t,n){u(this,e),l(this,"data",null),this.name=t,this.structReference=n,this.size=n[t].size,this.fields=n[t].fields}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.size)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t;if(!this.data)throw new Error("Data is not initialized");var n=this.fields[e];if(!n)throw new Error("Field ".concat(e," does not exist in struct ").concat(this.name));var r=n.offset,a=n.offset+n.size,s=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(r,a);return di(n.type,this.structReference).apply(Buffer.from(s))}}]),e}(),ci=function(){function e(t,n){u(this,e),l(this,"data",null),this.struct=t,this.length=n}return p(e,[{key:"apply",value:function(e){var t=e;return t||(t=Buffer.alloc(this.struct.size*this.length)),this.data=new DataView(t.buffer,t.byteOffset,t.byteLength),this}},{key:"getField",value:function(e){var t,n=this.struct.size*e,r=n+this.struct.size,a=null===(t=this.data)||void 0===t?void 0:t.buffer.slice(n,r);return this.struct.apply(a?Buffer.from(a):void 0)}}]),e}(),pi=function(){function e(){u(this,e)}return p(e,[{key:"apply",value:function(e){for(var t=BigInt(0),n=0;n2&&void 0!==arguments[2]?arguments[2]:Math.floor(Date.now()/1e3),a=(n=e,Math.floor(n*bs/ws)),s=t-r;return s<0?0:Us(a,s)},exports.combineAndRegroupSignerTxns=function(){for(var t=arguments.length,n=new Array(t),r=0;r=e.minimum_required_balance},exports.intToBytes=_a,exports.isAccountOptedIntoApp=function(e){var t=e.appID;return e.accountAppsLocalState.some((function(e){return e.id===t}))},exports.isSwapAssetInAmountLow=Ke,exports.isSwapQuoteErrorCausedByAmount=at,exports.poolUtils=Wt,exports.prepareCommitTransactions=function(e){return ca.apply(this,arguments)},exports.redeemAllExcessAsset=function(e){return Wr.apply(this,arguments)},exports.redeemExcessAsset=function(e){return Xr.apply(this,arguments)},exports.sendAndWaitRawTransaction=oe,exports.sumUpTxnFees=ue,exports.tinymanContract_v1_1=ht,exports.tinymanContract_v2=At,exports.tinymanJSSDKConfig=ut; diff --git a/src/liquid-stake/tAlgoClient.test.ts b/src/liquid-stake/tAlgoClient.test.ts index 14264430..1a263920 100644 --- a/src/liquid-stake/tAlgoClient.test.ts +++ b/src/liquid-stake/tAlgoClient.test.ts @@ -174,5 +174,5 @@ describe("TinymanTAlgoClient", () => { expect(finalTAlgoAccountAsset?.amount).toBe( (initialTAlgoAccountAsset?.amount ?? 0) - burnAmount ); - }, 10000); + }, 18000); }); diff --git a/src/swap/v2/router/swap-router.ts b/src/swap/v2/router/swap-router.ts index f6da5529..b03e5c85 100644 --- a/src/swap/v2/router/swap-router.ts +++ b/src/swap/v2/router/swap-router.ts @@ -161,8 +161,10 @@ export async function getSwapRoute({ Number((serializedResponse as SwapRouterResponse).asset_in.id) !== assetInID || Number((serializedResponse as SwapRouterResponse).asset_out.id) !== assetOutID || (serializedResponse as SwapRouterResponse).swap_type === SwapType.FixedInput - ? amount !== BigInt((serializedResponse as SwapRouterResponse).input_amount ?? 0) - : amount !== BigInt((serializedResponse as SwapRouterResponse).output_amount ?? 0) + ? BigInt(amount) !== + BigInt((serializedResponse as SwapRouterResponse).input_amount ?? 0) + : BigInt(amount) !== + BigInt((serializedResponse as SwapRouterResponse).output_amount ?? 0) ) { throw new SwapQuoteError( SwapQuoteErrorType.UnknownError,