教程:如何在BigchainDB上启动自己的令牌(How to launch your own token on BigchainDB)
如何在BigchainDB上启动自己的令牌(How to launch your own token on BigchainDB)
关于令牌生成事件(token generating events)
在过去的12个月中,我们目睹了令牌生成事件的指数增长。其中许多是以以太坊产生的。由于我们对BigchainDB上的潜在令牌启动越来越感兴趣,因此本教程旨在展示如何在BigchainDB上启动自己的令牌的非常简单的方法。
本教程旨在说明一个构建块(即可分割资产)的使用。实际的令牌启动要复杂得多,并且需要这里没有讨论的其他组件。此外,BigchainDB目前还不兼容ERC20。
1、安装
首先安装官方的BigchainDB JavaScript驱动程序、Python驱动程序或Java驱动程序:
# JavaScript driver npm i bigchaindb-driver
# Python driver pip install -U bigchaindb-driver
<!-- Java driver, in pom.xml for Maven users --> <dependency> <groupId>com.bigchaindb</groupId> <artifactId>bigchaindb-driver</artifactId> <version>1.0</version> </dependency>
然后,将其作为模块包含并连接到任何BigchainDB节点。
- js
const BigchainDB = require('bigchaindb-driver') const API_PATH = 'https://test.bigchaindb.com/api/v1/' const conn = new BigchainDB.Connection(API_PATH)
- python
from bigchaindb_driver import BigchainDB conn = BigchainDB('https://test.bigchaindb.com')
- java
BigchainDbConfigBuilder .baseUrl("https://test.bigchaindb.com/").setup();
2、使用可分割资产创建令牌
在BigchainDB中,令牌生成可以表示为可分割资产。可分割资产是指具有固定数量的子资产的资产。这意味着创建事务包含不止该资产。链接的固定子资产代表您的令牌。在BigchainDB中创建可分割资产时,需要在开始处指定要创建的子资产(令牌)的数量。这个数字代表您固定的总令牌供应量。
下面的代码说明了如何创建具有10000个与其相关联的令牌的可分割资产。
- js
const nTokens = 10000 let tokensLeft const tokenCreator = new BigchainDB .Ed25519Keypair(bip39.mnemonicToSeed('seedPhrase').slice(0,32)) let createTxId function tokenLaunch() { // Construct a transaction payload const tx = BigchainDB.Transaction.makeCreateTransaction({ token: 'TT (Tutorial Tokens)', number_tokens: nTokens }, // Metadata field, contains information about the transaction itself // (can be `null` if not needed) { datetime: new Date().toString() }, // Output: Divisible asset, include nTokens as parameter [BigchainDB.Transaction.makeOutput(BigchainDB.Transaction .makeEd25519Condition(tokenCreator.publicKey), nTokens.toString())], tokenCreator.publicKey ) // Sign the transaction with the private key of the token creator const txSigned = BigchainDB.Transaction .signTransaction(tx, tokenCreator.privateKey) // Send the transaction off to BigchainDB conn.postTransactionCommit(txSigned) .then(res => { createTxId = res.id tokensLeft = nTokens document.body.innerHTML ='<h3>Transaction created</h3>'; // txSigned.id corresponds to the asset id of the tokens document.body.innerHTML +=txSigned.id }) }
使用这些命令,您已经创建了10000个令牌。为此,为makeOutput()函数提供一个额外的参数。注意给函数一个字符串而不是一个普通数字。使用令牌创建者密钥对可以指示谁是令牌的所有者。例如,这可以是发出令牌的基础。一旦BigchainDB接受该事务,就更新创建者拥有的令牌的值。现在,创建的所有令牌都与创建者的公钥(tokenCreater.publicKey)相关联。
既然已经创建了令牌,就可以开始将它们分发给所有者。
3、令牌分发
令牌可以传送给无限数量的参与者。在这个示例中,您现在要做一个转移事务,以便向名为John的新用户转移200个令牌。为此,您首先需要创建一个新用户,然后进行传输。下面的代码显示了这一点。
- js
const amountToSend = 200 const newUser = new BigchainDB .Ed25519Keypair(bip39.mnemonicToSeed('newUserseedPhrase') .slice(0, 32)) function transferTokens() { // User who will receive the 200 tokens const newUser = new BigchainDB.Ed25519Keypair() // Search outputs of the transactions belonging the token creator // False argument to retrieve unspent outputs conn.getTransaction(createTxId) .then((txOutputs) => { // Create transfer transaction const createTranfer = BigchainDB.Transaction .makeTransferTransaction( [{ tx: txOutputs, output_index: 0 }], // Transaction output: Two outputs, because the whole input // must be spent [BigchainDB.Transaction.makeOutput( BigchainDB.Transaction .makeEd25519Condition(tokenCreator.publicKey), (tokensLeft - amountToSend).toString()), BigchainDB.Transaction.makeOutput( BigchainDB.Transaction .makeEd25519Condition(newUser.publicKey), amountToSend) ], // Metadata (optional) { transfer_to: 'john', tokens_left: tokensLeft } ) // Sign the transaction with the tokenCreator key const signedTransfer = BigchainDB.Transaction .signTransaction(createTranfer, tokenCreator.privateKey) return conn.postTransactionCommit(signedTransfer) }) .then(res => { // Update tokensLeft tokensLeft -= amountToSend document.body.innerHTML += '<h3>Transfer transaction created</h3>' document.body.innerHTML += res.id }) }
现在,您已经向用户John传输了200个令牌。您可以对多个其他用户重复相同的操作。
使用listOutputs使用false作为第二个参数,您检索了属于用户令牌Creator的所有尚未使用的输出。只有一个输出可以满足这些特性,因为当您将令牌传输到另一个用户时,您将花费这个输出并将所有权交给另一个用户。然后,你查询了那笔交易,并把它转给了约翰。但是,请注意,由于BigchainDB的事务模型,您需要“回馈更改”,所以还有一个事务返回到tokenCreator.publicKey。它的设计方式是所有输入都必须用在事务中。这意味着,如果向John发送部分令牌Left(200个令牌),则必须将其余的令牌(9800个令牌)发送回令牌Creator以保持该数量。
4、不同BigchainDB事务的组合
现在假设您已经接收到几个令牌交易,并且您想要将所有余额合并并将它们转移到另一个用户(例如,您最好的朋友,他保留了一些令牌作为托管)。在BigchainDB中,这也是可能的。下面的代码说明了如何做到这一点。
- js
const amoun 20000 tToSend = 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 [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.postTransactionCommit(signedTransfer) }
您刚刚做了一个转移事务,将两个不同的事务组合到一个输出中。注意,totalTokens数量是必需的变量。它是正在使用的两个输出的令牌的总和。正如您以前所看到的,如果这个数量不正确,则事务将失败,因为您实际上需要在事务中使用所有输出。
transaction1和transaction2可以类似于您之前所做的事务createTranfer。在这种情况下,outputIndex1和outputIndex2将为0。
注意,在我们的示例中,令牌的提供是固定的,并且在创建之后不能再更改。因此,您需要为自己明确定义需要多少令牌。然而,BigchainDB确实提供了可再填充、可分割的资产选项,允许更动态的令牌供应。你可以在这里了解更多。
- 如何掌控自己的时间和生活(how to get control of your time and your life)读书笔记
- How to Build Your Own Blockchain 1 Creating, Storing, Syncing, Displaying, Mining, and Proving Work
- How to Build Your Own Blockchain Part 2 — Syncing Chains From Different Nodes
- How to Build Your Own Blockchain Part 3 — Writing Nodes that Mine and Talk
- How to learn on your own
- How to Build Your Own Blockchain Part 4.1 — Bitcoin Proof of Work Difficulty Explained
- How to Bring Your Future Closer to You 如何让自己更接近梦想
- How to specify your own JVM parameters on Express WAS.
- How to Build Your Own Blockchain Part 4.2 — Ethereum Proof of Work Difficulty Explained
- 【转载】How to develop your own Boot Loader【怎么样开发自己的bootloader】
- “The execution of scripts is disabled on this system” … How to run your own PowerShell scripts
- 如何在Ubuntu9.10上安装RealPlayer(How to install RealPlayer on Ubuntu)
- 如何调整Dreamhost主机PHP上传尺寸的限制/How to change the maximal size of uploading in your Dreamhost
- Pressure hill big how to do?Ten strokes to release your stress!
- How to deal with "Could not find component on update server. Contact VMware Support or your system administrator." in Vmware.
- Mesos + Docker Tutorial: How to Build Your Own Fra
- wireshark lua: How to add your own tab to "Packet Bytes" pane (just like "reassembled TCP" tab) -- 用wireshark lua编写协议解析器dissecto
- 在Ext中如何使窗体总在最前面(how to set a window always on top in Ext)
- How To Make Your Websites Faster On Mobile Devices
- How to get MetaData on client side in WCF?如何在客户端获取WCF service的元数据