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,