UNPKG

@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) 5.05 kB
import{Decimal as e,tenPow as r,formatUnits as t,parseUnits as n}from"../../utils/numerics/index.js";import{Logger as i}from"../../common/logger/index.js";import{encodeOrders as o,decodeOrder as l,lowestPossibleRate as a,calculateRequiredLiquidity as u}from"../../utils/encoders/index.js";import{encodedStrategyBigIntToStr as c}from"../../utils/serializers/index.js";const g=new i("utils.ts");function d(t,n,i){return new e(t.toString()).times(r(n,i)).toFixed()}function s(t,n,i){return 0==+t.toString()?"0":new e(1).div(t.toString()).times(r(i,n)).toFixed()}const w=e=>{const[r,t]=o([e.order0,e.order1]);return{token0:e.token0,token1:e.token1,order0:r,order1:t}},b=e=>({id:e.id,token0:e.token0,token1:e.token1,order0:l(e.order0),order1:l(e.order1),encoded:e});async function f(e,r){g.debug("parseStrategy called",arguments);const{id:n,token0:i,token1:o,order0:l,order1:a,encoded:u}=e,w=await r.fetchDecimals(i),b=await r.fetchDecimals(o),f=d(a.lowestRate,w,b),m=d(a.marginalRate,w,b),y=d(a.highestRate,w,b),P=s(l.highestRate,b,w),h=s(l.marginalRate,b,w),p=s(l.lowestRate,b,w),v=t(l.liquidity,w),S=t(a.liquidity,b),k=n.toString(),B=c(u);return g.debug("parseStrategy info:",{id:k,token0:i,token1:o,order0:l,order1:a,decimals0:w,decimals1:b,baseToken:i,quoteToken:o,buyPriceLow:f,buyPriceMarginal:m,buyPriceHigh:y,buyBudget:S,sellPriceLow:P,sellPriceMarginal:h,sellPriceHigh:p,sellBudget:v,encoded:B}),{id:k,baseToken:i,quoteToken:o,buyPriceLow:f,buyPriceMarginal:m,buyPriceHigh:y,buyBudget:S,sellPriceLow:P,sellPriceMarginal:h,sellPriceHigh:p,sellBudget:v,encoded:B}}function m(r,t,n,i,o,l,a,u,c,d,s,w){if(g.debug("buildStrategyObject called",arguments),new e(o).isNegative()||new e(l).isNegative()||new e(a).isNegative()||new e(c).isNegative()||new e(d).isNegative()||new e(s).isNegative())throw new Error("prices cannot be negative");if(new e(o).gt(l)||new e(o).gt(a)||new e(l).gt(a)||new e(c).gt(d)||new e(c).gt(s)||new e(d).gt(s))throw new Error("low/marginal price must be lower than or equal to marginal/high price");if(new e(u).isNegative()||new e(w).isNegative())throw new Error("budgets cannot be negative");const{order0:b,order1:f}=h(n,i,o,l,a,u,c,d,s,w);return g.debug("buildStrategyObject info:",{token0:r,token1:t,order0:b,order1:f}),{token0:r,token1:t,order0:b,order1:f}}function y(e,r,t,i,o,l){g.debug("createFromBuyOrder called",arguments);const a=n(l,r),u=d(t,r,e),c=d(i,r,e),s=d(o,r,e),w={liquidity:a.toString(),lowestRate:u,highestRate:s,marginalRate:c};return g.debug("createFromBuyOrder info:",{order:w}),w}function P(e,r,t,i,o,l){g.debug("createFromSellOrder called",arguments);const a=n(l,e),u=s(o,r,e),c=s(i,r,e),d=s(t,r,e),w={liquidity:a.toString(),lowestRate:u,highestRate:d,marginalRate:c};return g.debug("createFromSellOrder info:",{order:w}),w}function h(e,r,t,n,i,o,l,a,u,c){g.debug("createOrders called",arguments);const d=P(e,r,l,a,u,c),s=y(e,r,t,n,i,o);return g.debug("createOrders info:",{order0:d,order1:s}),{order0:d,order1:s}}const p=1e6;function v(r,t){return new e(r.toString()).mul(p).div(p-t).ceil()}function S(r,t){return new e(r.toString()).mul(p-t).div(p).floor()}function k(e,r,t){return t.lte(e)?e:t.gte(r)?r:t}function B(e,r){return{minBuyPrice:d(a.toString(),e,r),maxSellPrice:s(a.toString(),r,e)}}function O(r,t,n,i){g.debug("calculateOverlappingPrices called",arguments);const o=new e(i).div(100).plus(1),l=new e(t).div(o),a=new e(r).mul(o),u=k(new e(r),l,new e(n).div(o.sqrt())),c=k(a,new e(t),new e(n).mul(o.sqrt())),d={buyPriceHigh:l.toString(),buyPriceMarginal:u.toString(),sellPriceLow:a.toString(),sellPriceMarginal:c.toString(),buyPriceLow:r,sellPriceHigh:t,marketPrice:n};return g.debug("calculateOverlappingPrices info:",{prices:d}),d}function R(r,n,i,o,l,a,c){if(g.debug("calculateOverlappingSellBudget called",arguments),"0"===c)return"0";const{buyPriceHigh:d,sellPriceLow:s,sellPriceMarginal:w,buyPriceMarginal:b}=O(i,o,l,a);if(new e(w).gte(o))return"0";if(new e(b).lte(i))throw new Error("calculateOverlappingSellBudget called with zero buy range and non zero buy budget");const f=y(r,n,i,b,d,c),m=P(r,n,s,w,o,"0"),h=u(f,m),p=t(h,r);return g.debug("calculateOverlappingSellBudget info:",{sellBudget:p}),p}function M(r,n,i,o,l,a,c){if(g.debug("calculateOverlappingBuyBudget called",arguments),"0"===c)return"0";const{sellPriceLow:d,buyPriceHigh:s,sellPriceMarginal:w,buyPriceMarginal:b}=O(i,o,l,a);if(new e(b).lte(i))return"0";if(new e(w).gte(o))throw new Error("calculateOverlappingBuyBudget called with zero sell range and non zero sell budget");const f=y(r,n,i,b,s,"0"),m=P(r,n,d,w,o,c),h=u(m,f),p=t(h,n);return g.debug("calculateOverlappingBuyBudget info:",{buyBudget:p}),p}export{p as PPM_RESOLUTION,v as addFee,m as buildStrategyObject,M as calculateOverlappingBuyBudget,O as calculateOverlappingPrices,R as calculateOverlappingSellBudget,y as createFromBuyOrder,P as createFromSellOrder,h as createOrders,b as decodeStrategy,w as encodeStrategy,k as enforcePriceRange,B as getMinMaxPricesByDecimals,s as normalizeInvertedRate,d as normalizeRate,f as parseStrategy,S as subtractFee};