UNPKG

openchain-sdk-yxl-ts

Version:

OpenChain SDK for browser

248 lines (221 loc) 7.75 kB
import humps from 'humps'; import errors from '../exception/index.js'; import CommonUtil from '../common/util.js'; class Block extends CommonUtil { constructor(options = {}) { super(options); if (!(this instanceof Block)) { return new Block(options); } } async getNumber() { try { const result = await this._request('get', 'block/number'); return result; } catch (err) { console.error('Error in getNumber:', err); return this._responseError(this.options.errors.INVALID_ARGUMENTS); } } async checkStatus() { try { const data = await this._request('get', 'getModulesStatus'); // console.log('checkStatus',data); if (data) { const info = data.ledger_manager; return this._responseData({ isSynchronous: info.chain_max_ledger_seq === info.ledger_sequence }); } return this._responseError(this.options.errors.INVALID_ARGUMENTS); } catch (err) { console.error('Error in checkStatus:', err); return this._responseError(this.options.errors.INVALID_ARGUMENTS); } } async getTransactions(blockNumber) { try { const verifyResult = this._verifyValue(blockNumber); // console.log('getTransactions',verifyResult); // Add this line to log the verifyResult information if (!verifyResult) { return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } const data = await this._request('get', 'getTransactionHistory', { ledger_seq: blockNumber, }); // console.log('getTransactions',data); // Add this line to log the data t if (data.error_code === 0) { return this._responseData({ total_count: data.result.total_count || 0, transactions: data.result.transactions || [] }); } return this._responseError(this.options.errors.QUERY_RESULT_NOT_EXIST); } catch (err) { console.error('Error in getTransactions:', err); return this._responseError(this.options.errors.QUERY_RESULT_NOT_EXIST); } } async getInfo(blockNumber) { try { const verifyResult = this._verifyValue(blockNumber); if (!verifyResult) { return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } const data = await this._request('get', 'getLedger', { seq: blockNumber, }); if (data.error_code === 0) { const result = data.result; return this._responseData({ closeTime: result.closeTime || result.header.close_time, number: result.number || result.header.seq, txCount: result.txCount || result.header.tx_count, version: result.version || result.header.version }); } return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } catch (err) { console.error('Error in getInfo:', err); return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } } async getLatestInfo() { try { const data = await this._request('get', 'getLedger'); if (data.error_code === 0) { const result = data.result; return this._responseData({ closeTime: result.closeTime || result.header.close_time, number: result.number || result.header.seq, txCount: result.txCount || result.header.tx_count, version: result.version || result.header.version }); } return this._responseError(this.options.errors.INVALID_ARGUMENTS); } catch (err) { console.error('Error in getLatestInfo:', err); return this._responseError(this.options.errors.INVALID_ARGUMENTS); } } async getValidators(blockNumber) { try { const verifyResult = this._verifyValue(blockNumber); if (!verifyResult) { return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } const data = await this._request('get', 'getLedger', { seq: blockNumber, with_validator: true, }); if (data.error_code === 0) { const result = data.result; return this._responseData({ validators: result.validators || [] }); } return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } catch (err) { console.error('Error in getValidators:', err); return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } } async getLatestValidators() { try { const data = await this._request('get', 'getLedger', { with_validator: true, }); if (data.error_code === 0) { const result = data.result; return this._responseData({ validators: result.validators || [] }); } return this._responseError(this.options.errors.INVALID_ARGUMENTS); } catch (err) { console.error('Error in getLatestValidators:', err); return this._responseError(this.options.errors.INVALID_ARGUMENTS); } } async getReward(blockNumber) { try { const verifyResult = this._verifyValue(blockNumber); if (!verifyResult) { return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } const data = await this._request('get', 'getLedger', { seq: blockNumber, with_block_reward: true, }); if (data.error_code === 0) { const result = data.result; return this._responseData({ blockReward: result.blockReward, validatorsReward: result.validatorsReward || [] }); } return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } catch (err) { console.error('Error in getReward:', err); return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } } async getLatestReward() { try { const data = await this._request('get', 'getLedger', { with_block_reward: true, }); if (data.error_code === 0) { const result = data.result; return this._responseData({ blockReward: result.blockReward, validatorsReward: result.validatorsReward || [] }); } return this._responseError(this.options.errors.INVALID_ARGUMENTS); } catch (err) { console.error('Error in getLatestReward:', err); return this._responseError(this.options.errors.INVALID_ARGUMENTS); } } async getFees(blockNumber) { try { const verifyResult = this._verifyValue(blockNumber); if (!verifyResult) { return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } const data = await this._request('get', 'getLedger', { seq: blockNumber, with_fee: true, }); if (data.error_code === 0) { const result = data.result; return this._responseData({ fees: result.fees }); } return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } catch (err) { console.error('Error in getFees:', err); return this._responseError(this.options.errors.INVALID_BLOCKNUMBER_ERROR); } } async getLatestFees() { try { const data = await this._request('get', 'getLedger', { with_fee: true, }); if (data.error_code === 0) { const result = data.result; return this._responseData({ fees: result.fees }); } return this._responseError(this.options.errors.INVALID_ARGUMENTS); } catch (err) { console.error('Error in getLatestFees:', err); return this._responseError(this.options.errors.INVALID_ARGUMENTS); } } } export default Block;