0.19.1 • Published 5 years ago

noah-sdk-js v0.19.1

Weekly downloads
3
License
MIT
Repository
github
Last release
5 years ago

Logo

NPM Package Build Status Coverage Status License: MIT

This package aims to provide with an easy-to-use JS helping developers to communicate with the Noah blockchain through its API.

This package should be an aid to any developer working on JS applications with the Noah blockchain.

Please note that this SDK is under active development and is subject to change.

It is complemented by the following packages:

Install

npm install noah-sdk-js

or from browser

<script src="https://unpkg.com/noah-sdk-js"></script>
<script>
const instance = new noahSDK.Noah({...});
const txParams = new noahSDK.SendTxParams({...});
</script>

Usage

Post transaction full example

import {Noah, SendTxParams} from "noah-sdk-js";

const noahSDK = new Noah({apiType: 'node', baseURL: 'https://noah-node-1.testnet.minter.network'});
const txParams = new SendTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    nonce: 1,
    chainId: 1,
    address: 'Mx7633980c000139dd3bd24a3f54e06474fa941e16',
    amount: 10,
    coinSymbol: 'MNT',
    feeCoinSymbol: 'ASD',
    gasPrice: 1,
    message: 'custom message',
});

noahSDK.postTx(txParams)
    .then((txHash) => {
        alert(`Tx created: ${txHash}`);
    }).catch((error) => {
        const errorMessage = error.response.data.error.log;
        alert(errorMessage);
    });

Initialization

Create noahSDK instance from Noah constructor Noah accept axios config as params and return axios instance

One extra field of options object is apiType, which is one of 'gate' or 'node'. It is used to determine what type of API to use.

// specify gate url
const noahGate = new Noah({apiType: 'gate', baseURL: 'https://gate.minter.network'});
// specify node url
const noahNode = new Noah({apiType: 'node', baseURL: 'https://noah-node-1.testnet.minter.network'});

Noah constructor has the following options:

  • apiType: 'gate' or 'node'
  • baseURL: API url
  • chainId: default chain ID, used if no chainId specified in the tx params

noahSDK instance has the following methods:

.postTx()

