astra-router-sdk
Version:
An sdk for routing swaps using Astra Classic and Astra CL.
3 lines (2 loc) • 37.8 kB
JavaScript
"use strict";function t(t){return t&&"object"==typeof t&&"default"in t?t.default:t}Object.defineProperty(exports,"__esModule",{value:!0});var e,n=require("astra-sdk-core"),r=t(require("jsbi")),o=require("@ethersproject/abi"),u=t(require("tiny-invariant")),i=require("@airdao/swap-router-contracts/artifacts/contracts/interfaces/IApproveAndCall.sol/IApproveAndCall.json"),a=require("astra-cl-sdk-dev"),c=require("@airdao/swap-router-contracts/artifacts/contracts/interfaces/IMulticallExtended.sol/IMulticallExtended.json"),s=require("@airdao/swap-router-contracts/artifacts/contracts/interfaces/IPeripheryPaymentsWithFeeExtended.sol/IPeripheryPaymentsWithFeeExtended.json"),p=require("@airdao/swap-router-contracts/artifacts/contracts/interfaces/ISwapRouter02.sol/ISwapRouter02.json"),l=require("astra-classic-sdk"),d=require("@ethersproject/solidity"),m="0x0000000000000000000000000000000000000001",f="0x0000000000000000000000000000000000000002",h=r.BigInt(0),A=r.BigInt(1),v=new n.Percent(h),y=new n.Percent(100,100);function w(t){return Object.keys(t).some((function(t){return"recipient"===t}))}(e=exports.ApprovalTypes||(exports.ApprovalTypes={}))[e.NOT_REQUIRED=0]="NOT_REQUIRED",e[e.MAX=1]="MAX",e[e.MAX_MINUS_ONE=2]="MAX_MINUS_ONE",e[e.ZERO_THEN_MAX=3]="ZERO_THEN_MAX",e[e.ZERO_THEN_MAX_MINUS_ONE=4]="ZERO_THEN_MAX_MINUS_ONE";var T=function(){function t(){}return t.encodeApproveMax=function(e){return t.INTERFACE.encodeFunctionData("approveMax",[e.address])},t.encodeApproveMaxMinusOne=function(e){return t.INTERFACE.encodeFunctionData("approveMaxMinusOne",[e.address])},t.encodeApproveZeroThenMax=function(e){return t.INTERFACE.encodeFunctionData("approveZeroThenMax",[e.address])},t.encodeApproveZeroThenMaxMinusOne=function(e){return t.INTERFACE.encodeFunctionData("approveZeroThenMaxMinusOne",[e.address])},t.encodeCallPositionManager=function(e){if(e.length>0||u(!1),1===e.length)return t.INTERFACE.encodeFunctionData("callPositionManager",e);var n=a.NonfungiblePositionManager.INTERFACE.encodeFunctionData("multicall",[e]);return t.INTERFACE.encodeFunctionData("callPositionManager",[n])},t.encodeAddLiquidity=function(e,n,o,u){var i=e.mintAmountsWithSlippage(u),c=i.amount0,s=i.amount1;return r.lessThan(n.amount0.quotient,c)&&(c=n.amount0.quotient),r.lessThan(n.amount1.quotient,s)&&(s=n.amount1.quotient),w(o)?t.INTERFACE.encodeFunctionData("mint",[{token0:e.pool.token0.address,token1:e.pool.token1.address,fee:e.pool.fee,tickLower:e.tickLower,tickUpper:e.tickUpper,amount0Min:a.toHex(c),amount1Min:a.toHex(s),recipient:o.recipient}]):t.INTERFACE.encodeFunctionData("increaseLiquidity",[{token0:e.pool.token0.address,token1:e.pool.token1.address,amount0Min:a.toHex(c),amount1Min:a.toHex(s),tokenId:a.toHex(o.tokenId)}])},t.encodeApprove=function(e,n){switch(n){case exports.ApprovalTypes.MAX:return t.encodeApproveMax(e.wrapped);case exports.ApprovalTypes.MAX_MINUS_ONE:return t.encodeApproveMaxMinusOne(e.wrapped);case exports.ApprovalTypes.ZERO_THEN_MAX:return t.encodeApproveZeroThenMax(e.wrapped);case exports.ApprovalTypes.ZERO_THEN_MAX_MINUS_ONE:return t.encodeApproveZeroThenMaxMinusOne(e.wrapped);default:throw new Error("Error: invalid ApprovalType")}},t}();T.INTERFACE=new o.Interface(i.abi);var x=function(){function t(){}return t.encodeMulticall=function(e,n){if(void 0===n)return a.Multicall.encodeMulticall(e);if(Array.isArray(e)||(e=[e]),"string"==typeof n&&n.startsWith("0x")){var r=function(t){if(!t.match(/^0x[0-9a-fA-F]{64}$/))throw new Error(t+" is not valid bytes32.");return t.toLowerCase()}(n);return t.INTERFACE.encodeFunctionData("multicall(bytes32,bytes[])",[r,e])}var o=a.toHex(n);return t.INTERFACE.encodeFunctionData("multicall(uint256,bytes[])",[o,e])},t}();function E(t){return a.toHex(t.multiply(1e4).quotient)}x.INTERFACE=new o.Interface(c.abi);var k=function(){function t(){}return t.encodeUnwrapSAMB=function(e,r,o){if("string"==typeof r)return a.Payments.encodeUnwrapSAMB(e,r,o);if(o){var u=E(o.fee),i=n.validateAndParseAddress(o.recipient);return t.INTERFACE.encodeFunctionData("unwrapSAMBWithFee(uint256,uint256,address)",[a.toHex(e),u,i])}return t.INTERFACE.encodeFunctionData("unwrapSAMB(uint256)",[a.toHex(e)])},t.encodeSweepToken=function(e,r,o,u){if("string"==typeof o)return a.Payments.encodeSweepToken(e,r,o,u);if(u){var i=E(u.fee),c=n.validateAndParseAddress(u.recipient);return t.INTERFACE.encodeFunctionData("sweepTokenWithFee(address,uint256,uint256,address)",[e.address,a.toHex(r),i,c])}return t.INTERFACE.encodeFunctionData("sweepToken(address,uint256)",[e.address,a.toHex(r)])},t.encodePull=function(e,n){return t.INTERFACE.encodeFunctionData("pull",[e.address,a.toHex(n)])},t.encodeWrapAMB=function(e){return t.INTERFACE.encodeFunctionData("wrapAMB",[a.toHex(e)])},t}();function g(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=Array(e);n<e;n++)r[n]=t[n];return r}function I(t,e,n,r,o,u,i){try{var a=t[u](i),c=a.value}catch(t){return void n(t)}a.done?e(c):Promise.resolve(c).then(r,o)}function P(t){return function(){var e=this,n=arguments;return new Promise((function(r,o){var u=t.apply(e,n);function i(t){I(u,r,o,i,a,"next",t)}function a(t){I(u,r,o,i,a,"throw",t)}i(void 0)}))}}function b(t,e){for(var n=0;n<e.length;n++){var r=e[n];r.enumerable=r.enumerable||!1,r.configurable=!0,"value"in r&&(r.writable=!0),Object.defineProperty(t,N(r.key),r)}}function _(t,e,n){return e&&b(t.prototype,e),n&&b(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function R(t,e){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return g(t,e);var n={}.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?g(t,e):void 0}}(t))||e&&t&&"number"==typeof t.length){n&&(t=n);var r=0;return function(){return r>=t.length?{done:!0}:{done:!1,value:t[r++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function C(){return(C=Object.assign?Object.assign.bind():function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)({}).hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(null,arguments)}function O(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,function(t,e){(Object.setPrototypeOf?Object.setPrototypeOf.bind():function(t,e){return t.__proto__=e,t})(t,e)}(t,e)}function F(){F=function(){return e};var t,e={},n=Object.prototype,r=n.hasOwnProperty,o=Object.defineProperty||function(t,e,n){t[e]=n.value},u="function"==typeof Symbol?Symbol:{},i=u.iterator||"@@iterator",a=u.asyncIterator||"@@asyncIterator",c=u.toStringTag||"@@toStringTag";function s(t,e,n){return Object.defineProperty(t,e,{value:n,enumerable:!0,configurable:!0,writable:!0}),t[e]}try{s({},"")}catch(t){s=function(t,e,n){return t[e]=n}}function p(t,e,n,r){var u=Object.create((e&&e.prototype instanceof A?e:A).prototype),i=new R(r||[]);return o(u,"_invoke",{value:I(t,n,i)}),u}function l(t,e,n){try{return{type:"normal",arg:t.call(e,n)}}catch(t){return{type:"throw",arg:t}}}e.wrap=p;var d="suspendedStart",m="executing",f="completed",h={};function A(){}function v(){}function y(){}var w={};s(w,i,(function(){return this}));var T=Object.getPrototypeOf,x=T&&T(T(C([])));x&&x!==n&&r.call(x,i)&&(w=x);var E=y.prototype=A.prototype=Object.create(w);function k(t){["next","throw","return"].forEach((function(e){s(t,e,(function(t){return this._invoke(e,t)}))}))}function g(t,e){function n(o,u,i,a){var c=l(t[o],t,u);if("throw"!==c.type){var s=c.arg,p=s.value;return p&&"object"==typeof p&&r.call(p,"__await")?e.resolve(p.__await).then((function(t){n("next",t,i,a)}),(function(t){n("throw",t,i,a)})):e.resolve(p).then((function(t){s.value=t,i(s)}),(function(t){return n("throw",t,i,a)}))}a(c.arg)}var u;o(this,"_invoke",{value:function(t,r){function o(){return new e((function(e,o){n(t,r,e,o)}))}return u=u?u.then(o,o):o()}})}function I(e,n,r){var o=d;return function(u,i){if(o===m)throw Error("Generator is already running");if(o===f){if("throw"===u)throw i;return{value:t,done:!0}}for(r.method=u,r.arg=i;;){var a=r.delegate;if(a){var c=P(a,r);if(c){if(c===h)continue;return c}}if("next"===r.method)r.sent=r._sent=r.arg;else if("throw"===r.method){if(o===d)throw o=f,r.arg;r.dispatchException(r.arg)}else"return"===r.method&&r.abrupt("return",r.arg);o=m;var s=l(e,n,r);if("normal"===s.type){if(o=r.done?f:"suspendedYield",s.arg===h)continue;return{value:s.arg,done:r.done}}"throw"===s.type&&(o=f,r.method="throw",r.arg=s.arg)}}}function P(e,n){var r=n.method,o=e.iterator[r];if(o===t)return n.delegate=null,"throw"===r&&e.iterator.return&&(n.method="return",n.arg=t,P(e,n),"throw"===n.method)||"return"!==r&&(n.method="throw",n.arg=new TypeError("The iterator does not provide a '"+r+"' method")),h;var u=l(o,e.iterator,n.arg);if("throw"===u.type)return n.method="throw",n.arg=u.arg,n.delegate=null,h;var i=u.arg;return i?i.done?(n[e.resultName]=i.value,n.next=e.nextLoc,"return"!==n.method&&(n.method="next",n.arg=t),n.delegate=null,h):i:(n.method="throw",n.arg=new TypeError("iterator result is not an object"),n.delegate=null,h)}function b(t){var e={tryLoc:t[0]};1 in t&&(e.catchLoc=t[1]),2 in t&&(e.finallyLoc=t[2],e.afterLoc=t[3]),this.tryEntries.push(e)}function _(t){var e=t.completion||{};e.type="normal",delete e.arg,t.completion=e}function R(t){this.tryEntries=[{tryLoc:"root"}],t.forEach(b,this),this.reset(!0)}function C(e){if(e||""===e){var n=e[i];if(n)return n.call(e);if("function"==typeof e.next)return e;if(!isNaN(e.length)){var o=-1,u=function n(){for(;++o<e.length;)if(r.call(e,o))return n.value=e[o],n.done=!1,n;return n.value=t,n.done=!0,n};return u.next=u}}throw new TypeError(typeof e+" is not iterable")}return v.prototype=y,o(E,"constructor",{value:y,configurable:!0}),o(y,"constructor",{value:v,configurable:!0}),v.displayName=s(y,c,"GeneratorFunction"),e.isGeneratorFunction=function(t){var e="function"==typeof t&&t.constructor;return!!e&&(e===v||"GeneratorFunction"===(e.displayName||e.name))},e.mark=function(t){return Object.setPrototypeOf?Object.setPrototypeOf(t,y):(t.__proto__=y,s(t,c,"GeneratorFunction")),t.prototype=Object.create(E),t},e.awrap=function(t){return{__await:t}},k(g.prototype),s(g.prototype,a,(function(){return this})),e.AsyncIterator=g,e.async=function(t,n,r,o,u){void 0===u&&(u=Promise);var i=new g(p(t,n,r,o),u);return e.isGeneratorFunction(n)?i:i.next().then((function(t){return t.done?t.value:i.next()}))},k(E),s(E,c,"Generator"),s(E,i,(function(){return this})),s(E,"toString",(function(){return"[object Generator]"})),e.keys=function(t){var e=Object(t),n=[];for(var r in e)n.push(r);return n.reverse(),function t(){for(;n.length;){var r=n.pop();if(r in e)return t.value=r,t.done=!1,t}return t.done=!0,t}},e.values=C,R.prototype={constructor:R,reset:function(e){if(this.prev=0,this.next=0,this.sent=this._sent=t,this.done=!1,this.delegate=null,this.method="next",this.arg=t,this.tryEntries.forEach(_),!e)for(var n in this)"t"===n.charAt(0)&&r.call(this,n)&&!isNaN(+n.slice(1))&&(this[n]=t)},stop:function(){this.done=!0;var t=this.tryEntries[0].completion;if("throw"===t.type)throw t.arg;return this.rval},dispatchException:function(e){if(this.done)throw e;var n=this;function o(r,o){return a.type="throw",a.arg=e,n.next=r,o&&(n.method="next",n.arg=t),!!o}for(var u=this.tryEntries.length-1;u>=0;--u){var i=this.tryEntries[u],a=i.completion;if("root"===i.tryLoc)return o("end");if(i.tryLoc<=this.prev){var c=r.call(i,"catchLoc"),s=r.call(i,"finallyLoc");if(c&&s){if(this.prev<i.catchLoc)return o(i.catchLoc,!0);if(this.prev<i.finallyLoc)return o(i.finallyLoc)}else if(c){if(this.prev<i.catchLoc)return o(i.catchLoc,!0)}else{if(!s)throw Error("try statement without catch or finally");if(this.prev<i.finallyLoc)return o(i.finallyLoc)}}}},abrupt:function(t,e){for(var n=this.tryEntries.length-1;n>=0;--n){var o=this.tryEntries[n];if(o.tryLoc<=this.prev&&r.call(o,"finallyLoc")&&this.prev<o.finallyLoc){var u=o;break}}u&&("break"===t||"continue"===t)&&u.tryLoc<=e&&e<=u.finallyLoc&&(u=null);var i=u?u.completion:{};return i.type=t,i.arg=e,u?(this.method="next",this.next=u.finallyLoc,h):this.complete(i)},complete:function(t,e){if("throw"===t.type)throw t.arg;return"break"===t.type||"continue"===t.type?this.next=t.arg:"return"===t.type?(this.rval=this.arg=t.arg,this.method="return",this.next="end"):"normal"===t.type&&e&&(this.next=e),h},finish:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.finallyLoc===t)return this.complete(n.completion,n.afterLoc),_(n),h}},catch:function(t){for(var e=this.tryEntries.length-1;e>=0;--e){var n=this.tryEntries[e];if(n.tryLoc===t){var r=n.completion;if("throw"===r.type){var o=r.arg;_(n)}return o}}throw Error("illegal catch attempt")},delegateYield:function(e,n,r){return this.delegate={iterator:C(e),resultName:n,nextLoc:r},"next"===this.method&&(this.arg=t),h}},e}function N(t){var e=function(t,e){if("object"!=typeof t||!t)return t;var n=t[Symbol.toPrimitive];if(void 0!==n){var r=n.call(t,"string");if("object"!=typeof r)return r;throw new TypeError("@@toPrimitive must return a primitive value.")}return String(t)}(t);return"symbol"==typeof e?e:e+""}function M(t,e){return!!t.involvesToken(e)}function q(t,e){return n.CurrencyAmount.fromFractionalAmount(S(t.currency,e),t.numerator,t.denominator)}function S(t,e){if(e.involvesToken(t))return t;if(e.involvesToken(t.wrapped))return t.wrapped;throw new Error("Expected currency "+t.symbol+" to be either "+e.token0.symbol+" or "+e.token1.symbol)}k.INTERFACE=new o.Interface(s.abi);var L=function(){return _((function(t,e,n){this._midPrice=null,t.length>0||u(!1);var r=t[0].chainId;t.every((function(t){return t.chainId===r}))||u(!1),this.pathInput=S(e,t[0]),this.pathOutput=S(n,t[t.length-1]),t[0].involvesToken(this.pathInput)||u(!1),t[t.length-1].involvesToken(n.wrapped)||u(!1);var o=[this.pathInput];t[0].token0.equals(this.pathInput)?o.push(t[0].token1):o.push(t[0].token0);for(var i=1;i<t.length;i++){var a=t[i],c=o[i],s=a.token0.wrapped.equals(c.wrapped)?a.token1:a.token0;M(a,c)||u(!1),o.push(s)}this.pools=t,this.path=o,this.input=e,this.output=null!=n?n:o[o.length-1]}),[{key:"chainId",get:function(){return this.pools[0].chainId}},{key:"midPrice",get:function(){if(null!==this._midPrice)return this._midPrice;var t=this.pools.slice(1).reduce((function(t,e){var n=t.price;return t.nextInput.equals(e.token0)?{nextInput:e.token1,price:n.multiply(e.token0Price.asFraction)}:{nextInput:e.token0,price:n.multiply(e.token1Price.asFraction)}}),this.pools[0].token0.equals(this.pathInput)?{nextInput:this.pools[0].token1,price:this.pools[0].token0Price.asFraction}:{nextInput:this.pools[0].token0,price:this.pools[0].token1Price.asFraction}).price;return this._midPrice=new n.Price(this.input,this.output,t.denominator,t.numerator)}}])}();function D(t,e){return t.inputAmount.currency.equals(e.inputAmount.currency)||u(!1),t.outputAmount.currency.equals(e.outputAmount.currency)||u(!1),t.outputAmount.equalTo(e.outputAmount)?t.inputAmount.equalTo(e.inputAmount)?t.swaps.reduce((function(t,e){return t+e.route.path.length}),0)-e.swaps.reduce((function(t,e){return t+e.route.path.length}),0):t.inputAmount.lessThan(e.inputAmount)?-1:1:t.outputAmount.lessThan(e.outputAmount)?1:-1}var U,H=function(){function t(t){var e=t.routes,r=t.tradeType,o=e[0].inputAmount.currency,i=e[0].outputAmount.currency;e.every((function(t){return o.wrapped.equals(t.route.input.wrapped)}))||u(!1),e.every((function(t){return i.wrapped.equals(t.route.output.wrapped)}))||u(!1);for(var c,s=e.map((function(t){return t.route.pools.length})).reduce((function(t,e){return t+e}),0),p=new Set,d=R(e);!(c=d()).done;)for(var m,f=R(c.value.route.pools);!(m=f()).done;){var h=m.value;if(h instanceof a.Pool)p.add(a.Pool.getAddress(h.token0,h.token1,h.fee));else{if(!(h instanceof l.Pair))throw new Error("Unexpected pool type in route when constructing trade object");p.add(l.Pair.getAddress(h.token0,h.token1))}}s!==p.size&&u(!1),r!==n.TradeType.EXACT_INPUT&&u(!1),this.swaps=e,this.tradeType=r}t.fromRoute=function(){var e=P(F().mark((function e(r,o,i){var a,c,s,p,l;return F().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:a=new Array(r.path.length),i!==n.TradeType.EXACT_INPUT&&u(!1),o.currency.equals(r.input)||u(!1),a[0]=q(o,r.pools[0]),p=0;case 5:if(!(p<r.path.length-1)){e.next=15;break}return l=r.pools[p],e.next=9,l.getOutputAmount(q(a[p],l));case 9:a[p+1]=e.sent[0];case 12:p++,e.next=5;break;case 15:return c=n.CurrencyAmount.fromFractionalAmount(r.input,o.numerator,o.denominator),s=n.CurrencyAmount.fromFractionalAmount(r.output,a[a.length-1].numerator,a[a.length-1].denominator),e.abrupt("return",new t({routes:[{inputAmount:c,outputAmount:s,route:r}],tradeType:i}));case 18:case"end":return e.stop()}}),e)})));return function(t,n,r){return e.apply(this,arguments)}}(),t.fromRoutes=function(){var e=P(F().mark((function e(r,o){var i,a,c,s,p,l,d,m,f,h,A;return F().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:i=[],o!==n.TradeType.EXACT_INPUT&&u(!1),a=R(r);case 3:if((c=a()).done){e.next=26;break}p=(s=c.value).route,l=s.amount,d=new Array(p.path.length),m=void 0,f=void 0,l.currency.equals(p.input)||u(!1),m=n.CurrencyAmount.fromFractionalAmount(p.input,l.numerator,l.denominator),d[0]=n.CurrencyAmount.fromFractionalAmount(p.pathInput,l.numerator,l.denominator),h=0;case 12:if(!(h<p.path.length-1)){e.next=22;break}return A=p.pools[h],e.next=16,A.getOutputAmount(q(d[h],A));case 16:d[h+1]=e.sent[0];case 19:h++,e.next=12;break;case 22:f=n.CurrencyAmount.fromFractionalAmount(p.output,d[d.length-1].numerator,d[d.length-1].denominator),i.push({route:p,inputAmount:m,outputAmount:f});case 24:e.next=3;break;case 26:return e.abrupt("return",new t({routes:i,tradeType:o}));case 27:case"end":return e.stop()}}),e)})));return function(t,n){return e.apply(this,arguments)}}(),t.createUncheckedTrade=function(e){return new t(C({},e,{routes:[{inputAmount:e.inputAmount,outputAmount:e.outputAmount,route:e.route}]}))},t.createUncheckedTradeWithMultipleRoutes=function(e){return new t(e)};var e=t.prototype;return e.minimumAmountOut=function(t,e){void 0===e&&(e=this.outputAmount),t.lessThan(h)&&u(!1);var r=new n.Fraction(A).add(t).invert().multiply(e.quotient).quotient;return n.CurrencyAmount.fromRawAmount(e.currency,r)},e.maximumAmountIn=function(t,e){return void 0===e&&(e=this.inputAmount),t.lessThan(h)&&u(!1),e},e.worstExecutionPrice=function(t){return new n.Price(this.inputAmount.currency,this.outputAmount.currency,this.maximumAmountIn(t).quotient,this.minimumAmountOut(t).quotient)},t.bestTradeExactIn=function(){var e=P(F().mark((function e(r,o,i,a,c,s,p){var d,m,f,A,v,y,w,T,x,E;return F().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:f=void 0===(m=(d=void 0===a?{}:a).maxNumResults)?3:m,v=void 0===(A=d.maxHops)?3:A,void 0===c&&(c=[]),void 0===s&&(s=o),void 0===p&&(p=[]),r.length>0||u(!1),v>0||u(!1),o===s||c.length>0||u(!1),y=0;case 8:if(!(y<r.length)){e.next=48;break}if((w=r[y]).token0.equals((T=s.wrapped).currency)||w.token1.equals(T.currency)){e.next=13;break}return e.abrupt("continue",45);case 13:if(!(w instanceof l.Pair)){e.next=16;break}if(!w.reserve0.equalTo(h)&&!w.reserve1.equalTo(h)){e.next=16;break}return e.abrupt("continue",45);case 16:return x=void 0,e.prev=17,e.next=21,w.getOutputAmount(T.wrapped);case 21:x=e.sent[0],e.next=30;break;case 25:if(e.prev=25,e.t0=e.catch(17),!e.t0.isInsufficientInputAmountError){e.next=29;break}return e.abrupt("continue",45);case 29:throw e.t0;case 30:if(!x.currency.wrapped.equals(i.wrapped)){e.next=41;break}return e.t1=n.sortedInsert,e.t2=p,e.next=35,t.fromRoute(new L([].concat(c,[w]),o.currency,i),o,n.TradeType.EXACT_INPUT);case 35:e.t3=e.sent,e.t4=f,e.t5=D,(0,e.t1)(e.t2,e.t3,e.t4,e.t5),e.next=45;break;case 41:if(!(v>1&&r.length>1)){e.next=45;break}return E=r.slice(0,y).concat(r.slice(y+1,r.length)),e.next=45,t.bestTradeExactIn(E,o,i,{maxNumResults:f,maxHops:v-1},[].concat(c,[w]),x,p);case 45:y++,e.next=8;break;case 48:return e.abrupt("return",p);case 49:case"end":return e.stop()}}),e,null,[[17,25]])})));return function(t,n,r,o,u,i,a){return e.apply(this,arguments)}}(),_(t,[{key:"route",get:function(){return 1!==this.swaps.length&&u(!1),this.swaps[0].route}},{key:"inputAmount",get:function(){if(this._inputAmount)return this._inputAmount;var t=this.swaps[0].inputAmount.currency,e=this.swaps.map((function(t){return t.inputAmount})).reduce((function(t,e){return t.add(e)}),n.CurrencyAmount.fromRawAmount(t,0));return this._inputAmount=e,this._inputAmount}},{key:"outputAmount",get:function(){if(this._outputAmount)return this._outputAmount;var t=this.swaps[0].outputAmount.currency;return this._outputAmount=this.swaps.map((function(t){return t.outputAmount})).reduce((function(t,e){return t.add(e)}),n.CurrencyAmount.fromRawAmount(t,0)),this._outputAmount}},{key:"executionPrice",get:function(){var t;return null!=(t=this._executionPrice)?t:this._executionPrice=new n.Price(this.inputAmount.currency,this.outputAmount.currency,this.inputAmount.quotient,this.outputAmount.quotient)}},{key:"priceImpact",get:function(){if(this._priceImpact)return this._priceImpact;for(var t,e=n.CurrencyAmount.fromRawAmount(this.outputAmount.currency,0),r=R(this.swaps);!(t=r()).done;){var o=t.value;e=e.add(o.route.midPrice.quote(o.inputAmount))}var u=e.subtract(this.outputAmount).divide(e);return this._priceImpact=new n.Percent(u.numerator,u.denominator),this._priceImpact}}])}();(U=exports.Protocol||(exports.Protocol={})).Classic="Classic",U.CL="CL",U.MIXED="MIXED";var j=function(t){function e(e){var n;return(n=t.call(this,e.pairs,e.input,e.output)||this).protocol=exports.Protocol.Classic,n.pools=n.pairs,n}return O(e,t),e}(l.Route),X=function(t){function e(e){var n;return(n=t.call(this,e.pools,e.input,e.output)||this).protocol=exports.Protocol.CL,n.path=e.tokenPath,n}return O(e,t),e}(a.Route),B=function(t){function e(e){var n;return(n=t.call(this,e.pools,e.input,e.output)||this).protocol=exports.Protocol.MIXED,n}return O(e,t),e}(L),Z=function(){function t(t){var e=t.classicRoutes,n=void 0===e?[]:e,r=t.clRoutes,o=void 0===r?[]:r,i=t.mixedRoutes,c=void 0===i?[]:i,s=t.tradeType;this.swaps=[],this.routes=[];for(var p,d=R(n);!(p=d()).done;){var m=p.value,f=m.inputAmount,h=m.outputAmount,A=new j(m.routeclassic);this.routes.push(A),this.swaps.push({route:A,inputAmount:f,outputAmount:h})}for(var v,y=R(o);!(v=y()).done;){var w=v.value,T=w.inputAmount,x=w.outputAmount,E=new X(w.routecl);this.routes.push(E),this.swaps.push({route:E,inputAmount:T,outputAmount:x})}for(var k,g=R(c);!(k=g()).done;){var I=k.value,P=I.inputAmount,b=I.outputAmount,_=new B(I.mixedRoute);this.routes.push(_),this.swaps.push({route:_,inputAmount:P,outputAmount:b})}if(0===this.swaps.length)throw new Error("No routes provided when calling Trade constructor");this.tradeType=s;var C=this.swaps[0].inputAmount.currency,O=this.swaps[0].outputAmount.currency;this.swaps.every((function(t){return C.wrapped.equals(t.route.input.wrapped)}))||u(!1),this.swaps.every((function(t){return O.wrapped.equals(t.route.output.wrapped)}))||u(!1);for(var F,N=this.swaps.map((function(t){return t.route.pools.length})).reduce((function(t,e){return t+e}),0),M=new Set,q=R(this.swaps);!(F=q()).done;)for(var S,L=R(F.value.route.pools);!(S=L()).done;){var D=S.value;if(D instanceof a.Pool)M.add(a.Pool.getAddress(D.token0,D.token1,D.fee));else{if(!(D instanceof l.Pair))throw new Error("Unexpected pool type in route when constructing trade object");M.add(l.Pair.getAddress(D.token0,D.token1))}}N!==M.size&&u(!1)}var e=t.prototype;return e.minimumAmountOut=function(t,e){if(void 0===e&&(e=this.outputAmount),t.lessThan(h)&&u(!1),this.tradeType===n.TradeType.EXACT_OUTPUT)return e;var r=new n.Fraction(A).add(t).invert().multiply(e.quotient).quotient;return n.CurrencyAmount.fromRawAmount(e.currency,r)},e.maximumAmountIn=function(t,e){if(void 0===e&&(e=this.inputAmount),t.lessThan(h)&&u(!1),this.tradeType===n.TradeType.EXACT_INPUT)return e;var r=new n.Fraction(A).add(t).multiply(e.quotient).quotient;return n.CurrencyAmount.fromRawAmount(e.currency,r)},e.worstExecutionPrice=function(t){return new n.Price(this.inputAmount.currency,this.outputAmount.currency,this.maximumAmountIn(t).quotient,this.minimumAmountOut(t).quotient)},t.fromRoutes=function(){var e=P(F().mark((function e(n,r,o,u){var i,c,s,p,d,m,f,h,A,v,y,w,T,x,E,k,g,I,P,b;return F().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:for(i=[],c=[],s=[],p=R(n);!(d=p()).done;)h=new l.Trade(f=(m=d.value).routeclassic,m.amount,o),i.push({routeclassic:f,inputAmount:h.inputAmount,outputAmount:h.outputAmount});A=R(r);case 5:if((v=A()).done){e.next=14;break}return w=(y=v.value).routecl,T=y.amount,e.next=9,a.Trade.fromRoute(w,T,o);case 9:c.push({routecl:w,inputAmount:(x=e.sent).inputAmount,outputAmount:x.outputAmount});case 12:e.next=5;break;case 14:if(!u){e.next=25;break}E=R(u);case 16:if((k=E()).done){e.next=25;break}return I=(g=k.value).mixedRoute,P=g.amount,e.next=20,H.fromRoute(I,P,o);case 20:s.push({mixedRoute:I,inputAmount:(b=e.sent).inputAmount,outputAmount:b.outputAmount});case 23:e.next=16;break;case 25:return e.abrupt("return",new t({classicRoutes:i,clRoutes:c,mixedRoutes:s,tradeType:o}));case 26:case"end":return e.stop()}}),e)})));return function(t,n,r,o){return e.apply(this,arguments)}}(),t.fromRoute=function(){var e=P(F().mark((function e(n,r,o){var u,i,c,s,p,d;return F().wrap((function(e){for(;;)switch(e.prev=e.next){case 0:if(u=[],i=[],c=[],!(n instanceof l.Route)){e.next=9;break}s=new l.Trade(n,r,o),u=[{routeclassic:n,inputAmount:s.inputAmount,outputAmount:s.outputAmount}],e.next=26;break;case 9:if(!(n instanceof a.Route)){e.next=17;break}return e.next=12,a.Trade.fromRoute(n,r,o);case 12:i=[{routecl:n,inputAmount:(p=e.sent).inputAmount,outputAmount:p.outputAmount}],e.next=26;break;case 17:if(!(n instanceof L)){e.next=25;break}return e.next=20,H.fromRoute(n,r,o);case 20:c=[{mixedRoute:n,inputAmount:(d=e.sent).inputAmount,outputAmount:d.outputAmount}],e.next=26;break;case 25:throw new Error("Invalid route type");case 26:return e.abrupt("return",new t({classicRoutes:u,clRoutes:i,mixedRoutes:c,tradeType:o}));case 27:case"end":return e.stop()}}),e)})));return function(t,n,r){return e.apply(this,arguments)}}(),_(t,[{key:"inputAmount",get:function(){if(this._inputAmount)return this._inputAmount;var t=this.swaps[0].inputAmount.currency;return this._inputAmount=this.swaps.map((function(t){return t.inputAmount})).reduce((function(t,e){return t.add(e)}),n.CurrencyAmount.fromRawAmount(t,0)),this._inputAmount}},{key:"outputAmount",get:function(){if(this._outputAmount)return this._outputAmount;var t=this.swaps[0].outputAmount.currency;return this._outputAmount=this.swaps.map((function(t){return t.outputAmount})).reduce((function(t,e){return t.add(e)}),n.CurrencyAmount.fromRawAmount(t,0)),this._outputAmount}},{key:"executionPrice",get:function(){var t;return null!=(t=this._executionPrice)?t:this._executionPrice=new n.Price(this.inputAmount.currency,this.outputAmount.currency,this.inputAmount.quotient,this.outputAmount.quotient)}},{key:"inputTax",get:function(){var t=this.inputAmount.currency;return t.isNative||!t.wrapped.sellFeeBps?v:new n.Percent(t.wrapped.sellFeeBps.toNumber(),1e4)}},{key:"outputTax",get:function(){var t=this.outputAmount.currency;return t.isNative||!t.wrapped.buyFeeBps?v:new n.Percent(t.wrapped.buyFeeBps.toNumber(),1e4)}},{key:"priceImpact",get:function(){if(this._priceImpact)return this._priceImpact;if(this.outputTax.equalTo(y))return v;for(var t,e=n.CurrencyAmount.fromRawAmount(this.outputAmount.currency,0),r=R(this.swaps);!(t=r()).done;){var o=t.value,u=o.route.midPrice,i=o.inputAmount.multiply(new n.Fraction(A).subtract(this.inputTax));e=e.add(u.quote(i))}if(e.equalTo(h))return v;var a=this.outputAmount.divide(new n.Fraction(A).subtract(this.outputTax)),c=e.subtract(a).divide(e);return this._priceImpact=new n.Percent(c.numerator,c.denominator),this._priceImpact}}])}();function W(t){var e=t.pools.reduce((function(t,e,n){var r=t.inputToken,o=t.path,u=t.types,i=e.token0.equals(r)?e.token1:e.token0;return 0===n?{inputToken:i,types:["address","uint24","address"],path:[r.wrapped.address,e instanceof a.Pool?e.fee:1<<23,i.wrapped.address]}:{inputToken:i,types:[].concat(u,["uint24","address"]),path:[].concat(o,[e instanceof a.Pool?e.fee:1<<23,i.wrapped.address])}}),{inputToken:t.input,path:[],types:[]});return d.pack(e.types,e.path)}var G=function(t){for(var e=[],n=0,r=0;r<t.pools.length;)(t.pools[n]instanceof a.Pool&&!(t.pools[r]instanceof a.Pool)||t.pools[n]instanceof l.Pair&&!(t.pools[r]instanceof l.Pair))&&(e.push(t.pools.slice(n,r)),n=r),++r===t.pools.length&&e.push(t.pools.slice(n,r));return e},Q=function(t,e){return t.reduce((function(t,e){var n=t.inputToken;if(!e.involvesToken(n))throw new Error("PATH");return{inputToken:e.token0.equals(n)?e.token1:e.token0}}),{inputToken:e}).inputToken},z=r.BigInt(0),Y=new n.Percent(r.BigInt(50),r.BigInt(100)),$=function(){function t(){}return t.encodeClassicSwap=function(e,r,o,u){var i=a.toHex(e.maximumAmountIn(r.slippageTolerance).quotient),c=a.toHex(e.minimumAmountOut(r.slippageTolerance).quotient),s=e.route.path.map((function(t){return t.address})),p=o?f:void 0===r.recipient?m:n.validateAndParseAddress(r.recipient);return e.tradeType===n.TradeType.EXACT_INPUT?t.INTERFACE.encodeFunctionData("swapExactTokensForTokens",[i,u?0:c,s,p]):t.INTERFACE.encodeFunctionData("swapTokensForExactTokens",[c,i,s,p])},t.encodeCLSwap=function(e,r,o,u){for(var i,c=[],s=R(e.swaps);!(i=s()).done;){var p=i.value,l=p.route,d=p.outputAmount,h=a.toHex(e.maximumAmountIn(r.slippageTolerance,p.inputAmount).quotient),A=a.toHex(e.minimumAmountOut(r.slippageTolerance,d).quotient),v=1===l.pools.length,y=o?f:void 0===r.recipient?m:n.validateAndParseAddress(r.recipient);if(v)c.push(e.tradeType===n.TradeType.EXACT_INPUT?t.INTERFACE.encodeFunctionData("exactInputSingle",[{tokenIn:l.tokenPath[0].address,tokenOut:l.tokenPath[1].address,fee:l.pools[0].fee,recipient:y,amountIn:h,amountOutMinimum:u?0:A,sqrtPriceLimitX96:0}]):t.INTERFACE.encodeFunctionData("exactOutputSingle",[{tokenIn:l.tokenPath[0].address,tokenOut:l.tokenPath[1].address,fee:l.pools[0].fee,recipient:y,amountOut:A,amountInMaximum:h,sqrtPriceLimitX96:0}]));else{var w=a.encodeRouteToPath(l,e.tradeType===n.TradeType.EXACT_OUTPUT);c.push(e.tradeType===n.TradeType.EXACT_INPUT?t.INTERFACE.encodeFunctionData("exactInput",[{path:w,recipient:y,amountIn:h,amountOutMinimum:u?0:A}]):t.INTERFACE.encodeFunctionData("exactOutput",[{path:w,recipient:y,amountOut:A,amountInMaximum:h}]))}}return c},t.encodeMixedRouteSwap=function(e,r,o,i){var c=[];e.tradeType!==n.TradeType.EXACT_INPUT&&u(!1);for(var s,p=function(){var u=s.value,p=u.route,l=u.outputAmount,d=a.toHex(e.maximumAmountIn(r.slippageTolerance,u.inputAmount).quotient),h=a.toHex(e.minimumAmountOut(r.slippageTolerance,l).quotient),A=1===p.pools.length,v=o?f:void 0===r.recipient?m:n.validateAndParseAddress(r.recipient),y=function(t){return t.pools.every((function(t){return t instanceof a.Pool}))};if(A)if(y(p))c.push(t.INTERFACE.encodeFunctionData("exactInputSingle",[{tokenIn:p.path[0].wrapped.address,tokenOut:p.path[1].wrapped.address,fee:p.pools[0].fee,recipient:v,amountIn:d,amountOutMinimum:i?0:h,sqrtPriceLimitX96:0}]));else{var w=p.path.map((function(t){return t.wrapped.address}));c.push(t.INTERFACE.encodeFunctionData("swapExactTokensForTokens",[d,i?0:h,w,v]))}else for(var T,x=G(p),E=function(t){return t===x.length-1},k=p.input.wrapped,g=0;g<x.length;g++){var I=x[g];T=Q(I,k);var P=new L([].concat(I),I[0].token0.equals(k)?I[0].token0:I[0].token1,T),b=new B(P);if(k=T.wrapped,y(b)){var _={path:W(b),recipient:E(g)?v:f,amountIn:0===g?d:0,amountOutMinimum:E(g)?h:0};c.push(t.INTERFACE.encodeFunctionData("exactInput",[_]))}else{var R=[0===g?d:0,E(g)?h:0,b.path.map((function(t){return t.wrapped.address})),E(g)?v:f];c.push(t.INTERFACE.encodeFunctionData("swapExactTokensForTokens",R))}}},l=R(e.swaps);!(s=l()).done;)p();return c},t.encodeSwaps=function(e,r,o){if(e instanceof Z){e.swaps.every((function(t){return t.route.protocol===exports.Protocol.CL||t.route.protocol===exports.Protocol.Classic||t.route.protocol===exports.Protocol.MIXED}))||u(!1);for(var i,c=[],s=R(e.swaps);!(i=s()).done;){var p=i.value,d=p.route,m=p.inputAmount,f=p.outputAmount;if(d.protocol===exports.Protocol.Classic)c.push(new l.Trade(d,e.tradeType===n.TradeType.EXACT_INPUT?m:f,e.tradeType));else if(d.protocol===exports.Protocol.CL)c.push(a.Trade.createUncheckedTrade({route:d,inputAmount:m,outputAmount:f,tradeType:e.tradeType}));else{if(d.protocol!==exports.Protocol.MIXED)throw new Error("UNSUPPORTED_TRADE_PROTOCOL");c.push(H.createUncheckedTrade({route:d,inputAmount:m,outputAmount:f,tradeType:e.tradeType}))}}e=c}Array.isArray(e)||(e=[e]);var h=e.reduce((function(t,e){return t+(e instanceof a.Trade||e instanceof H?e.swaps.length:1)}),0),A=e[0];e.every((function(t){return t.inputAmount.currency.equals(A.inputAmount.currency)}))||u(!1),e.every((function(t){return t.outputAmount.currency.equals(A.outputAmount.currency)}))||u(!1),e.every((function(t){return t.tradeType===A.tradeType}))||u(!1);var v=[],y=A.inputAmount.currency.isNative,w=A.outputAmount.currency.isNative,T=A.tradeType===n.TradeType.EXACT_INPUT&&h>2,x=w||!!r.fee||!!o||T;r.inputTokenPermit&&(A.inputAmount.currency.isToken||u(!1),v.push(a.SelfPermit.encodePermit(A.inputAmount.currency,r.inputTokenPermit)));for(var E,k=R(e);!(E=k()).done;){var g=E.value;if(g instanceof l.Trade)v.push(t.encodeClassicSwap(g,r,x,T));else if(g instanceof a.Trade)for(var I,P=R(t.encodeCLSwap(g,r,x,T));!(I=P()).done;)v.push(I.value);else{if(!(g instanceof H))throw new Error("Unsupported trade object");for(var b,_=R(t.encodeMixedRouteSwap(g,r,x,T));!(b=_()).done;)v.push(b.value)}}var C=n.CurrencyAmount.fromRawAmount(A.inputAmount.currency,0),O=n.CurrencyAmount.fromRawAmount(A.outputAmount.currency,0),F=e.reduce((function(t,e){return t.add(e.minimumAmountOut(r.slippageTolerance))}),O),N=e.reduce((function(t,e){return t.add(e.outputAmount)}),O),M=e.reduce((function(t,e){return t.add(e.maximumAmountIn(r.slippageTolerance))}),C);return{calldatas:v,sampleTrade:A,routerMustCustody:x,inputIsNative:y,outputIsNative:w,totalAmountIn:M,minimumAmountOut:F,quoteAmountOut:N}},t.swapCallParameters=function(e,r){var o=t.encodeSwaps(e,r),u=o.calldatas,i=o.sampleTrade,c=o.inputIsNative,s=o.totalAmountIn,p=o.minimumAmountOut;return o.routerMustCustody&&u.push(o.outputIsNative?k.encodeUnwrapSAMB(p.quotient,r.recipient,r.fee):k.encodeSweepToken(i.outputAmount.currency.wrapped,p.quotient,r.recipient,r.fee)),c&&(i.tradeType===n.TradeType.EXACT_OUTPUT||t.riskOfPartialFill(e))&&u.push(a.Payments.encodeRefundAMB()),{calldata:x.encodeMulticall(u,r.deadlineOrPreviousBlockhash),value:a.toHex(c?s.quotient:z)}},t.swapAndAddCallParameters=function(e,r,o,i,c,s){var p=t.encodeSwaps(e,r,!0),l=p.calldatas,d=p.inputIsNative,m=p.outputIsNative,f=p.sampleTrade,h=p.totalAmountIn,A=p.quoteAmountOut,v=p.minimumAmountOut;r.outputTokenPermit&&(A.currency.isToken||u(!1),l.push(a.SelfPermit.encodePermit(A.currency,r.outputTokenPermit)));var y=f.route.chainId,w=o.pool.token0.wrapped.address===h.currency.wrapped.address,E=t.getPositionAmounts(o,w),g=E.positionAmountIn,I=E.positionAmountOut,P=d?n.SAMB[y]:g.currency.wrapped,b=m?n.SAMB[y]:I.currency.wrapped,_=I.subtract(A.wrapped);_.greaterThan(n.CurrencyAmount.fromRawAmount(I.currency,0))&&l.push(m?k.encodeWrapAMB(_.quotient):k.encodePull(b,_.quotient)),l.push(d?k.encodeWrapAMB(g.quotient):k.encodePull(P,g.quotient)),c!==exports.ApprovalTypes.NOT_REQUIRED&&l.push(T.encodeApprove(P,c)),s!==exports.ApprovalTypes.NOT_REQUIRED&&l.push(T.encodeApprove(b,s));var R,C=a.Position.fromAmounts({pool:o.pool,tickLower:o.tickLower,tickUpper:o.tickUpper,amount0:w?o.amount0.quotient.toString():v.quotient.toString(),amount1:w?v.quotient.toString():o.amount1.quotient.toString(),useFullPrecision:!1});return l.push(T.encodeAddLiquidity(o,C,i,r.slippageTolerance)),l.push(d?k.encodeUnwrapSAMB(z):k.encodeSweepToken(P,z)),l.push(m?k.encodeUnwrapSAMB(z):k.encodeSweepToken(b,z)),R=d?h.wrapped.add(g.wrapped).quotient:m?_.quotient:z,{calldata:x.encodeMulticall(l,r.deadlineOrPreviousBlockhash),value:R.toString()}},t.riskOfPartialFill=function(e){return Array.isArray(e)?e.some((function(e){return t.clTradeWithHighPriceImpact(e)})):t.clTradeWithHighPriceImpact(e)},t.clTradeWithHighPriceImpact=function(t){return!(t instanceof l.Trade)&&t.priceImpact.greaterThan(Y)},t.getPositionAmounts=function(t,e){var r=t.mintAmounts,o=r.amount1,u=n.CurrencyAmount.fromRawAmount(t.pool.token0,r.amount0),i=n.CurrencyAmount.fromRawAmount(t.pool.token1,o),a=e?[u,i]:[i,u];return{positionAmountIn:a[0],positionAmountOut:a[1]}},t}();$.INTERFACE=new o.Interface(p.abi),exports.ADDRESS_THIS=f,exports.ADDRESS_ZERO="0x0000000000000000000000000000000000000000",exports.ApproveAndCall=T,exports.MIXED_QUOTER_CLASSIC_CLASSIC_FEE_PATH_PLACEHOLDER=32,exports.MIXED_QUOTER_CLASSIC_CL_FEE_PATH_PLACEHOLDER=3<<20,exports.MIXED_QUOTER_V1_CLASSIC_FEE_PATH_PLACEHOLDER=1<<23,exports.MSG_SENDER=m,exports.MixedRoute=B,exports.MixedRouteSDK=L,exports.MixedRouteTrade=H,exports.MulticallExtended=x,exports.ONE=A,exports.ONE_HUNDRED_PERCENT=y,exports.PaymentsExtended=k,exports.RouteCL=X,exports.RouteClassic=j,exports.SwapRouter=$,exports.Trade=Z,exports.ZERO=h,exports.ZERO_PERCENT=v,exports.amountWithPathCurrency=q,exports.encodeMixedRouteToPath=W,exports.getOutputOfPools=Q,exports.getPathCurrency=S,exports.isMint=w,exports.partitionMixedRouteByProtocol=G,exports.tradeComparator=D;
//# sourceMappingURL=astra-router-sdk.cjs.production.min.js.map