0chain 中文文档教程
0chain Client SDK for Node
这是 0chain js-client-sdk 的 Node.js 实现。 使用此 SDK 与 0Chain 区块链进行交互。 SDK支持钱包创建、交易提交和验证。 除此之外,sdk 还提供 API 来查询区块链本身的健康状况。 有关详细信息,请参阅 example.js 文件。
Installation
通过 Github:https://github.com/0chain/js-client-sdk
APIs
除了 之外的所有函数init()
返回承诺。
首先从 github https://github.com/0chain/js-client import / require
库-sdk
import jsClientSdk from 'js-client';
Initialize
在调用 SDK 中的任何函数之前,应调用具有集群配置和 bls-wasm 实例的 init 函数。
bls-wasm dependency
在初始化 js-client-sdk 时,要传递一个 bls 实例。 可以使用以下命令通过 npm 安装 bls:
npm install bls-wasm
Github 存储库: https://github.com/herumi/bls-wasm
Input Parameters
config -- json string containig cluster configuration
Output
None
以下代码显示使用 0chain-local-cluster 初始化 SDK 的示例。
var config = {
"miners": [
"http://one.devnet-0chain.net:31071/",
"http://one.devnet-0chain.net:31072/",
"http://one.devnet-0chain.net:31073/",
"http://one.devnet-0chain.net:31074/",
"http://one.devnet-0chain.net:31075/",
"http://one.devnet-0chain.net:31076/",
"http://one.devnet-0chain.net:31077/",
"http://one.devnet-0chain.net:31078/",
"http://one.devnet-0chain.net:31079/"
],
"sharders": [
"http://one.devnet-0chain.net:31171/",
"http://one.devnet-0chain.net:31172/",
"http://one.devnet-0chain.net:31173/",
"http://one.devnet-0chain.net:31174/",
"http://one.devnet-0chain.net:31175/",
"http://one.devnet-0chain.net:31176/",
"http://one.devnet-0chain.net:31177/",
"http://one.devnet-0chain.net:31178/",
"http://one.devnet-0chain.net:31179/"
],
"chain_id" : "0afc093ffb509f059c55478bc1a60351cef7b4e9c008a53a6cc8241ca8617dfe",
"clusterName" : "0chain-local-cluster",
"proxyServerUrl" : "http://localhost:9082",
"transaction_timeout" : 20,
"state " : true
}
jsClientSdk.init(config, bls)
要使用本地集群,请使用 bls-wasm 实例配置调用 init 向
jsClientSdk.init(bls)
registerClient
0Chain 区块链注册一个客户端。
输入参数 None
Output
Wallet object 在成功解决承诺的情况下。
jsClientSdk.registerClient()
restoreWallet
使用 0Chain Blockchain 恢复现有客户端。 需要传递秘密助记词
Input Parameters
mnemonic -- string
Output
Wallet --if successful。 否则会出错
jsClientSdk.restoreWallet(mnemonic)
sendTransaction
使用此功能将代币从一个钱包发送到另一个钱包。
输入参数
fromAccount - 从钱包
toWalletId - 到钱包 clientId
值 - 发送金额
- 交易说明
输出
交易 - 如果成功。 否则会出错
jsClientSdk.sendTransaction(fromAccount, toWalletId, value, note)
storeData
使用此函数将数据存储在钱包上
输入参数
fromAccount - 来自钱包
数据 - 要保存在区块链中的数据(字符串类型)
note - 交易说明
输出
事务——如果成功。 否则出错
jsClientSdk.storeData(fromAccount, data, note)
checkTransactionStatus
使用此功能检查之前放置的交易的状态。
注意 交易状态只有在交易被处理后才可用。 请提供至少 3 秒的时间来处理交易。 您可以多次检查状态。
输入参数
hash - 先前放置的交易的哈希
Ouput
TransactionDetails - 具有状态字段的交易的详细信息。
jsClientSdk.checkTransactionStatus(hash)
getBalance
使用此功能获取特定钱包的余额。
输入参数
client_id - 您对其余额感兴趣的 clientId
输出
TransactionDetails - 具有余额字段的交易的详细信息。
jsClientSdk.getBalance(client_id)
executeSmartContract
发送指定金额的交易,如值参数 fromAccount toWalletId 中所述。
输入参数
fromAccount - 来自钱包
smartContractAddress - 预期智能合约
有效负载的地址 - 取决于正在执行的智能合约函数
transactionValue - 交易价值 - 默认为零
输出 事务——如果成功则对象。
jsClientSdk.executeSmartContract(fromAccount, smartContractAddress, payload, transactionValue)
getBlockInfoByHash
使用此函数通过块哈希获取块摘要或块详细信息。
输入参数
hash -- 块
选项的哈希 -- 使用 BlockInfoOptions.HEADER 只获取摘要或 BlockInfoOptions.Full 获取整个块。 默认为 HEADER
callback -- 成功时调用的
函数 errCallback -- 失败时调用的函数
Output
块摘要或块对象,具体取决于指定的选项
jsClientSdk.getBlockInfoByHash(hash, options, callback, errCallback)
getBlockInfoByRound
使用此函数获取块摘要或块详细信息按轮数。
输入参数
round -- 轮数 选项——使用 BlockInfoOptions.HEADER 仅获取摘要或使用 BlockInfoOptions.Full 获取整个块。 默认为 HEADER
回调——成功时调用的
errCallback——失败时调用
函数
的
jsClientSdk.getBlockInfoByRound(round, options, callback, errCallback)
getChainStats
函数当前轮数,平均块生成/完成时间等
输入参数
无
输出
ChainStats - 一个包含有关区块链详细信息的对象。
jsClientSdk.getChainStats()
getLatestFinalized
使用此函数获取最后确定的块摘要。
输入参数
无
输出
BlockSummary --如果成功,则对象具有最新确定块的信息
jsClientSdk.getLatestFinalized()
getRecentFinalized
使用此函数获取最近 10 个最近确定块的摘要。
输入参数
无
输出
BlockSummary 数组。 ——如果成功。
jsClientSdk.getRecentFinalized()
Example file
该 SDK 还附带一个 example.js 文件,用于演示如何使用 API。
0chain Client SDK for Node
This is Node.js implementation of 0chain js-client-sdk. Use this SDK to interact with 0Chain blockchain. The SDK supports wallet creation, transaction submission and verification. In addition to that, the sdk provides APIs to query blockchain health itself. See example.js file for more details.
Installation
Via Github: https://github.com/0chain/js-client-sdk
APIs
All functions except init()
returns promise.
First import / require
the library from github https://github.com/0chain/js-client-sdk
import jsClientSdk from 'js-client';
Initialize
Before any function in the SDK to be called, the init function with the cluster configuration and bls-wasm instance should be called.
bls-wasm dependency
While initialising js-client-sdk, a bls instance is to be passed. bls can be installed via npm using the below command:
npm install bls-wasm
Github Repository: https://github.com/herumi/bls-wasm
Input Parameters
config -- json string containig cluster configuration
Output
None
The below code shows an example of initiailizing the SDK with 0chain-local-cluster.
var config = {
"miners": [
"http://one.devnet-0chain.net:31071/",
"http://one.devnet-0chain.net:31072/",
"http://one.devnet-0chain.net:31073/",
"http://one.devnet-0chain.net:31074/",
"http://one.devnet-0chain.net:31075/",
"http://one.devnet-0chain.net:31076/",
"http://one.devnet-0chain.net:31077/",
"http://one.devnet-0chain.net:31078/",
"http://one.devnet-0chain.net:31079/"
],
"sharders": [
"http://one.devnet-0chain.net:31171/",
"http://one.devnet-0chain.net:31172/",
"http://one.devnet-0chain.net:31173/",
"http://one.devnet-0chain.net:31174/",
"http://one.devnet-0chain.net:31175/",
"http://one.devnet-0chain.net:31176/",
"http://one.devnet-0chain.net:31177/",
"http://one.devnet-0chain.net:31178/",
"http://one.devnet-0chain.net:31179/"
],
"chain_id" : "0afc093ffb509f059c55478bc1a60351cef7b4e9c008a53a6cc8241ca8617dfe",
"clusterName" : "0chain-local-cluster",
"proxyServerUrl" : "http://localhost:9082",
"transaction_timeout" : 20,
"state " : true
}
jsClientSdk.init(config, bls)
To use local cluster, call init with bls-wasm instance configuration
jsClientSdk.init(bls)
registerClient
Registers a client with 0Chain Blockchain.
Input Parameters None
Output
Wallet object in case of successful promise resolved.
jsClientSdk.registerClient()
restoreWallet
Restore a existing client with 0Chain Blockchain. Need to pass the secret mnemonic
Input Parameters
mnemonic -- string
Output
Wallet --if successful. Error otherwise
jsClientSdk.restoreWallet(mnemonic)
sendTransaction
Use this function send tokens from one wallet to the other.
Input Parameters
fromAccount - From Wallet
toWalletId - To wallet clientId
value - amount to send
note - description for the transaction
Ouput
Transaction --if successful. Error otherwise
jsClientSdk.sendTransaction(fromAccount, toWalletId, value, note)
storeData
Use this function to store data on a wallet
Input Parameters
fromAccount - From Wallet
data - data to save in the blockchain (string type)
note - description for the transaction
Ouput
Transaction --if successful. Error otherwise
jsClientSdk.storeData(fromAccount, data, note)
checkTransactionStatus
Use this function to check the status of the transaction that has been placed earlier.
Note Transaction status will be available only after the transaction is processed. Please provide at least 3 seconds time for the transaction to process. You can check status multiple times.
Input Parameters
hash - Hash of the transaction that was placed earlier
Ouput
TransactionDetails -- Details of the transaction which has the status field.
jsClientSdk.checkTransactionStatus(hash)
getBalance
Use this function to get the balance of particular wallet.
Input Parameters
client_id - The clientId whose balance you are interested in
Ouput
TransactionDetails -- Details of the transaction which has the balance field.
jsClientSdk.getBalance(client_id)
executeSmartContract
sends a transaction of the specified amount as mentioned in the value parameter fromAccount toWalletId.
Input Parameters
fromAccount - from Wallet
smartContractAddress - address of intended smartContract
payload - depends on the smartContract function that is being executed
transactionValue - transaction value --by default zero
Output Transaction -- object if successful.
jsClientSdk.executeSmartContract(fromAccount, smartContractAddress, payload, transactionValue)
getBlockInfoByHash
Use this function to get the block summary or block detail by block hash.
Input Parameters
hash -- hash of the block
options -- use BlockInfoOptions.HEADER to get only the summary or BlockInfoOptions.Full to get entire block. Default is HEADER
callback -- function to be called upon success
errCallback -- function to be called upon failure
Output
Block Summary or Block object depending on the options specified
jsClientSdk.getBlockInfoByHash(hash, options, callback, errCallback)
getBlockInfoByRound
Use this function to get the block summary or block detail by round number.
Input Parameters
round -- round number options -- use BlockInfoOptions.HEADER to get only the summary or BlockInfoOptions.Full to get entire block. Default is HEADER
callback -- function to be called upon success
errCallback -- function to be called upon failure
Output
Block Summary or Block object depending on the options specified
jsClientSdk.getBlockInfoByRound(round, options, callback, errCallback)
getChainStats
Use this function to get current information about the blockchain like current roundnumber, average block generation/finalization time, etc
Input Parameters
none
Output
ChainStats --an object that has detailed information about the blockchain.
jsClientSdk.getChainStats()
getLatestFinalized
use this function to get the last finalized block summary.
Input Parameters
None
Output
BlockSummary --object if successful that has information of latest finalized block
jsClientSdk.getLatestFinalized()
getRecentFinalized
Use this function to get summaries of last 10 recently finalized blocks.
Input Parameters
none
Output
array of BlockSummary . --if successful.
jsClientSdk.getRecentFinalized()
Example file
The SDK also comes with an example.js file that demonstrates how the APIs can be used.