@0bsnetwork/zbs-api 中文文档教程

发布于 6年前 浏览 24 项目主页 更新于 3年前

Zbs API npm versiondownloads/month

适用于 Node.js 和浏览器的 Zbs 平台核心功能和 Zbs API 库。

本文档的最新和最实际版本 托管在 GitHub 上

Installation

npm install @0bsnetwork/zbs-api --save

在 Node.js 中:

const ZbsAPI = require('@0bsnetwork/zbs-api');

在浏览器中:

<script src="./node_modules/@0bsnetwork/zbs-api/dist/zbs-api.min.js"></script>

您甚至可以在 Web Worker 中使用 @0bsnetwork/zbs-api

Usage

const Zbs = ZbsAPI.create(ZbsAPI.TESTNET_CONFIG);

Seed

您可以创建一个新的随机种子:

const seed = Zbs.Seed.create();

console.log(seed.phrase); // 'hole law front bottom then mobile fabric under horse drink other member work twenty boss'
console.log(seed.address); // '3Mr5af3Y7r7gQej3tRtugYbKaPr5qYps2ei'
console.log(seed.keyPair); // { privateKey: 'HkFCbtBHX1ZUF42aNE4av52JvdDPWth2jbP88HPTDyp4', publicKey: 'AF9HLq2Rsv2fVfLPtsWxT7Y3S9ZTv6Mw4ZTp8K8LNdEp' }

该种子可以使用密码加密:

const password = '0123456789';
const encrypted = seed.encrypt(password);

console.log(encrypted); // 'U2FsdGVkX1+5TpaxcK/eJyjht7bSpjLYlSU8gVXNapU3MG8xgWm3uavW37aPz/KTcROK7OjOA3dpCLXfZ4YjCV3OW2r1CCaUhOMPBCX64QA/iAlgPJNtfMvjLKTHZko/JDgrxBHgQkz76apORWdKEQ=='

并解密(当然使用相同的密码):

const restoredPhrase = Zbs.Seed.decryptSeedPhrase(encrypted, password);

console.log(restoredPhrase); // 'hole law front bottom then mobile fabric under horse drink other member work twenty boss'

使用错误的密码调用 Zbs.Seed.decryptSeedPhrase() 抛出一个例外。

您还可以从现有种子创建一个 Seed 对象:

const anotherSeed = Zbs.Seed.fromExistingPhrase('a seed which was backed up some time ago');

console.log(seed.phrase); // 'a seed which was backed up some time ago'
console.log(seed.address); // '3N3dy1P8Dccup5WnYsrC6VmaGHF6wMxdLn4'
console.log(seed.keyPair); // { privateKey: '2gSboTPsiQfi1i3zNtFppVJVgjoCA9P4HE9K95y8yCMm', publicKey: 'CFr94paUnDSTRk8jz6Ep3bzhXb9LKarNmLYXW6gqw6Y3' }

Node API

尽管此 API 的结构和命名可能看起来很奇怪,但它们反映了后端 Node API 的结构和命名。

首先,快速介绍一下该结构:

  • addresses
    • balance — your regular ZBS balance
    • balanceDetails — the details on your ZBS balance (see below)
  • aliases
    • byAlias — Zbs address related to a given alias
    • byAddress — a list of aliases related to a given Zbs address
  • assets
    • balances — your token balances
    • balance — your balance for a given token
    • distribution — the distribution of a given token between addresses
  • blocks
    • get — get a block by its signature (ID)
    • at — get the block at a certain height
    • first — get the first block
    • last — get the last block
    • height — get the current height of the blockchain
  • leasing
    • getAllActiveLeases — get all your active Lease transactions
  • transactions
    • get — get a transaction by its signature (ID)
    • getList — get the list of last N transactions for a given Zbs address
    • utxSize — get the current size of the unconfirmed transactions pool
    • utxGet — get an unconfirmed transaction by its signature (ID)
    • utxGetList — get the list of unconfirmed transactions for a given Zbs address
    • broadcast — POST-methods to send the following transaction types
    • rawBroadcast — POST-method to send any JSON to the /transactions/broadcast path
  • utils
    • time — get the current Node timestamp

Sending transactions

您需要一对来自账户余额的密钥来发送交易:

