@bancor/carbon-sdk
Version:
The SDK is a READ-ONLY tool, intended to facilitate working with Carbon contracts. It's a convenient wrapper around our matching algorithm, allowing programs and users get a ready to use transaction data that will allow them to manage strategies and fulfi
2 lines (1 loc) • 3.19 kB
JavaScript
;var t=require("ethers");const e=require("decimal.js").clone();e.set({precision:100,rounding:e.ROUND_HALF_DOWN,toExpNeg:-300,toExpPos:300});const n=2n**48n,r=new e(10),o=t=>new e(t.toString()),i=t=>BigInt(t.toFixed());function s(t,n){return new e(t).toFixed(n,e.ROUND_DOWN).replace(/(\.\d*?[1-9])0+$|\.0*$/,"$1")}function a(t){return t>0n?e.log2(t.toString()).add(1).floor().toNumber():0}const l=t=>{const r=new e(n.toString()),o=i(t.sqrt().mul(r).floor()),s=a(o/n);return o>>BigInt(s)<<BigInt(s)},g=t=>{const r=new e(n.toString());return t.div(r).pow(2)},c=g(new e(1)),u=t=>{const e=a(t/n),r=t>>BigInt(e);return n*BigInt(e)|r},d=t=>t%n<<BigInt(Number(t/n)),w=(t,n)=>{const r=new e(t.liquidity),o=new e(t.lowestRate),s=new e(t.highestRate),a=new e(t.marginalRate),g=i(r),c=l(o),d=l(s),w=l(a);if(0n===c&&(0n!==d||0n!==w))throw new Error(`Encoded lowest rate cannot be zero unless the highest and marginal rates are also zero. This may be the result of passing a rate that is zero or too close to zero:\nlowestRate = ${o}, highestRate = ${s}, marginalRate = ${a}\nL = ${c}, H = ${d}, M = ${w}`);if(!(d>=w&&w>c||d===w&&w===c||d>w&&w===c&&0n===g))throw new Error(`Either one of the following must hold:\n- highestRate >= marginalRate > lowestRate\n- highestRate == marginalRate == lowestRate\n- (highestRate > marginalRate == lowestRate) AND liquidity == 0\n(highestRate = ${s}, marginalRate = ${a}, lowestRate = ${o}), liquidity = ${r}`);return{y:g,z:void 0!==n?n:d===w||0n===g?g:g*(d-c)/(w-c),A:u(d-c),B:u(c)}},R=t=>{const n=o(w(t).z),r=new e(t.lowestRate),s=new e(t.highestRate),a=r.mul(s).sqrt();return i(n.div(a).floor())};exports.BigNumberMax=(t,e)=>{const n=BigInt(t),r=BigInt(e);return n>r?n:r},exports.BigNumberMin=(t,e)=>{const n=BigInt(t),r=BigInt(e);return n<r?n:r},exports.BnToDec=o,exports.DecToBn=i,exports.Decimal=e,exports.MAX_UINT256=2n**256n-1n,exports.ONE=n,exports.TEN=r,exports.areScaledRatesEqual=(t,n)=>{const r=new e(t),o=new e(n);return l(r)===l(o)},exports.calculateCorrelatedZ=R,exports.calculateRequiredLiquidity=(t,n)=>{const r=R(t),o=l(new e(n.lowestRate)),i=l(new e(n.highestRate));return(r*(l(new e(n.marginalRate))-o)/(i-o)).toString()},exports.decodeFloat=d,exports.decodeOrder=t=>{const e=o(t.y),n=o(t.z),r=o(d(t.A)),i=o(d(t.B));return{liquidity:e.toString(),lowestRate:g(i).toString(),highestRate:g(i.add(r)).toString(),marginalRate:g(e.eq(n)?i.add(r):i.add(r.mul(e).div(n))).toString()}},exports.decodeRate=g,exports.encodeFloat=u,exports.encodeOrder=w,exports.encodeOrders=([t,n])=>{const r=new e(t.liquidity),o=new e(t.lowestRate),i=new e(t.highestRate),s=new e(n.liquidity),a=new e(n.lowestRate),l=new e(n.highestRate);return r.eq(0)&&s.gt(0)&&a.gt(0)&&l.gt(0)?[w(t,R(n)),w(n)]:s.eq(0)&&r.gt(0)&&o.gt(0)&&i.gt(0)?[w(t),w(n,R(t))]:[w(t),w(n)]},exports.encodeRate=l,exports.formatUnits=function(n,r){const o=BigInt(n),i=t.formatUnits(o,r);return new e(i).toFixed()},exports.isOrderEncodable=t=>{try{return w(t),!0}catch{return!1}},exports.lowestPossibleRate=c,exports.mulDiv=(t,e,n)=>e===n?t:t*e/n,exports.parseUnits=function(e,n){const r=s(e,n);return t.parseUnits(r,n)},exports.tenPow=(t,e)=>{const n=t-e;return r.pow(n)},exports.trimDecimal=s;