@koibanx/ledger-sdk
v0.0.10
Published
Ledger SDK
Downloads
1,253
Readme
Koibanx Ledger SDK
Ledger SDK based in module Ledger
The ledger module provides a way to handle custom tokens transactions among accounts. Each token supply can be increased by minting, or decreased by burning. Every transaction is made asynchronously and it´s state notified by POST callbacks. Every token, account and transaction is backed by a blockchain providing all its benefits and hiding the complexity associated to the technology.
What can be done?
- Create tokens (example US Dolar)
- Create accounts to hold any number of different tokens (example, an account can hold, US Dolars, Mexican Pesos, …etc)
- Mint and burn tokens into accounts: Every time you mint you are incrementing the total supply (circulating amount of tokens), and every time you burn, you decrease that supply
- Transfer tokens between any account: Here the supply is not changed
- Swap tokens: Given two tokens and 2 accounts (can be between the same account) exchange the tokens one for another (equivalent to burn X amount from one token and mint Y to the other in an atomic transaction)
- Check token´s supply
What do i need to care about?
As the ledger uses the blockchain to settle its transactions, and for using the blockchain you need to pay, you must ensure that:
- The gas station you configured has enough funds for you to transact
- If a budget manager is involved in the fueling process (check the gas station configuration for that), provide while creating a token a budget allowance with enough funds. Keep in mind that you will be charged for every set of 50 transactions you generate (this is configurable, as greater the amount of transactions you book, the cheaper the fueling process gets)
Endpoint documentation
Check the OpenApi 3.0 documentation Here
SDK Documentation
Installation
npm install @koibanx/ledger-sdk
NOTE: you must have the npm token in your .npmrc file
Initialization
Node
Using ES6 import
import LedgerSdk from '@koibanx/ledger-sdk';
const ledger = LedgerSdk({
baseURL: 'http://your-url',
});
With require
exports.__esModule = true;
const LedgerSdk = require('@koibanx/ledger-sdk')["default"];
const ledger = LedgerSdk({
baseURL: 'http://your-url',
});
Types
- Typescript (
@koibanx/ledger-sdk/dist/index.d.ts
)
Examples
Using ES6 import
import ledgerSdk from "@koibanx/ledger-sdk";
const ledger = LedgerSdk({
baseURL: 'http://your-url',
})
const catchError = (err, modulo) => {
console.log('Modulo: ', modulo);
console.log('details: ', err.details);
console.log('shortMessage: ', err.message);
console.log('errorCode: ', err.code);
}
/** Default */
ledgerSdk.getLedgerStatus().then((res) => {
console.log('getLedgerStatus: ', res.toDate);
}).catch((err) => catchError(err, 'getLedgerStatus'));
ledgerSdk.getEstimatedRemainingLedgerUsage().then((res) => {
console.log('getEstimatedRemainingLedgerUsage: ', res);
}).catch((err) => catchError(err, 'getEstimatedRemainingLedgerUsage'));
ledgerSdk.getFuelingLedgerAccount().then((res) => {
console.log('getFuelingLedgerAccount: ', res);
}).catch((err) => catchError(err, 'getFuelingLedgerAccount'));
ledgerSdk.wallets.createWallet({})
.then((res) => console.log('createWallet: ', res))
.catch((err) => catchError(err, 'createWallet'));
ledgerSdk.getLedgerTransaction({ id: '62ec9b642b0be63e4b5cbbba' })
.then((res) => console.log('getLedgerTransaction: ', res.status))
.catch((err) => catchError(err, 'getLedgerTransaction'));
/** Wallet */
ledgerSdk.wallets.getWallet(({ id: '62ec970b2b0be63e4b5cbb97' }))
.then((res) => console.log('getWallet: ', res.status))
.catch((err) => catchError(err, 'getWallet'));
ledgerSdk.wallets.createWallet({ callback: 'http://testing.com' })
.then((res) => console.log('createWallet: ', res.status))
.catch((err) => catchError(err, 'createWallet'));
/** Tokens */
ledgerSdk.tokens.createToken({
budgetAllowance: '62f42d17176cc9be63d5d1b1',
callback: 'http://testing.com',
}).then((res) => console.log('createToken: ', JSON.stringify(res)))
.catch((err) => catchError(err, 'createToken'));
ledgerSdk.tokens.getToken({
id: '6302b2da6ee0cd90f068b93e',
}).then((res) => console.log('getToken: ', JSON.stringify(res)))
.catch((err) => catchError(err, 'getToken'));
ledgerSdk.wallets.createWallet({}).then((wall) => {
ledgerSdk.tokens.createToken({}).then((tok) => {
ledgerSdk.tokens.mintToken({
receiverWalletId: wall._id,
amount: 10,
token: tok._id,
})
.then((res) => console.log('mintToken: ', JSON.stringify(res)))
.catch((err) => catchError(err, 'mintToken'));
});
});
ledgerSdk.wallets.createWallet({}).then((wall) => {
ledgerSdk.tokens.createToken({}).then((tok) => {
ledgerSdk.tokens.burnToken({
receiverWalletId: wall._id,
amount: 10,
token: tok._id,
})
.then((res) => console.log('burnToken: ', JSON.stringify(res)))
.catch((err) => catchError(err, 'burnToken'));
});
});
ledgerSdk.wallets.createWallet({}).then((wall) => {
ledgerSdk.tokens.createToken({
budgetAllowance: 'dummy',
}).then((tok) => {
ledgerSdk.tokens.mintToken({
receiverWalletId: wall._id,
amount: 10,
token: tok._id,
})
.then((mint) => {
sleep(60000).then(() => {
allTransfersPerformed([mint]).then(() => {
ledgerSdk.tokens.getTokenBalance({
walletId: wall._id,
tokenId: tok._id,
}).then((res) => console.log('getTokenBalance: ', JSON.stringify(res.balance)))
.catch((err) => catchError(err, 'getTokenBalance'));
}).catch((err) => catchError(err, 'allTransfersPerformed'));
});
})
.catch((err) => catchError(err, 'mintToken'));
});
});
ledgerSdk.wallets.createWallet({}).then((wall) => {
ledgerSdk.wallets.createWallet({}).then((wall2) => {
ledgerSdk.tokens.createToken({
budgetAllowance: 'dummy2',
}).then((tok) => {
ledgerSdk.tokens.mintToken({
receiverWalletId: wall._id,
amount: 10,
token: tok._id,
}).then((mint) => {
sleep(120000).then(() => {
allTransfersPerformed([mint]).then(() => {
ledgerSdk.tokens.getTokenBalance({
walletId: wall._id,
tokenId: tok._id,
}).then(() => {
ledgerSdk.tokens.transferTokens({
amount: 10,
senderWalletId: wall._id,
receiverWalletId: wall2._id,
token: tok._id,
}).then((res) => console.log('transferTokens', JSON.stringify(res)))
.catch((err) => catchError(err, 'transferTokens'));
}).catch((err) => catchError(err, 'getTokenBalance'));
}).catch((err) => catchError(err, 'allTransfersPerformed'));
});
})
.catch((err) => catchError(err, 'mintToken'));
});
});
});