Step by Step Guide | P2P Derivatives Application

Crypto Garage
Crypto Garage
Published in
6 min readJul 28, 2020

As announced on our blog, we have launched the P2P Derivatives Beta application. This article will guide you step-by-step on the setup and usage. Also,You can watch the tutorial video below.

This article is for beginners, and provides guidance for using the application with the bitcoin regtest and the Crypto Garage Oracle and server. You can run two different users on the same computer and make a contract between them.

If you want to use testnet or run an Oracle and a server by yourself, please refer to our Github.

Terms and Conditions agreement

Before using the application, make sure to read the terms and conditions.

Application Download

You can download the application here.

Bitcoind Setup(for regtest)

The application requires connecting to a running bitcoind instance.

You can download bitcoin core software here. There are also other ways to install or download it depending on your operating system, which can be found easily on any search engine.

Once bitcoin-core is installed, you can use the scripts on the github repository to start a node and then create some wallets for testing. (Note that these are bash scripts and will thus not work on a windows shell. If using windows, you might be able to use them with git bash, but this is currently untested.)

Start by creating a folder (name it like `p2pderivatives` or what you would like), and create two subfolder `bitcoind` and `scripts`). In the bitcoind folder place the `bitcoin.regtest.conf` file. In the scripts folder place the `start_bitcoind.sh` and `stop_bitcoind.sh` files. From the `p2pderivatives` folder run the following commands:

chmod +x ./scripts/start_bitcoind.sh
chmod +x ./scripts/stop_bitcoind.sh
./scripts/start_bitcoind.sh

Creating a wallet

You can create two separate wallets to play around with two users on the same computer. Here we will create a wallet named “alice” and another one “bob” for Trade and a third wallet named “carol” for block generation on regtest.

bitcoin-cli -datadir=./bitcoind -conf=”bitcoin.regtest.conf” createwallet “alice” “false” “false” “str0nGP@ssw9rd”bitcoin-cli -datadir=./bitcoind -conf=”bitcoin.regtest.conf” createwallet “bob” “false” “false” “str0nGP@ssw9rd”bitcoin-cli -datadir=./bitcoind -conf=”bitcoin.regtest.conf” createwallet “carol” “false” “false” “str0nGP@ssw9rd”

Block generation

You can fund the the wallets by generating blocks to their addresses. You can do so with the following command from the `p2pderivatives` folder.

bitcoin-cli -datadir=./bitcoind -conf=bitcoin.regtest.conf generatetoaddress 101 $(bitcoin-cli -datadir=./bitcoind -conf=bitcoin.regtest.conf -rpcwallet=carol getnewaddress)

Now bitcoind is running and you have created 3 wallets named “alice” , “bob” and “carol”.

Registration and login

You need to register your own ID and password to login.
In order to make a contract between two different users on the same computer, start two applications and register two different users respectively. There is no need to register any personal information.

Wallet configuration

As mentioned previously, connecting to a running bitcoind instance is required for this application. Please fill in the items under ‘Wallet Configuration’. Note that this application can only be used on the bitcoin regtest and testnet. If you are using the default configuration where the bitcoind instance is running locally, you can leave the ‘IP Address’ and ‘Port’ fields empty.
Assuming you already created the wallets named “Alice” and “Bob”, You need to input the values below to configure your wallets.

One party
RPC Username :user
RPC Password :pass
Wallet :alice
Wallet Passphrase :str0nGP@ssw9rd
The other party
RPC Username :user
RPC Password :pass
Wallet :bob
Wallet Passphrase :str0nGP@ssw9rd

After completing the ‘Wallet Configuration,’ your screen will transition to the screen below. Click “New contract” on the upper left to create a contract.

Contract agreement and Contribution of Collateral

You need to choose your counterparty, and set the contract information. Fill in each item as described in the figure below and click “publish”.

How to create a CSV file to be imported in the application is described hereafter.

CSV file creation

In order to create a contract, you need to create a CSV file that contains the Bitcoin distribution amount based on the BTC-USD price results at the time of maturity. Here are two CSV template, digital.csv and spread.csv
The CSV file defines the distribution amount of Bitcoin for both parties depending on the BTCUSD price at contract maturity. Each template defines the distribution as follows.

digital.csv
spread.csv

Each value can be set freely by the user. After creating the CSV file, please save it in your local drive. In our example, Alice uploaded the digital.csv file.

Be careful to use a range of value adequate with respect to the BTCUSD rate at the time of establishing the contract, as if the rate at contract maturity is outside of the range, the application will not be able to settle the contract (the contract collaterals will then be refunded after a seven days period).

Contract agreement and Contribution of Collateral

After Alice publishes the contract with Bob, Bob’s screen shows the Offered contract. If he accepts it, the application will transmit the required information and broadcast a transaction locking up the collaterals on the blockchain.

Contract detail (Bob’s screen)

In this case, Alice and Bob each contribute 1 BTC as a collateral.
If the BTCUSD price at July 2020 11:49 AM UTC is 9000~10000, 2BTC will be distributed to Alice, and if it is 10000~10999, 2BTC will be distributed to Bob.

Block generation

For the contract to be settled, you will have to generate blocks on regtest. You can do so with the following command from the `p2pderivatives` folder(assuming that you have created a wallet named ”carol” previously):

bitcoin-cli -datadir=./bitcoind -conf=bitcoin.regtest.conf generatetoaddress 7 $(bitcoin-cli -datadir=./bitcoind -conf=bitcoin.regtest.conf -rpcwallet=carol getnewaddress)

Note that the application waits for 6 confirmations before considering the fund transaction as confirmed, so you will have to generate at least 6 blocks.

Settlement at maturity time

At the time of contract maturity, the Oracle will announce the outcome of the contract and provide a signature for the contract. The application will use this information to close the contract.
If both parties are online at maturity time, they communicate with each other and mutually create a closing transaction and one party will broadcast it (the status shows up as “Mutual Closed”).
If one party is offline, the online party will unilaterally broadcast the transactions to close the contract (the status shows up as “Unilaterally Closed”).
In both cases, the parties will receive the amount corresponding to the outcome that was stated in the contract.

In the example below, Alice contributed 1BTC as collateral and BTCUSD price at maturity time was 9118. The price was between 9,000 to 10,000, so Alice got 2BTC as Payout.
Alice’s collateral was 1BTC, Payout was 2BTC, so PnL was 1BTC.

Result at maturity time (Alice’s screen)

If you want to check the balance of Alice and Bob, execute the following command from the `p2pderivatives` folder and generate blocks.

bitcoin-cli -datadir=./bitcoind -conf=bitcoin.regtest.conf generatetoaddress 6 $(bitcoin-cli -datadir=./bitcoind -conf=bitcoin.regtest.conf -rpcwallet=carol getnewaddress)

Now you can see the entire trade flow of our P2P derivative app from contract creation to settlement. I hope you give it a try and conduct a test transaction on various financial contracts.

--

--