Project Setup
This page is part of the typescript tutorial for creating your first COMIT-app, that builds two simple command-line application using the COMIT protocol to execute a Bitcoin to Ethereum atomic swap locally on your machine.
This tutorial is designed for Javascript/Typescript developers that want to integrate atomic swaps into their application.
This section of the tutorial shows how to set up the project and prepare the maker and taker COMIT-apps.
Introduction
The goal of the tutorial is to have a simple command-line application that handles the negotiation and execution of an atomic swap for both sides of a trade.
For the negotiation phase we introduce two roles, the maker and the taker. The maker creates orders and publishes them. The taker takes orders published by the maker. In this tutorial our maker has Bitcoin and wants Ether. Our taker has Ether and wants Bitcoin.
This tutorial uses the comit-js-sdk's MakerNegotiator
and TakerNegotiator classes for negotiating the trade.
This tutorial does not tackle the problem of "finding a trading partner".
It is assumed that the taker already knows how to reach the "order-server" of the maker.
When it comes to the execution of the swap there are also two roles, Alice and Bob, that represent the cryptographic roles of the swap protocol. Alice is the role that comes up with the secret. She funds and redeems first. Bob is the role that receives the secret hash from Alice prior to swap execution. After the negotiation the taker will default to the role of Alice. The maker will default to Bob. This means that Alice (the taker) has to fund on the Ethereum side and redeem Bob's (the maker) Bitcoin. Bob funds the Bitcoin side and redeems Alice's Ether.
Let's jump right into the setup.
Set up the project with create-comit-app
This setup was tested with yarn
version 1.22.0
and above and Node.js
version 10
and above.
If you encounter problems with the commands below, please make sure yarn
and Node.js
are up to date.
In the previous section about comit-scripts we took a look at how to use comit-scripts to setup your development environment. The same setup is used for this tutorial.
This tutorial was written using create-comit-app version 0.9.1. It is recommended to use version 0.9.1 specifically. To add create-comit-app version 0.9.1 specifically you can use this command:
You can then use this command to set up the project:
Note it you just run yarn create comit-app my-first-app
the latest version of create-comit-app will be used, which may result in a different project structure than outlined in this tutorial.
Start the dev-environment
Navigate into the newly created my-first-comit-app
folder and start the development environment:
An Ethereum parity
and a Bitcoin bitcoind
node as well as one cnd
node for the maker and one cnd
node for the taker are now started.
tip
Keep the development environment running in this terminal and use a different terminal for further steps!
Understand the dev-environment
The environment created through comit-scripts's start-env
gives you pre-funded accounts that we will use in this tutorial.
All variables related to the environment are stored in the ${HOME}/.create-comit-app/env
file.
In your project folder you find an index.js
file that is currently the entry point for running the application.
Open a new terminal and run:
This will print the environment information of the dev-environment started through start-env
:
By default start-env
will create two accounts for both Bitcoin and Ethereum and fund them.
Initial funding is 10 BTC and 1000 ETH.
We will use these accounts for our maker and taker when creating the maker and taker COMIT-apps.
Additionally start-env
creates an ERC20 contract for swapping from/to ERC20 tokens. This is not relevant for this tutorial.
The connection information for the Bitcoin node, Ethereum node and the two cnd nodes is printed as well.
Typescript configuration
The example generated by create-comit-app is pure Javascript, but we use Typescript in this tutorial. Before we start coding we have to configure Typescript in order to use it in our project:
Prepare Maker and Taker app
Since the goal of this tutorial is to have the maker and taker run separately, we need one file that will represent the COMIT-app of the maker and one for the taker.
Let's create two typescript files in the src
directory:
maker.ts
for the COMIT-app representing the makertaker.ts
for the COMIT-app representing the taker
Both apps will be runnable command line applications, so let's add a main function to maker.ts
:
and taker.ts
:
Additionally we add commands to run these two COMIT-apps for the respective actor in the package.json
:
With this in place we can already run our maker app:
and taker app:
At the moment our taker and maker app merely print a line. In the next sections we will ad functionality. We will first focus on the maker application and then on the taker application.