1
0
mirror of https://github.com/bigchaindb/site.git synced 2024-11-26 11:38:58 +01:00
site/_src/_guides/tutorial-token-launch.md

100 lines
4.8 KiB
Markdown
Raw Normal View History

2017-11-19 18:57:39 +01:00
---
layout: guide
2017-11-22 21:05:09 +01:00
title: "Tutorial: How to launch your own token on BigchainDB"
tagline: Learn how to use divisible assets in BigchainDB for token distribution events
2017-11-19 18:57:39 +01:00
header: header-token.jpg
2017-11-20 11:04:08 +01:00
learn: >
2017-11-19 18:57:39 +01:00
2017-11-20 11:04:08 +01:00
- How to use divisible assets on BigchainDB
2017-11-22 21:05:09 +01:00
- How assets in BigchainDB can represent tokens
2017-11-22 21:05:09 +01:00
- How tokens can be distributed to participants using TRANSFER transactions
2017-11-20 11:04:08 +01:00
---
2017-11-19 18:57:39 +01:00
2017-11-22 21:05:09 +01:00
Hi there! Welcome to our second tutorial about divisible assets. For this tutorial, we assume that you are familiar with the BigchainDB primitives (assets, inputs, outputs, transactions etc.). If you are not, familiarize yourself with [Key concepts of BigchainDB](../key-concepts-of-bigchaindb/).
# About token distribution events
In the last 12 months we have witnessed exponential growth in token distribution events. Most of them have been launched on Ethereum. Since we are experiencing rising interest in potential token launches on BigchainDB, this tutorial aims at showing a very simple approach on how to launch your own token on BigchainDB.
2017-11-20 13:19:16 +01:00
Note however, that we do not support ERC20 and no one has launched tokens on BigchainDB yet. This tutorial just aims at showing one possible approach.
2017-11-20 13:19:16 +01:00
{% include_relative _setup.md %}
2017-11-19 18:57:39 +01:00
2017-11-22 21:05:09 +01:00
# Usage of divisible assets to create tokens
2017-11-20 10:18:32 +01:00
BigchainDB supports divisible assets. A divisible asset is an asset that has a fixed number of sub-assets linked to it. These fixed sub-assets that are linked to it, represent your tokens. When creating a divisible asset in BigchainDB, the number of the sub-assets (tokens) that you want to create needs to be specified. That represents your fixed total supply of tokens. The code below illustrates how to create a divisible asset with 10000 tokens associated to it.
2017-11-20 13:19:16 +01:00
2017-11-19 18:57:39 +01:00
```js
const nTokens = 10000
let tokensLeft
function tokenLaunch() {
const tx = BigchainDB.Transaction.makeCreateTransaction({
token: tokenName.value,
number_tokens: nTokens
}, {
datetime: new Date().toString()
}, [BigchainDB.Transaction.makeOutput(BigchainDB.Transaction.makeEd25519Condition(tokenCreator.publicKey), nTokens.toString())],
tokenCreator.publicKey
)
// Sign the transaction with private keys
const txSigned = BigchainDB.Transaction.signTransaction(tx, tokenCreator.privateKey)
conn.postTransaction(txSigned)
.then(() => conn.pollStatusAndFetchTransaction(txSigned.id))
.then(res => {
lastTx = txSigned.id
console.log('Create Transaction', txSigned, 'accepted')
tokensLeft = nTokens
})
}
```
2017-11-22 21:05:09 +01:00
Now, we have minted 10000 tokens. For that there is an extra parameter to the `makeOutput()` function. Pay attention to give the function a string instead of a plain number. With the `tokenCreator` keypair we indicate who the owner of the tokens will be. Once the transaction is accepted by BigchainDB we update the value of the tokens left in the possesion of the creator.
2017-11-19 18:57:39 +01:00
Once the tokens are created we can start to spread it over our users.
# Transfer tokens
2017-11-20 10:18:32 +01:00
2017-11-19 18:57:39 +01:00
```js
const amountToSend = 200
function transferTokens() {
const newUser = new BigchainDB.Ed25519Keypair()
conn.listOutputs(tokenCreator.publicKey, 'false')
.then((txs) => {
conn.getTransaction(txs[0].transaction_id)
.then((tx) => {
console.log('the search found', tx)
const createTranfer = BigchainDB.Transaction.makeTransferTransaction(
tx, {
tranferTo: 'john'
}, [BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(tokenCreator.publicKey), (tokensLeft - amountToSend).toString()),
BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(newUser.publicKey), amountToSend)
],
0
)
const signedTransfer = BigchainDB.Transaction.signTransaction(createTranfer, tokenCreator.privateKey)
conn.postTransaction(signedTransfer)
.then(() => conn.pollStatusAndFetchTransaction(signedTransfer.id))
.then(res => {
tokensLeft -= amountToSend
console.log('Transfer Transaction', signedTransfer.id, 'accepted')
})
})
})
}
```
With `listOutputs` using `false` as the second argument we can retrieve all the outputs that were not spent yet. Then we query for that transaction and we make a transfer with it. As the transaction model of BigchainDB is designed, all of the inputs have to be spent in a transaction. That means that if we send part of the `tokensLeft` to some user, we have to send the rest to the `tokenCreator` to preserve that amount.