Skip to content

Commit

Permalink
Exchange calc
Browse files Browse the repository at this point in the history
  • Loading branch information
1aerostorm committed Nov 22, 2023
1 parent 7e9ec62 commit 3e1fa37
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/pages/api/v1/[[...all]].js
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -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

0 comments on commit 3e1fa37

Please sign in to comment.