diff --git a/src/pages/api/v1/[[...all]].js b/src/pages/api/v1/[[...all]].js index d8b4944..cda41d1 100644 --- a/src/pages/api/v1/[[...all]].js +++ b/src/pages/api/v1/[[...all]].js @@ -1,6 +1,7 @@ import config from 'config' import git from 'git-rev-sync' import golos from 'golos-lib-js' +import { Asset, Price } from 'golos-lib-js/lib/utils' import nextConnect from '@/nextConnect' import { parseMarketPair } from '@/utils/misc' @@ -219,4 +220,51 @@ let handler = nextConnect({ attachParams: true, }) res.json(ret) }) + .get('/api/v1/exchange/:sell/:buy_sym', async (req, res) => { + let { sell, buy_sym } = req.params + try { + sell = await Asset(sell.split('%20').join(' ')) + } catch (err) { + res.json({ + error: 'wrong_sell_asset' + }) + return + } + + const orders = await golos.api.getOrderBookExtendedAsync(500, [sell.symbol, buy_sym]) + + if (!orders.bids.length) { + res.json({ + error: 'no_orders' + }) + return + } + + let ret, best_price, limit_price + for (const bid of orders.bids) { + if (!ret) { + ret = await Asset(bid.order_price.quote) + ret.amount = 0 + } + + const price = await Price(bid.order_price) + best_price = best_price || price.clone() + limit_price = price.clone() + + const amount = sell.min(bid.asset1) + const receive = amount.mul(price) + ret = ret.plus(receive) + sell = sell.minus(bid.asset1) + + if (sell.lte(0)) { + break + } + } + res.json({ + receive: ret, + best_price, + limit_price + }) + }) + export default handler