hydra-v2-sdk
Version:
🛠An SDK for building applications on top of Hydraswap V2
3 lines (2 loc) • 14.4 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=t(require("jsbi")),n=require("@uniswap/sdk-core"),r=t(require("tiny-invariant")),o=require("@ethersproject/solidity"),u=require("@ethersproject/address"),i=e.BigInt(1e3),a=e.BigInt(0),s=e.BigInt(1),c=e.BigInt(5),p=e.BigInt(997),l=e.BigInt(1e3);function f(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 d(t,e,n){return e&&f(t.prototype,e),n&&f(t,n),t}function h(t,e){t.prototype=Object.create(e.prototype),t.prototype.constructor=t,t.__proto__=e}function m(t){return(m=Object.setPrototypeOf?Object.getPrototypeOf:function(t){return t.__proto__||Object.getPrototypeOf(t)})(t)}function y(t,e){return(y=Object.setPrototypeOf||function(t,e){return t.__proto__=e,t})(t,e)}function v(){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 T(t,e,n){return(T=v()?Reflect.construct:function(t,e,n){var r=[null];r.push.apply(r,e);var o=new(Function.bind.apply(t,r));return n&&y(o,n.prototype),o}).apply(null,arguments)}function A(t){var e="function"==typeof Map?new Map:void 0;return(A=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 T(t,arguments,m(this).constructor)}return n.prototype=Object.create(t.prototype,{constructor:{value:n,enumerable:!1,writable:!0,configurable:!0}}),y(n,t)})(t)}function q(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 b(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 g="setPrototypeOf"in Object,w=function(t){function e(){var n;return(n=t.call(this)||this).isInsufficientReservesError=!0,n.name=n.constructor.name,g&&Object.setPrototypeOf(q(n),(this instanceof e?this.constructor:void 0).prototype),n}return h(e,t),e}(A(Error)),x=function(t){function e(){var n;return(n=t.call(this)||this).isInsufficientInputAmountError=!0,n.name=n.constructor.name,g&&Object.setPrototypeOf(q(n),(this instanceof e?this.constructor:void 0).prototype),n}return h(e,t),e}(A(Error)),I=function(t){var e=t.factoryAddress,n=t.tokenA,r=t.tokenB,i=t.chainId,a=n.sortsBefore(r)?[n,r]:[r,n];return u.getCreate2Address(e,o.keccak256(["bytes"],[o.pack(["address","address"],[a[0].address,a[1].address])]),2===i?"0x00f5a7aa52ba04ca4b2f7f2ef08b82a61ac4e8fead11ebd9c4fbc48edc95b708":"0x8bd2e44ddacff4efbf1f9ead74bd191fe945f9824cae3109bfc068d82a3cb3d1").toLowerCase()},O=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,o[0].currency.chainId),8,"HYD","Hydraswap"),this.tokenAmounts=o}t.getAddress=function(t,e,n){return I({factoryAddress:2===n?"0xd8350cf44cb326c81fdf91ded2bf92e6b785d64c":"0x5a2a927bea6c5f4a48d4e0116049c1e36d52a528",tokenA:t,tokenB:e,chainId:n})};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,a)||e.equal(this.reserve1.quotient,a))throw new w;var u=this.reserveOf(o.currency),i=this.reserveOf(o.currency.equals(this.token0)?this.token1:this.token0),s=e.multiply(o.quotient,p),c=e.multiply(s,i.quotient),f=e.add(e.multiply(u.quotient,l),s),d=n.CurrencyAmount.fromRawAmount(o.currency.equals(this.token0)?this.token1:this.token0,e.divide(c,f));if(e.equal(d.quotient,a))throw new x;return[d,new t(u.add(o),i.subtract(d))]},o.getInputAmount=function(o){if(this.involvesToken(o.currency)||r(!1),e.equal(this.reserve0.quotient,a)||e.equal(this.reserve1.quotient,a)||e.greaterThanOrEqual(o.quotient,this.reserveOf(o.currency).quotient))throw new w;var u=this.reserveOf(o.currency),i=this.reserveOf(o.currency.equals(this.token0)?this.token1:this.token0),c=e.multiply(e.multiply(i.quotient,o.quotient),l),f=e.multiply(e.subtract(u.quotient,o.quotient),p),d=n.CurrencyAmount.fromRawAmount(o.currency.equals(this.token0)?this.token1:this.token0,e.add(e.divide(c,f),s));return[d,new t(i.add(d),u.subtract(o))]},o.getLiquidityMinted=function(t,o,u){t.currency.equals(this.liquidityToken)||r(!1);var s,c=o.currency.sortsBefore(u.currency)?[o,u]:[u,o];if(c[0].currency.equals(this.token0)&&c[1].currency.equals(this.token1)||r(!1),e.equal(t.quotient,a))s=e.subtract(n.sqrt(e.multiply(c[0].quotient,c[1].quotient)),i);else{var p=e.divide(e.multiply(c[0].quotient,t.quotient),this.reserve0.quotient),l=e.divide(e.multiply(c[1].quotient,t.quotient),this.reserve1.quotient);s=e.lessThanOrEqual(p,l)?p:l}if(!e.greaterThan(s,a))throw new x;return n.CurrencyAmount.fromRawAmount(this.liquidityToken,s)},o.getLiquidityValue=function(t,o,u,i,s){var p;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 l=e.BigInt(s);if(e.equal(l,a))p=o;else{var f=n.sqrt(e.multiply(this.reserve0.quotient,this.reserve1.quotient)),d=n.sqrt(l);if(e.greaterThan(f,d)){var h=e.multiply(o.quotient,e.subtract(f,d)),m=e.add(e.multiply(f,c),d),y=e.divide(h,m);p=o.add(n.CurrencyAmount.fromRawAmount(this.liquidityToken,y))}else p=o}}else p=o;return n.CurrencyAmount.fromRawAmount(t,e.divide(e.multiply(u.quotient,this.reserveOf(t).quotient),p.quotient))},d(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}(),E=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,a=[u],s=b(t.entries());!(i=s()).done;){var c=i.value,p=c[1],l=a[c[0]];l.equals(p.token0)||l.equals(p.token1)||r(!1);var f=l.equals(p.token0)?p.token1:p.token0;a.push(f)}this.pairs=t,this.path=a,this.input=e,this.output=n}return d(t,[{key:"midPrice",get:function(){if(null!==this._midPrice)return this._midPrice;for(var t,e=[],r=b(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 P(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 _(t,e){var n=P(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 C=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 a=t.pairs[i].getOutputAmount(u[i]);u[i+1]=a[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 s=t.path.length-1;s>0;s--){var c=t.pairs[s-1].getInputAmount(u[s]);u[s-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(a)&&r(!1),this.tradeType===n.TradeType.EXACT_OUTPUT)return this.outputAmount;var e=new n.Fraction(s).add(t).invert().multiply(this.outputAmount.quotient).quotient;return n.CurrencyAmount.fromRawAmount(this.outputAmount.currency,e)},e.maximumAmountIn=function(t){if(t.lessThan(a)&&r(!1),this.tradeType===n.TradeType.EXACT_INPUT)return this.inputAmount;var e=new n.Fraction(s).add(t).multiply(this.inputAmount.quotient).quotient;return n.CurrencyAmount.fromRawAmount(this.inputAmount.currency,e)},t.bestTradeExactIn=function(e,o,u,i,s,c,p){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===c&&(c=o),void 0===p&&(p=[]),e.length>0||r(!1),m>0||r(!1),o===c||s.length>0||r(!1);for(var y=c.wrapped,v=u.wrapped,T=0;T<e.length;T++){var A=e[T];if((A.token0.equals(y.currency)||A.token1.equals(y.currency))&&!A.reserve0.equalTo(a)&&!A.reserve1.equalTo(a)){var q=void 0;try{q=A.getOutputAmount(y)[0]}catch(t){if(t.isInsufficientInputAmountError)continue;throw t}if(q.currency.equals(v))n.sortedInsert(p,new t(new E([].concat(s,[A]),o.currency,u),o,n.TradeType.EXACT_INPUT),d,_);else if(m>1&&e.length>1){var k=e.slice(0,T).concat(e.slice(T+1,e.length));t.bestTradeExactIn(k,o,u,{maxNumResults:d,maxHops:m-1},[].concat(s,[A]),q,p)}}}return p},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,c,p){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===c&&(c=u),void 0===p&&(p=[]),e.length>0||r(!1),m>0||r(!1),u===c||s.length>0||r(!1);for(var y=c.wrapped,v=o.wrapped,T=0;T<e.length;T++){var A=e[T];if((A.token0.equals(y.currency)||A.token1.equals(y.currency))&&!A.reserve0.equalTo(a)&&!A.reserve1.equalTo(a)){var q=void 0;try{q=A.getInputAmount(y)[0]}catch(t){if(t.isInsufficientReservesError)continue;throw t}if(q.currency.equals(v))n.sortedInsert(p,new t(new E([A].concat(s),o,u.currency),u,n.TradeType.EXACT_OUTPUT),d,_);else if(m>1&&e.length>1){var k=e.slice(0,T).concat(e.slice(T+1,e.length));t.bestTradeExactOut(k,o,u,{maxNumResults:d,maxHops:m-1},[A].concat(s),q,p)}}}return p},t}();function R(t){return"0x"+t.quotient.toString(16)}var F=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,a,s,c=n.validateAndParseAddress(e.recipient),p=R(t.maximumAmountIn(e.allowedSlippage)),l=R(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",a=[l,f,c,d],s=p):u?(i=h?"swapExactTokensForETHSupportingFeeOnTransferTokens":"swapExactTokensForETH",a=[p,l,f,c,d],s="0x0"):(i=h?"swapExactTokensForTokensSupportingFeeOnTransferTokens":"swapExactTokensForTokens",a=[p,l,f,c,d],s="0x0");break;case n.TradeType.EXACT_OUTPUT:h&&r(!1),o?(i="swapETHForExactTokens",a=[l,f,c,d],s=p):u?(i="swapTokensForExactETH",a=[l,p,f,c,d],s="0x0"):(i="swapTokensForExactTokens",a=[l,p,f,c,d],s="0x0")}return{methodName:i,args:a,value:s}},t}();exports.InsufficientInputAmountError=x,exports.InsufficientReservesError=w,exports.MAINNET_INIT_CODE_HASH="0x8bd2e44ddacff4efbf1f9ead74bd191fe945f9824cae3109bfc068d82a3cb3d1",exports.MAINNET_V2_FACTORY="0x5a2a927bea6c5f4a48d4e0116049c1e36d52a528",exports.MINIMUM_LIQUIDITY=i,exports.Pair=O,exports.Route=E,exports.Router=F,exports.TESTNET_INIT_CODE_HASH="0x00f5a7aa52ba04ca4b2f7f2ef08b82a61ac4e8fead11ebd9c4fbc48edc95b708",exports.TESTNET_V2_FACTORY="0xd8350cf44cb326c81fdf91ded2bf92e6b785d64c",exports.Trade=C,exports.computePairAddress=I,exports.inputOutputComparator=P,exports.tradeComparator=_;
//# sourceMappingURL=hydra-v2-sdk.cjs.production.min.js.map