UNPKG

xek-sdk

Version:

SDK for katana blockchain

191 lines (175 loc) 4.13 kB
'use strict'; const rp = require ('request-promise'); const {DEFAULT_API} = require ('./constant'); /** * class API provide for users some methods to interacting with api of Katana's blockchain */ class API { constructor (api = DEFAULT_API) { this.api = api; } /** * getBlock return block with height, that is provided by users or latest block. * @param height * @returns {Promise<>} */ async getBlock (height) { try { let block = await this.requestAPI (`/block/${height}`, 'GET'); return block; } catch (e) { throw new Error('Get block failed'); } }; /** * * @param txHash * @returns {Promise<>} */ async getTransaction (txHash) { try { return await this.requestAPI (`/transaction/${txHash}`, 'GET'); } catch (e) { throw new Error("API_GET_TRANSACTION_ERROR"); } }; /** * * @param address * @returns {Promise<>} */ async getAccountDetail (address) { try { let account = await this.requestAPI (`/wallet/${address}`, 'GET'); return account; } catch (e) { throw new Error('Get details account failed'); } }; /** * * @param address * @param page * @param limit * @returns {Promise<void>} */ async getGetHistoryTransaction ({address, page, limit = 10}) { try { return await this.requestAPI (`wallet/history/account?address=${address}&page=${page}&limit=${limit}`, "GET"); } catch (e) { throw new Error("ERROR_API_GET_HISTORY_TRANSACTION"); } }; /** * * @param address * @returns {Promise<>} */ async getSequence (address) { try { let sequence = await this.getAccountDetail (address); return sequence.data.sequence; } catch (e) { throw new Error ('ERROR_GET_SEQUENCE'); } }; /** * * @param address * @returns {Promise<>} */ async getBalance (address) { try { let balance = await this.getAccountDetail (address); return balance.data.balance; } catch (e) { throw new Error ('ERROR_GET_BALANCE'); } }; /** * * @param address : address of user * @returns {Promise<>} */ async getPermission (address) { try { let permission = await this.getAccountDetail (address); return permission; } catch (e) { throw new Error ('Get permission failed' + e.message); } }; /** * * @param method * @param body * @param headers * @returns {{headers: *, method: *, body: (*)}} */ static getParams (method, body, headers, uri) { const requestHeader = { Accept: 'application/json', 'Content-Type': 'application/json', ...headers }; return { method: method, headers: requestHeader, body: body, uri: uri, json: true }; }; /** * * @param tx * @returns {Promise<>} */ async sendTransaction (tx) { try { if (!tx) { return new Error('Transaction empty'); } let result = await this.requestAPI (`/wallet/broadcast_tx`, 'POST', tx); return result; } catch (e) { throw new Error ('Send transaction failed' + e.message); } }; async callTransaction(tx) { try { if (!tx) { return new Error('Transaction empty'); } let result = await this.requestAPI (`/wallet/call_tx_sim`, 'POST', tx); return result; } catch (e) { throw new Error ('Send transaction failed' + e.message); } } async getTransactionFee() { try { return await this.requestAPI('/transaction/fee/info','GET'); } catch (e) { throw new Error ('Get transaction fee' + e.message); } } /** * * @param endpoint * @param method * @param body * @param headers * @returns {Promise<void>} */ async requestAPI (endpoint, method, body, headers) { const fullEndpoint = this.api + endpoint; let params = API.getParams (method, body, headers, fullEndpoint); try { return await rp (params); } catch (e) { throw new Error ('Error when request data'); } }; } module.exports = API;