@galacticexchange/gex-client-js
v0.2.24
Published
Galactic Exchange client tools
Downloads
70
Readme
gex-client-js
Galactic Exchange client tools.
You can use this library to develop browser and Node.js applications that use Galactic Exchange.
Documentation (v. 0.2+)
This README contains outdated documentation (for v.0.1.4).
You can find documentation for the current version here: https://galacticexchange.github.io/gex-client-js/
Old documentation (v. 0.1)
Installation
NPM
npm install @galacticexchange/gex-client-js
Yarn
yarn add @galacticexchange/gex-client-js
Initialization
The package includes both non-minified version and a minified version.
For web browsers you will typycally use non-minified version for development and minified version for production mode.
For Node.js projects you will need to use non-minified version.
Require library (minified)
const gex = require('@galacticexchange/gex-client-js');
Require library (non-minified)
const gex = require('@galacticexchange/gex-client-js/src/index');
Init library
Find out the IP addess and port of the Ethereum blockchain node that you want to connect to.
Note that the standard port is 8546
. Use the address and the port to init the library as illustrated in the example below.
Note: your blockchain node needs to have WebSockets enabled.
let wsAddr = 'ws://0.0.0.0:8546'; // address of the web3 web socket provider
gex.init(wsAddr);
Usage
Main functionality
Create node
// lib init, then:
let ip = '255.255.255.255';
let port = 6000;
gex.createNode(ip, port);
Create mChain
mChain is used to execute transactions and smart contracts. To create an mChain you need to specify the following parametrs:
- storage bytes
- lifetime of the chain (in seconds)
- number of nodes in chain
- deposit
let storageBytes = 1111;
let lifetime = 355;
let maxNodes = 123;
let deposit = 4444;
gex.managerContract().createBasicMchain(storageBytes, lifetime, maxNodes, deposit);
Create aggregation mChain
let storageBytes = 245678;
let lifetime = 3456789;
let maxNodes = 444;
let deposit = 8909;
gex.managerContract().createAggregationMchain(storageBytes, lifetime, maxNodes, deposit);
Get basic mChain
Get info for basic channel by id
async function test() {
let channel = await gex.managerContract().getBasicChannel(2);
console.log(channel);
}
Get basic mChain list (with info)
Get an array of basic mChains for current account (with info)
async function test() {
let channelsInfo = await gex.managerContract().getBasicChannelListInfo();
console.log(channelsInfo);
}
Get basic mChain list (id only)
async function test() {
let channelsIds = await gex.managerContract().getBasicChannelList();
console.log(channelsIds);
}
Same for aggregation mChains:
async function test() {
let aggrChannelsIds = await gex.managerContract().getAggregationChannelList();
let aggrChannel = await gex.managerContract().getAggregationChannel(2);
let aggrChannels = await gex.managerContract().getAggregationChannelListInfo();
}
Withdraw from mChains
// lib init, then:
gex.managerContract().withdrawFromChannels();
Listeners
Full example of nodeCreated listener usage
const gex = require('../src/index');
class Test {
constructor(){
this.val = 0;
let wsAddr = 'ws://51.0.1.99:8546';
gex.init(wsAddr);
this.initListener();
}
initListener(){
let self = this;
let listener = new gex.listener(gex.managerContract().events.NodeCreated(), function (event) {
console.log(event.returnValues);
self.val = event.returnValues.nodeID;
self.checkValue();
});
}
createNode(){
gex.createNode('255.255.255.255', 6000);
}
checkValue(){
console.log('Check value: ');
console.log(this.val)
}
}
let ts = new Test();
ts.createNode();
Available events
Updated for v. 0.0.14
Usage example:
// gex v. 0.0.14
let listener = new gex.listener(gex.manager().events.MchainCreated(), function (event) {
console.log('EVENT');
console.log(event.returnValues);
});
Manager contact
- NodeCreated
- MchainCreated
- AggregationMchainCreated
- MchainAdded
Token contact
// todo
Short API
// todo
Other
Check balance
async function checkBalances() {
let accounts = await gex.gexWeb3.getAccounts();
let firstAccountBalance = await gex.tokenContract().balanceOf(accounts[0]);
console.log('first account balance: ' + firstAccountBalance);
let tokenAddr = gex.managerContract().contractAddress;
let nodeManagerBalance = await gex.tokenContract().balanceOf(tokenAddr);
console.log('node manager balance: ' + nodeManagerBalance);
}
Init with custom web3 provider
const gcjs = require('../src/GexClientJs');
// custom web3 provider
let web3Provider = new Web3.providers.WebsocketProvider();
gcjs.initWithProvider(web3Provider)
Dev init with custom contract
const gcjs = require('../src/GexClientJs');
let wsAddr = 'ws://0.0.0.0:8546';
let abi = { };
let contractAddress = '0x345678...';
gcjs.devInit(wsAddr, abi, contractAddress)
Contribution
Requirements
- Node.js
- npm
Build
npm run build
Testing (mocha)
npm run test