apla-blockchain-tools
Version:
Module contains a number of tools to work with Apla Blockchain
195 lines (140 loc) • 5.49 kB
Markdown
# Session
This package allows developers to interact with the Apla Platform via REST API.
It works fine with the latest version of Apla Node, so it can be used for:
- [Quick-start](https://github.com/AplaProject/quick-start/releases) from 0.7.0 version
- [TestNet](https://testapla0.apla.io)
- [MainNet](https://main-node0.apla.io)
# Getting started
## Generate new public and private keys
```ecmascript 6
let {Session} = require("apla-blockchain-tools");
/*
* Put here address of the node you want to connect to
* MainNet - http://main-node0.apla.io
* Testnet - https://testapla[0-3].apla.io:7079/api/v2
* Quickstart - http://127.0.0.1:17301/api/v2
*/
let apiUrl = "http://127.0.0.1:17301/api/v2";
let run = async () => {
let networkId = 1;
let session = new Session(apiUrl, networkId, {});
// generate new keys
session.generateKeys();
// login to the platform
await session.login({
ecosystem: 1
});
// params to call the contract with
let params = {
str: "testString",
n: 1299,
f: 983.9,
b: true
};
let options = {
maxSum: null,
payOver: null
};
// calling the contract
let res = await session.callContract("@1test", params, options);
console.log(res)
};
```
## Use existing public and private keys
```ecmascript 6
let {Session} = require("apla-blockchain-tools");
let privKey = "0804a7f64250c23254ff911aa822af9d72905b00919d49c46e70c0d3f245ccd4";
let apiUrl = "http://127.0.0.1:17301/api/v2";
let run = async () => {
let networkId = 1;
let session = new Session(apiUrl, networkId, {
privateKey: privKey
});
await session.login({
ecosystem: 1
});
let params = {
str: "testString",
n: 1299,
f: 983.9,
b: true
};
let options = {
maxSum: null,
payOver: null
};
let res = await session.callContract("@1test", params, options);
console.log(res)
};
```
# Methods
Class for interacting with the Apla Netowrk
**Kind**: global class
* [Session](#Session)
* [new Session(apiUrl, networkId, options)](#new_Session_new)
* [.generateKeys()](#Session+generateKeys)
* [.login(options)](#Session+login)
* [.waitTxStatus(hash)](#Session+waitTxStatus) ⇒ <code>object</code>
* [.callContract(name, params, options)](#Session+callContract) ⇒ <code>object</code>
* [.getDetailedBlocks(blockId, count)](#Session+getDetailedBlocks) ⇒ <code>Array</code>
* [.getMaxBlockId()](#Session+getMaxBlockId) ⇒ <code>number</code>
<a name="new_Session_new"></a>
### new Session(apiUrl, networkId, options)
| Param | Type | Description |
| --- | --- | --- |
| apiUrl | <code>string</code> | url to connect to |
| networkId | <code>string</code> | id of the network |
| options | <code>object</code> | |
| options.privateKey | <code>string</code> | private key |
| options.maxSum | <code>number</code> \| <code>string</code> | not used for now |
| options.payOver | <code>number</code> \| <code>string</code> | not used for now |
| options.txStatusMaxTries | <code>number</code> | how many times client will try to get the result |
| options.waitBeforeNextTry | <code>number</code> | how many ms client has to wait before next try to get the result of tx |
<a name="Session+generateKeys"></a>
### session.generateKeys()
generates new private key and corresponding public key
**Kind**: instance method of [<code>Session</code>](#Session)
**Access**: public
<a name="Session+login"></a>
### session.login(options)
login to the system
**Kind**: instance method of [<code>Session</code>](#Session)
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| options | | |
| options.ecosystem | <code>number</code> \| <code>string</code> | ecosystem to login to |
| options.roleId | <code>number</code> \| <code>string</code> | login to login to |
<a name="Session+waitTxStatus"></a>
### session.waitTxStatus(hash) ⇒ <code>object</code>
Waiting for the sent transaction to be executed
**Kind**: instance method of [<code>Session</code>](#Session)
| Param | Description |
| --- | --- |
| hash | (for now supports only one hash at a time) |
<a name="Session+callContract"></a>
### session.callContract(name, params, options) ⇒ <code>object</code>
Call the contract by name
**Kind**: instance method of [<code>Session</code>](#Session)
**Access**: public
| Param | Type | Description |
| --- | --- | --- |
| name | <code>string</code> | name of the contract |
| params | <code>object</code> | name->value mapping |
| options | | |
| options.maxSum | <code>string</code> \| <code>number</code> | max cost of transaction |
| options.payOver | <code>string</code> \| <code>number</code> | (fuelRate + options.payOver) * fuelVolume |
<a name="Session+getDetailedBlocks"></a>
### session.getDetailedBlocks(blockId, count) ⇒ <code>Array</code>
returns list of blocks of size `count` from block with id = `blockId`
**Kind**: instance method of [<code>Session</code>](#Session)
**Access**: public
| Param | Type |
| --- | --- |
| blockId | <code>number</code> \| <code>string</code> |
| count | <code>number</code> |
<a name="Session+getMaxBlockId"></a>
### session.getMaxBlockId() ⇒ <code>number</code>
returns id of the last block for the node
**Kind**: instance method of [<code>Session</code>](#Session)
**Access**: public