Skip to content

Commit

Permalink
Fix SQL ORDER BY
Browse files Browse the repository at this point in the history
  • Loading branch information
protinam committed Feb 10, 2018
1 parent 9504f30 commit 879e6bd
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "0x-orderbook",
"name": "0x-orderbook-server",
"version": "0.1.0",
"description": "Open-source 0x orderbook server, written for ForkDelta.",
"main": "index.js",
Expand Down
5 changes: 5 additions & 0 deletions src/db.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ const { ZeroEx } = require('0x.js')
module.exports = (config) => {
const sequelize = new Sequelize(config.database, config.username, config.password, config)

sequelize.query('CREATE OR REPLACE FUNCTION add(text, text) RETURNS integer AS \'select ($1::integer) + ($2::integer);\' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT')
sequelize.query('CREATE OR REPLACE FUNCTION sub(text, text) RETURNS integer AS \'select ($1::integer) - ($2::integer);\' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT')
sequelize.query('CREATE OR REPLACE FUNCTION div(text, text) RETURNS integer AS \'select ($1::integer) / ($2::integer);\' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT')
sequelize.query('CREATE OR REPLACE FUNCTION mul(text, text) RETURNS integer AS \'select ($1::integer) * ($2::integer);\' LANGUAGE SQL IMMUTABLE RETURNS NULL ON NULL INPUT')

const Order = sequelize.define('order', {
hash: {type: Sequelize.TEXT, allowNull: false, primaryKey: true},
exchangeContractAddress: {type: Sequelize.TEXT, allowNull: false},
Expand Down
15 changes: 8 additions & 7 deletions src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ const paged = (func) => {
const go = (config) => {
const { sequelize, Op, Order, orderToJSON, orderFromJSON } = db(config.sequelize)

const price = sequelize.fn('div', sequelize.col('takerTokenAmount'), sequelize.col('makerTokenAmount'))
const totalFees = sequelize.fn('add', sequelize.col('makerFee'), sequelize.col('takerFee'))

const app = express()
app.use(bodyParser.json())
if (config.production) {
Expand All @@ -64,7 +67,7 @@ const go = (config) => {
query.where.takerTokenAddress = req.query.takerTokenAddress
}
if (req.query.makerTokenAddress && req.query.takerTokenAddress) {
query.order = sequelize.literal('(takerTokenAmount / makerTokenAmount) ASC (makerFee + takerFee) ASC expirationUnixTimestampSec ASC')
query.order = [[price, 'ASC'], [totalFees, 'ASC'], ['expirationUnixTimestampSec', 'ASC']]
}
if (req.query.tokenAddress) {
query.where[Op.or] = [{makerTokenAddress: req.query.tokenAddress, takerTokenAddress: req.query.tokenAddress}]
Expand Down Expand Up @@ -99,13 +102,13 @@ const go = (config) => {
const quoteTokenAddress = req.query.quoteTokenAddress
const bidQuery = {
where: {makerTokenAddress: baseTokenAddress, takerTokenAddress: quoteTokenAddress},
order: sequelize.literal('(takerTokenAmount / makerTokenAmount) DESC (makerFee + takerFee) ASC expirationUnixTimestampSec ASC'),
order: [[price, 'DESC'], [totalFees, 'ASC'], ['expirationUnixTimestampSec', 'ASC']],
limit,
offset
}
const askQuery = {
where: {makerTokenAddress: quoteTokenAddress, takerTokenAddress: baseTokenAddress},
order: sequelize.literal('(takerTokenAmount / makerTokenAmount) ASC (makerFee + takerFee) ASC expirationUnixTimestampSec ASC'),
order: [[price, 'ASC'], [totalFees, 'ASC'], ['expirationUnixTimestampSec', 'ASC']],
limit,
offset
}
Expand All @@ -132,7 +135,7 @@ const go = (config) => {
})
}))

app.use('v0', router)
app.use('/v0', router)

sequelize
.sync()
Expand All @@ -144,6 +147,4 @@ const go = (config) => {
})
}

module.exports = {
go: go
}
module.exports = { go }

0 comments on commit 879e6bd

Please sign in to comment.