Post new transaction to the blockchain Accept tx params(#Tx params constructors) object and make asynchronous request to the blockchain API. txParams.nonce - optional, if no nonce given, it will be requested by getNonce automatically. txParams.gasPrice - 1 by default, fee multiplier, should be equal or greater than current mempool's min gas price. gasRetryLimit - count of repeating request, 2 by default. If first request fails because of low gas, it will be repeated with updated gasPrice Returns promise that resolves with sent transaction hash.

/**
 * @typedef {Object} TxParams
 * @property {string|Buffer} privateKey
 * @property {number} [nonce] - can be omitted, will be received by `getNonce`
 * @property {number} [chainId=1] - 1 = mainnet, 2 = testnet
 * @property {number} [gasPrice=1] - can be updated automatically on retry, if gasRetryLimit > 1
 * @property {string} [gasCoin='NOAH']
 * @property {string|Buffer} txType
 * @property {Buffer} txData
 * @property {string} [message]
 */

/**
* @param {TxParams} txParams
* @param {number} [gasRetryLimit=2] - number of retries, if request was failed because of low gas
* @return {Promise<string>}
*/
noahSDK.postTx(txParams, {gasRetryLimit: 2})
    .then((txHash) => {
        console.log(txHash);
        // 'Mt...'
    })
    .catch((error) => {
        // ...
    })

.postSignedTx()

Post new transaction to the blockchain Accept signed tx string or Buffer and make asynchronous request to the blockchain API. Returns promise that resolves with sent transaction hash.

/**
* @param {string|Buffer} signedTx
* @return {Promise<string>}
*/
noahSDK.postSignedTx('f8920102018a4d4e540000000000000001aae98a4d4e5400000000...')
    .then((txHash) => {
        console.log(txHash);
        // 'Mt...'
    })
    .catch((error) => {
        // ...
    })

.getNonce()

Get nonce for the new transaction from given address. Accept address string and make asynchronous request to the blockchain API. Returns promise that resolves with nonce for new tx (current address tx count + 1).

noahSDK.getNonce('Mx...')
    .then((nonce) => {
        console.log(nonce);
        // 123
    })
    .catch((error) => {
        // ...
    })

.estimateCoinSell()

Estimate how much coins you will get for selling some other coins.

noahSDK.estimateCoinSell({
    coinToSell: 'MNT',
    valueToSell: '10',
    coinToBuy: 'MYCOIN',
})
    .then((result) => {
        console.log(result.will_get, result.commission);
        // 123, 0.1
    })
    .catch((error) => {
        // ...
    })

.estimateCoinBuy()

Estimate how much coins you will pay for buying some other coins.

noahSDK.estimateCoinBuy({
    coinToBuy: 'MYCOIN',
    valueToBuy: '10',
    coinToSell: 'MNT',
})
    .then((result) => {
        console.log(result.will_pay, result.commission);
        // 123, 0.1
    })
    .catch((error) => {
        // ...
    })

.estimateTxCommission()

Estimate transaction fee. Useful for transactions with gasCoin different from base coin NOAH (or MNT). Accept string with raw signed tx. Resolves with commission value.

noahSDK.estimateTxCommission({
        transaction: 'f8920101028a4d4e540000000000000001aae98a4d4e...'
    })
    .then((commission) => {
        console.log(commission);
        // 0.1
    })
    .catch((error) => {
        // ...
    })

.issueCheck()

Noah Checks are issued offline and do not exist in blockchain before “cashing”.

// Since issuing checks is offline, you can use it standalone without instantiating SDK
import {issueCheck} from "noah-sdk-js";
const check = issueCheck({
    privateKey: '2919c43d5c712cae66f869a524d9523999998d51157dc40ac4d8d80a7602ce02',
    passPhrase: 'pass',
    nonce: '1', // it is string and must be unique
    chainId: 1,
    coin: 'MNT',
    value: 10,
    dueBlock: 999999, // at this block number check will be expired
});
console.log(check);
// => 'Mcf8a002843b9ac9ff8a4d4e5400000000000000888ac7230489e80000b841ed4e21035ad4d56901422c19e7fc867a63dcab709d6d0dcc0b6333cb7365d187519e1291bbc002189e7030dedfbbc4feb733da73f9409de4f01365dd3f5f4927011ca0507210c64b3aeb7c81a2db06204b935814c28482175dee756b1f05414d18e594a06173c7c8ee51ad76e9704a39ffc5c0ab11514d8b68efcbc8df1db194d9e296ee'

// This method also available on the SDK instance
const check = noahSDK.issueCheck({...});

.decodeCheck()

Decode raw check

// Since issuing checks is offline, you can use it standalone without instantiating SDK
import {decodeCheck} from "noah-sdk-js";
const check = decodeCheck('Mcf8a002843b9ac9ff8a4d4e5400000000000000888ac7230489e80000b841ed4e21035ad4d56901422c19e7fc867a63dcab709d6d0dcc0b6333cb7365d187519e1291bbc002189e7030dedfbbc4feb733da73f9409de4f01365dd3f5f4927011ca0507210c64b3aeb7c81a2db06204b935814c28482175dee756b1f05414d18e594a06173c7c8ee51ad76e9704a39ffc5c0ab11514d8b68efcbc8df1db194d9e296ee');
console.log(check);
// =>
// {
//   nonce: '1',
//   chainId: 1,
//   coin: 'MNT',
//   value: '10',
//   dueBlock: 999999,
// } 

// This method also available on the SDK instance
const check = noahSDK.decodeCheck('...');

Tx params constructors

Get params object from constructor and pass it to postTx method to post transaction to the blockchain

Send

import {SendTxParams} from "noah-sdk-js";
const txParams = new SendTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    address: 'Mx7633980c000139dd3bd24a3f54e06474fa941e16',
    amount: 10,
    coinSymbol: 'MNT',
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Multisend

import {MultisendTxParams} from "noah-sdk-js";
const txParams = new MultisendTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    list: [
        {
            value: 10,
            coin: 'MNT',
            to: 'Mx7633980c000139dd3bd24a3f54e06474fa941e16',
        },
        {
            value: 2,
            coin: 'MNT',
            to: 'Mxfe60014a6e9ac91618f5d1cab3fd58cded61ee99',
        }
    ],
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Sell

import {SellTxParams} from "noah-sdk-js";
const txParams = new SellTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    coinFrom: 'MNT',
    coinTo: 'BELTCOIN',
    sellAmount: 10,
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Sell All

import {SellAllTxParams} from "noah-sdk-js";
const txParams = new SellAllTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    coinFrom: 'MNT',
    coinTo: 'BELTCOIN',
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Buy

import {BuyTxParams} from "noah-sdk-js";
const txParams = new BuyTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    coinFrom: 'MNT',
    coinTo: 'BELTCOIN',
    buyAmount: 10,
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Create Coin

import {CreateCoinTxParams} from "noah-sdk-js";
const txParams = new CreateCoinTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    coinName: 'My Coin',
    coinSymbol: 'MYCOIN',
    initialAmount: 5,
    crr: 10,
    initialReserve: 20,
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Declare Candidacy

import {DeclareCandidacyTxParams} from "noah-sdk-js";
const txParams = new DeclareCandidacyTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    address: 'Mx7633980c000139dd3bd24a3f54e06474fa941e16',
    publicKey: 'Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3',
    commission: 10,
    coinSymbol: 'MNT',
    stake: 100,
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Edit Candidate

import {EditCandidateTxParams} from "noah-sdk-js";
const txParams = new DeclareCandidacyTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    publicKey: 'Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3',
    rewardAddress: 'Mx7633980c000139dd3bd24a3f54e06474fa941e16',
    ownerAddress: 'Mx7633980c000139dd3bd24a3f54e06474fa941e16',
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Delegate

import {DelegateTxParams} from "noah-sdk-js";
const txParams = new DelegateTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    publicKey: 'Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3',
    coinSymbol: 'MNT',
    stake: 100,
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Unbond

import {UnbondTxParams} from "noah-sdk-js";
const txParams = new UnbondTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    publicKey: 'Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3',
    coinSymbol: 'MNT',
    stake: 100,
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Set Candidate On

import {SetCandidateOnTxParams} from "noah-sdk-js";
const txParams = new SetCandidateOnTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    publicKey: 'Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3',
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Set Candidate Off

import {SetCandidateOffTxParams} from "noah-sdk-js";
const txParams = new SetCandidateOffTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    publicKey: 'Mpf9e036839a29f7fba2d5394bd489eda927ccb95acc99e506e688e4888082b3a3',
    feeCoinSymbol: 'ASD',
    message: 'custom message',
});

