add server_info request typing (#895)

This commit is contained in:
Fred K. Schott
2018-05-17 16:37:17 -07:00
committed by Elliot Lee
parent 54f12862dc
commit 2e5b435b11
6 changed files with 79 additions and 31 deletions

View File

@@ -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<LedgerResponse>
async request(command: 'ledger_entry', params: LedgerEntryRequest):
Promise<LedgerEntryResponse>
async request(command: 'server_info', params?: ServerInfoRequest):
Promise<ServerInfoResponse>
async request(command: string, params: object):
Promise<object>

View File

@@ -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<GetServerInfoResponse> {
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<GetServerInfoResponse> {
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<GetServerInfoResponse> {
})
}
function computeFeeFromServerInfo(cushion: number,
serverInfo: GetServerInfoResponse
): string {
return (new BigNumber(serverInfo.validatedLedger.baseFeeXRP)).
times(serverInfo.loadFactor).
times(cushion).toString()
async function getFee(this: RippleAPI, cushion?: number): Promise<string> {
if (cushion === undefined) {
cushion = this._feeCushion
}
function getFee(connection: Connection, cushion: number): Promise<string> {
return getServerInfo(connection).then(serverInfo => {
return computeFeeFromServerInfo(cushion, serverInfo)
})
if (cushion === undefined) {
cushion = 1.2
}
const response = await this.request('server_info')
return computeFeeFromServerInfo(cushion, response)
}
export {

View File

@@ -6,3 +6,4 @@ export * from './book_offers'
export * from './gateway_balances'
export * from './ledger'
export * from './ledger_entry'
export * from './server_info'

View File

@@ -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,
}

View File

@@ -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<void> {
return this.connection.disconnect()
}
function getServerInfo(): Promise<GetServerInfoResponse> {
return common.serverInfo.getServerInfo(this.connection)
}
function getFee(): Promise<string> {
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
}

View File

@@ -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' ||