mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-11-23 05:35:48 +00:00
add server_info request typing (#895)
This commit is contained in:
committed by
Elliot Lee
parent
54f12862dc
commit
2e5b435b11
@@ -4,8 +4,6 @@ import {
|
|||||||
connect,
|
connect,
|
||||||
disconnect,
|
disconnect,
|
||||||
isConnected,
|
isConnected,
|
||||||
getServerInfo,
|
|
||||||
getFee,
|
|
||||||
getLedgerVersion,
|
getLedgerVersion,
|
||||||
formatLedgerClose
|
formatLedgerClose
|
||||||
} from './server/server'
|
} from './server/server'
|
||||||
@@ -52,13 +50,15 @@ import {
|
|||||||
BookOffersRequest, BookOffersResponse,
|
BookOffersRequest, BookOffersResponse,
|
||||||
GatewayBalancesRequest, GatewayBalancesResponse,
|
GatewayBalancesRequest, GatewayBalancesResponse,
|
||||||
LedgerRequest, LedgerResponse,
|
LedgerRequest, LedgerResponse,
|
||||||
LedgerEntryRequest, LedgerEntryResponse
|
LedgerEntryRequest, LedgerEntryResponse,
|
||||||
|
ServerInfoRequest, ServerInfoResponse
|
||||||
} from './common/types/commands'
|
} from './common/types/commands'
|
||||||
|
|
||||||
|
|
||||||
import RangeSet from './common/rangeset'
|
import RangeSet from './common/rangeset'
|
||||||
import * as ledgerUtils from './ledger/utils'
|
import * as ledgerUtils from './ledger/utils'
|
||||||
import * as schemaValidator from './common/schema-validator'
|
import * as schemaValidator from './common/schema-validator'
|
||||||
|
import {getServerInfo, getFee} from './common/serverinfo'
|
||||||
import {clamp} from './ledger/utils'
|
import {clamp} from './ledger/utils'
|
||||||
|
|
||||||
export type APIOptions = {
|
export type APIOptions = {
|
||||||
@@ -151,6 +151,8 @@ class RippleAPI extends EventEmitter {
|
|||||||
Promise<LedgerResponse>
|
Promise<LedgerResponse>
|
||||||
async request(command: 'ledger_entry', params: LedgerEntryRequest):
|
async request(command: 'ledger_entry', params: LedgerEntryRequest):
|
||||||
Promise<LedgerEntryResponse>
|
Promise<LedgerEntryResponse>
|
||||||
|
async request(command: 'server_info', params?: ServerInfoRequest):
|
||||||
|
Promise<ServerInfoResponse>
|
||||||
|
|
||||||
async request(command: string, params: object):
|
async request(command: string, params: object):
|
||||||
Promise<object>
|
Promise<object>
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
import * as _ from 'lodash'
|
import * as _ from 'lodash'
|
||||||
import {convertKeysFromSnakeCaseToCamelCase} from './utils'
|
import {convertKeysFromSnakeCaseToCamelCase} from './utils'
|
||||||
import Connection from './connection'
|
|
||||||
import BigNumber from 'bignumber.js'
|
import BigNumber from 'bignumber.js'
|
||||||
|
import {RippleAPI} from '../index'
|
||||||
|
import {ServerInfoResponse} from './types/commands'
|
||||||
|
|
||||||
export type GetServerInfoResponse = {
|
export type GetServerInfoResponse = {
|
||||||
buildVersion: string,
|
buildVersion: string,
|
||||||
@@ -39,8 +40,16 @@ function renameKeys(object, mapping) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function getServerInfo(connection: Connection): Promise<GetServerInfoResponse> {
|
function computeFeeFromServerInfo(
|
||||||
return connection.request({command: 'server_info'}).then(response => {
|
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)
|
const info = convertKeysFromSnakeCaseToCamelCase(response.info)
|
||||||
renameKeys(info, {hostid: 'hostID'})
|
renameKeys(info, {hostid: 'hostID'})
|
||||||
if (info.validatedLedger) {
|
if (info.validatedLedger) {
|
||||||
@@ -61,18 +70,15 @@ function getServerInfo(connection: Connection): Promise<GetServerInfoResponse> {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
function computeFeeFromServerInfo(cushion: number,
|
async function getFee(this: RippleAPI, cushion?: number): Promise<string> {
|
||||||
serverInfo: GetServerInfoResponse
|
if (cushion === undefined) {
|
||||||
): string {
|
cushion = this._feeCushion
|
||||||
return (new BigNumber(serverInfo.validatedLedger.baseFeeXRP)).
|
}
|
||||||
times(serverInfo.loadFactor).
|
if (cushion === undefined) {
|
||||||
times(cushion).toString()
|
cushion = 1.2
|
||||||
}
|
}
|
||||||
|
const response = await this.request('server_info')
|
||||||
function getFee(connection: Connection, cushion: number): Promise<string> {
|
return computeFeeFromServerInfo(cushion, response)
|
||||||
return getServerInfo(connection).then(serverInfo => {
|
|
||||||
return computeFeeFromServerInfo(cushion, serverInfo)
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export {
|
export {
|
||||||
|
|||||||
@@ -6,3 +6,4 @@ export * from './book_offers'
|
|||||||
export * from './gateway_balances'
|
export * from './gateway_balances'
|
||||||
export * from './ledger'
|
export * from './ledger'
|
||||||
export * from './ledger_entry'
|
export * from './ledger_entry'
|
||||||
|
export * from './server_info'
|
||||||
|
|||||||
51
src/common/types/commands/server_info.ts
Normal file
51
src/common/types/commands/server_info.ts
Normal 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,
|
||||||
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import * as common from '../common'
|
import * as common from '../common'
|
||||||
import {GetServerInfoResponse} from '../common/serverinfo'
|
|
||||||
|
|
||||||
function isConnected(): boolean {
|
function isConnected(): boolean {
|
||||||
return this.connection.isConnected()
|
return this.connection.isConnected()
|
||||||
@@ -17,15 +16,6 @@ function disconnect(): Promise<void> {
|
|||||||
return this.connection.disconnect()
|
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 {
|
function formatLedgerClose(ledgerClose: any): Object {
|
||||||
return {
|
return {
|
||||||
baseFeeXRP: common.dropsToXrp(ledgerClose.fee_base),
|
baseFeeXRP: common.dropsToXrp(ledgerClose.fee_base),
|
||||||
@@ -43,8 +33,6 @@ export {
|
|||||||
connect,
|
connect,
|
||||||
disconnect,
|
disconnect,
|
||||||
isConnected,
|
isConnected,
|
||||||
getServerInfo,
|
|
||||||
getFee,
|
|
||||||
getLedgerVersion,
|
getLedgerVersion,
|
||||||
formatLedgerClose
|
formatLedgerClose
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ function prepareTransaction(txJSON: any, api: RippleAPI,
|
|||||||
return Promise.resolve(txJSON)
|
return Promise.resolve(txJSON)
|
||||||
}
|
}
|
||||||
const cushion = api._feeCushion
|
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 => {
|
return api.connection.getFeeRef().then(feeRef => {
|
||||||
const extraFee =
|
const extraFee =
|
||||||
(txJSON.TransactionType !== 'EscrowFinish' ||
|
(txJSON.TransactionType !== 'EscrowFinish' ||
|
||||||
|
|||||||
Reference in New Issue
Block a user