Update some API methods to use api.request() internally (#896)

* use any instead of object
This commit is contained in:
Fred K. Schott
2018-07-26 12:23:07 -07:00
committed by Elliot Lee
parent 4f40b5cb6d
commit b94698df0b
4 changed files with 39 additions and 51 deletions

View File

@@ -131,19 +131,16 @@ class RippleAPI extends EventEmitter {
} }
/**
* Makes a request to the API with the given command and
* additional request body parameters.
*/
async request(command: 'account_info', params: AccountInfoRequest): async request(command: 'account_info', params: AccountInfoRequest):
Promise<AccountInfoResponse> Promise<AccountInfoResponse>
async request(command: 'account_lines', params: AccountLinesRequest): async request(command: 'account_lines', params: AccountLinesRequest):
Promise<AccountLinesResponse> Promise<AccountLinesResponse>
/**
* Returns objects owned by an account.
* For an account's trust lines and balances,
* see `getTrustlines` and `getBalances`.
*/
async request(command: 'account_objects', params: AccountObjectsRequest): async request(command: 'account_objects', params: AccountObjectsRequest):
Promise<AccountObjectsResponse> Promise<AccountObjectsResponse>
async request(command: 'account_offers', params: AccountOffersRequest): async request(command: 'account_offers', params: AccountOffersRequest):
Promise<AccountOffersResponse> Promise<AccountOffersResponse>
async request(command: 'book_offers', params: BookOffersRequest): async request(command: 'book_offers', params: BookOffersRequest):
@@ -156,15 +153,9 @@ class RippleAPI extends EventEmitter {
Promise<LedgerEntryResponse> Promise<LedgerEntryResponse>
async request(command: 'server_info', params?: ServerInfoRequest): async request(command: 'server_info', params?: ServerInfoRequest):
Promise<ServerInfoResponse> Promise<ServerInfoResponse>
async request(command: string, params: any):
async request(command: string, params: object): Promise<any>
Promise<object> async request(command: string, params: any = {}): Promise<any> {
/**
* Makes a request to the API with the given command and
* additional request body parameters.
*/
async request(command: string, params: object = {}): Promise<object> {
return this.connection.request({ return this.connection.request({
...params, ...params,
command command

View File

@@ -18,7 +18,7 @@ type TransactionResponse = FormattedTransactionType & {
function attachTransactionDate(connection: Connection, tx: any function attachTransactionDate(connection: Connection, tx: any
): Promise<FormattedTransactionType> { ): Promise<TransactionResponse> {
if (tx.date) { if (tx.date) {
return Promise.resolve(tx) return Promise.resolve(tx)
} }
@@ -88,26 +88,20 @@ function formatResponse(options: TransactionOptions, tx: TransactionResponse
return parseTransaction(tx) return parseTransaction(tx)
} }
function getTransaction(id: string, options: TransactionOptions = {} async function getTransaction(id: string, options: TransactionOptions = {}
): Promise<FormattedTransactionType> { ): Promise<FormattedTransactionType> {
validate.getTransaction({id, options}) validate.getTransaction({id, options})
const _options = await utils.ensureLedgerVersion.call(this, options)
const request = { try {
command: 'tx', const tx = await this.request('tx', {
transaction: id, transaction: id,
binary: false binary: false
})
const txWithDate = await attachTransactionDate(this.connection, tx)
return formatResponse(_options, txWithDate)
} catch (error) {
throw (await convertError(this.connection, _options, error))
} }
return utils.ensureLedgerVersion.call(this, options).then(_options => {
return this.connection.request(request).then((tx: TransactionResponse) =>
attachTransactionDate(this.connection, tx)
).then(_.partial(formatResponse, _options))
.catch(error => {
return convertError(this.connection, _options, error).then(_error => {
throw _error
})
})
})
} }
export default getTransaction export default getTransaction

View File

@@ -1,7 +1,12 @@
import * as _ from 'lodash' import * as _ from 'lodash'
import * as utils from './utils' import * as utils from './utils'
import {validate} from '../common' import {validate} from '../common'
import {Submit} from './types' import {RippleAPI} from '..'
export interface FormattedSubmitResponse {
resultCode: string,
resultMessage: string
}
function isImmediateRejection(engineResult: string): boolean { function isImmediateRejection(engineResult: string): boolean {
// note: "tel" errors mean the local server refused to process the // note: "tel" errors mean the local server refused to process the
@@ -13,7 +18,7 @@ function isImmediateRejection(engineResult: string): boolean {
return _.startsWith(engineResult, 'tem') return _.startsWith(engineResult, 'tem')
} }
function formatSubmitResponse(response) { function formatSubmitResponse(response): FormattedSubmitResponse {
const data = { const data = {
resultCode: response.engine_result, resultCode: response.engine_result,
resultMessage: response.engine_result_message resultMessage: response.engine_result_message
@@ -24,14 +29,15 @@ function formatSubmitResponse(response) {
return data return data
} }
function submit(signedTransaction: string): Promise<Submit> { async function submit(
this: RippleAPI, signedTransaction: string
): Promise<FormattedSubmitResponse> {
// 1. Validate
validate.submit({signedTransaction}) validate.submit({signedTransaction})
// 2. Make Request
const request = { const response = await this.request('submit', {tx_blob: signedTransaction})
command: 'submit', // 3. Return Formatted Response
tx_blob: signedTransaction return formatSubmitResponse(response)
}
return this.connection.request(request).then(formatSubmitResponse)
} }
export default submit export default submit

View File

@@ -94,19 +94,16 @@ function prepareTransaction(txJSON: any, api: RippleAPI,
}) })
} }
function prepareSequence(): Promise<Object> { async function prepareSequence(): Promise<Object> {
if (instructions.sequence !== undefined) { if (instructions.sequence !== undefined) {
txJSON.Sequence = instructions.sequence txJSON.Sequence = instructions.sequence
return Promise.resolve(txJSON) return Promise.resolve(txJSON)
} }
const request = { const response = await api.request('account_info', {
command: 'account_info', account: account as string
account: account
}
return api.connection.request(request).then(response => {
txJSON.Sequence = response.account_data.Sequence
return txJSON
}) })
txJSON.Sequence = response.account_data.Sequence
return txJSON
} }
return Promise.all([ return Promise.all([