UNPKG

@materia-dex/sdk

Version:

An SDK for building applications on top of Materia.

3 lines (2 loc) 25.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")),h=require("@ethersproject/solidity"),f=t(require("web3")),p=require("@ethersproject/units"),m=require("@ethersproject/contracts"),v=require("@ethersproject/networks"),y=require("@ethersproject/providers"),T=t(require("@materia-dex/materia-contracts-core/build/IMateriaPair.json"));(r=exports.ChainId||(exports.ChainId={}))[r.MAINNET=1]="MAINNET",r[r.ROPSTEN=3]="ROPSTEN",r[r.RINKEBY=4]="RINKEBY",r[r["GÖRLI"]=5]="GÖRLI",r[r.KOVAN=42]="KOVAN",(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",(e={})[exports.ChainId.MAINNET]="mainnet",e[exports.ChainId.ROPSTEN]="ropsten",e[exports.ChainId.RINKEBY]="rinkeby",e[exports.ChainId.GÖRLI]="goerli",e[exports.ChainId.KOVAN]="kovan";var w,g="0xB0F720Baa5BD1715897d4790A59f5c7aa1377D79",I="0x1974917c1e01e6369c1b45f631eae6a71d24cb5108c460cc7f0b1c608b3a7c94",k=u.BigInt(1e3),x=u.BigInt(0),A=u.BigInt(1),E=u.BigInt(2),O=u.BigInt(3),b=u.BigInt(5),_=u.BigInt(10),N=u.BigInt(100),q=u.BigInt(997),R=u.BigInt(1e3),P="0x0000000000000000000000000000000000000000";!function(t){t.uint8="uint8",t.uint256="uint256"}(w||(w={}));var C=((n={})[w.uint8]=u.BigInt("0xff"),n[w.uint256]=u.BigInt("0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"),n);function U(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 S(t,e,n){return e&&U(t.prototype,e),n&&U(t,n),t}function D(){return(D=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 F(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}function B(t){return(B=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function j(t,e){return(j=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function H(){if("undefined"==typeof Reflect||!Reflect.construct)return!1;if(Reflect.construct.sham)return!1;if("function"==typeof Proxy)return!0;try{return Date.prototype.toString.call(Reflect.construct(Date,[],(function(){}))),!0}catch(t){return!1}}function M(t,e,n){return(M=H()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&j(o,n.prototype),o}).apply(null,arguments)}function X(t){var e="function"==typeof Map?new Map:void 0;return(X=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 M(t,arguments,B(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),j(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 K(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 W(t,e){var n;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return K(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)?K(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.")}return(n=t[Symbol.iterator]()).next.bind(n)}var V="setPrototypeOf"in Object,Y=function(t){function e(){var n;return(n=t.call(this)||this).isInsufficientReservesError=!0,n.name=n.constructor.name,V&&Object.setPrototypeOf(L(n),(this instanceof e?this.constructor:void 0).prototype),n}return F(e,t),e}(X(Error)),G=function(t){function e(){var n;return(n=t.call(this)||this).isInsufficientInputAmountError=!0,n.name=n.constructor.name,V&&Object.setPrototypeOf(L(n),(this instanceof e?this.constructor:void 0).prototype),n}return F(e,t),e}(X(Error));function Q(t,e){u.greaterThanOrEqual(t,x)||a(!1),u.lessThanOrEqual(t,C[e])||a(!1)}function J(t){try{return s.getAddress(t)}catch(t){a(!1)}}function $(t){return t instanceof u?t:u.BigInt("bigint"==typeof t?t.toString():t)}function z(t){Q(t,w.uint256);var e,n=x;if(u.greaterThan(t,O))for(n=t,e=u.add(u.divide(t,E),A);u.lessThan(e,n);)n=e,e=u.divide(u.add(u.divide(t,e),e),E);else u.notEqual(t,x)&&(n=A);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 tt,et=function(t,e,n){Q(u.BigInt(t),w.uint8),this.decimals=t,this.symbol=e,this.name=n},nt=et.ETHER=new et(18,"ETH","Ether"),rt=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}F(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}(et);function ot(t,e){return t instanceof rt&&e instanceof rt?t.equals(e):!(t instanceof rt||e instanceof rt||t!==e)}var it,ut,at=((tt={})[exports.ChainId.MAINNET]=new rt(exports.ChainId.MAINNET,"0x1D6316dbbE18b6E9B75AE064aA114FE7dC208eDC",18,"IETH","EthereumItem"),tt[exports.ChainId.ROPSTEN]=new rt(exports.ChainId.ROPSTEN,"0xEDf7dE64832b6D0998fE7E7D556A38005B994565",18,"IETH","EthereumItem"),tt[exports.ChainId.RINKEBY]=new rt(exports.ChainId.RINKEBY,P,18,"IETH","EthereumItem"),tt[exports.ChainId.GÖRLI]=new rt(exports.ChainId.GÖRLI,P,18,"IETH","EthereumItem"),tt[exports.ChainId.KOVAN]=new rt(exports.ChainId.KOVAN,P,18,"IETH","EthereumItem"),tt),st=d(l),ct=d(c),dt=((it={})[exports.Rounding.ROUND_DOWN]=st.ROUND_DOWN,it[exports.Rounding.ROUND_HALF_UP]=st.ROUND_HALF_UP,it[exports.Rounding.ROUND_UP]=st.ROUND_UP,it),lt=((ut={})[exports.Rounding.ROUND_DOWN]=0,ut[exports.Rounding.ROUND_HALF_UP]=1,ut[exports.Rounding.ROUND_UP]=3,ut),ht=function(){function t(t,e){void 0===e&&(e=A),this.numerator=$(t),this.denominator=$(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($(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($(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($(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($(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($(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($(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($(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),st.set({precision:t+1,rounding:dt[n]});var r=new st(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),ct.DP=t,ct.RM=lt[n],new ct(this.numerator.toString()).div(this.denominator.toString()).toFormat(t,e)},S(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}(),ft=d(c),pt=function(t){function e(e,n){var r,o=$(n);return Q(o,w.uint256),(r=t.call(this,o,u.exponentiate(_,u.BigInt(e.decimals)))||this).currency=e,r}F(e,t),e.ether=function(t){return new e(nt,t)};var n=e.prototype;return n.add=function(t){return ot(this.currency,t.currency)||a(!1),new e(this.currency,u.add(this.raw,t.raw))},n.subtract=function(t){return ot(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:""}),ft.DP=this.currency.decimals,new ft(this.numerator.toString()).div(this.denominator.toString()).toFormat(t)},S(e,[{key:"raw",get:function(){return this.numerator}}]),e}(ht),mt=function(t){function e(e,n){var r;return(r=t.call(this,e,n)||this).token=e,r}F(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}(pt),vt=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 ht(u.exponentiate(_,u.BigInt(e.decimals)),u.exponentiate(_,u.BigInt(n.decimals))),i}F(e,t),e.fromRoute=function(t){for(var n,r=[],o=W(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){ot(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 ot(e.currency,this.baseCurrency)||a(!1),this.quoteCurrency instanceof rt?new mt(this.quoteCurrency,t.prototype.multiply.call(this,e.raw).quotient):pt.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)},S(e,[{key:"raw",get:function(){return new ht(this.numerator,this.denominator)}},{key:"adjusted",get:function(){return t.prototype.multiply.call(this,this.scalar)}}]),e}(ht),yt={},Tt=u.BigInt(30),wt=function(){function t(e,n,r){var o=e.token.sortsBefore(n.token)?[e,n]:[n,e];this.liquidityToken=new rt(o[0].token.chainId,t.getAddress(o[0].token,o[1].token),18,"MP","Materia Pool"),this.tokenAmounts=o,this.swapFee=null!=r?r:Tt}t.getAddress=function(t,e){var n,r,o,i,u,a=t.sortsBefore(e)?[t,e]:[e,t];return void 0===(null===(n=yt)||void 0===n||null===(r=n[a[0].address])||void 0===r?void 0:r[a[1].address])&&(yt=D({},yt,((u={})[a[0].address]=D({},null===(o=yt)||void 0===o?void 0:o[a[0].address],((i={})[a[1].address]=s.getCreate2Address("0xB498a69fF7b9a73C58491d564Fc6a462b259c860",h.keccak256(["bytes"],[h.pack(["address","address"],[a[0].address,a[1].address])]),I),i)),u))),yt[a[0].address][a[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){if(this.involvesToken(e.token)||a(!1),u.equal(this.reserve0.raw,x)||u.equal(this.reserve1.raw,x))throw new Y;var n=this.reserveOf(e.token),r=this.reserveOf(e.token.equals(this.token0)?this.token1:this.token0),o=u.multiply(e.raw,q),i=u.multiply(o,r.raw),s=u.add(u.multiply(n.raw,R),o),c=new mt(e.token.equals(this.token0)?this.token1:this.token0,u.divide(i,s));if(u.equal(c.raw,x))throw new G;return[c,new t(n.add(e),r.subtract(c))]},e.getInputAmount=function(e){if(this.involvesToken(e.token)||a(!1),u.equal(this.reserve0.raw,x)||u.equal(this.reserve1.raw,x)||u.greaterThanOrEqual(e.raw,this.reserveOf(e.token).raw))throw new Y;var n=this.reserveOf(e.token),r=this.reserveOf(e.token.equals(this.token0)?this.token1:this.token0),o=u.multiply(u.multiply(r.raw,e.raw),R),i=u.multiply(u.subtract(n.raw,e.raw),q),s=new mt(e.token.equals(this.token0)?this.token1:this.token0,u.add(u.divide(o,i),A));return[s,new t(r.add(s),n.subtract(e))]},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,x))r=u.subtract(z(u.multiply(o[0].raw,o[1].raw)),k);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,x))throw new G;return new mt(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=$(o);if(u.equal(s,x))i=e;else{var c=z(u.multiply(this.reserve0.raw,this.reserve1.raw)),d=z(s);if(u.greaterThan(c,d)){var l=u.multiply(e.raw,u.subtract(c,d)),h=u.add(u.multiply(c,b),d),f=u.divide(l,h);i=e.add(new mt(this.liquidityToken,f))}else i=e}}else i=e;return new mt(t,u.divide(u.multiply(n.raw,this.reserveOf(t).raw),i.raw))},S(t,[{key:"token0Price",get:function(){return new vt(this.token0,this.token1,this.tokenAmounts[0].raw,this.tokenAmounts[1].raw)}},{key:"token1Price",get:function(){return new vt(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}(),gt=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 rt&&t[0].involvesToken(e)||e===nt&&t[0].involvesToken(at[t[0].chainId])||a(!1),void 0===n||n instanceof rt&&t[t.length-1].involvesToken(n)||n===nt&&t[t.length-1].involvesToken(at[t[0].chainId])||a(!1);for(var r,o=[e instanceof rt?e:at[t[0].chainId]],i=W(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=vt.fromRoute(this),this.input=e,this.output=null!=n?n:o[o.length-1]}return S(t,[{key:"chainId",get:function(){return this.pairs[0].chainId}}]),t}(),It=new ht(N),kt=function(t){function e(){return t.apply(this,arguments)||this}F(e,t);var n=e.prototype;return n.toSignificant=function(t,e,n){return void 0===t&&(t=5),this.multiply(It).toSignificant(t,e,n)},n.toFixed=function(t,e,n){return void 0===t&&(t=2),this.multiply(It).toFixed(t,e,n)},e}(ht);function xt(t,e){return ot(t.inputAmount.currency,e.inputAmount.currency)||a(!1),ot(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 At(t,e){var n=xt(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 Et(t,e){return t instanceof mt?t:t.currency===nt?new mt(at[e],t.raw):void a(!1)}function Ot(t,e){return t instanceof rt?t:t===nt?at[e]:void a(!1)}var bt=function(){function t(t,e,n){var r,o,i,u=new Array(t.path.length),s=new Array(t.pairs.length);if(n===exports.TradeType.EXACT_INPUT){ot(e.currency,t.input)||a(!1),u[0]=Et(e,t.chainId);for(var c=0;c<t.path.length-1;c++){var d=t.pairs[c].getOutputAmount(u[c]),l=d[1];u[c+1]=d[0],s[c]=l}}else{ot(e.currency,t.output)||a(!1),u[u.length-1]=Et(e,t.chainId);for(var h=t.path.length-1;h>0;h--){var f=t.pairs[h-1].getInputAmount(u[h]),p=f[1];u[h-1]=f[0],s[h-1]=p}}this.route=t,this.tradeType=n,this.inputAmount=n===exports.TradeType.EXACT_INPUT?e:t.input===nt?pt.ether(u[0].raw):u[0],this.outputAmount=n===exports.TradeType.EXACT_OUTPUT?e:t.output===nt?pt.ether(u[u.length-1].raw):u[u.length-1],this.executionPrice=new vt(this.inputAmount.currency,this.outputAmount.currency,this.inputAmount.raw,this.outputAmount.raw),this.nextMidPrice=vt.fromRoute(new gt(s,t.input)),this.priceImpact=(r=this.outputAmount,i=(o=t.midPrice.raw.multiply(this.inputAmount.raw)).subtract(r.raw).divide(o),new kt(i.numerator,i.denominator))}t.exactIn=function(e,n){return new t(e,n,exports.TradeType.EXACT_INPUT)},t.exactOut=function(e,n){return new t(e,n,exports.TradeType.EXACT_OUTPUT)};var e=t.prototype;return e.minimumAmountOut=function(t){if(t.lessThan(x)&&a(!1),this.tradeType===exports.TradeType.EXACT_OUTPUT)return this.outputAmount;var e=new ht(A).add(t).invert().multiply(this.outputAmount.raw).quotient;return this.outputAmount instanceof mt?new mt(this.outputAmount.token,e):pt.ether(e)},e.maximumAmountIn=function(t){if(t.lessThan(x)&&a(!1),this.tradeType===exports.TradeType.EXACT_INPUT)return this.inputAmount;var e=new ht(A).add(t).multiply(this.inputAmount.raw).quotient;return this.inputAmount instanceof mt?new mt(this.inputAmount.token,e):pt.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,h=c.maxHops,f=void 0===h?2:h;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 p=n instanceof mt?n.token.chainId:r instanceof rt?r.chainId:void 0;void 0===p&&a(!1);for(var m=Et(n,p),v=Ot(r,p),y=0;y<e.length;y++){var T=e[y];if((T.token0.equals(m.token)||T.token1.equals(m.token))&&!T.reserve0.equalTo(x)&&!T.reserve1.equalTo(x)){var w=void 0;try{w=T.getOutputAmount(m)[0]}catch(t){if(t.isInsufficientInputAmountError)continue;throw t}if(w.token.equals(v))Z(s,new t(new gt([].concat(i,[T]),u.currency,r),u,exports.TradeType.EXACT_INPUT),l,At);else if(f>1&&e.length>1){var g=e.slice(0,y).concat(e.slice(y+1,e.length));t.bestTradeExactIn(g,w,r,{maxNumResults:l,maxHops:f-1},[].concat(i,[T]),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,h=c.maxHops,f=void 0===h?2:h;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 p=r instanceof mt?r.token.chainId:n instanceof rt?n.chainId:void 0;void 0===p&&a(!1);for(var m=Et(r,p),v=Ot(n,p),y=0;y<e.length;y++){var T=e[y];if((T.token0.equals(m.token)||T.token1.equals(m.token))&&!T.reserve0.equalTo(x)&&!T.reserve1.equalTo(x)){var w=void 0;try{w=T.getInputAmount(m)[0]}catch(t){if(t.isInsufficientReservesError)continue;throw t}if(w.token.equals(v))Z(s,new t(new gt([T].concat(i),n,u.currency),u,exports.TradeType.EXACT_OUTPUT),l,At);else if(f>1&&e.length>1){var g=e.slice(0,y).concat(e.slice(y+1,e.length));t.bestTradeExactOut(g,n,w,{maxNumResults:l,maxHops:f-1},[T].concat(i),u,s)}}}return s},t}();function _t(t){return"0x"+t.raw.toString(16)}var Nt,qt=function(){function t(){}return t.swapCallParameters=function(e,n,r,o,i,u,s,c){var d=new f;i&&u&&a(!1),!("ttl"in n)||n.ttl>0||a(!1);var l=J(n.recipient),h=e.maximumAmountIn(n.allowedSlippage),p=e.minimumAmountOut(n.allowedSlippage),m=t.decodeInteroperableValueToERC20TokenAmount(h,r,i),v=t.decodeInteroperableValueToERC20TokenAmount(p,o,u),y=_t(null!=m?m:h),T=_t(null!=v?v:p),w=e.route.path.map((function(t){return t.address})),I=w[w.length-1]==(null==o?void 0:o.address)||u?w[w.length-1]:null==o?void 0:o.address;w[0]=w[0]==(null==r?void 0:r.address)||i?w[0]:null==r?void 0:r.address,w[w.length-1]=I;var k,x,A,E,O="ttl"in n?"0x"+(Math.floor((new Date).getTime()/1e3)+n.ttl).toString(16):"0x"+n.deadline.toString(16);if(s)switch(k="safeTransferFrom",e.tradeType){case exports.TradeType.EXACT_INPUT:E=d.eth.abi.encodeParameters(["uint256","bytes"],[u?4:2,d.eth.abi.encodeParameters(["uint","address[]","address","uint"],[T,w,l,O])]),x=[l,g,null!=c?c:"0",y,E],A="0x0";break;case exports.TradeType.EXACT_OUTPUT:E=d.eth.abi.encodeParameters(["uint256","bytes"],[u?5:3,d.eth.abi.encodeParameters(["uint","address[]","address","uint"],[T,w,l,O])]),x=[l,g,null!=c?c:"0",y,E],A="0x0"}else switch(e.tradeType){case exports.TradeType.EXACT_INPUT:i?(k="swapExactETHForTokens",x=[T,w,l,O],A=y):u?(k="swapExactTokensForETH",x=[y,T,w,l,O],A="0x0"):(k="swapExactTokensForTokens",x=[y,T,w,l,O],A="0x0");break;case exports.TradeType.EXACT_OUTPUT:if(i)k="swapETHForExactTokens",x=[T,w,l,O],A=y;else if(u){var b=t.adjustTokenAmount(y,r);k="swapTokensForExactETH",x=[T,null!=b?b:y,w,l,O],A="0x0"}else{var _=t.adjustTokenAmount(y,r);k="swapTokensForExactTokens",x=[T,null!=_?_:y,w,l,O],A="0x0"}}return{methodName:k,args:x,value:A}},t.adjustTokenAmount=function(t,e){if(t&&e)try{if(e.decimals<18)return function(t){return"0x"+t.toString(16)}(u.add(u.BigInt(t),u.BigInt(1)));if(18==e.decimals)return t;throw"Too much decimals for EthItem"}catch(e){return void console.log('Failed to adjust input amount: "'+t+'"',e)}},t.decodeInteroperableValueToERC20TokenAmount=function(t,e,n){if(t&&e&&!n){var r=t.toExact(),o=t.currency;if(r&&o&&e)try{var i=o.decimals-e.decimals,a=u.BigInt(0);if(i>0)a=u.BigInt(p.parseUnits(r,e.decimals));else{if(0!=i)throw"Too much decimals for EthItem";a=u.BigInt(p.parseUnits(r,o.decimals))}return e instanceof rt?new mt(e,a):pt.ether(a)}catch(t){return void console.log('Failed to parse input amount: "'+r+'"',t)}}},t}(),Rt=[{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"}],Pt=((Nt={})[exports.ChainId.MAINNET]={"0xE0B7927c4aF23765Cb51314A0E0521A9645F0E2A":9},Nt),Ct=function(){function t(){}return t.fetchTokenData=function(t,e,n,r,o){try{var i,u,a=function(n){return new rt(t,e,n,r,o)};void 0===n&&(n=y.getDefaultProvider(v.getNetwork(t)));var s="number"==typeof(null===(i=Pt)||void 0===i||null===(u=i[t])||void 0===u?void 0:u[e]);return Promise.resolve(s?a(Pt[t][e]):Promise.resolve(new m.Contract(e,Rt,n).decimals().then((function(n){var r,o,i;return Pt=D({},Pt,((i={})[t]=D({},null===(r=Pt)||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=y.getDefaultProvider(v.getNetwork(t.chainId))),t.chainId!==e.chainId&&a(!1);var r=wt.getAddress(t,e);return Promise.resolve(new m.Contract(r,T.abi,n).getReserves()).then((function(n){var r=n[0],o=n[1],i=t.sortsBefore(e)?[r,o]:[o,r];return new wt(new mt(t,i[0]),new mt(e,i[1]))}))}catch(t){return Promise.reject(t)}},t}();exports.JSBI=u,exports.ADD_LIQUIDITY_ACTION_SAFE_TRANSFER_TOKEN=1,exports.Currency=et,exports.CurrencyAmount=pt,exports.ETHER=nt,exports.FACTORY_ADDRESS="0xB498a69fF7b9a73C58491d564Fc6a462b259c860",exports.Fetcher=Ct,exports.Fraction=ht,exports.IETH=at,exports.INIT_CODE_HASH=I,exports.InsufficientInputAmountError=G,exports.InsufficientReservesError=Y,exports.MINIMUM_LIQUIDITY=k,exports.ORCHESTRATOR_ADDRESS=g,exports.Pair=wt,exports.Percent=kt,exports.Price=vt,exports.Route=gt,exports.Router=qt,exports.SWAP_ACTION_EXACT_TOKENS_FOR_ETH=4,exports.SWAP_ACTION_EXACT_TOKENS_FOR_TOKENS=2,exports.SWAP_ACTION_TOKENS_FOR_EXACT_ETH=5,exports.SWAP_ACTION_TOKENS_FOR_EXACT_TOKENS=3,exports.Token=rt,exports.TokenAmount=mt,exports.Trade=bt,exports.currencyEquals=ot,exports.inputOutputComparator=xt,exports.tradeComparator=At; //# sourceMappingURL=sdk.cjs.production.min.js.map