diff --git a/src/api.ts b/src/api.ts index 9c78903e..cdd6b76e 100644 --- a/src/api.ts +++ b/src/api.ts @@ -4,8 +4,6 @@ import { connect, disconnect, isConnected, - getServerInfo, - getFee, getLedgerVersion, formatLedgerClose } from './server/server' @@ -52,13 +50,15 @@ import { BookOffersRequest, BookOffersResponse, GatewayBalancesRequest, GatewayBalancesResponse, LedgerRequest, LedgerResponse, - LedgerEntryRequest, LedgerEntryResponse + LedgerEntryRequest, LedgerEntryResponse, + ServerInfoRequest, ServerInfoResponse } from './common/types/commands' import RangeSet from './common/rangeset' import * as ledgerUtils from './ledger/utils' import * as schemaValidator from './common/schema-validator' +import {getServerInfo, getFee} from './common/serverinfo' import {clamp} from './ledger/utils' export type APIOptions = { @@ -151,6 +151,8 @@ class RippleAPI extends EventEmitter { Promise async request(command: 'ledger_entry', params: LedgerEntryRequest): Promise + async request(command: 'server_info', params?: ServerInfoRequest): + Promise async request(command: string, params: object): Promise diff --git a/src/common/serverinfo.ts b/src/common/serverinfo.ts index fe1addfe..b45e38b1 100644 --- a/src/common/serverinfo.ts +++ b/src/common/serverinfo.ts @@ -1,7 +1,8 @@ import * as _ from 'lodash' import {convertKeysFromSnakeCaseToCamelCase} from './utils' -import Connection from './connection' import BigNumber from 'bignumber.js' +import {RippleAPI} from '../index' +import {ServerInfoResponse} from './types/commands' export type GetServerInfoResponse = { buildVersion: string, @@ -39,8 +40,16 @@ function renameKeys(object, mapping) { }) } -function getServerInfo(connection: Connection): Promise { - return connection.request({command: 'server_info'}).then(response => { +function computeFeeFromServerInfo( + cushion: number, serverInfo: ServerInfoResponse +): string { + return (new BigNumber(serverInfo.info.validated_ledger.base_fee_xrp)). + times(serverInfo.info.load_factor). + times(cushion).toString() +} + +function getServerInfo(this: RippleAPI): Promise { + return this.request('server_info').then(response => { const info = convertKeysFromSnakeCaseToCamelCase(response.info) renameKeys(info, {hostid: 'hostID'}) if (info.validatedLedger) { @@ -61,18 +70,15 @@ function getServerInfo(connection: Connection): Promise { }) } -function computeFeeFromServerInfo(cushion: number, - serverInfo: GetServerInfoResponse -): string { - return (new BigNumber(serverInfo.validatedLedger.baseFeeXRP)). - times(serverInfo.loadFactor). - times(cushion).toString() -} - -function getFee(connection: Connection, cushion: number): Promise { - return getServerInfo(connection).then(serverInfo => { - return computeFeeFromServerInfo(cushion, serverInfo) - }) +async function getFee(this: RippleAPI, cushion?: number): Promise { + if (cushion === undefined) { + cushion = this._feeCushion + } + if (cushion === undefined) { + cushion = 1.2 + } + const response = await this.request('server_info') + return computeFeeFromServerInfo(cushion, response) } export { diff --git a/src/common/types/commands/index.ts b/src/common/types/commands/index.ts index e30954c1..0c3e581e 100644 --- a/src/common/types/commands/index.ts +++ b/src/common/types/commands/index.ts @@ -6,3 +6,4 @@ export * from './book_offers' export * from './gateway_balances' export * from './ledger' export * from './ledger_entry' +export * from './server_info' diff --git a/src/common/types/commands/server_info.ts b/src/common/types/commands/server_info.ts new file mode 100644 index 00000000..7de6a588 --- /dev/null +++ b/src/common/types/commands/server_info.ts @@ -0,0 +1,51 @@ +export interface ServerInfoRequest { + id?: number +} + +export interface ServerInfoResponse { + info: { + amendment_blocked?: boolean, + build_version: string, + closed_ledger?: LedgerInfo, + complete_ledgers: string, + hostid: string, + io_latency_ms: number, + last_close: { + converge_time_s: number, + proposers: number + }, + load?: { + job_types: { + job_type: string, + per_second: number, + in_progress: number + }[], + threads: number + }, + load_factor: number, + load_factor_local?: number, + load_factor_net?: number, + load_factor_cluster?: number, + load_factor_fee_escalation?: number, + load_factor_fee_queue?: number, + load_factor_server?: number, + peers: number, + pubkey_node: string, + pubkey_validator: string, + server_state: string, + state_accounting: any, + uptime: number, + validated_ledger?: LedgerInfo, + validation_quorum: number, + validator_list_expires: string + }, +} + +export interface LedgerInfo { + age: number, + base_fee_xrp: number, + hash: string, + reserve_base_xrp: number, + reserve_inc_xrp: number, + seq: number, +} diff --git a/src/server/server.ts b/src/server/server.ts index 060e8d16..d7ed3251 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -1,5 +1,4 @@ import * as common from '../common' -import {GetServerInfoResponse} from '../common/serverinfo' function isConnected(): boolean { return this.connection.isConnected() @@ -17,15 +16,6 @@ function disconnect(): Promise { return this.connection.disconnect() } -function getServerInfo(): Promise { - return common.serverInfo.getServerInfo(this.connection) -} - -function getFee(): Promise { - const cushion = this._feeCushion || 1.2 - return common.serverInfo.getFee(this.connection, cushion) -} - function formatLedgerClose(ledgerClose: any): Object { return { baseFeeXRP: common.dropsToXrp(ledgerClose.fee_base), @@ -43,8 +33,6 @@ export { connect, disconnect, isConnected, - getServerInfo, - getFee, getLedgerVersion, formatLedgerClose } diff --git a/src/transaction/utils.ts b/src/transaction/utils.ts index e00c008b..57a447dd 100644 --- a/src/transaction/utils.ts +++ b/src/transaction/utils.ts @@ -67,7 +67,7 @@ function prepareTransaction(txJSON: any, api: RippleAPI, return Promise.resolve(txJSON) } const cushion = api._feeCushion - return common.serverInfo.getFee(api.connection, cushion).then(fee => { + return api.getFee(cushion).then(fee => { return api.connection.getFeeRef().then(feeRef => { const extraFee = (txJSON.TransactionType !== 'EscrowFinish' ||