Skip to content

Commit

Permalink
Done
Browse files Browse the repository at this point in the history
  • Loading branch information
huuhait committed Nov 24, 2018
1 parent ae5638e commit 891a92a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 45 deletions.
85 changes: 49 additions & 36 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ const express = require('express')
const app = express()

const morgan = require('morgan')
app.use(morgan('tiny'))

const cors = require('cors')
app.use(cors())

app.use(morgan('tiny'))
app.use(cors({credentials: true, origin: true}))

const PeatioUtils = require('./peatio/utils')
const PeatioAPI = require('./peatio/api')
Expand Down Expand Up @@ -41,38 +41,29 @@ const RESOLUTIONS_INTERVALS_MAP = {

function convertSymbolToSearch(symbol) {
return {
symbol: symbol.symbol,
full_name: symbol.symbol,
description: symbol.baseAsset + ' / ' + symbol.quoteAsset,
ticker: symbol.symbol,
exchange: 'PEATIO',
type: SEPARATE_BY_QUOTE ? symbol.quoteAsset.toLowerCase() : 'crypto'
symbol: symbol.id.toUpperCase(),
full_name: symbol.id.toUpperCase(),
description: symbol.ask_unit.toUpperCase() + ' / ' + symbol.bid_unit.toUpperCase(),
ticker: symbol.id.toUpperCase(),
exchange: 'STOCK',
type: SEPARATE_BY_QUOTE ? symbol.bid_unit.toLowerCase() : 'crypto'
}
}

function convertSymbolToResolve(symbol) {
function pricescale(symbol) {
for (let filter of symbol.filters) {
if (filter.filterType == 'PRICE_FILTER') {
return Math.round(1 / parseFloat(filter.tickSize))
}
}
return 1
}

return {
name: symbol.symbol,
ticker: symbol.symbol,
description: `${symbol.baseAsset}/${symbol.quoteAsset}`,
type: SEPARATE_BY_QUOTE ? symbol.quoteAsset.toLowerCase() : 'crypto',
name: symbol.id.toUpperCase(),
ticker: symbol.id.toUpperCase(),
description: `${symbol.ask_unit.toUpperCase()}/${symbol.bid_unit.toUpperCase()}`,
type: SEPARATE_BY_QUOTE ? symbol.bid_unit.toLowerCase() : 'crypto',
session: '24x7',
exchange: 'PEATIO',
listed_exchange: 'PEATIO',
timezone: 'Etc/UTC',
exchange: 'STOCK',
listed_exchange: 'STOCK',
timezone: 'Asia/Singapore',
has_intraday: true,
has_daily: true,
has_weekly_and_monthly: true,
pricescale: pricescale(symbol),
pricescale: Math.pow(10, symbol.bid_precision),
minmovement: 1,
minmov: 1,
minmovement2: 0,
Expand All @@ -83,7 +74,7 @@ function convertSymbolToResolve(symbol) {
function convertKlinesToBars(klines) {
return {
s: 'ok',
t: klines.map(b => Math.floor(b[0] / 1000)),
t: klines.map(b => parseFloat(b[0])),
c: klines.map(b => parseFloat(b[4])),
o: klines.map(b => parseFloat(b[1])),
h: klines.map(b => parseFloat(b[2])),
Expand All @@ -95,10 +86,11 @@ function convertKlinesToBars(klines) {
function resolve(ticker) {
const comps = ticker.split(':')
const exchange = (comps.length > 1 ? comps[0] : '').toUpperCase()
const symbol = (comps.length > 1 ? comps[1] : ticker).toUpperCase()
const symbol = (comps.length > 1 ? comps[1] : ticker)
console.log(symbol)

for (let item of symbols) {
if (item.symbol == symbol && (exchange.length == 0 || exchange == 'PEATIO')) {
if (item.id == symbol.toLowerCase() && (exchange.length == 0 || exchange == 'STOCK')) {
return item
}
}
Expand Down Expand Up @@ -128,7 +120,7 @@ app.get('/config', (req, res) => {
supports_time: true,
exchanges: [
{
value: 'PEATIO',
value: 'STOCK',
name: 'Peatio',
desc: ''
}
Expand Down Expand Up @@ -157,6 +149,19 @@ app.get('/symbols', (req, res) => {
res.send(convertSymbolToResolve(symbol))
})

app.get('/quotes', (req, res) => {
if (!req.query.symbols) {
return res.status(400).send({ s: 'error', errmsg: 'Need symbols in query' })
}

const symbols = resolve(req.query.symbols)
if (!symbols) {
return res.status(404).send({ s: 'no_data' })
}

res.send(convertSymbolToResolve(symbols))
})

app.get('/search', (req, res) => {
res.send(searcher.search(
req.query.query,
Expand All @@ -177,9 +182,6 @@ app.get('/history', (req, res) => {
return res.status(400).send({s: 'error', errmsg: 'Need to in query'})
}

from *= 1000
to *= 1000

if (!req.query.symbol) {
return res.status(400).send({s: 'error', errmsg: 'Need symbol in query'})
}
Expand All @@ -188,7 +190,8 @@ app.get('/history', (req, res) => {
return res.status(400).send({s: 'error', errmsg: 'Need resolution in query'})
}

const interval = RESOLUTIONS_INTERVALS_MAP[req.query.resolution]
const interval = req.query.resolution
const symbol = req.query.symbol.toLowerCase()
if (!interval) {
return res.status(400).send({s: 'error', errmsg: 'Unsupported resolution'})
}
Expand All @@ -211,7 +214,7 @@ app.get('/history', (req, res) => {
}

function getKlines(from, to) {
peatio.klines(req.query.symbol, interval, from, to, 500).then(klines => {
peatio.klines(symbol, interval, from, to, 500).then(klines => {
totalKlines = totalKlines.concat(klines)
//console.log(klines.length)

Expand All @@ -232,7 +235,7 @@ app.get('/history', (req, res) => {

app.get('/time', (req, res) => {
peatio.time().then(json => {
res.send(Math.floor(json.serverTime / 1000) + '')
res.send(Math.floor(json.serverTime) + '')
}).catch(err => {
console.error(err)
res.status(500).send()
Expand All @@ -245,3 +248,13 @@ function listen() {
console.log(`Listening on port ${port}\n`)
})
}

peatio.exchangeInfo().then(info => {
console.log(`Load ${info.length} symbols`)
symbols = info
searcher = new Searcher(info.map(convertSymbolToSearch))
listen()
}).catch(err => {
console.error(err)
process.exit(1)
})
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"api"
],
"dependencies": {
"cors": "^2.8.4",
"cors": "^2.8.5",
"express": "^4.16.3",
"morgan": "^1.9.0",
"request": "^2.88.0"
Expand Down
11 changes: 8 additions & 3 deletions peatio/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ const request = require('request')

module.exports = class PeatioAPI {
time() {
return this.request('/api/v1/timestamp')
return this.request('/api/v2/timestamp')
}

klines(symbol, interval, startTime, endTime, limit) {
return this.request('/api/v2/k_with_pending_trades', { qs: { symbol, interval, startTime, endTime, limit } })
exchangeInfo() {
return this.request('/api/v2/markets')
}

klines(market, period, time_from, time_to, limit) {
return this.request('/api/v2/k?market='+market+'&period='+period+'&time_from='+time_from+'&time_to='+time_to+'&limit='+limit)
}

request(path, options) {
console.log(path)
return new Promise((resolve, reject) => {
request('http://api.wb.local' + path, options, (err, res, body) => {
if (err) return reject(err)
Expand Down
2 changes: 1 addition & 1 deletion peatio/utils.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
quotes: (symbols) => {
return [...symbols.reduce((a, s) => a.add(s.quoteAsset), new Set())]
return [...symbols.reduce((a, s) => a.add(s.bid_unit), new Set())]
}
}
1 change: 0 additions & 1 deletion searcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ module.exports = class Searcher {
if (exchange.length > 0 && exchange != symbol.exchange) {
continue
}

if (symbol.symbol.toUpperCase().indexOf(query) >= 0) {
result.push(symbol)
}
Expand Down

0 comments on commit 891a92a

Please sign in to comment.