noahSDK.postTx(txParams);

Redeem Check

import {RedeemCheckTxParams} from "noah-sdk-js";
const txParams = new RedeemCheckTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    check: 'Mcf8a002843b9ac9ff8a4d4e5400000000000000888ac7230489e80000b841ed4e21035ad4d56901422c19e7fc867a63dcab709d6d0dcc0b6333cb7365d187519e1291bbc002189e7030dedfbbc4feb733da73f9409de4f01365dd3f5f4927011ca0507210c64b3aeb7c81a2db06204b935814c28482175dee756b1f05414d18e594a06173c7c8ee51ad76e9704a39ffc5c0ab11514d8b68efcbc8df1db194d9e296ee',
    password: '123',
    feeCoinSymbol: 'MNT',
});

noahSDK.postTx(txParams);

Create Multisig

import {CreateMultisigTxParams} from "noah-sdk-js";
const txParams = new CreateMultisigTxParams({
    privateKey: '5fa3a8b186f6cc2d748ee2d8c0eb7a905a7b73de0f2c34c5e7857c3b46f187da',
    chainId: 1,
    addresses: ['Mx7633980c000139dd3bd24a3f54e06474fa941e00', 'Mxfe60014a6e9ac91618f5d1cab3fd58cded61ee99'],
    weights: [40, 80],
    threshold: 100,
    feeCoinSymbol: 'MNT',
});

noahSDK.postTx(txParams);

Prepare Signed Transaction

Used under the hood of PostTx, accepts txParams object as argument

import {prepareSignedTx} from 'noah-sdk-js';
const tx = prepareSignedTx(txParams);
console.log('signed tx', tx.serialize().toString('hex'));

// get actual nonce first
noahSDK.getNonce('Mx...')
    .then((nonce) => {
        const tx = prepareSignedTx({...txParams, nonce});
        console.log('signed tx', tx.serialize().toString('hex'));
    });

License

MIT License