From 829800ed6e1efa7011b57f0004a1ff1616cd96e7 Mon Sep 17 00:00:00 2001 From: manolodewiner Date: Sun, 19 Nov 2017 18:57:39 +0100 Subject: [PATCH] tutorial token launch --- _src/_guides/tutorial-car-telemetry-app.md | 2 +- _src/_guides/tutorial-token-launch.md | 91 ++++++++++++++++++++++ 2 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 _src/_guides/tutorial-token-launch.md diff --git a/_src/_guides/tutorial-car-telemetry-app.md b/_src/_guides/tutorial-car-telemetry-app.md index f59247b..81fe0dc 100644 --- a/_src/_guides/tutorial-car-telemetry-app.md +++ b/_src/_guides/tutorial-car-telemetry-app.md @@ -169,4 +169,4 @@ function updateMileage(assetId, mileageValue) { Once we have the last transaction we create the transfer transaction with the new metadata value. Is also needed an ouput which will be the Alice to preserve the ownership of the car and the index of the input being spending which is 0, as there is just one input. Then we sign the transaction and we send it to BigchainDB. -That's it, we have created a car asset, and every time the car travels new kilometers the updateMileage should be called with the new value of it. +That's it, we have created a car asset, and every time the car travels new kilometers the `updateMileage` should be called with the new value of it. diff --git a/_src/_guides/tutorial-token-launch.md b/_src/_guides/tutorial-token-launch.md new file mode 100644 index 0000000..5fdfced --- /dev/null +++ b/_src/_guides/tutorial-token-launch.md @@ -0,0 +1,91 @@ +--- +layout: guide + +title: "Tutorial: Token distribution launch" +tagline: Build a token launch with BigchainDB +header: header-token.jpg +--- + +You will learn: + +- How BDB can be used to record the transactions made by a token distribution launch + +- How to use divisible assets on BigchainDB + +We show, how divisible assets work in BigchainDB by showing, how you could create your own token launch on BigchainDB. The token distribution is represented by divisible assets (tokens) linked to one specific application (company/network). + +When creating a divisible asset in BigchainDB, the number of the sub-assets that you want to create should be specified. + + +# Create divisible asset +```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 + }) +} +``` + +We have decided to create 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 BDB we update the value of the tokens left in the possesion of the creator. + + +Once the tokens are created we can start to spread it over our users. + +# Transfer tokens +```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.