@raydium-io/raydium-sdk-v2
Version:
An SDK for building applications on top of Raydium.
2 lines • 2.92 kB
JavaScript
import A from"bn.js";import Q from"decimal.js";import n from"bn.js";var p=new n(1).shln(64),l=64,d=new n(1).shln(128),T=new n(1).shln(64).subn(1),R=new n(1).shln(128).subn(1);var h=new n("4295048016"),I=new n("79226673521066979257578248091"),g=new n("59543866431248"),D=new n("184467440737095516"),M=new n("15793534762490258745");var v=new n("18446743708227953217"),Z=[{bit:0,factor:new n("fffcb933bd6fb800",16)},{bit:1,factor:new n("fff97272373d4000",16)},{bit:2,factor:new n("fff2e50f5f657000",16)},{bit:3,factor:new n("ffe5caca7e10f000",16)},{bit:4,factor:new n("ffcb9843d60f7000",16)},{bit:5,factor:new n("ff973b41fa98e800",16)},{bit:6,factor:new n("ff2ea16466c9b000",16)},{bit:7,factor:new n("fe5dee046a9a3800",16)},{bit:8,factor:new n("fcbe86c7900bb000",16)},{bit:9,factor:new n("f987a7253ac65800",16)},{bit:10,factor:new n("f3392b0822bb6000",16)},{bit:11,factor:new n("e7159475a2caf000",16)},{bit:12,factor:new n("d097f3bdfd2f2000",16)},{bit:13,factor:new n("a9f746462d9f8000",16)},{bit:14,factor:new n("70d869a156f31c00",16)},{bit:15,factor:new n("31be135f97ed3200",16)},{bit:16,factor:new n("9aa508b5b85a500",16)},{bit:17,factor:new n("5d6af8dedc582c",16)},{bit:18,factor:new n("2216e584f5fa",16)}];var f=new n(0),s=new n(1),a=new n(-1);var F=new n("18446744073700000000");function _(u,e,t){if(t.isZero())throw new Error("Division by zero");return u.mul(e).div(t)}function B(u,e,t){if(t.isZero())throw new Error("Division by zero");let o=u.mul(e),i=o.div(t);return o.mod(t).isZero()?i:i.addn(1)}function E(u,e){return u.div(e).add(u.mod(e).isZero()?f:s)}var L=new A(1).shln(256).subn(1);var x=class{static getNextSqrtPriceFromAmountARoundingUp(e,t,o,i){if(o.isZero())return e;let r=t.shln(l);if(i){let N=o.mul(e),c=r.add(N);if(c.gte(r))return B(r,e,c);let m=_(r,s,e);return B(r,s,m.add(o))}else{let N=o.mul(e);if(r.lte(N))throw new Error("Insufficient liquidity for token0 removal");let c=r.sub(N);return B(r,e,c)}}static getNextSqrtPriceFromAmountBRoundingDown(e,t,o,i){if(o.isZero())return e;if(i){let r=o.shln(l).div(t);return e.add(r)}else{let r=E(o.shln(l),t);return e.sub(r)}}static getNextSqrtPriceFromInput(e,t,o,i){if(!e.gt(f))throw Error("sqrtPriceX64.gt(BN_ZERO)");if(!t.gt(f))throw Error("liquidity.gt(BN_ZERO)");return i?this.getNextSqrtPriceFromAmountARoundingUp(e,t,o,!0):this.getNextSqrtPriceFromAmountBRoundingDown(e,t,o,!0)}static getNextSqrtPriceFromOutput(e,t,o,i){if(!e.gt(f))throw Error("sqrtPriceX64.gt(BN_ZERO)");if(!t.gt(f))throw Error("liquidity.gt(BN_ZERO)");return i?this.getNextSqrtPriceFromAmountBRoundingDown(e,t,o,!1):this.getNextSqrtPriceFromAmountARoundingUp(e,t,o,!1)}static getAmountADeltaUnsigned(e,t,o,i){e.gt(t)&&([e,t]=[t,e]);let r=t.sub(e),N=o.mul(r).shln(64),c=e.mul(t);return i?B(N,s,c):_(N,s,c)}static getAmountBDeltaUnsigned(e,t,o,i){e.gt(t)&&([e,t]=[t,e]);let r=t.sub(e);return i?B(o,r,p):_(o,r,p)}};export{x as SqrtPriceMath};
//# sourceMappingURL=sqrtPriceMath.mjs.map