Diff Contract: marketmaker

(a111c5e vs. b416595)
  • const BASE_SYMBOL="STEEMP",BASE_SYMBOL_PRECISION=8,DEBUG_MODE=!1;actions.createSSC=async()=>{};const getOrderBook=async(table,symbol,descending)=>await api.db.findInTable("market",table,{symbol:symbol},0,0,[{index:"priceDec",descending:descending},{index:"_id",descending:!1}]),cancelOrders=async(orders,type)=>{for(let k=0;k<orders.length;k+=1){const order=orders[k];await api.executeSmartContract("market","cancel",{account:order.account,type:type,id:order.txId})}},getClosestAmount=(baseCurrency,price,precision)=>baseCurrency.dividedBy(price).toFixed(precision),countBalanceInBuyOrders=orders=>orders.reduce((t,v)=>t.plus(api.BigNumber(v.tokensLocked)),api.BigNumber(0)),countBalanceInSellOrders=orders=>orders.reduce((t,v)=>t.plus(api.BigNumber(v.quantity)),api.BigNumber(0)),getOrderData=(orders,myOrders,account)=>{const data={};return data.topOrder=orders.length>0?orders[0]:null,data.isTopMine=!(!data.topOrder||data.topOrder.account!==account),data.topPrice=data.topOrder?api.BigNumber(data.topOrder.price):api.BigNumber(0),data.myTopPrice=myOrders.length>0?api.BigNumber(myOrders[0].price):api.BigNumber(0),data.numOrdersAtMyPrice=0,data.myTopPrice.gt(0)&&(data.numOrdersAtMyPrice=orders.reduce((t,v)=>api.BigNumber(v.price).eq(data.myTopPrice)?t+1:t,0),data.myTopPrice.eq(data.topPrice)&&(data.isTopMine=!0)),data},tickMarket=async market=>{if(!market.isEnabled)return;const maxBidPrice=api.BigNumber(market.maxBidPrice),minSellPrice=api.BigNumber(market.minSellPrice);let maxBaseToSpend=api.BigNumber(market.maxBaseToSpend);const minBaseToSpend=api.BigNumber(market.minBaseToSpend);let maxTokensToSell=api.BigNumber(market.maxTokensToSell);const minTokensToSell=api.BigNumber(market.minTokensToSell),priceIncrement=api.BigNumber(market.priceIncrement),minSpread=api.BigNumber(market.minSpread);let baseBalance=api.BigNumber(0),tokenBalance=api.BigNumber(0);const balances=await api.db.findInTable("tokens","balances",{account:market.account,symbol:{$in:["STEEMP",market.symbol]}},2,0,[{index:"_id",descending:!1}]);for(let j=0;j<balances.length;j+=1){const balance=balances[j];"STEEMP"===balance.symbol?baseBalance=api.BigNumber(balance.balance):balance.symbol===market.symbol&&(tokenBalance=api.BigNumber(balance.balance))}const buyOrders=await getOrderBook("buyBook",market.symbol,!0),sellOrders=await getOrderBook("sellBook",market.symbol,!1),myBuyOrders=buyOrders.filter(o=>o.account===market.account),mySellOrders=sellOrders.filter(o=>o.account===market.account),isBuyBookEmpty=0===buyOrders.length||buyOrders.length===myBuyOrders.length,isSellBookEmpty=0===sellOrders.length||sellOrders.length===mySellOrders.length;if(isBuyBookEmpty&&isSellBookEmpty)return void 0;baseBalance=baseBalance.plus(myBuyOrders.reduce((t,v)=>t.plus(api.BigNumber(v.tokensLocked)),api.BigNumber(0))),tokenBalance=tokenBalance.plus(countBalanceInSellOrders(mySellOrders)),baseBalance.lt(maxBaseToSpend)&&(maxBaseToSpend=baseBalance),tokenBalance.lt(maxTokensToSell)&&(maxTokensToSell=tokenBalance);const bb=getOrderData(buyOrders,myBuyOrders,market.account),sb=getOrderData(sellOrders,mySellOrders,market.account);let newTopBuyPrice=bb.topPrice.plus(priceIncrement);newTopBuyPrice.gt(maxBidPrice)&&(newTopBuyPrice=maxBidPrice);let newTopSellPrice=sb.topPrice.minus(priceIncrement);if(newTopSellPrice.lt(minSellPrice)&&(newTopSellPrice=minSellPrice),sellOrders.length>0){if(newTopBuyPrice.gte(newTopSellPrice))return;if(newTopSellPrice.minus(newTopBuyPrice).lt(minSpread))return}let shouldReplaceBuyOrder=!1;myBuyOrders.length>0&&(bb.myTopPrice.lt(bb.topPrice)&&bb.topPrice.lt(maxBidPrice)||bb.myTopPrice.gt(maxBidPrice)||bb.numOrdersAtMyPrice>1&&bb.isTopMine||bb.numOrdersAtMyPrice>1&&!bb.isTopMine&&bb.myTopPrice.lt(newTopBuyPrice))&&(await cancelOrders(myBuyOrders,"buy"),shouldReplaceBuyOrder=!0);let shouldReplaceSellOrder=!1;if(mySellOrders.length>0&&(sb.myTopPrice.gt(sb.topPrice)&&sb.topPrice.gt(minSellPrice)||sb.myTopPrice.lt(minSellPrice)||sb.numOrdersAtMyPrice>1&&sb.isTopMine||sb.numOrdersAtMyPrice>1&&!sb.isTopMine&&sb.myTopPrice.gt(newTopSellPrice))&&(await cancelOrders(mySellOrders,"sell"),shouldReplaceSellOrder=!0),(0===myBuyOrders.length||shouldReplaceBuyOrder)&&maxBaseToSpend.gte(minBaseToSpend)){const tokensToBuy=(baseCurrency=maxBaseToSpend,price=newTopBuyPrice,precision=market.precision,baseCurrency.dividedBy(price).toFixed(precision));await api.executeSmartContract("market","buy",{account:market.account,symbol:market.symbol,quantity:tokensToBuy,price:newTopBuyPrice.toFixed(8)})}var baseCurrency,price,precision;0!==mySellOrders.length&&!shouldReplaceSellOrder||!maxTokensToSell.gte(minTokensToSell)||isSellBookEmpty||await api.executeSmartContract("market","sell",{account:market.account,symbol:market.symbol,quantity:maxTokensToSell.toFixed(market.precision),price:newTopSellPrice.toFixed(8)})};actions.tick=async payload=>{if("null"!==api.sender)return;const{markets:markets}=payload;for(let i=0;i<markets.length;i+=1)await tickMarket(markets[i])};
  • const BASE_SYMBOL="STEEMP",BASE_SYMBOL_PRECISION=8,DEBUG_MODE=!1;actions.createSSC=async()=>{};const getOrderBook=async(table,symbol,descending)=>await api.db.findInTable("market",table,{symbol:symbol},0,0,[{index:"priceDec",descending:descending},{index:"_id",descending:!1}]),cancelOrders=async(orders,type)=>{for(let k=0;k<orders.length;k+=1){const order=orders[k];await api.executeSmartContract("market","cancel",{account:order.account,type:type,id:order.txId})}},getClosestAmount=(baseCurrency,price,precision)=>baseCurrency.dividedBy(price).toFixed(precision),countBalanceInBuyOrders=orders=>orders.reduce((t,v)=>t.plus(api.BigNumber(v.tokensLocked)),api.BigNumber(0)),countBalanceInSellOrders=orders=>orders.reduce((t,v)=>t.plus(api.BigNumber(v.quantity)),api.BigNumber(0)),getOrderData=(orders,myOrders,account)=>{const data={};return data.topOrder=orders.length>0?orders[0]:null,data.isTopMine=!(!data.topOrder||data.topOrder.account!==account),data.topPrice=data.topOrder?api.BigNumber(data.topOrder.price):api.BigNumber(0),data.myTopPrice=myOrders.length>0?api.BigNumber(myOrders[0].price):api.BigNumber(0),data.numOrdersAtMyPrice=0,data.myTopPrice.gt(0)&&(data.numOrdersAtMyPrice=orders.reduce((t,v)=>api.BigNumber(v.price).eq(data.myTopPrice)?t+1:t,0),data.myTopPrice.eq(data.topPrice)&&(data.isTopMine=!0)),data},tickMarket=async(market,txIdPrefix)=>{if(!market.isEnabled)return;const maxBidPrice=api.BigNumber(market.maxBidPrice),minSellPrice=api.BigNumber(market.minSellPrice);let maxBaseToSpend=api.BigNumber(market.maxBaseToSpend);const minBaseToSpend=api.BigNumber(market.minBaseToSpend);let maxTokensToSell=api.BigNumber(market.maxTokensToSell);const minTokensToSell=api.BigNumber(market.minTokensToSell),priceIncrement=api.BigNumber(market.priceIncrement),minSpread=api.BigNumber(market.minSpread);let baseBalance=api.BigNumber(0),tokenBalance=api.BigNumber(0);const balances=await api.db.findInTable("tokens","balances",{account:market.account,symbol:{$in:["STEEMP",market.symbol]}},2,0,[{index:"_id",descending:!1}]);for(let j=0;j<balances.length;j+=1){const balance=balances[j];"STEEMP"===balance.symbol?baseBalance=api.BigNumber(balance.balance):balance.symbol===market.symbol&&(tokenBalance=api.BigNumber(balance.balance))}const buyOrders=await getOrderBook("buyBook",market.symbol,!0),sellOrders=await getOrderBook("sellBook",market.symbol,!1),myBuyOrders=buyOrders.filter(o=>o.account===market.account),mySellOrders=sellOrders.filter(o=>o.account===market.account),isBuyBookEmpty=0===buyOrders.length||buyOrders.length===myBuyOrders.length,isSellBookEmpty=0===sellOrders.length||sellOrders.length===mySellOrders.length;if(isBuyBookEmpty&&isSellBookEmpty)return void 0;baseBalance=baseBalance.plus(myBuyOrders.reduce((t,v)=>t.plus(api.BigNumber(v.tokensLocked)),api.BigNumber(0))),tokenBalance=tokenBalance.plus(countBalanceInSellOrders(mySellOrders)),baseBalance.lt(maxBaseToSpend)&&(maxBaseToSpend=baseBalance),tokenBalance.lt(maxTokensToSell)&&(maxTokensToSell=tokenBalance);const bb=getOrderData(buyOrders,myBuyOrders,market.account),sb=getOrderData(sellOrders,mySellOrders,market.account);let newTopBuyPrice=bb.topPrice.plus(priceIncrement);newTopBuyPrice.gt(maxBidPrice)&&(newTopBuyPrice=maxBidPrice);let newTopSellPrice=sb.topPrice.minus(priceIncrement);if(newTopSellPrice.lt(minSellPrice)&&(newTopSellPrice=minSellPrice),sellOrders.length>0){if(newTopBuyPrice.gte(newTopSellPrice))return;if(newTopSellPrice.minus(newTopBuyPrice).lt(minSpread))return}let shouldReplaceBuyOrder=!1;myBuyOrders.length>0&&(bb.myTopPrice.lt(bb.topPrice)&&bb.topPrice.lt(maxBidPrice)||bb.myTopPrice.gt(maxBidPrice)||bb.numOrdersAtMyPrice>1&&bb.isTopMine||bb.numOrdersAtMyPrice>1&&!bb.isTopMine&&bb.myTopPrice.lt(newTopBuyPrice))&&(await cancelOrders(myBuyOrders,"buy"),shouldReplaceBuyOrder=!0);let shouldReplaceSellOrder=!1;mySellOrders.length>0&&(sb.myTopPrice.gt(sb.topPrice)&&sb.topPrice.gt(minSellPrice)||sb.myTopPrice.lt(minSellPrice)||sb.numOrdersAtMyPrice>1&&sb.isTopMine||sb.numOrdersAtMyPrice>1&&!sb.isTopMine&&sb.myTopPrice.gt(newTopSellPrice))&&(await cancelOrders(mySellOrders,"sell"),shouldReplaceSellOrder=!0);let orderCount=0;if((0===myBuyOrders.length||shouldReplaceBuyOrder)&&maxBaseToSpend.gte(minBaseToSpend)){const tokensToBuy=(baseCurrency=maxBaseToSpend,price=newTopBuyPrice,precision=market.precision,baseCurrency.dividedBy(price).toFixed(precision));0,await api.executeSmartContract("market","buy",{account:market.account,txId:`${txIdPrefix}-${orderCount}`,symbol:market.symbol,quantity:tokensToBuy,price:newTopBuyPrice.toFixed(8)}),orderCount+=1}var baseCurrency,price,precision;0!==mySellOrders.length&&!shouldReplaceSellOrder||!maxTokensToSell.gte(minTokensToSell)||isSellBookEmpty||(await api.executeSmartContract("market","sell",{account:market.account,txId:`${txIdPrefix}-${orderCount}`,symbol:market.symbol,quantity:maxTokensToSell.toFixed(market.precision),price:newTopSellPrice.toFixed(8)}),orderCount+=1)};actions.tick=async payload=>{if("null"!==api.sender)return;const{markets:markets,txIdBase:txIdBase}=payload;for(let i=0;i<markets.length;i+=1)await tickMarket(markets[i],`${txIdBase}-${i}`)};