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.77 kB
import{DefaultMulticallService as r,isETHAddress as e}from"../utils/index.js";import{Logger as t}from"../../common/logger/index.js";const a=new t("Reader.ts");function n(r){return{id:r[0],token0:r[2][0],token1:r[2][1],order0:{y:r[3][0][0],z:r[3][0][1],A:r[3][0][2],B:r[3][0][3]},order1:{y:r[3][1][0],z:r[3][1][1],A:r[3][1][2],B:r[3][1][3]}}}class o{_contracts;_multicallService;constructor(e,t){this._contracts=e,this._multicallService=t??new r(e.multicall)}_multicall(r,e){return this._multicallService.execute(r,e)}async strategy(r){a.debug("strategy called",r);try{return n(await this._contracts.carbonController.strategy(r))}catch(r){throw a.error("strategy error",r),r}}async strategies(r){a.debug("strategies called",r);try{const e=await this._multicall(r.map((r=>({contractAddress:this._contracts.carbonController.target,interface:this._contracts.carbonController.interface,methodName:"strategy",methodParameters:[r]}))));return a.debug("strategies results",e),e&&0!==e.length?e.map((r=>n(r[0]))):[]}catch(r){throw a.error("strategies error",r),r}}async pairs(){a.debug("pairs called");try{return(await this._contracts.carbonController.pairs()).map((r=>[r[0].toString(),r[1].toString()]))}catch(r){throw a.error("pairs error",r),r}}async strategiesByPair(r,e){a.debug("strategiesByPair called",r,e);try{const t=[];let a=0;const o=1e3;for(;;){const s=await this._contracts.carbonController.strategiesByPair(r,e,a,a+o)??[];if(t.push(...s.map((r=>n(r)))),s.length<o)break;a+=o}return t}catch(r){throw a.error("strategiesByPair error",r),r}}async strategiesByPairs(r){a.debug("strategiesByPairs called",r);try{const e=1e3,t=[],o=[];a.debug("strategiesByPairs first chunk");try{const s=await this._multicall(r.map((r=>({contractAddress:this._contracts.carbonController.target,interface:this._contracts.carbonController.interface,methodName:"strategiesByPair",methodParameters:[r[0],r[1],0,e]}))));if(a.debug("strategiesByPairs first chunk results count",s.length),!s||0===s.length)return[];s.forEach(((a,s)=>{const c=a[0]??[],i=r[s];t.push({pair:i,strategies:c.map((r=>n(r)))}),c.length===e&&o.push({pair:i,index:s})}))}catch(r){throw a.error("strategiesByPairs first chunk error",r),r}a.debug("number of pairs needing more",o.length);try{for(const{pair:r,index:a}of o){let o=e;for(;;){const s=await this._contracts.carbonController.strategiesByPair(r[0],r[1],o,o+e)??[];if(t[a].strategies.push(...s.map((r=>n(r)))),s.length<e)break;o+=e}}}catch(r){throw a.error("strategiesByPairs remaining chunks error",r),r}return t}catch(r){throw a.error("strategiesByPairs error",r),r}}async tokensByOwner(r){if(a.debug("tokensByOwner called",r),!r)return[];try{return(await this._contracts.voucher.tokensByOwner(r,0,0)).map((r=>BigInt(r)))}catch(r){throw a.error("tokensByOwner error",r),r}}async tradingFeePPM(){a.debug("tradingFeePPM called");try{const r=await this._contracts.carbonController.tradingFeePPM();return Number(r)}catch(r){throw a.error("tradingFeePPM error",r),r}}onTradingFeePPMUpdated(r){return this._contracts.carbonController.on(this._contracts.carbonController.getEvent("TradingFeePPMUpdated"),((e,t,n)=>{a.debug("TradingFeePPMUpdated fired with",{prevFeePPM:e,newFeePPM:t}),r(Number(e),Number(t))}))}async pairTradingFeePPM(r,e){a.debug("pairTradingFeePPM called",r,e);try{const t=await this._contracts.carbonController.pairTradingFeePPM(r,e);return Number(t)}catch(r){throw a.error("pairTradingFeePPM error",r),r}}async pairsTradingFeePPM(r){a.debug("pairsTradingFeePPM called",r);try{const e=await this._multicall(r.map((r=>({contractAddress:this._contracts.carbonController.target,interface:this._contracts.carbonController.interface,methodName:"pairTradingFeePPM",methodParameters:[r[0],r[1]]}))));return a.debug("pairsTradingFeePPM results",e),e&&0!==e.length?e.map(((e,t)=>[r[t][0],r[t][1],Number(e[0])])):[]}catch(r){throw a.error("pairsTradingFeePPM error",r),r}}onPairTradingFeePPMUpdated(r){return this._contracts.carbonController.on(this._contracts.carbonController.getEvent("PairTradingFeePPMUpdated"),((e,t,n,o,s)=>{a.debug("PairTradingFeePPMUpdated fired with",{token0:e,token1:t,prevFeePPM:n,newFeePPM:o}),r(e,t,Number(n),Number(o))}))}getDecimalsByAddress=async r=>{if(e(r))return 18;const t=await this._contracts.token(r).decimals();return Number(t)};getBlockNumber=async()=>this._contracts.provider.getBlockNumber();getBlock=async r=>{const e=await this._contracts.provider.getBlock(r);if(!e)throw new Error(`Block ${r} not found`);return{number:e.number,hash:e.hash??""}};async getEvents(r,e,t=2e3){const a=e-r+1,n=Math.ceil(a/t),o=Array.from({length:n},((a,n)=>{const o=r+n*t;return{start:o,end:Math.min(o+t-1,e)}}));return(await Promise.all(o.map((async({start:r,end:e})=>await this._contracts.provider.getLogs({address:this._contracts.carbonController.target,fromBlock:r,toBlock:e}))))).flat().map((r=>{const e=this._contracts.carbonController.interface.parseLog({topics:r.topics,data:r.data});if(!e)return null;const t=e.name;switch(t){case"StrategyCreated":case"StrategyUpdated":case"StrategyDeleted":{const a={id:e.args.id,token0:e.args.token0,token1:e.args.token1,order0:{A:e.args.order0.A,B:e.args.order0.B,y:e.args.order0.y,z:e.args.order0.z},order1:{A:e.args.order1.A,B:e.args.order1.B,y:e.args.order1.y,z:e.args.order1.z}};return{type:t,blockNumber:r.blockNumber,logIndex:r.index,data:a}}case"TradingFeePPMUpdated":{const a=e.args.newFeePPM;return{type:t,blockNumber:r.blockNumber,logIndex:r.index,data:a}}case"PairTradingFeePPMUpdated":{const a=[e.args.token0,e.args.token1,e.args.newFeePPM];return{type:t,blockNumber:r.blockNumber,logIndex:r.index,data:a}}default:return null}})).filter((r=>null!==r)).sort(((r,e)=>r.blockNumber!==e.blockNumber?r.blockNumber-e.blockNumber:r.logIndex-e.logIndex))}}export{o as default};