1
0
mirror of https://github.com/bigchaindb/site.git synced 2024-11-22 09:46:57 +01:00

changes due new js driver

This commit is contained in:
manolodewiner 2018-01-02 13:53:40 +01:00
parent b61d5416f9
commit 20a7bc298a
2 changed files with 66 additions and 22 deletions

View File

@ -61,7 +61,7 @@ As a next step, you need to generate a `CREATE` transaction to link the defined
```js
function createPaint() {
// Construct a transaction payload
const txCreateCar = BigchainDB.Transaction.makeCreateTransaction(
const txCreatePaint = BigchainDB.Transaction.makeCreateTransaction(
// Asset field
{
...painting,
@ -83,7 +83,7 @@ function createPaint() {
alice.publicKey
)
// The owner of the painting signs the transaction
const txSigned = BigchainDB.Transaction.signTransaction(txCreateCar,
const txSigned = BigchainDB.Transaction.signTransaction(txCreatePaint,
alice.privateKey)
// Send the transaction off to BigchainDB
@ -114,23 +114,28 @@ The `listTransactions` command of BigchainDB retrieves all of the create and tra
Based on that, we can now create the transfer transaction:
```js
function transferOnwership(txCreated, newOwner) {
// Update the painting with a new owner
// First, we query for the asset that we created
function transferOwnership(txCreated, newOwner) {
const createTranfer = BigchainDB.Transaction.
makeTransferTransaction(
txCreated, {
mileage: txCreated.metadata.mileage + mileageValue,
units: 'km'
}, [BigchainDB.Transaction.makeOutput(
// The output index 0 is the one that is being spent
[{
tx: txCreated,
output_index: 0
}], [BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(
newOwner.publicKey))],
0
newOwner.publicKey))] {
datetime: new Date().toString(),
value: {
value_eur: '30000000€',
value_btc: '2100',
}
},
)
// Sign with the owner of the car as she was the creator of the car
// Sign with the owner of the paint (Alice)
const signedTransfer = BigchainDB.Transaction
.signTransaction(createTranfer, carOwner.privateKey)
.signTransaction(createTranfer, alice.privateKey)
conn.postTransaction(signedTransfer)
.then((signedTransfer) => conn
.pollStatusAndFetchTransaction(signedTransfer.id))
@ -143,7 +148,7 @@ function transferOnwership(txCreated, newOwner) {
Note again that in the output of this transfer transaction we have `newOwner.publicKey`. This shows that Alice has transferred the ownership of the Meninas to anybody else (newOwner). Furthermore, the input being spent is 0, as there is just one input. Additionally, note that the metadata field was used to update information about the painting (the price of the transaction, which increased to 30 mn. EUR etc.).
You have now updated your asset and it is now not anymore you who will be able to transfer the painting, because someone else is the owner.
You have now updated your asset and it is now not anymore you who will be able to transfer the painting, because someone else is now the owner.
That's it, we have created a digital representation of a painting and transferred the ownership to another user.

View File

@ -97,8 +97,8 @@ function transferTokens() {
tx,
// Metadata (optional)
{
tranferTo: 'john',
tokensLeft: tokensLeft
tranfe_to: 'john',
tokens_left: tokensLeft
},
// Transaction output: Two outputs, because the whole input must be spent
[BigchainDB.Transaction.makeOutput(
@ -133,6 +133,45 @@ function transferTokens() {
You have now transferred 200 tokens to the user John. You could repeat the same with multiple other users.
With `listOutputs` using `false` as the second argument you retrieved all the outputs belonging to the user `tokenCreator`, that were not spent yet. There will just be one output that fulfills these characteristics, because when you transfer tokens to another user, you are spending this output and giving the ownership to the other user. Then, you queried for that transaction and made a transfer to John with it. Note however, that there is also a transaction back to `tokenCreator.publicKey`, as you need to 'give back change' due to BigchainDB's transaction model. It is designed in a way that all of the inputs have to be spent in a transaction. That means that if you send part of the `tokensLeft` (200 tokens) to John, you have to send the rest (9800 tokens) back to the `tokenCreator` to preserve that amount.
Imagine you have received several transactions of tokens and you want to combine all of the quantities and transfer to you best friend. That is possible as well
```js
const amountToSend = 200
const bestFriend = new driver.Ed25519Keypair()
function combineTokens(transaction1, outputIndex1, transaction2, outputIndex2,
totalTokens) {
const combineTranfer = BigchainDB.Transaction.makeTransferTransaction(
[{
tx: transaction1,
output_index: outputIndex1
}, {
tx: transaction2,
output_index: outputIndex2
}],
// Output. Two outputs. The hole input must be spent
[BigchainDB.Transaction.makeOutput(
BigchainDB.Transaction.makeEd25519Condition(
bestFriend.publicKey),
(totalTokens).toString())],
{
transfer_to: 'my best friend'
}
)
// Sign the transaction with the newUser key
const signedTransfer = BigchainDB.Transaction
.signTransaction(combineTranfer, newUser.privateKey)
return conn.postTransaction(signedTransfer)
}
```
You just made a transfer transaction combining two different transactions into one output. The `totalToken` quantity is needed which is the sum of the tokens of the two outputs being spent. As you have seen before if this quantity is not correct, the transaction will fail, as you literally need to spend all of the outputs in a transaction.
`transaction1` and `transaction2` can look like the transaction `createTranfer` that you did before, then the `outputIndex1` and `outputIndex2` would be `0`.
Note that in our example, the supply of your tokens was fixed and cannot be changed anymore after creation. So, you would need to clearly define for yourself, how many tokens you will need. However, BigchainDB does offer the option of refillable, divisible assets that allow for a more dynamic token supply. You can learn more about that [here](https://github.com/bigchaindb/bigchaindb/issues/1741).
That's it! Now you know, how divisible assets in BigchainDB can be used as a potential building block for token launches. Of course, in practice a token distribution event is much more complex and requires other important building blocks like smart contracts etc. But, this tutorial showed you how divisible assets can play a part of that.