Hi and congratulations to your progress with Bitfinex!
Your task is to create a simplified distributed exchange
- Each client will have its own instance of the orderbook.
- Clients submit orders to their own instance of orderbook. The order is distributed to other instances, too.
- If a client's order matches with another order, any remainer is added to the orderbook, too.
Requirement:
- Code in Javascript
- Use Grenache for communication between nodes
- Simple order matching engine
- You don't need to create a UI or HTTP API
You should not spend more time than 6-8 hours on the task. We know that its probably not possible to complete the task 100% in the given time.
If you don't get to the end, just write up what is missing for a complete implementation of the task. Also, if your implementation has limitation and issues, that's no big deal. Just write everything down and indicate how you could solve them, given there was more time.
Good luck!
- you don't need to store state in a DB or filesystem
- it is possible to solve the task with the node std lib, async and grenache libraries
- beware of race conditions!
- no need for express or any other http api layers
npm i -g grenache-grape
# boot two grape servers
grape --dp 20001 --aph 30001 --bn '127.0.0.1:20002'
grape --dp 20002 --aph 40001 --bn '127.0.0.1:20001'
npm install --save grenache-nodejs-http
npm install --save grenache-nodejs-link
// This RPC server will announce itself as `rpc_test`
// in our Grape Bittorrent network
// When it receives requests, it will answer with 'world'
'use strict'
const { PeerRPCServer } = require('grenache-nodejs-http')
const Link = require('grenache-nodejs-link')
const link = new Link({
grape: 'http://127.0.0.1:30001'
})
link.start()
const peer = new PeerRPCServer(link, {
timeout: 300000
})
peer.init()
const port = 1024 + Math.floor(Math.random() * 1000)
const service = peer.transport('server')
service.listen(port)
setInterval(function () {
link.announce('rpc_test', service.port, {})
}, 1000)
service.on('request', (rid, key, payload, handler) => {
console.log(payload) // { msg: 'hello' }
handler.reply(null, { msg: 'world' })
})
// This client will as the DHT for a service called `rpc_test`
// and then establishes a P2P connection it.
// It will then send { msg: 'hello' } to the RPC server
'use strict'
const { PeerRPCClient } = require('grenache-nodejs-http')
const Link = require('grenache-nodejs-link')
const link = new Link({
grape: 'http://127.0.0.1:30001'
})
link.start()
const peer = new PeerRPCClient(link, {})
peer.init()
peer.request('rpc_test', { msg: 'hello' }, { timeout: 10000 }, (err, data) => {
if (err) {
console.error(err)
process.exit(-1)
}
console.log(data) // { msg: 'world' }
})