Alphabill JavaScript SDK
The Alphabill JavaScript SDK provides the option to connect to Alphabill JSON-RPC server.
Prerequisites
To use the Alphabill JavaScript SDK, you need to set up a JavaScript development environment and have a package manager installed.
The SDK package is available at https://www.npmjs.com/package/@alphabill/alphabill-js-sdk
Installation
Installation is straightforward; use one of the following commands based on your package manager:
- npm
- yarn
npm install @alphabill/alphabill-js-sdk
yarn add @alphabill/alphabill-js-sdk
Client Creation
Currently, the SDK consists of functionality for the Money Partition and the User Token Partition. Before creating a client, you need to know what partition you are going to use. Below are code snippets to create clients based on your platform selection.
- NodeJS
- Web
import { CborCodecNode } from '@alphabill/alphabill-js-sdk/lib/codec/cbor/CborCodecNode.js';
import { DefaultSigningService } from '@alphabill/alphabill-js-sdk/lib/signing/DefaultSigningService.js';
import { createMoneyClient, createTokenClient, http } from '@alphabill/alphabill-js-sdk/lib/StateApiClientFactory.js';
import { TransactionOrderFactory } from '@alphabill/alphabill-js-sdk/lib/transaction/TransactionOrderFactory.js';
const cborCodec = new CborCodecNode();
const signingService = new DefaultSigningService(Base16Converter.decode(config.privateKey));
const transactionOrderFactory = new TransactionOrderFactory(cborCodec, signingService);
const moneyClient = createMoneyClient({
transport: http(config.moneyPartitionUrl, cborCodec),
transactionOrderFactory,
});
const tokenClient = createTokenClient({
transport: http(config.tokenPartitionUrl, cborCodec),
transactionOrderFactory,
});
import { CborCodecWeb } from '@alphabill/alphabill-js-sdk/lib/codec/cbor/CborCodecWeb.js';
import { DefaultSigningService } from '@alphabill/alphabill-js-sdk/lib/signing/DefaultSigningService.js';
import { createMoneyClient, createTokenClient, http } from '@alphabill/alphabill-js-sdk/lib/StateApiClientFactory.js';
import { TransactionOrderFactory } from '@alphabill/alphabill-js-sdk/lib/transaction/TransactionOrderFactory.js';
const cborCodec = new CborCodecNode();
const signingService = new DefaultSigningService(Base16Converter.decode(config.privateKey));
const transactionOrderFactory = new TransactionOrderFactory(cborCodec, signingService);
const moneyClient = createMoneyClient({
transport: http(config.moneyPartitionUrl, cborCodec),
transactionOrderFactory,
});
const tokenClient = createTokenClient({
transport: http(config.tokenPartitionUrl, cborCodec),
transactionOrderFactory,
});
Client Usage
After creating the client, you can call the following functions based on the client type. For a better overview of how to use these functions, refer to the examples that demonstrate the usage of different transactions within the Alphabill JavaScript SDK.
createMoneyClient
This client also contains publicClient functionality.
transferToFeeCredit
(link to example)addFeeCredit
(link to example)lockBill
(link to example)lockFeeCredit
(link to example)closeFeeCredit
(link to example)splitBill
(link to example)transferBillToDustCollector
(link to example)swapBillsWithDustCollector
(link to example)transferBill
(link to example)unlockBill
(link to example)unlockFeeCredit
(link to example)
createTokenClient
This client also contains publicClient functionality.
createNonFungibleTokenType
(link to example)createNonFungibleToken
(link to example)transferNonFungibleToken
(link to example)updateNonFungibleToken
(link to example)createFungibleTokenType
(link to example)createFungibleToken
(link to example)transferFungibleToken
(link to example)splitFungibleToken
(link to example)burnFungibleToken
(link to example)joinFungibleTokens
(link to example)addFeeCredit
(link to example)closeFeeCredit
(link to example)lockFeeCredit
(link to example)unlockFeeCredit
(link to example)lockToken
unlockToken
createPublicClient
This is a lower-level client where you can put together transaction orders and get information about transactions.
getRoundNumber
- Get current round number for given partition (link to example).getUnitsByOwnerId
- Get all unit identifiers for given owner ID on selected partition (link to example).getUnit
- Get unit information for given unit ID on selected partition (link to example).getBlock
- Get block for given block number on selected partition (link to example).getTransactionProof
- Get transaction proof for given transaction hash on selected partition.sendTransaction
- Sends a raw CBOR encoded signed transaction to the network. Returns the transaction hash that can be used with getTransactionProof method to confirm the execution of the transaction.