Posts

Showing posts from August, 2017

Make Your Own Bitcoin Exchange in Haskell

A stock exchange is a complex beast, but much of it can be reduced to a single data structure: the Order Book. A Bitcoin exchange uses the same data structure when trading currency pairs of USD, BTC, or ETH. This article will show you how to: Design an order book that can handle limit orders and market orders Install automated sanity checks that run on every write to the order book, preventing hacks and implementation bugs Build an HTTP API that people can use to interact with the order book We won’t work with actual bitcoins or wallets, since they add a lot of complexity and risk without making the article any more interesting. Instead, we’ll assume the “billing code” has already been written and focus only on the order book portion of the exchange. Types So what is an order book, really? First we’ll define our orders: import Data.Tagged import qualified Data.Map as M import qualified Data.Sequence as Q type UserId = Integer type Currency = Text ty