UNPKG

@thenafi/v1-sdk

Version:

🛠 An SDK for building applications on top of THENA V1

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