AirSwap Developer Series Part 2: Building an Order Server

Graham Perich
Aug 2, 2018 · 4 min read

In Part 1 of the AirSwap Developer Series, we learned about the roles of Makers and Takers. We also learned how to use the API Client Server to request orders and fill them. In Part 2, we’ll build a simple Order Server which will allow us to signal intent to trade as a Maker on the network.


Requirements

Set Up

First, make a new folder and create a JavaScript file called orderServer.js

We’re going to use the Express framework to power our server, so let’s start by creating a package.json file and installing Express. Run the following to output your package.json file:

Then, run npm install

Coding the Order Server

Now that we have our dependencies set up, we’re ready to write our Order Server. Let’s open orderServer.js in the text editor and lay the foundation.

This code sets up a simple HTTP server that listens on port 5004 for calls to the /getOrder route. We specify this port and route because that’s where the API Client Server forwards incoming order requests. When a taker sends a getOrder request, your API Client Server receives the request and sends it to the Order Server so you can decide how you want to respond. Inside of the getOrder route is where we’ll write our order handling logic. Let’s update the getOrder route now.

We haven’t implemented any complex pricing logic. This code is simply responding to every getOrder request with an order that offers 1 test net AST for 0.001 test net ETH. This is a barebones example from which you can build whatever pricing logic you feel is appropriate. Maybe you want to offer a bulk discount if someone asks for a large number of tokens, or maybe you want to pull in live data from a centralized exchange API and base your prices off of that. The possibilities are endless!

Note: If you’re wondering why we specified the makerAmount as “10000” and the takerAmount as “1000000000000000", it’s because Ethereum smart contracts require us to always write values in the smallest possible denomination. ETH and AST have 18 and 4 decimals respectively. So, if we want to offer 1 AST, we actually need to write “1” with the decimal place moved 4 places to the right. Thus, we get “10000”. The same rule applies to ETH and to every other token. You can easily look up how many decimals a token has with Etherscan.

Putting It All Together

The last step is to run our Order Server in parallel with the API Client Server.

From the directory containing the API Client Server:

In a new command line window, from the directory containing your new orderServer.js:

When both of these servers are running, your address will instantly serve orders to takers who request them.

That’s all for this post. Stay tuned for Part 3! In the third and final tutorial, we’ll be making a simple front end interface that allows users on the web to get tokens directly from your Order Server. We’ll also build a trading bot that periodically requests orders and executes trades if the price is right.


About AirSwap

AirSwap enables peer-to-peer trading on the Ethereum blockchain. Built on a decentralized protocol, traders can add or remove liquidity through a suite of trustless products that are easy to use and free. Our mission is to empower the world with frictionless trade.

Blog | Twitter | Discord | Developers | Makers | Reddit | Facebook | Linkedin | Subscribe | Support | Request a Feature | FAQ | Trade Now

Fluidity

Rebuilding Finance for a Frictionless World

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch

Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore

Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store