Tech Tuesday Development Update - 2019 Week 20 Build an Asset Exchange: Token DEX Example

myloMay 14, 2019

This series of posts is called the Tech Tuesday Updates. Along with Komodo's Five Bullet Fridays weekly newsletter, Tech Tuesday Updates is another weekly piece of content to look forward to.

In case you’ve missed a previous edition and want to catch up, you can find all the previous Tech Tuesday updates here.

Build An Asset Exchange: Token DEX Example

This technical introduction is the GUI developer’s guide to using some technology covered in previous weeks in regards to asset exchange:

The underlying technology has been abstracted away into developer friendly API calls.

Komodo’s blockchain application framework uses the Crypto Conditions IETF draft to standardize how objects can be stored on a blockchain. It is an algorithm & language agnostic method of storing data. Komodo is the first project to capitalize on this standardization process.

Komodo Platform is a platform of platforms. Where the token system is not used by the main Komodo chain (KMD the currency of the ecosystem), it is available for a project to use and customize with the Blockchain Starter Kit.

The Blockchain Starter Kit does not require any KMD to function or extend. It is open source and there are no fees due for using it, nor any gas. Your blockchain transaction fees are managed and paid by your own blockchain’s native coin.

The built-in token and trading system is a blockchain application. It is a deterministic and trustless full-node system where every node maintains its own sovereignty— in other words, it is a blockchain full node with an application layer. The application layer is standardized.

The token system has several built-in methods that are common to on-chain DEX trading platforms:

  • Bids
  • Asks
  • Cancels
  • List open orders
  • Create new tokens (fungible & non-fungible NFT)

I spent the last two days testing it out to create the basis for any project that requires their own Token DEX application. (Note: This is different to the atomic swap DEX). The on-chain DEX is much simpler to develop & integrate into a broader blockchain application suite.

token dexThree steps to a Token DEX

  1. Create your blockchain with Komodo or the upcoming Blockchain Generator SaaS
  2. Create token
  3. Create a buy or sell order

Let’s start from the bottom and work our way back. Orders for buy & sell of these tokens is represented below and is easily explored with the tokenorders RPC call.

[
 {
 "funcid": "b",
 "txid": "2758b5324767acb7058b87b937bdd50c1744ca940d790faa7b507e0019b30521",
 "vout": 0,
 "amount": "8.88000000",
 "bidamount": "8.88000000",
 "origaddress": "RSefAPkRsd2JcQ4pkPHUzF4z3ExaDC1sJQ",
 "origtokenaddress": "RUK3ejSkNayJKYzANRzpC6yqxzaCMa6Hng",
 "tokenid": "ecd8b4ff52604997629d73c8797fa5b028295f918fbcc807bf2fb8536294f389",
 "totalrequired": 100,
 "price": "0.08880000"
 },
 {
 "funcid": "s",
 "txid": "5411ecabc8f36485ecb6ee02843321bdfcbb9f4bdc63050356987a91958ff75a",
 "vout": 0,
 "amount": "1000",
 "askamount": "1000",
 "origaddress": "RR2tjpFKaF36u4JesWG1vWjkgt2Wa448Qb",
 "origtokenaddress": "RBjnhNaqtSGym1PqpaQL3jY3XHgpH9qTRK",
 "tokenid": "ecd8b4ff52604997629d73c8797fa5b028295f918fbcc807bf2fb8536294f389",
 "totalrequired": "111.10000000",
 "price": "0.11110000"
 },
 {
 "funcid": "s",
 "txid": "28bcd2e68c9c4c0bc64ff31da6dd49402157ed8bc56a3d4088ba16d599e9aef3",
 "vout": 0,
 "amount": "1000,
 "askamount": "1000",
 "origaddress": "RR2tjpFKaF36u4JesWG1vWjkgt2Wa448Qb",
 "origtokenaddress": "RBjnhNaqtSGym1PqpaQL3jY3XHgpH9qTRK",
 "tokenid": "ecd8b4ff52604997629d73c8797fa5b028295f918fbcc807bf2fb8536294f389",
 "totalrequired": "99.98000000",
 "price": "0.09998000"
 }
]

This is a response from the RPC tokenorders method.

Create a buy or sell order.

I had my own confusion about the amount/totalrequired fields and the response that cleared my confusion was:

for buy you are actually 'selling' your coins for tokens thats why amount in coins for sell you selling your tokens for coins - amount in tokens

To create these orders:

  • one RPC method is used for placing a buy order - using tokenbid
  • One RPC method is used for placing a sell order - using tokenask

Working backwards from buying and selling, first we need to have created the token.

Create a fungible token

Another simple one RPC method call will create a token. Simply calling tokencreate with the arguments:

  • Name
  • Supply
  • Description

Each of your native blockchain coins— let’s say my blockchain is called TDEX— 1 TDEX coin can make 100,000,000 tokens. That means each satoshi of TDEX can create a token. A satoshi is the atomic unit and cannot be divided further. This is the birth of your first of many tokens. Create more, there is no limit to the number of tokens you create. It is your own TDEX coins that these tokens are created from. Every satoshi creates one token. Moving onto an NFT.

Create a non-fungible token (NFT)

The same RPC method is called, tokencreate. What makes an NFT special is it’s non-fungibility In order to make an NFT, the tokencreate method spends 1 single satoshi. The single satoshi has no equal in this run of the token creation factory. It is special.

Additionally, data can be attached to a tokencreate event - this unique single token creation event can have some unique data attached to it in the blockchain.

Create Your Blockchain

This is where you have many more options compared to calling single methods to represent worldly value. For simple blockchains and for testing, simply fire up komodod with a supply of coins and you can start.

For more complex emission and economics - configure the parameters of your blockchain’s characteristics to share mining vs staking. Or create a slower curve of coin emission. Or simply make a fund from every block that is created that makes sure there will always be coins to fund future enhancements.

  1. komodod -ac_name=TDEX -ac_supply=1000 -ac_pubkey=$pubkey -ac_cc=2
  2. komodod -ac_name=TDEX -ac_supply=1000 -ac_pubkey=$pubkey -ac_cc=2 -ac_reward=250000000 -ac_halving=10000 -ac_decay=1000000
  3. komodod -ac_name=TDEX -ac_supply=1000 -ac_pubkey=$pubkey -ac_cc=2 -ac_reward=250000000 -ac_halving=10000 -ac_decay=1000000 -ac_perc=10000000

In 1, it simply a chain with 1000 coins created (premine, ac_supply) that goes to the address belonging to the pubkey which receives payments from the network (ac_pubkey).

In 2, in addition to the first scenario, the block reward is customized (ac_reward) to 250 TDEX coins (250 million satoshis) reducing every 10000 blocks (ac_halving) by 1% (ac_decay, 1 million divided by 100 million satoshi), therefore 247.5 TDEX after 10000 blocks.

In 3, in addition to the second scenario, a percentage (ac_perc) of 10% of the block reward is paid to the public key at every block. So for the first 10000 blocks, the address of the pubkey would get 25 TDEX each block, then at the first halving (of 1% decay) for the next 10000 blocks, the address of the pubkey would get 24.75 TDEX until the next halving event (of 1% decay), and so on and so forth.

There are many different ways to customize your blockchain to create collaborative communities.