UNPKG

@pollum-io/pegasys-sdk

Version:

🛠 An SDK for building applications on top of Pegasys.

3 lines (2 loc) • 23.6 kB
"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,r,o,i,u=t(require("jsbi")),a=t(require("tiny-invariant")),s=(t(require("tiny-warning")),require("@ethersproject/address")),c=t(require("big.js")),d=t(require("toformat")),l=t(require("decimal.js-light")),p=require("@ethersproject/solidity"),f=require("@ethersproject/contracts"),h=require("@ethersproject/networks"),m=require("@ethersproject/providers"),v=t(require("@pollum-io/pegasys-protocol/artifacts/contracts/pegasys-core/PegasysPair.sol/PegasysPair.json"));(r=exports.ChainId||(exports.ChainId={}))[r.TANENBAUM=5700]="TANENBAUM",r[r.NEVM=57]="NEVM",r[r.ROLLUX=2814]="ROLLUX",(o=exports.TradeType||(exports.TradeType={}))[o.EXACT_INPUT=0]="EXACT_INPUT",o[o.EXACT_OUTPUT=1]="EXACT_OUTPUT",(i=exports.Rounding||(exports.Rounding={}))[i.ROUND_DOWN=0]="ROUND_DOWN",i[i.ROUND_HALF_UP=1]="ROUND_HALF_UP",i[i.ROUND_UP=2]="ROUND_UP";var y,w=((e={})[exports.ChainId.TANENBAUM]="0x577CCB2eF53F56AC9b16E0Db6550a6bAe6ba27bc",e[exports.ChainId.NEVM]="0x7Bbbb6abaD521dE677aBe089C85b29e3b2021496",e[exports.ChainId.ROLLUX]="0x1411121972e34093a0b190be37ea8d3310b094b4",e),g="0x85c9b07c539b322c33da730d88df8396983c35a411da73d3d6c2278474890833",T=u.BigInt(1e3),k=u.BigInt(0),x=u.BigInt(1),A=u.BigInt(2),I=u.BigInt(3),b=u.BigInt(5),O=u.BigInt(10),q=u.BigInt(100),P=u.BigInt(997),S=u.BigInt(1e3);!function(t){t.uint8="uint8",t.uint256="uint256"}(y||(y={}));var U=((n={})[y.uint8]=u.BigInt("0xff"),n[y.uint256]=u.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),n);function E(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,r.key,r)}}function N(t,e,n){return e&&E(t.prototype,e),n&&E(t,n),Object.defineProperty(t,"prototype",{writable:!1}),t}function R(){return(R=Object.assign||function(t){for(var e=1;e<arguments.length;e++){var n=arguments[e];for(var r in n)Object.prototype.hasOwnProperty.call(n,r)&&(t[r]=n[r])}return t}).apply(this,arguments)}function C(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,D(t,e)}function _(t){return(_=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function D(t,e){return(D=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function F(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Boolean.prototype.valueOf.call(Reflect.construct(Boolean,[],(function(){}))),!0}catch(t){return!1}}function B(t,e,n){return(B=F()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&D(o,n.prototype),o}).apply(null,arguments)}function j(t){var e="function"==typeof Map?new Map:void 0;return(j=function(t){if(null===t||-1===Function.toString.call(t).indexOf("[native code]"))return t;if("function"!=typeof t)throw new TypeError("Super expression must either be null or a function");if(void 0!==e){if(e.has(t))return e.get(t);e.set(t,n)}function n(){return B(t,arguments,_(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),D(n,t)})(t)}function L(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function M(t,e){(null==e||e>t.length)&&(e=t.length);for(var n=0,r=new Array(e);n<e;n++)r[n]=t[n];return r}function X(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 M(t,void 0);var n=Object.prototype.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)?M(t,void 0):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.")}var Y="setPrototypeOf"in Object,W=function(t){function e(){var n;return(n=t.call(this)||this).isInsufficientReservesError=!0,n.name=n.constructor.name,Y&&Object.setPrototypeOf(L(n),(this instanceof e?this.constructor:void 0).prototype),n}return C(e,t),e}(j(Error)),H=function(t){function e(){var n;return(n=t.call(this)||this).isInsufficientInputAmountError=!0,n.name=n.constructor.name,Y&&Object.setPrototypeOf(L(n),(this instanceof e?this.constructor:void 0).prototype),n}return C(e,t),e}(j(Error));function V(t,e){u.greaterThanOrEqual(t,k)||a(!1),u.lessThanOrEqual(t,U[e])||a(!1)}function J(t){try{return s.getAddress(t)}catch(t){a(!1)}}function Q(t){return t instanceof u?t:u.BigInt("bigint"==typeof t?t.toString():t)}function $(t){V(t,y.uint256);var e,n=k;if(u.greaterThan(t,I))for(n=t,e=u.add(u.divide(t,A),x);u.lessThan(e,n);)n=e,e=u.divide(u.add(u.divide(t,e),e),A);else u.notEqual(t,k)&&(n=x);return n}function z(t,e,n,r){if(n>0||a(!1),t.length<=n||a(!1),0===t.length)return t.push(e),null;var o=t.length===n;if(o&&r(t[t.length-1],e)<=0)return e;for(var i=0,u=t.length;i<u;){var s=i+u>>>1;r(t[s],e)<=0?i=s+1:u=s}return t.splice(i,0,e),o?t.pop():null}var G,K=function(t,e,n){V(u.BigInt(t),y.uint8),this.decimals=t,this.symbol=e,this.name=n},Z=K.NSYS=new K(18,"SYS","Syscoin"),tt=function(t){function e(e,n,r,o,i){var u;return(u=t.call(this,r,o,i)||this).chainId=e,u.address=J(n),u}C(e,t);var n=e.prototype;return n.equals=function(t){return this===t||this.chainId===t.chainId&&this.address===t.address},n.sortsBefore=function(t){return this.chainId!==t.chainId&&a(!1),this.address===t.address&&a(!1),this.address.toLowerCase()<t.address.toLowerCase()},e}(K);function et(t,e){return t instanceof tt&&e instanceof tt?t.equals(e):!(t instanceof tt||e instanceof tt||t!==e)}var nt,rt,ot=((G={})[exports.ChainId.TANENBAUM]=new tt(exports.ChainId.TANENBAUM,"0xa66b2E50c2b805F31712beA422D0D9e7D0Fd0F35",18,"WSYS","Wrapped SYS"),G[exports.ChainId.NEVM]=new tt(exports.ChainId.NEVM,"0xd3e822f3ef011Ca5f17D82C956D952D8d7C3A1BB",18,"WSYS","Wrapped SYS"),G[exports.ChainId.ROLLUX]=new tt(exports.ChainId.ROLLUX,"0x4200000000000000000000000000000000000006",18,"WSYS","Wrapped SYS"),G),it=d(l),ut=d(c),at=((nt={})[exports.Rounding.ROUND_DOWN]=it.ROUND_DOWN,nt[exports.Rounding.ROUND_HALF_UP]=it.ROUND_HALF_UP,nt[exports.Rounding.ROUND_UP]=it.ROUND_UP,nt),st=((rt={})[exports.Rounding.ROUND_DOWN]=0,rt[exports.Rounding.ROUND_HALF_UP]=1,rt[exports.Rounding.ROUND_UP]=3,rt),ct=function(){function t(t,e){void 0===e&&(e=x),this.numerator=Q(t),this.denominator=Q(e)}var e=t.prototype;return e.invert=function(){return new t(this.denominator,this.numerator)},e.add=function(e){var n=e instanceof t?e:new t(Q(e));return u.equal(this.denominator,n.denominator)?new t(u.add(this.numerator,n.numerator),this.denominator):new t(u.add(u.multiply(this.numerator,n.denominator),u.multiply(n.numerator,this.denominator)),u.multiply(this.denominator,n.denominator))},e.subtract=function(e){var n=e instanceof t?e:new t(Q(e));return u.equal(this.denominator,n.denominator)?new t(u.subtract(this.numerator,n.numerator),this.denominator):new t(u.subtract(u.multiply(this.numerator,n.denominator),u.multiply(n.numerator,this.denominator)),u.multiply(this.denominator,n.denominator))},e.lessThan=function(e){var n=e instanceof t?e:new t(Q(e));return u.lessThan(u.multiply(this.numerator,n.denominator),u.multiply(n.numerator,this.denominator))},e.equalTo=function(e){var n=e instanceof t?e:new t(Q(e));return u.equal(u.multiply(this.numerator,n.denominator),u.multiply(n.numerator,this.denominator))},e.greaterThan=function(e){var n=e instanceof t?e:new t(Q(e));return u.greaterThan(u.multiply(this.numerator,n.denominator),u.multiply(n.numerator,this.denominator))},e.multiply=function(e){var n=e instanceof t?e:new t(Q(e));return new t(u.multiply(this.numerator,n.numerator),u.multiply(this.denominator,n.denominator))},e.divide=function(e){var n=e instanceof t?e:new t(Q(e));return new t(u.multiply(this.numerator,n.denominator),u.multiply(this.denominator,n.numerator))},e.toSignificant=function(t,e,n){void 0===e&&(e={groupSeparator:""}),void 0===n&&(n=exports.Rounding.ROUND_HALF_UP),Number.isInteger(t)||a(!1),t>0||a(!1),it.set({precision:t+1,rounding:at[n]});var r=new it(this.numerator.toString()).div(this.denominator.toString()).toSignificantDigits(t);return r.toFormat(r.decimalPlaces(),e)},e.toFixed=function(t,e,n){return void 0===e&&(e={groupSeparator:""}),void 0===n&&(n=exports.Rounding.ROUND_HALF_UP),Number.isInteger(t)||a(!1),t>=0||a(!1),ut.DP=t,ut.RM=st[n],new ut(this.numerator.toString()).div(this.denominator.toString()).toFormat(t,e)},N(t,[{key:"quotient",get:function(){return u.divide(this.numerator,this.denominator)}},{key:"remainder",get:function(){return new t(u.remainder(this.numerator,this.denominator),this.denominator)}}]),t}(),dt=d(c),lt=function(t){function e(e,n){var r,o=Q(n);return V(o,y.uint256),(r=t.call(this,o,u.exponentiate(O,u.BigInt(e.decimals)))||this).currency=e,r}C(e,t),e.ether=function(t){return new e(Z,t)};var n=e.prototype;return n.add=function(t){return et(this.currency,t.currency)||a(!1),new e(this.currency,u.add(this.raw,t.raw))},n.subtract=function(t){return et(this.currency,t.currency)||a(!1),new e(this.currency,u.subtract(this.raw,t.raw))},n.toSignificant=function(e,n,r){return void 0===e&&(e=6),void 0===r&&(r=exports.Rounding.ROUND_DOWN),t.prototype.toSignificant.call(this,e,n,r)},n.toFixed=function(e,n,r){return void 0===e&&(e=this.currency.decimals),void 0===r&&(r=exports.Rounding.ROUND_DOWN),e<=this.currency.decimals||a(!1),t.prototype.toFixed.call(this,e,n,r)},n.toExact=function(t){return void 0===t&&(t={groupSeparator:""}),dt.DP=this.currency.decimals,new dt(this.numerator.toString()).div(this.denominator.toString()).toFormat(t)},N(e,[{key:"raw",get:function(){return this.numerator}}]),e}(ct),pt=function(t){function e(e,n){var r;return(r=t.call(this,e,n)||this).token=e,r}C(e,t);var n=e.prototype;return n.add=function(t){return this.token.equals(t.token)||a(!1),new e(this.token,u.add(this.raw,t.raw))},n.subtract=function(t){return this.token.equals(t.token)||a(!1),new e(this.token,u.subtract(this.raw,t.raw))},e}(lt),ft=function(t){function e(e,n,r,o){var i;return(i=t.call(this,o,r)||this).baseCurrency=e,i.quoteCurrency=n,i.scalar=new ct(u.exponentiate(O,u.BigInt(e.decimals)),u.exponentiate(O,u.BigInt(n.decimals))),i}C(e,t),e.fromRoute=function(t){for(var n,r=[],o=X(t.pairs.entries());!(n=o()).done;){var i=n.value,u=i[1];r.push(t.path[i[0]].equals(u.token0)?new e(u.reserve0.currency,u.reserve1.currency,u.reserve0.raw,u.reserve1.raw):new e(u.reserve1.currency,u.reserve0.currency,u.reserve1.raw,u.reserve0.raw))}return r.slice(1).reduce((function(t,e){return t.multiply(e)}),r[0])};var n=e.prototype;return n.invert=function(){return new e(this.quoteCurrency,this.baseCurrency,this.numerator,this.denominator)},n.multiply=function(n){et(this.quoteCurrency,n.baseCurrency)||a(!1);var r=t.prototype.multiply.call(this,n);return new e(this.baseCurrency,n.quoteCurrency,r.denominator,r.numerator)},n.quote=function(e){return et(e.currency,this.baseCurrency)||a(!1),this.quoteCurrency instanceof tt?new pt(this.quoteCurrency,t.prototype.multiply.call(this,e.raw).quotient):lt.ether(t.prototype.multiply.call(this,e.raw).quotient)},n.toSignificant=function(t,e,n){return void 0===t&&(t=6),this.adjusted.toSignificant(t,e,n)},n.toFixed=function(t,e,n){return void 0===t&&(t=4),this.adjusted.toFixed(t,e,n)},N(e,[{key:"raw",get:function(){return new ct(this.numerator,this.denominator)}},{key:"adjusted",get:function(){return t.prototype.multiply.call(this,this.scalar)}}]),e}(ct),ht={},mt=function(){function t(e,n,r){var o=e.token.sortsBefore(n.token)?[e,n]:[n,e];this.liquidityToken=new tt(o[0].token.chainId,t.getAddress(o[0].token,o[1].token,r),18,"PLP","Pegasys LP Token"),this.tokenAmounts=o}t.getAddress=function(t,e,n){var r,o,i,u,a,c=t.sortsBefore(e)?[t,e]:[e,t];return void 0===(null===(r=ht)||void 0===r||null===(o=r[c[0].address])||void 0===o?void 0:o[c[1].address])&&(ht=R({},ht,((a={})[c[0].address]=R({},null===(i=ht)||void 0===i?void 0:i[c[0].address],((u={})[c[1].address]=s.getCreate2Address(w[n],p.keccak256(["bytes"],[p.pack(["address","address"],[c[0].address,c[1].address])]),g),u)),a))),ht[c[0].address][c[1].address]};var e=t.prototype;return e.involvesToken=function(t){return t.equals(this.token0)||t.equals(this.token1)},e.priceOf=function(t){return this.involvesToken(t)||a(!1),t.equals(this.token0)?this.token0Price:this.token1Price},e.reserveOf=function(t){return this.involvesToken(t)||a(!1),t.equals(this.token0)?this.reserve0:this.reserve1},e.getOutputAmount=function(e,n){if(this.involvesToken(e.token)||a(!1),u.equal(this.reserve0.raw,k)||u.equal(this.reserve1.raw,k))throw new W;var r=this.reserveOf(e.token),o=this.reserveOf(e.token.equals(this.token0)?this.token1:this.token0),i=u.multiply(e.raw,P),s=u.multiply(i,o.raw),c=u.add(u.multiply(r.raw,S),i),d=new pt(e.token.equals(this.token0)?this.token1:this.token0,u.divide(s,c));if(u.equal(d.raw,k))throw new H;return[d,new t(r.add(e),o.subtract(d),n)]},e.getInputAmount=function(e,n){if(this.involvesToken(e.token)||a(!1),u.equal(this.reserve0.raw,k)||u.equal(this.reserve1.raw,k)||u.greaterThanOrEqual(e.raw,this.reserveOf(e.token).raw))throw new W;var r=this.reserveOf(e.token),o=this.reserveOf(e.token.equals(this.token0)?this.token1:this.token0),i=u.multiply(u.multiply(o.raw,e.raw),S),s=u.multiply(u.subtract(r.raw,e.raw),P),c=new pt(e.token.equals(this.token0)?this.token1:this.token0,u.add(u.divide(i,s),x));return[c,new t(o.add(c),r.subtract(e),n)]},e.getLiquidityMinted=function(t,e,n){t.token.equals(this.liquidityToken)||a(!1);var r,o=e.token.sortsBefore(n.token)?[e,n]:[n,e];if(o[0].token.equals(this.token0)&&o[1].token.equals(this.token1)||a(!1),u.equal(t.raw,k))r=u.subtract($(u.multiply(o[0].raw,o[1].raw)),T);else{var i=u.divide(u.multiply(o[0].raw,t.raw),this.reserve0.raw),s=u.divide(u.multiply(o[1].raw,t.raw),this.reserve1.raw);r=u.lessThanOrEqual(i,s)?i:s}if(!u.greaterThan(r,k))throw new H;return new pt(this.liquidityToken,r)},e.getLiquidityValue=function(t,e,n,r,o){var i;if(void 0===r&&(r=!1),this.involvesToken(t)||a(!1),e.token.equals(this.liquidityToken)||a(!1),n.token.equals(this.liquidityToken)||a(!1),u.lessThanOrEqual(n.raw,e.raw)||a(!1),r){o||a(!1);var s=Q(o);if(u.equal(s,k))i=e;else{var c=$(u.multiply(this.reserve0.raw,this.reserve1.raw)),d=$(s);if(u.greaterThan(c,d)){var l=u.multiply(e.raw,u.subtract(c,d)),p=u.add(u.multiply(c,b),d),f=u.divide(l,p);i=e.add(new pt(this.liquidityToken,f))}else i=e}}else i=e;return new pt(t,u.divide(u.multiply(n.raw,this.reserveOf(t).raw),i.raw))},N(t,[{key:"token0Price",get:function(){return new ft(this.token0,this.token1,this.tokenAmounts[0].raw,this.tokenAmounts[1].raw)}},{key:"token1Price",get:function(){return new ft(this.token1,this.token0,this.tokenAmounts[1].raw,this.tokenAmounts[0].raw)}},{key:"chainId",get:function(){return this.token0.chainId}},{key:"token0",get:function(){return this.tokenAmounts[0].token}},{key:"token1",get:function(){return this.tokenAmounts[1].token}},{key:"reserve0",get:function(){return this.tokenAmounts[0]}},{key:"reserve1",get:function(){return this.tokenAmounts[1]}}]),t}(),vt=function(){function t(t,e,n){t.length>0||a(!1),t.every((function(e){return e.chainId===t[0].chainId}))||a(!1),e instanceof tt&&t[0].involvesToken(e)||e===Z&&t[0].involvesToken(ot[t[0].chainId])||a(!1),void 0===n||n instanceof tt&&t[t.length-1].involvesToken(n)||n===Z&&t[t.length-1].involvesToken(ot[t[0].chainId])||a(!1);for(var r,o=[e instanceof tt?e:ot[t[0].chainId]],i=X(t.entries());!(r=i()).done;){var u=r.value,s=u[1],c=o[u[0]];c.equals(s.token0)||c.equals(s.token1)||a(!1);var d=c.equals(s.token0)?s.token1:s.token0;o.push(d)}this.pairs=t,this.path=o,this.midPrice=ft.fromRoute(this),this.input=e,this.output=null!=n?n:o[o.length-1]}return N(t,[{key:"chainId",get:function(){return this.pairs[0].chainId}}]),t}(),yt=new ct(q),wt=function(t){function e(){return t.apply(this,arguments)||this}C(e,t);var n=e.prototype;return n.toSignificant=function(t,e,n){return void 0===t&&(t=5),this.multiply(yt).toSignificant(t,e,n)},n.toFixed=function(t,e,n){return void 0===t&&(t=2),this.multiply(yt).toFixed(t,e,n)},e}(ct);function gt(t,e){return et(t.inputAmount.currency,e.inputAmount.currency)||a(!1),et(t.outputAmount.currency,e.outputAmount.currency)||a(!1),t.outputAmount.equalTo(e.outputAmount)?t.inputAmount.equalTo(e.inputAmount)?0:t.inputAmount.lessThan(e.inputAmount)?-1:1:t.outputAmount.lessThan(e.outputAmount)?1:-1}function Tt(t,e){var n=gt(t,e);return 0!==n?n:t.priceImpact.lessThan(e.priceImpact)?-1:t.priceImpact.greaterThan(e.priceImpact)?1:t.route.path.length-e.route.path.length}function kt(t,e){return t instanceof pt?t:t.currency===Z?new pt(ot[e],t.raw):void a(!1)}function xt(t,e){return t instanceof tt?t:t===Z?ot[e]:void a(!1)}var At=function(){function t(t,e,n,r){var o,i,u,s=new Array(t.path.length),c=new Array(t.pairs.length);if(n===exports.TradeType.EXACT_INPUT){et(e.currency,t.input)||a(!1),s[0]=kt(e,t.chainId);for(var d=0;d<t.path.length-1;d++){var l=t.pairs[d].getOutputAmount(s[d],r),p=l[1];s[d+1]=l[0],c[d]=p}}else{et(e.currency,t.output)||a(!1),s[s.length-1]=kt(e,t.chainId);for(var f=t.path.length-1;f>0;f--){var h=t.pairs[f-1].getInputAmount(s[f],r),m=h[1];s[f-1]=h[0],c[f-1]=m}}this.route=t,this.tradeType=n,this.inputAmount=n===exports.TradeType.EXACT_INPUT?e:t.input===Z?lt.ether(s[0].raw):s[0],this.outputAmount=n===exports.TradeType.EXACT_OUTPUT?e:t.output===Z?lt.ether(s[s.length-1].raw):s[s.length-1],this.executionPrice=new ft(this.inputAmount.currency,this.outputAmount.currency,this.inputAmount.raw,this.outputAmount.raw),this.nextMidPrice=ft.fromRoute(new vt(c,t.input)),this.priceImpact=(o=this.outputAmount,u=(i=t.midPrice.raw.multiply(this.inputAmount.raw)).subtract(o.raw).divide(i),new wt(u.numerator,u.denominator)),this.chainId=r}t.exactIn=function(e,n,r){return new t(e,n,exports.TradeType.EXACT_INPUT,r)},t.exactOut=function(e,n,r){return new t(e,n,exports.TradeType.EXACT_OUTPUT,r)};var e=t.prototype;return e.minimumAmountOut=function(t){if(t.lessThan(k)&&a(!1),this.tradeType===exports.TradeType.EXACT_OUTPUT)return this.outputAmount;var e=new ct(x).add(t).invert().multiply(this.outputAmount.raw).quotient;return this.outputAmount instanceof pt?new pt(this.outputAmount.token,e):lt.ether(e)},e.maximumAmountIn=function(t){if(t.lessThan(k)&&a(!1),this.tradeType===exports.TradeType.EXACT_INPUT)return this.inputAmount;var e=new ct(x).add(t).multiply(this.inputAmount.raw).quotient;return this.inputAmount instanceof pt?new pt(this.inputAmount.token,e):lt.ether(e)},t.bestTradeExactIn=function(e,n,r,o,i,u,s){var c=void 0===o?{}:o,d=c.maxNumResults,l=void 0===d?3:d,p=c.maxHops,f=void 0===p?3:p;void 0===i&&(i=[]),void 0===u&&(u=n),void 0===s&&(s=[]),e.length>0||a(!1),f>0||a(!1),u===n||i.length>0||a(!1);var h=n instanceof pt?n.token.chainId:r instanceof tt?r.chainId:void 0;void 0===h&&a(!1);for(var m=kt(n,h),v=xt(r,h),y=0;y<e.length;y++){var w=e[y];if((w.token0.equals(m.token)||w.token1.equals(m.token))&&!w.reserve0.equalTo(k)&&!w.reserve1.equalTo(k)){var g=void 0;try{g=w.getOutputAmount(m,h)[0]}catch(t){if(t.isInsufficientInputAmountError)continue;throw t}if(g.token.equals(v))z(s,new t(new vt([].concat(i,[w]),u.currency,r),u,exports.TradeType.EXACT_INPUT,h),l,Tt);else if(f>1&&e.length>1){var T=e.slice(0,y).concat(e.slice(y+1,e.length));t.bestTradeExactIn(T,g,r,{maxNumResults:l,maxHops:f-1},[].concat(i,[w]),u,s)}}}return s},t.bestTradeExactOut=function(e,n,r,o,i,u,s){var c=void 0===o?{}:o,d=c.maxNumResults,l=void 0===d?3:d,p=c.maxHops,f=void 0===p?3:p;void 0===i&&(i=[]),void 0===u&&(u=r),void 0===s&&(s=[]),e.length>0||a(!1),f>0||a(!1),u===r||i.length>0||a(!1);var h=r instanceof pt?r.token.chainId:n instanceof tt?n.chainId:void 0;void 0===h&&a(!1);for(var m=kt(r,h),v=xt(n,h),y=0;y<e.length;y++){var w=e[y];if((w.token0.equals(m.token)||w.token1.equals(m.token))&&!w.reserve0.equalTo(k)&&!w.reserve1.equalTo(k)){var g=void 0;try{g=w.getInputAmount(m,h)[0]}catch(t){if(t.isInsufficientReservesError)continue;throw t}if(g.token.equals(v))z(s,new t(new vt([w].concat(i),n,u.currency),u,exports.TradeType.EXACT_OUTPUT,h),l,Tt);else if(f>1&&e.length>1){var T=e.slice(0,y).concat(e.slice(y+1,e.length));t.bestTradeExactOut(T,n,g,{maxNumResults:l,maxHops:f-1},[w].concat(i),u,s)}}}return s},t}();function It(t){return"0x"+t.raw.toString(16)}var bt=function(){function t(){}return t.swapCallParameters=function(t,e){var n=t.inputAmount.currency===Z,r=t.outputAmount.currency===Z;n&&r&&a(!1),!("ttl"in e)||e.ttl>0||a(!1);var o,i,u,s=J(e.recipient),c=It(t.maximumAmountIn(e.allowedSlippage)),d=It(t.minimumAmountOut(e.allowedSlippage)),l=t.route.path.map((function(t){return t.address})),p="ttl"in e?"0x"+(Math.floor((new Date).getTime()/1e3)+e.ttl).toString(16):"0x"+e.deadline.toString(16),f=Boolean(e.feeOnTransfer);switch(t.tradeType){case exports.TradeType.EXACT_INPUT:n?(o=f?"swapExactSYSForTokensSupportingFeeOnTransferTokens":"swapExactSYSForTokens",i=[d,l,s,p],u=c):r?(o=f?"swapExactTokensForSYSSupportingFeeOnTransferTokens":"swapExactTokensForSYS",i=[c,d,l,s,p],u="0x0"):(o=f?"swapExactTokensForTokensSupportingFeeOnTransferTokens":"swapExactTokensForTokens",i=[c,d,l,s,p],u="0x0");break;case exports.TradeType.EXACT_OUTPUT:f&&a(!1),n?(o="swapSYSForExactTokens",i=[d,l,s,p],u=c):r?(o="swapTokensForExactSYS",i=[d,c,l,s,p],u="0x0"):(o="swapTokensForExactTokens",i=[d,c,l,s,p],u="0x0")}return{methodName:o,args:i,value:u}},t}(),Ot=[{constant:!0,inputs:[],name:"decimals",outputs:[{name:"",type:"uint8"}],payable:!1,stateMutability:"view",type:"function"},{constant:!0,inputs:[{name:"",type:"address"}],name:"balanceOf",outputs:[{name:"",type:"uint256"}],payable:!1,stateMutability:"view",type:"function"}],qt={},Pt=function(){function t(){}return t.fetchTokenData=function(t,e,n,r,o){try{var i,u,a=function(n){return new tt(t,e,n,r,o)};void 0===n&&(n=m.getDefaultProvider(h.getNetwork(t)));var s="number"==typeof(null===(i=qt)||void 0===i||null===(u=i[t])||void 0===u?void 0:u[e]);return Promise.resolve(s?a(qt[t][e]):Promise.resolve(new f.Contract(e,Ot,n).decimals().then((function(n){var r,o,i;return qt=R({},qt,((i={})[t]=R({},null===(r=qt)||void 0===r?void 0:r[t],((o={})[e]=n,o)),i)),n}))).then(a))}catch(t){return Promise.reject(t)}},t.fetchPairData=function(t,e,n){try{void 0===n&&(n=m.getDefaultProvider(h.getNetwork(t.chainId))),t.chainId!==e.chainId&&a(!1);var r=mt.getAddress(t,e,t.chainId);return Promise.resolve(new f.Contract(r,v.abi,n).getReserves()).then((function(n){var r=n[0],o=n[1],i=t.sortsBefore(e)?[r,o]:[o,r];return new mt(new pt(t,i[0]),new pt(e,i[1]),t.chainId)}))}catch(t){return Promise.reject(t)}},t}();exports.JSBI=u,exports.Currency=K,exports.CurrencyAmount=lt,exports.FACTORY_ADDRESS=w,exports.Fetcher=Pt,exports.Fraction=ct,exports.INIT_CODE_HASH=g,exports.InsufficientInputAmountError=H,exports.InsufficientReservesError=W,exports.MINIMUM_LIQUIDITY=T,exports.NSYS=Z,exports.Pair=mt,exports.Percent=wt,exports.Price=ft,exports.Route=vt,exports.Router=bt,exports.Token=tt,exports.TokenAmount=pt,exports.Trade=At,exports.WSYS=ot,exports.currencyEquals=et,exports.inputOutputComparator=gt,exports.tradeComparator=Tt; //# sourceMappingURL=pegasys-sdk.cjs.production.min.js.map