UNPKG

@pollum-io/v1-sdk

Version:

🛠 An SDK for building applications on top of Pegasys V2

3 lines (2 loc) • 14.3 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=t(require("jsbi")),n=require("@pollum-io/sdk-core"),r=t(require("tiny-invariant")),o=require("@ethersproject/solidity"),u=require("@ethersproject/address"),i={570:"0x302518cc2C03A8f4fD1C2c8AC1Ff4C195A1236Ca",57e3:"0xD7b7B9585C3A4B2a2f7B11D548A3d7B8D1884B6d",5701:"0xECee82d05fa8fAB14797FfBcEBe37B6181C4bf91"},s="0x85c9b07c539b322c33da730d88df8396983c35a411da73d3d6c2278474890833",a={570:s,57e3:s,5701:"0x0100065f12b29bae29aa36044ea9a641888ad99cee9b54d7d52ff1219d9b3a2b"},c=e.BigInt(1e3),p=e.BigInt(0),l=e.BigInt(1),f=e.BigInt(5),d=e.BigInt(997),h=e.BigInt(1e3);function m(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 y(t,e,n){return e&&m(t.prototype,e),n&&m(t,n),t}function v(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}function A(t){return(A=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function T(t,e){return(T=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function q(){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 k(t,e,n){return(k=q()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&T(o,n.prototype),o}).apply(null,arguments)}function g(t){var e="function"==typeof Map?new Map:void 0;return(g=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 k(t,arguments,A(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),T(n,t)})(t)}function w(t){if(void 0===t)throw new ReferenceError("this hasn't been initialised - super() hasn't been called");return t}function I(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;if("undefined"==typeof Symbol||null==t[Symbol.iterator]){if(Array.isArray(t)||(n=function(t,e){if(t){if("string"==typeof t)return I(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)?I(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 b="setPrototypeOf"in Object,O=function(t){function e(){var n;return(n=t.call(this)||this).isInsufficientReservesError=!0,n.name=n.constructor.name,b&&Object.setPrototypeOf(w(n),(this instanceof e?this.constructor:void 0).prototype),n}return v(e,t),e}(g(Error)),E=function(t){function e(){var n;return(n=t.call(this)||this).isInsufficientInputAmountError=!0,n.name=n.constructor.name,b&&Object.setPrototypeOf(w(n),(this instanceof e?this.constructor:void 0).prototype),n}return v(e,t),e}(g(Error)),P=function(t){var e,r=t.factoryAddress,i=t.tokenA,c=t.tokenB,p=i.sortsBefore(c)?[i,c]:[c,i],l=p[0],f=null!=(e=a[l.chainId])?e:s,d=o.keccak256(["bytes"],[o.pack(["address","address"],[l.address,p[1].address])]);switch(l.chainId){case n.SupportedChainId.ZKSYS_TANENBAUM:return n.computeZksyncCreate2Address(r,f,d);default:return u.getCreate2Address(r,d,f)}},C=function(){function t(e,r){var o=e.currency.sortsBefore(r.currency)?[e,r]:[r,e];this.liquidityToken=new n.Token(o[0].currency.chainId,t.getAddress(o[0].currency,o[1].currency),18,"PSYS-V1","Pegasys V1"),this.tokenAmounts=o}t.getAddress=function(t,e){var n,r=null!=(n=i[t.chainId])?n:"0x302518cc2C03A8f4fD1C2c8AC1Ff4C195A1236Ca";return P({factoryAddress:r,tokenA:t,tokenB:e})};var o=t.prototype;return o.involvesToken=function(t){return t.equals(this.token0)||t.equals(this.token1)},o.priceOf=function(t){return this.involvesToken(t)||r(!1),t.equals(this.token0)?this.token0Price:this.token1Price},o.reserveOf=function(t){return this.involvesToken(t)||r(!1),t.equals(this.token0)?this.reserve0:this.reserve1},o.getOutputAmount=function(o){if(this.involvesToken(o.currency)||r(!1),e.equal(this.reserve0.quotient,p)||e.equal(this.reserve1.quotient,p))throw new O;var u=this.reserveOf(o.currency),i=this.reserveOf(o.currency.equals(this.token0)?this.token1:this.token0),s=e.multiply(o.quotient,d),a=e.multiply(s,i.quotient),c=e.add(e.multiply(u.quotient,h),s),l=n.CurrencyAmount.fromRawAmount(o.currency.equals(this.token0)?this.token1:this.token0,e.divide(a,c));if(e.equal(l.quotient,p))throw new E;return[l,new t(u.add(o),i.subtract(l))]},o.getInputAmount=function(o){if(this.involvesToken(o.currency)||r(!1),e.equal(this.reserve0.quotient,p)||e.equal(this.reserve1.quotient,p)||e.greaterThanOrEqual(o.quotient,this.reserveOf(o.currency).quotient))throw new O;var u=this.reserveOf(o.currency),i=this.reserveOf(o.currency.equals(this.token0)?this.token1:this.token0),s=e.multiply(e.multiply(i.quotient,o.quotient),h),a=e.multiply(e.subtract(u.quotient,o.quotient),d),c=n.CurrencyAmount.fromRawAmount(o.currency.equals(this.token0)?this.token1:this.token0,e.add(e.divide(s,a),l));return[c,new t(i.add(c),u.subtract(o))]},o.getLiquidityMinted=function(t,o,u){t.currency.equals(this.liquidityToken)||r(!1);var i,s=o.currency.sortsBefore(u.currency)?[o,u]:[u,o];if(s[0].currency.equals(this.token0)&&s[1].currency.equals(this.token1)||r(!1),e.equal(t.quotient,p))i=e.subtract(n.sqrt(e.multiply(s[0].quotient,s[1].quotient)),c);else{var a=e.divide(e.multiply(s[0].quotient,t.quotient),this.reserve0.quotient),l=e.divide(e.multiply(s[1].quotient,t.quotient),this.reserve1.quotient);i=e.lessThanOrEqual(a,l)?a:l}if(!e.greaterThan(i,p))throw new E;return n.CurrencyAmount.fromRawAmount(this.liquidityToken,i)},o.getLiquidityValue=function(t,o,u,i,s){var a;if(void 0===i&&(i=!1),this.involvesToken(t)||r(!1),o.currency.equals(this.liquidityToken)||r(!1),u.currency.equals(this.liquidityToken)||r(!1),e.lessThanOrEqual(u.quotient,o.quotient)||r(!1),i){s||r(!1);var c=e.BigInt(s);if(e.equal(c,p))a=o;else{var l=n.sqrt(e.multiply(this.reserve0.quotient,this.reserve1.quotient)),d=n.sqrt(c);if(e.greaterThan(l,d)){var h=e.multiply(o.quotient,e.subtract(l,d)),m=e.add(e.multiply(l,f),d),y=e.divide(h,m);a=o.add(n.CurrencyAmount.fromRawAmount(this.liquidityToken,y))}else a=o}}else a=o;return n.CurrencyAmount.fromRawAmount(t,e.divide(e.multiply(u.quotient,this.reserveOf(t).quotient),a.quotient))},y(t,[{key:"token0Price",get:function(){var t=this.tokenAmounts[1].divide(this.tokenAmounts[0]);return new n.Price(this.token0,this.token1,t.denominator,t.numerator)}},{key:"token1Price",get:function(){var t=this.tokenAmounts[0].divide(this.tokenAmounts[1]);return new n.Price(this.token1,this.token0,t.denominator,t.numerator)}},{key:"chainId",get:function(){return this.token0.chainId}},{key:"token0",get:function(){return this.tokenAmounts[0].currency}},{key:"token1",get:function(){return this.tokenAmounts[1].currency}},{key:"reserve0",get:function(){return this.tokenAmounts[0]}},{key:"reserve1",get:function(){return this.tokenAmounts[1]}}]),t}(),_=function(){function t(t,e,n){this._midPrice=null,t.length>0||r(!1);var o=t[0].chainId;t.every((function(t){return t.chainId===o}))||r(!1);var u=e.wrapped;t[0].involvesToken(u)||r(!1),void 0===n||t[t.length-1].involvesToken(n.wrapped)||r(!1);for(var i,s=[u],a=x(t.entries());!(i=a()).done;){var c=i.value,p=c[1],l=s[c[0]];l.equals(p.token0)||l.equals(p.token1)||r(!1);var f=l.equals(p.token0)?p.token1:p.token0;s.push(f)}this.pairs=t,this.path=s,this.input=e,this.output=n}return y(t,[{key:"midPrice",get:function(){if(null!==this._midPrice)return this._midPrice;for(var t,e=[],r=x(this.pairs.entries());!(t=r()).done;){var o=t.value,u=o[1];e.push(this.path[o[0]].equals(u.token0)?new n.Price(u.reserve0.currency,u.reserve1.currency,u.reserve0.quotient,u.reserve1.quotient):new n.Price(u.reserve1.currency,u.reserve0.currency,u.reserve1.quotient,u.reserve0.quotient))}var i=e.slice(1).reduce((function(t,e){return t.multiply(e)}),e[0]);return this._midPrice=new n.Price(this.input,this.output,i.denominator,i.numerator)}},{key:"chainId",get:function(){return this.pairs[0].chainId}}]),t}();function S(t,e){return t.inputAmount.currency.equals(e.inputAmount.currency)||r(!1),t.outputAmount.currency.equals(e.outputAmount.currency)||r(!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 F(t,e){var n=S(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}var R=function(){function t(t,e,o){this.route=t,this.tradeType=o;var u=new Array(t.path.length);if(o===n.TradeType.EXACT_INPUT){e.currency.equals(t.input)||r(!1),u[0]=e.wrapped;for(var i=0;i<t.path.length-1;i++){var s=t.pairs[i].getOutputAmount(u[i]);u[i+1]=s[0]}this.inputAmount=n.CurrencyAmount.fromFractionalAmount(t.input,e.numerator,e.denominator),this.outputAmount=n.CurrencyAmount.fromFractionalAmount(t.output,u[u.length-1].numerator,u[u.length-1].denominator)}else{e.currency.equals(t.output)||r(!1),u[u.length-1]=e.wrapped;for(var a=t.path.length-1;a>0;a--){var c=t.pairs[a-1].getInputAmount(u[a]);u[a-1]=c[0]}this.inputAmount=n.CurrencyAmount.fromFractionalAmount(t.input,u[0].numerator,u[0].denominator),this.outputAmount=n.CurrencyAmount.fromFractionalAmount(t.output,e.numerator,e.denominator)}this.executionPrice=new n.Price(this.inputAmount.currency,this.outputAmount.currency,this.inputAmount.quotient,this.outputAmount.quotient),this.priceImpact=n.computePriceImpact(t.midPrice,this.inputAmount,this.outputAmount)}t.exactIn=function(e,r){return new t(e,r,n.TradeType.EXACT_INPUT)},t.exactOut=function(e,r){return new t(e,r,n.TradeType.EXACT_OUTPUT)};var e=t.prototype;return e.minimumAmountOut=function(t){if(t.lessThan(p)&&r(!1),this.tradeType===n.TradeType.EXACT_OUTPUT)return this.outputAmount;var e=new n.Fraction(l).add(t).invert().multiply(this.outputAmount.quotient).quotient;return n.CurrencyAmount.fromRawAmount(this.outputAmount.currency,e)},e.maximumAmountIn=function(t){if(t.lessThan(p)&&r(!1),this.tradeType===n.TradeType.EXACT_INPUT)return this.inputAmount;var e=new n.Fraction(l).add(t).multiply(this.inputAmount.quotient).quotient;return n.CurrencyAmount.fromRawAmount(this.inputAmount.currency,e)},t.bestTradeExactIn=function(e,o,u,i,s,a,c){var l=void 0===i?{}:i,f=l.maxNumResults,d=void 0===f?3:f,h=l.maxHops,m=void 0===h?3:h;void 0===s&&(s=[]),void 0===a&&(a=o),void 0===c&&(c=[]),e.length>0||r(!1),m>0||r(!1),o===a||s.length>0||r(!1);for(var y=a.wrapped,v=u.wrapped,A=0;A<e.length;A++){var T=e[A];if((T.token0.equals(y.currency)||T.token1.equals(y.currency))&&!T.reserve0.equalTo(p)&&!T.reserve1.equalTo(p)){var q=void 0;try{q=T.getOutputAmount(y)[0]}catch(t){if(t.isInsufficientInputAmountError)continue;throw t}if(q.currency.equals(v))n.sortedInsert(c,new t(new _([].concat(s,[T]),o.currency,u),o,n.TradeType.EXACT_INPUT),d,F);else if(m>1&&e.length>1){var k=e.slice(0,A).concat(e.slice(A+1,e.length));t.bestTradeExactIn(k,o,u,{maxNumResults:d,maxHops:m-1},[].concat(s,[T]),q,c)}}}return c},e.worstExecutionPrice=function(t){return new n.Price(this.inputAmount.currency,this.outputAmount.currency,this.maximumAmountIn(t).quotient,this.minimumAmountOut(t).quotient)},t.bestTradeExactOut=function(e,o,u,i,s,a,c){var l=void 0===i?{}:i,f=l.maxNumResults,d=void 0===f?3:f,h=l.maxHops,m=void 0===h?3:h;void 0===s&&(s=[]),void 0===a&&(a=u),void 0===c&&(c=[]),e.length>0||r(!1),m>0||r(!1),u===a||s.length>0||r(!1);for(var y=a.wrapped,v=o.wrapped,A=0;A<e.length;A++){var T=e[A];if((T.token0.equals(y.currency)||T.token1.equals(y.currency))&&!T.reserve0.equalTo(p)&&!T.reserve1.equalTo(p)){var q=void 0;try{q=T.getInputAmount(y)[0]}catch(t){if(t.isInsufficientReservesError)continue;throw t}if(q.currency.equals(v))n.sortedInsert(c,new t(new _([T].concat(s),o,u.currency),u,n.TradeType.EXACT_OUTPUT),d,F);else if(m>1&&e.length>1){var k=e.slice(0,A).concat(e.slice(A+1,e.length));t.bestTradeExactOut(k,o,u,{maxNumResults:d,maxHops:m-1},[T].concat(s),q,c)}}}return c},t}();function B(t){return"0x"+t.quotient.toString(16)}var j=function(){function t(){}return t.swapCallParameters=function(t,e){var o=t.inputAmount.currency.isNative,u=t.outputAmount.currency.isNative;o&&u&&r(!1),!("ttl"in e)||e.ttl>0||r(!1);var i,s,a,c=n.validateAndParseAddress(e.recipient),p=B(t.maximumAmountIn(e.allowedSlippage)),l=B(t.minimumAmountOut(e.allowedSlippage)),f=t.route.path.map((function(t){return t.address})),d="ttl"in e?"0x"+(Math.floor((new Date).getTime()/1e3)+e.ttl).toString(16):"0x"+e.deadline.toString(16),h=Boolean(e.feeOnTransfer);switch(t.tradeType){case n.TradeType.EXACT_INPUT:o?(i=h?"swapExactETHForTokensSupportingFeeOnTransferTokens":"swapExactETHForTokens",s=[l,f,c,d],a=p):u?(i=h?"swapExactTokensForETHSupportingFeeOnTransferTokens":"swapExactTokensForETH",s=[p,l,f,c,d],a="0x0"):(i=h?"swapExactTokensForTokensSupportingFeeOnTransferTokens":"swapExactTokensForTokens",s=[p,l,f,c,d],a="0x0");break;case n.TradeType.EXACT_OUTPUT:h&&r(!1),o?(i="swapETHForExactTokens",s=[l,f,c,d],a=p):u?(i="swapTokensForExactETH",s=[l,p,f,c,d],a="0x0"):(i="swapTokensForExactTokens",s=[l,p,f,c,d],a="0x0")}return{methodName:i,args:s,value:a}},t}();exports.FACTORY_ADDRESS_MAP=i,exports.INIT_CODE_HASH=s,exports.INIT_CODE_HASH_MAP=a,exports.InsufficientInputAmountError=E,exports.InsufficientReservesError=O,exports.MINIMUM_LIQUIDITY=c,exports.Pair=C,exports.Route=_,exports.Router=j,exports.Trade=R,exports.computePairAddress=P,exports.inputOutputComparator=S,exports.tradeComparator=F; //# sourceMappingURL=v1-sdk.cjs.production.min.js.map