const seed = Zbs.Seed.fromExistingPhrase('a seed from an account with some funds');
Issue transaction

这是创建您自己的代币的方法,该代币可以在用户之间进行交易、分发并用于您的业务目的。

const issueData = {

    name: 'Your token name',
    description: 'Some words about it',

    // With given options you'll have 100000.00000 tokens
    quantity: 10000000000,
    precision: 5,

    // This flag defines whether additional emission is possible
    reissuable: false,

    fee: 100000000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('issue', issueData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Transfer transaction

转账交易允许您将 ZBS 或您拥有的任何代币发送到另一个 Zbs 地址。

const transferData = {

    // An arbitrary address; mine, in this example
    recipient: '3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8',

    // ID of a token, or ZBS
    assetId: 'ZBS',

    // The real amount is the given number divided by 10^(precision of the token)
    amount: 10000000,

    // The same rules for these two fields
    feeAssetId: 'ZBS',
    fee: 100000,

    // 140 bytes of data (it's allowed to use Uint8Array here)
    attachment: '',

    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('transfer', transferData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Reissue transaction

尽管有这个交易名称,但它允许您发行额外数量的最初由您发行的代币。

const reissueData = {

    // Asset ID which is to be additionnaly emitted
    assetId: '5xN8XPkKi7RoYUAT5hNKC26FKCcX6Rj6epASpgFEYZss',

    // Additional quantity is the given number divided by 10^(precision of the token)
    quantity: 100000000,

    reissuable: false,
    fee: 100000000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('reissue', reissueData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Burn transaction

在这里,您可以销毁由您发行且仍在您的余额中的任意数量的代币。

const burnData = {

    // Asset ID and its quantity to be burned
    assetId: '5xN8XPkKi7RoYUAT5hNKC26FKCcX6Rj6epASpgFEYZss',
    quantity: 20000000000,

    fee: 100000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('burn', burnData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Lease transaction

这是您可以将 ZBS 租用到不同地址的方式。

const leaseData = {

    recipient: '5xN8XPkKi7RoYUAT5hNKC26FKCcX6Rj6epASpgFEYZss',

    // Both amount and fee may be presented as divided by 10^8 (8 is Zbs precision)
    amount: 1000000000, // 10 Zbs
    fee: 100000, // 0.001 Zbs

    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('lease', leaseData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Cancel Leasing transaction

此交易为您提供了一种取消先前发送的租赁交易的方法。

const cancelLeasingData = {

    // Related Lease transaction ID
    transactionId: '2kPvxtAit2nsumxBL7xYjvaWYmvmMfDL5oPgs4nZsHvZ',

    fee: 100000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('cancelLeasing', cancelLeasingData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Create Alias transaction

Zbs 地址可以有别名——可以用来代替地址的简短易读名称。 此事务创建一个别名。

const createAliasData = {

    // That's a kind of a nickname you attach to your address
    alias: 'xenohunter',

    fee: 100000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('createAlias', createAliasData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});

Getting the information from Node

最常用的 GET 请求是那些与余额和交易历史相关的请求。

Zbs balance

Zbs 余额有两种类型:简单的,带有可选的 confirmations 参数,以及详细的,显示不同类型的 Zbs 余额。

对于第一种类型,无需额外的参数,您可以获得地址的当前余额:

Zbs.API.Node.addresses.balance('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8').then((balance) => {
    console.log(balance);
});

如果您传递可选的 confirmations 参数,您将获得 N 个确认的余额,即 N 个区块前的余额从现在开始:

Zbs.API.Node.addresses.balance('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8', 100).then((balance) => {
    console.log(balance);
});

对于第二种类型,有一个单独的方法:

Zbs.API.Node.addresses.balanceDetails('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8').then((balanceDetails) => {
   console.log(balanceDetails);
});
Token balances

您可以获得地址上所有余额的列表:

Zbs.API.Node.assets.balances(address).then((balancesList) => {
   console.log(balancesList);
});

您还可以获取给定代币的余额:

Zbs.API.Node.assets.balance(address, assetId).then((balance) => {
   console.log(balance);
});
Token distribution

一种非常有用的方法,允许您获取带有余额的地图拥有令牌的所有地址:

Zbs.API.Node.assets.distribution(assetId).then((distributionMap) => {
   console.log(distributionMap);
});
Transactions

区块链中的每笔交易都有自己的 ID。 您既可以通过 ID 获取一个,也可以获取所有最近交易的列表。

Zbs.API.Node.transactions.get('Bn2opYvcmYAMCaJHKP1uXYCHFGnAyrzGoiboBLT8RALt').then((tx) => {
    console.log(tx);
});

要获得列表,您需要提供一个地址,该地址是结果列表中交易的发送者或接收者:

Zbs.API.Node.transactions.getList('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8').then((txList) => {
    console.log(txList);
}):

大多数区块链中的概念之一是 UTX,未确认的交易池。 在块出现之间的时间内,来自用户的交易存储在其中。

有一些方法可以获取 UTX 池和 UTX 池本身的大小(注意这里不需要地址):

Zbs.API.Node.transactions.utxSize().then((utxSize) => {
    console.log(utxSize);
});

Zbs.API.Node.transactions.utxGetList().then((utxList) => {
    console.log(utxList);
});

此外,如果交易仍在 UTX 池中并且您知道它的 ID,则只能从 UTX 中获取它:

Zbs.API.Node.transactions.utxGet('Bn2opYvcmYAMCaJHKP1uXYCHFGnAyrzGoiboBLT8RALt').then((tx) => {
    console.log(tx);
});
Aliases

除了创建别名,您还可以获取绑定到地址的别名列表,或获取与给定别名相关的地址。

Zbs.API.Node.aliases.byAddress('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8').then((aliasesList) => {
    console.log(aliasesList);
});

Zbs.API.Node.aliases.byAlias('xenohunter').then((address) => {
    console.log(address);
});
Blocks

这里的一切都很简单。 您可以通过其签名(get())或高度(at())来获取整个块。 方法 height() 返回 Zbs 区块链的当前高度。 其余方法的名称不言而喻。

Zbs.API.Node.blocks.get(signature).then((block) => console.log(block));

Zbs.API.Node.blocks.at(height).then((block) => console.log(block));

Zbs.API.Node.blocks.height().then((currentHeight) => console.log(currentHeight));

Zbs.API.Node.blocks.first().then((firstBlock) => console.log(firstBlock));

Zbs.API.Node.blocks.last().then((lastBlock) => console.log(lastBlock));

Configuration

即使在运行时,配置也是可以更改的。 配置的结构如下:

const newConfig = {

    // The byte allowing to distinguish networks (mainnet, testnet, devnet, etc)
    networkByte: Zbs.constants.MAINNET_BYTE,

    // Node and Matcher addresses, no comments here
    nodeAddress: 'https://nodes.zbsnodes.com',
    matcherAddress: 'https://nodes.zbsnodes.com/matcher',

    // If a seed phrase length falls below that value an error will be thrown
    minimumSeedLength: 50

};

所有字段都是可选的,只有填充的才会被替换。

您可以这样更改配置:

Zbs.config.set(newConfig);

Tools

Get address from public key

const address = Zbs.tools.getAddressFromPublicKey('GL6Cbk3JnD9XiBRK5ntCavSrGGD5JT9pXSRkukcEcaSW');
console.log(address); // '3N1JKsPcQ5x49utR79Maey4tbjssfrn2RYp'

Common pitfalls

Precision and coins-to-tokens transformation

在 Zbs 区块链中,不同的令牌具有不同的精度,即小数位数。 例如,它将是 10.00 美元和 10.00000000 BTC。 这种区别允许为各种目的创建令牌,但有时也会使事情更难理解。

出现了两个词:tokencoin。 Token用于指代金额的全部部分。 硬币描述了给定令牌可能的最小值。 对于 USD,token 是一美元,coin 是一美分。 如果您熟悉比特币,您可能会遇到 Satoshi 这个词,它指的是单个比特币的亿分之一。

在区块链中,每个令牌都以其明确指定的精度和硬币数量存储。 每笔交易都以其代币形式签署并存储在区块链中。 因此,如果您向某人发送 2 美元代币,您实际上发送的是 200 美元代币。

费用、发行交易和租赁金额等也是如此。

Zbs 精度等于 8。因此 Zbs 区块链中有 100000000 * 10^8 Zbs 币(Wavelets)。

Reissuability and the additive nature of it

增发交易中的金额不是指增发后资产的最终数量,而是指将添加到当前代币数量中的数量。

Zbs ID in the library and in the blockchain

Zbs 区块链最棘手的事情之一是 Zbs ID 等于空字符串。 在 Node API 的第一个版本中,它也等于空字符串。 这是一种不明显且具有潜在危险的行为。 因此在这个库中 Zbs ID 严格等于字符串 ZBS。 请注意这个事实。

Fee asset choice for transfer transactions

只有一种类型的交易(目前)我们可以使用任意代币作为费用。 唯一的限制是您连接的节点必须支持您用作费用的令牌。 请注意,使用 Zbs 费用的交易将优先于使用其他代币费用的交易。

Impossibility of transactions with the absolutely same data

交易 ID 是根据交易中除签名之外的所有数据构建的。 该过程是确定性的。 因此,不可能有两个具有完全相同数据的事务。

Delays in the leasing process

出于安全原因,所有租用的 Zb 都在 1000 个区块后才生效。 如果您的生成余额没有立即更新,请不要担心。

Mess with balances in the first version of API

碰巧 Zbs 余额和代币余额在 Zbs API 的第一个版本中通过不同的 API 方法提供服务。 这不是很有用,我们以其他方式设计了新版本。

Different types of Zbs balance

有一种最容易理解的 Zbs 平衡类型。 这是常规余额。 它通过 Zbs.API.Node.addresses.balance() 提供。 还有几种与租赁相关的 Zbs 余额及其处理延迟。

  1. Regular — that's how much Zbs you have, including those you leased;
  2. Available — the same as regular only without Zbs you leased;
  3. Effectiveavailable plus those Zbs which is leased to you;
  4. Generating — the minimal effective for last 1000 blocks;
  5. Leased — the amount you leased to other addresses.

您可以租赁和消费的可用余额。

产生平衡给你挖矿的力量。

Tests

cd ./node_modules/@0bsnetwork/zbs-api/
npm install
npm run test # to run tests in Node.js
npm run test-browser # to run test in Chrome browser

可以在 ./nodemodules/@0bsnetwork/zbs-api/karma.conf.js_ 文件中更改测试配置。

Authors

另请参阅参与此项目的贡献者列表。

License

该项目根据 MIT 许可证获得许可 - 有关详细信息,请参阅 LICENSE.md 文件。

Zbs API npm versiondownloads/month

Zbs Platform core features and Zbs API library for both Node.js and browser.

The latest and most actual version of this documentation is hosted on GitHub.

Installation

npm install @0bsnetwork/zbs-api --save

In Node.js:

const ZbsAPI = require('@0bsnetwork/zbs-api');

In browser:

<script src="./node_modules/@0bsnetwork/zbs-api/dist/zbs-api.min.js"></script>

You can use @0bsnetwork/zbs-api even within Web Workers.

Usage

const Zbs = ZbsAPI.create(ZbsAPI.TESTNET_CONFIG);

Seed

You can create a new random seed:

const seed = Zbs.Seed.create();

console.log(seed.phrase); // 'hole law front bottom then mobile fabric under horse drink other member work twenty boss'
console.log(seed.address); // '3Mr5af3Y7r7gQej3tRtugYbKaPr5qYps2ei'
console.log(seed.keyPair); // { privateKey: 'HkFCbtBHX1ZUF42aNE4av52JvdDPWth2jbP88HPTDyp4', publicKey: 'AF9HLq2Rsv2fVfLPtsWxT7Y3S9ZTv6Mw4ZTp8K8LNdEp' }

That seed may be encrypted with a password:

const password = '0123456789';
const encrypted = seed.encrypt(password);

console.log(encrypted); // 'U2FsdGVkX1+5TpaxcK/eJyjht7bSpjLYlSU8gVXNapU3MG8xgWm3uavW37aPz/KTcROK7OjOA3dpCLXfZ4YjCV3OW2r1CCaUhOMPBCX64QA/iAlgPJNtfMvjLKTHZko/JDgrxBHgQkz76apORWdKEQ=='

And decrypted (with the same password, of course):

const restoredPhrase = Zbs.Seed.decryptSeedPhrase(encrypted, password);

console.log(restoredPhrase); // 'hole law front bottom then mobile fabric under horse drink other member work twenty boss'

Being called with a wrong password Zbs.Seed.decryptSeedPhrase() throws an exception.

You also can create a Seed object from an existing seed:

const anotherSeed = Zbs.Seed.fromExistingPhrase('a seed which was backed up some time ago');

console.log(seed.phrase); // 'a seed which was backed up some time ago'
console.log(seed.address); // '3N3dy1P8Dccup5WnYsrC6VmaGHF6wMxdLn4'
console.log(seed.keyPair); // { privateKey: '2gSboTPsiQfi1i3zNtFppVJVgjoCA9P4HE9K95y8yCMm', publicKey: 'CFr94paUnDSTRk8jz6Ep3bzhXb9LKarNmLYXW6gqw6Y3' }

Node API

Although the structure and naming of this API may seem strange, they reflect those of the backend Node API.

First, a quick introduction into the structure:

  • addresses
    • balance — your regular ZBS balance
    • balanceDetails — the details on your ZBS balance (see below)
  • aliases
    • byAlias — Zbs address related to a given alias
    • byAddress — a list of aliases related to a given Zbs address
  • assets
    • balances — your token balances
    • balance — your balance for a given token
    • distribution — the distribution of a given token between addresses
  • blocks
    • get — get a block by its signature (ID)
    • at — get the block at a certain height
    • first — get the first block
    • last — get the last block
    • height — get the current height of the blockchain
  • leasing
    • getAllActiveLeases — get all your active Lease transactions
  • transactions
    • get — get a transaction by its signature (ID)
    • getList — get the list of last N transactions for a given Zbs address
    • utxSize — get the current size of the unconfirmed transactions pool
    • utxGet — get an unconfirmed transaction by its signature (ID)
    • utxGetList — get the list of unconfirmed transactions for a given Zbs address
    • broadcast — POST-methods to send the following transaction types
    • rawBroadcast — POST-method to send any JSON to the /transactions/broadcast path
  • utils
    • time — get the current Node timestamp

Sending transactions

You will need a pair of keys from an account with a balance to send transactions:

const seed = Zbs.Seed.fromExistingPhrase('a seed from an account with some funds');
Issue transaction

This is the way to create your own token which can be traded, distributed amongst users and used for your business purposes.

const issueData = {

    name: 'Your token name',
    description: 'Some words about it',

    // With given options you'll have 100000.00000 tokens
    quantity: 10000000000,
    precision: 5,

    // This flag defines whether additional emission is possible
    reissuable: false,

    fee: 100000000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('issue', issueData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Transfer transaction

The Transfer transaction allows you to send ZBS or any token you possess to another Zbs address.

const transferData = {

    // An arbitrary address; mine, in this example
    recipient: '3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8',

    // ID of a token, or ZBS
    assetId: 'ZBS',

    // The real amount is the given number divided by 10^(precision of the token)
    amount: 10000000,

    // The same rules for these two fields
    feeAssetId: 'ZBS',
    fee: 100000,

    // 140 bytes of data (it's allowed to use Uint8Array here)
    attachment: '',

    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('transfer', transferData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Reissue transaction

Despite this transaction name, it allows you to issue an additional amount of a token which was initially issued by you.

const reissueData = {

    // Asset ID which is to be additionnaly emitted
    assetId: '5xN8XPkKi7RoYUAT5hNKC26FKCcX6Rj6epASpgFEYZss',

    // Additional quantity is the given number divided by 10^(precision of the token)
    quantity: 100000000,

    reissuable: false,
    fee: 100000000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('reissue', reissueData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Burn transaction

Here you can burn any amount of token which was issued by you and is still on your balance.

const burnData = {

    // Asset ID and its quantity to be burned
    assetId: '5xN8XPkKi7RoYUAT5hNKC26FKCcX6Rj6epASpgFEYZss',
    quantity: 20000000000,

    fee: 100000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('burn', burnData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Lease transaction

This is the way you can lease your ZBS to a different address.

const leaseData = {

    recipient: '5xN8XPkKi7RoYUAT5hNKC26FKCcX6Rj6epASpgFEYZss',

    // Both amount and fee may be presented as divided by 10^8 (8 is Zbs precision)
    amount: 1000000000, // 10 Zbs
    fee: 100000, // 0.001 Zbs

    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('lease', leaseData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Cancel Leasing transaction

This transaction gives you a mean to cancel previously sent Lease transactions.

const cancelLeasingData = {

    // Related Lease transaction ID
    transactionId: '2kPvxtAit2nsumxBL7xYjvaWYmvmMfDL5oPgs4nZsHvZ',

    fee: 100000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('cancelLeasing', cancelLeasingData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});
Create Alias transaction

A Zbs address can have aliases — short readable names which can be used instead of address. This transaction creates an alias.

const createAliasData = {

    // That's a kind of a nickname you attach to your address
    alias: 'xenohunter',

    fee: 100000,
    timestamp: Date.now()

};

Zbs.API.Node.transactions.broadcast('createAlias', createAliasData, seed.keyPair).then((responseData) => {
    console.log(responseData);
});

Getting the information from Node

The most used GET requests are those related to balances and transactions history.

Zbs balance

There are two types of Zbs balance: simple, with optional confirmations parameter, and detailed, showing different types of Zbs balance.

With the first type, without additional arguments, you get the current balance on an address:

Zbs.API.Node.addresses.balance('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8').then((balance) => {
    console.log(balance);
});

If you pass an optional confirmations argument, you get the balance with N confirmations, i.e. the balance as it was N blocks ago from the moment:

Zbs.API.Node.addresses.balance('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8', 100).then((balance) => {
    console.log(balance);
});

For the second type, there is a separate method:

Zbs.API.Node.addresses.balanceDetails('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8').then((balanceDetails) => {
   console.log(balanceDetails);
});
Token balances

You can get the list of all balances on an address:

Zbs.API.Node.assets.balances(address).then((balancesList) => {
   console.log(balancesList);
});

You also can get the balance of a given token:

Zbs.API.Node.assets.balance(address, assetId).then((balance) => {
   console.log(balance);
});
Token distribution

A very useful method allowing you to get a map with balances of all addresses in possession of a token:

Zbs.API.Node.assets.distribution(assetId).then((distributionMap) => {
   console.log(distributionMap);
});
Transactions

Every transaction in the blockchain has its own ID. You can both get one by ID, or get a list of all recent transactions.

Zbs.API.Node.transactions.get('Bn2opYvcmYAMCaJHKP1uXYCHFGnAyrzGoiboBLT8RALt').then((tx) => {
    console.log(tx);
});

To get the list you need to provide an address which is either the sender or the recipient of the transactions in the resulting list:

Zbs.API.Node.transactions.getList('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8').then((txList) => {
    console.log(txList);
}):

One of the concepts in most blockchains is UTX, unconfirmed transactions pool. During the time between blocks appearance, transactions from users are stored in it.

There are methods to get the size of UTX pool and UTX pool itself (note that the address is not needed here):

Zbs.API.Node.transactions.utxSize().then((utxSize) => {
    console.log(utxSize);
});

Zbs.API.Node.transactions.utxGetList().then((utxList) => {
    console.log(utxList);
});

Also if a transaction is still in UTX pool and you know its ID, you can get only it from UTX:

Zbs.API.Node.transactions.utxGet('Bn2opYvcmYAMCaJHKP1uXYCHFGnAyrzGoiboBLT8RALt').then((tx) => {
    console.log(tx);
});
Aliases

Aside from creating an alias, you also can get the list of aliases bound to an address, or get the address related to the given alias.

Zbs.API.Node.aliases.byAddress('3PMgh8ra7v9USWUJxUCxKQKr6PM3MgqNVR8').then((aliasesList) => {
    console.log(aliasesList);
});

Zbs.API.Node.aliases.byAlias('xenohunter').then((address) => {
    console.log(address);
});
Blocks

Everything is simple here. You can get the whole block by its signature (get()) or height (at()). Method height() returns the current height of the Zbs blockchain. The names of the remaining methods speak for themselves.

Zbs.API.Node.blocks.get(signature).then((block) => console.log(block));

Zbs.API.Node.blocks.at(height).then((block) => console.log(block));

Zbs.API.Node.blocks.height().then((currentHeight) => console.log(currentHeight));

Zbs.API.Node.blocks.first().then((firstBlock) => console.log(firstBlock));

Zbs.API.Node.blocks.last().then((lastBlock) => console.log(lastBlock));

Configuration

The configuration is changeable even during the runtime. The structure of the config is following:

const newConfig = {

    // The byte allowing to distinguish networks (mainnet, testnet, devnet, etc)
    networkByte: Zbs.constants.MAINNET_BYTE,

    // Node and Matcher addresses, no comments here
    nodeAddress: 'https://nodes.zbsnodes.com',
    matcherAddress: 'https://nodes.zbsnodes.com/matcher',

    // If a seed phrase length falls below that value an error will be thrown
    minimumSeedLength: 50

};

All fields are optional, only filled ones will be replaced.

You can change the config like that:

Zbs.config.set(newConfig);

Tools

Get address from public key

const address = Zbs.tools.getAddressFromPublicKey('GL6Cbk3JnD9XiBRK5ntCavSrGGD5JT9pXSRkukcEcaSW');
console.log(address); // '3N1JKsPcQ5x49utR79Maey4tbjssfrn2RYp'

Common pitfalls

Precision and coins-to-tokens transformation

In Zbs blockchain different tokens have different precision, i.e. number of decimal digits. For example, it would be 10.00 USD and 10.00000000 BTC. That distinction allows to create tokens for various purposes but also makes things harder to understand sometimes.

Two words have emerged: token and coin. Token is used to refer to the whole part of the amount. Coin describes the smallest value which is possible for a given token. For USD token would be one dollar, and coin would be one cent. If you are familiar with Bitcoin you could have encountered the word Satoshi which refers to one hundred millionth of a single Bitcoin.

In the blockchain every token is stored with its explicitly specified precision and amount of coins. Every transaction is signed and stored in the blockchain in its coin representation. So if you send 2 USD tokens to someone, you really send 200 USD token coins instead.

The same goes for the fees, and issue transactions, and leasing amounts, and so on.

Zbs precision equals 8. Therefore there are 100000000 * 10^8 of Zbs coins (Wavelets) in Zbs blockchain.

Reissuability and the additive nature of it

The amount in reissue transactions refer not to the final amount of asset after reissuing but to the amount which will be added to the current token amount.

Zbs ID in the library and in the blockchain

One of the trickiest things about Zbs blockchain is that Zbs ID equals empty string. In the first version on Node API it also equals to empty string. That is an unobvious and potentially dangerous behavior. Therefore in this library Zbs ID strictly equals string ZBS. Please mind that fact.

Fee asset choice for transfer transactions

There is only one type of transactions (currently) in which we can use arbitrary tokens as fees. The only limitation is that the Node to which you connect must support the token you use as fees. Please note that transactions with the Zbs fee will be prior over transactions with fees in other tokens.

Impossibility of transactions with the absolutely same data

Transaction IDs are built from all the data in a transaction except the signature. That process is deterministic. So there cannot be two transactions with the absolutely same data.

Delays in the leasing process

For the security reasons all leased Zbs take effect only after 1000 blocks. Don't worry when your generating balance isn't updated right away.

Mess with balances in the first version of API

It happened so that Zbs balance and token balances are served through different API methods in the first version of Zbs API. That's not very useful and we designed the new version otherwise.

Different types of Zbs balance

There is the most understandable type of Zbs balance. It is the regular balance. It is served through Zbs.API.Node.addresses.balance(). There are also several types of Zbs balance related to leasing and the delays in its processing.

  1. Regular — that's how much Zbs you have, including those you leased;
  2. Available — the same as regular only without Zbs you leased;
  3. Effectiveavailable plus those Zbs which is leased to you;
  4. Generating — the minimal effective for last 1000 blocks;
  5. Leased — the amount you leased to other addresses.

Available balance you can lease and spend.

Generating balance gives you mining power.

Tests

cd ./node_modules/@0bsnetwork/zbs-api/
npm install
npm run test # to run tests in Node.js
npm run test-browser # to run test in Chrome browser

Test configuration may be changed in the ./nodemodules/@0bsnetwork/zbs-api/karma.conf.js_ file.

Authors

See also the list of contributors who participated in this project.

License

This project is licensed under the MIT License - see the LICENSE.md file for details.

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文