diff --git a/src/api.ts b/src/api.ts index 2cc95404..f313736a 100644 --- a/src/api.ts +++ b/src/api.ts @@ -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): Promise async request(command: 'account_lines', params: AccountLinesRequest): Promise - - /** - * 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): Promise - async request(command: 'account_offers', params: AccountOffersRequest): Promise async request(command: 'book_offers', params: BookOffersRequest): @@ -156,15 +153,9 @@ class RippleAPI extends EventEmitter { Promise async request(command: 'server_info', params?: ServerInfoRequest): Promise - - async request(command: string, params: object): - Promise - - /** - * Makes a request to the API with the given command and - * additional request body parameters. - */ - async request(command: string, params: object = {}): Promise { + async request(command: string, params: any): + Promise + async request(command: string, params: any = {}): Promise { return this.connection.request({ ...params, command diff --git a/src/ledger/transaction.ts b/src/ledger/transaction.ts index 90b06de5..7bb7e95e 100644 --- a/src/ledger/transaction.ts +++ b/src/ledger/transaction.ts @@ -18,7 +18,7 @@ type TransactionResponse = FormattedTransactionType & { function attachTransactionDate(connection: Connection, tx: any -): Promise { +): Promise { if (tx.date) { return Promise.resolve(tx) } @@ -88,26 +88,20 @@ function formatResponse(options: TransactionOptions, tx: TransactionResponse return parseTransaction(tx) } -function getTransaction(id: string, options: TransactionOptions = {} +async function getTransaction(id: string, options: TransactionOptions = {} ): Promise { validate.getTransaction({id, options}) - - const request = { - command: 'tx', - transaction: id, - binary: false + const _options = await utils.ensureLedgerVersion.call(this, options) + try { + const tx = await this.request('tx', { + transaction: id, + 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 diff --git a/src/transaction/submit.ts b/src/transaction/submit.ts index b893e842..9e7d13a9 100644 --- a/src/transaction/submit.ts +++ b/src/transaction/submit.ts @@ -1,7 +1,12 @@ import * as _ from 'lodash' import * as utils from './utils' import {validate} from '../common' -import {Submit} from './types' +import {RippleAPI} from '..' + +export interface FormattedSubmitResponse { + resultCode: string, + resultMessage: string +} function isImmediateRejection(engineResult: string): boolean { // note: "tel" errors mean the local server refused to process the @@ -13,7 +18,7 @@ function isImmediateRejection(engineResult: string): boolean { return _.startsWith(engineResult, 'tem') } -function formatSubmitResponse(response) { +function formatSubmitResponse(response): FormattedSubmitResponse { const data = { resultCode: response.engine_result, resultMessage: response.engine_result_message @@ -24,14 +29,15 @@ function formatSubmitResponse(response) { return data } -function submit(signedTransaction: string): Promise { +async function submit( + this: RippleAPI, signedTransaction: string +): Promise { + // 1. Validate validate.submit({signedTransaction}) - - const request = { - command: 'submit', - tx_blob: signedTransaction - } - return this.connection.request(request).then(formatSubmitResponse) + // 2. Make Request + const response = await this.request('submit', {tx_blob: signedTransaction}) + // 3. Return Formatted Response + return formatSubmitResponse(response) } export default submit diff --git a/src/transaction/utils.ts b/src/transaction/utils.ts index b7ad1b81..de6c2398 100644 --- a/src/transaction/utils.ts +++ b/src/transaction/utils.ts @@ -94,19 +94,16 @@ function prepareTransaction(txJSON: any, api: RippleAPI, }) } - function prepareSequence(): Promise { + async function prepareSequence(): Promise { if (instructions.sequence !== undefined) { txJSON.Sequence = instructions.sequence return Promise.resolve(txJSON) } - const request = { - command: 'account_info', - account: account - } - return api.connection.request(request).then(response => { - txJSON.Sequence = response.account_data.Sequence - return txJSON + const response = await api.request('account_info', { + account: account as string }) + txJSON.Sequence = response.account_data.Sequence + return txJSON } return Promise.all([