Skip to content

Commit e43d0f8

Browse files
committed
Basic server
1 parent b8ef2ba commit e43d0f8

File tree

4 files changed

+97
-4
lines changed

4 files changed

+97
-4
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
node_modules/
2+
db.sqlite

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Copyright 2017 Project Wyvern Developers
1+
Copyright 2017-2018 Project Wyvern Developers
22

33
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
44

server/db.js

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,71 @@ const sequelize = new Sequelize('wyvernOrderbook', null, null, {
1212
})
1313

1414
const Order = sequelize.define('order', {
15+
hash: {type: Sequelize.TEXT, allowNull: false, unique: true},
16+
metadata: {type: Sequelize.JSON, allowNull: false},
17+
exchange: {type: Sequelize.TEXT, allowNull: false},
18+
initiator: {type: Sequelize.TEXT, allowNull: false},
19+
side: {type: Sequelize.INTEGER.UNSIGNED, allowNull: false},
20+
saleKind: {type: Sequelize.INTEGER.UNSIGNED, allowNull: false},
21+
target: {type: Sequelize.TEXT, allowNull: false},
22+
howToCall: {type: Sequelize.INTEGER.UNSIGNED, allowNull: false},
23+
calldata: {type: Sequelize.TEXT, allowNull: false},
24+
replacementPattern: {type: Sequelize.TEXT, allowNull: false},
25+
metadataHash: {type: Sequelize.TEXT, allowNull: false},
26+
paymentToken: {type: Sequelize.TEXT, allowNull: false},
27+
basePrice: {type: Sequelize.TEXT, allowNull: false},
28+
extra: {type: Sequelize.TEXT, allowNull: false},
29+
listingTime: {type: Sequelize.TEXT, allowNull: false},
30+
expirationTime: {type: Sequelize.TEXT, allowNull: false},
31+
frontend: {type: Sequelize.TEXT, allowNull: false},
32+
salt: {type: Sequelize.TEXT, allowNull: false}
33+
})
34+
35+
const encodeOrder = (order) => ({
36+
hash: order.hash,
37+
metadata: order.metadata,
38+
exchange: order.exchange,
39+
initiator: order.initiator,
40+
side: order.side,
41+
saleKind: order.saleKind,
42+
target: order.target,
43+
howToCall: order.howToCall,
44+
calldata: order.calldata,
45+
replacementPattern: order.replacementPattern,
46+
metadataHash: order.metadataHash,
47+
paymentToken: order.paymentToken,
48+
basePrice: JSON.stringify(order.basePrice),
49+
extra: JSON.stringify(order.extra),
50+
listingTime: JSON.stringify(order.listingTime),
51+
expirationTime: JSON.stringify(order.expirationTime),
52+
frontend: order.frontend,
53+
salt: JSON.stringify(order.salt)
54+
})
55+
56+
const decodeOrder = (order) => ({
57+
hash: order.hash,
58+
metadata: order.metadata,
59+
exchange: order.exchange,
60+
initiator: order.initiator,
61+
side: order.side,
62+
saleKind: order.saleKind,
63+
target: order.target,
64+
howToCall: order.howToCall,
65+
calldata: order.calldata,
66+
replacementPattern: order.replacementPattern,
67+
metadataHash: order.metadataHash,
68+
paymentToken: order.paymentToken,
69+
basePrice: JSON.parse(order.basePrice),
70+
extra: JSON.parse(order.extra),
71+
listingTime: JSON.parse(order.listingTime),
72+
expirationTime: JSON.parse(order.expirationTime),
73+
frontend: order.frontend,
74+
salt: JSON.parse(order.salt)
1575
})
1676

1777
module.exports = {
1878
sequelize: sequelize,
19-
Order: Order
79+
Order: Order,
80+
encodeOrder: encodeOrder,
81+
decodeOrder: decodeOrder
2082
}

server/index.js

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,53 @@
11
const express = require('express')
22
const bodyParser = require('body-parser')
3+
// const helmet = require('helmet')
34

4-
const { sequelize } = require('./db.js')
5+
const { sequelize, Order, encodeOrder, decodeOrder } = require('./db.js')
56
const log = require('./logging.js')
67

78
const app = express()
9+
// app.use(helmet())
810
app.use(bodyParser.json())
911
app.use((req, res, next) => {
1012
log.debug({path: req.path, method: req.method, ip: req.headers['X-Forwarded-For']}, 'Request received')
13+
res.header('Access-Control-Allow-Origin', '*')
1114
res.header('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content-Type, Accept')
1215
next()
1316
})
1417

18+
const router = express.Router()
19+
20+
router.get('/orders', (req, res) => {
21+
Order.findAll().then(orders => {
22+
res.json(orders.map(decodeOrder))
23+
})
24+
})
25+
26+
router.get('/orders/:hash', (req, res) => {
27+
Order.findOne({hash: req.params.hash}).then(order => {
28+
res.json(decodeOrder(order))
29+
})
30+
})
31+
32+
router.post('/orders/post', (req, res) => {
33+
const order = req.body
34+
Order.create(encodeOrder(order)).then(() => {
35+
res.json({result: true, errors: []})
36+
})
37+
})
38+
39+
router.post('/orders/validate', (req, res) => {
40+
res.json({result: true, errors: []})
41+
})
42+
43+
app.use('/v1', router)
44+
1545
const go = ({ port }) => {
1646
sequelize
1747
.sync()
1848
.then(() => {
1949
app.listen(port, () => {
20-
log.debug({port: port}, 'Server started')
50+
log.debug({origin: 'express', port: port}, 'Server started')
2151
})
2252
})
2353
}

0 commit comments

Comments
 (0)