From 97049e11206fe7963eeb1b5288daaafdbcc4ff73 Mon Sep 17 00:00:00 2001 From: Mayukha Vadari Date: Fri, 24 Sep 2021 18:40:03 -0400 Subject: [PATCH] feat: add `client.getLedgerIndex` (#1668) * write getLedgerIndex * add to client * rename file * add tests * remove unused import * fix browser tests * respond to comments, more cleanup --- src/client/index.ts | 12 ++++++------ src/sugar/autofill.ts | 13 ++----------- src/sugar/fee.ts | 1 - src/sugar/ledgerIndex.ts | 15 +++++++++++++++ test/client/errors.ts | 5 +++-- test/client/getLedgerIndex.ts | 15 +++++++++++++++ test/client/getOrderbook.ts | 4 ++-- test/fixtures/rippled/ledger.json | 5 ++++- test/integration/setup.ts | 9 --------- 9 files changed, 47 insertions(+), 32 deletions(-) create mode 100644 src/sugar/ledgerIndex.ts create mode 100644 test/client/getLedgerIndex.ts diff --git a/src/client/index.ts b/src/client/index.ts index 1aec3de1..7ee2d40d 100644 --- a/src/client/index.ts +++ b/src/client/index.ts @@ -88,6 +88,7 @@ import { BaseRequest, BaseResponse } from '../models/methods/baseMethod' import autofill from '../sugar/autofill' import getBalances from '../sugar/balances' import getFee from '../sugar/fee' +import getLedgerIndex from '../sugar/ledgerIndex' import getOrderbook from '../sugar/orderbook' import { submitTransaction, submitSignedTransaction } from '../sugar/submit' import { ensureClassicAddress } from '../sugar/utils' @@ -522,23 +523,22 @@ class Client extends EventEmitter { return this.connection.isConnected() } + // syntactic sugar + public autofill = autofill - - public getFee = getFee - // @deprecated Use autofill instead public prepareTransaction = autofill - public submitTransaction = submitTransaction + public getFee = getFee + public getLedgerIndex = getLedgerIndex + public submitTransaction = submitTransaction public submitSignedTransaction = submitSignedTransaction public getBalances = getBalances public getOrderbook = getOrderbook public generateFaucetWallet = generateFaucetWallet - - public errors = errors } export { Client } diff --git a/src/sugar/autofill.ts b/src/sugar/autofill.ts index bc8ca01d..08c5875d 100644 --- a/src/sugar/autofill.ts +++ b/src/sugar/autofill.ts @@ -3,11 +3,7 @@ import { xAddressToClassicAddress, isValidXAddress } from 'ripple-address-codec' import type { Client } from '..' import { ValidationError, XrplError } from '../errors' -import { - AccountInfoRequest, - AccountObjectsRequest, - LedgerRequest, -} from '../models/methods' +import { AccountInfoRequest, AccountObjectsRequest } from '../models/methods' import { Transaction } from '../models/transactions' import setTransactionFlagsToNumber from '../models/utils/flags' import { xrpToDrops } from '../utils' @@ -199,12 +195,7 @@ async function setLatestValidatedLedgerSequence( client: Client, tx: Transaction, ): Promise { - const request: LedgerRequest = { - command: 'ledger', - ledger_index: 'validated', - } - const data = await client.request(request) - const ledgerSequence = data.result.ledger_index + const ledgerSequence = await client.getLedgerIndex() // eslint-disable-next-line no-param-reassign -- param reassign is safe tx.LastLedgerSequence = ledgerSequence + LEDGER_OFFSET } diff --git a/src/sugar/fee.ts b/src/sugar/fee.ts index e13eb254..ef78a795 100644 --- a/src/sugar/fee.ts +++ b/src/sugar/fee.ts @@ -8,7 +8,6 @@ const BASE_10 = 10 /** * Calculates the current transaction fee for the ledger. * Note: This is a public API that can be called directly. - * This is not used by the `prepare*` methods. See `src/transaction/utils.ts`. * * @param this - The Client used to connect to the ledger. * @param cushion - The fee cushion to use. diff --git a/src/sugar/ledgerIndex.ts b/src/sugar/ledgerIndex.ts new file mode 100644 index 00000000..5ca4988d --- /dev/null +++ b/src/sugar/ledgerIndex.ts @@ -0,0 +1,15 @@ +import type { Client } from '..' + +/** + * Returns the index of the most recently validated ledger. + * + * @param this - The Client used to connect to the ledger. + * @returns The ledger index. + */ +export default async function getLedgerIndex(this: Client): Promise { + const ledgerResponse = await this.request({ + command: 'ledger', + ledger_index: 'validated', + }) + return ledgerResponse.result.ledger_index +} diff --git a/test/client/errors.ts b/test/client/errors.ts index 764fd667..94171447 100644 --- a/test/client/errors.ts +++ b/test/client/errors.ts @@ -1,5 +1,6 @@ import { assert } from 'chai' +import { XrplError, NotFoundError } from '../../src' import { setupClient, teardownClient } from '../setupClient' describe('client errors', function () { @@ -7,12 +8,12 @@ describe('client errors', function () { afterEach(teardownClient) it('XrplError with data', async function () { - const error = new this.client.errors.XrplError('_message_', '_data_') + const error = new XrplError('_message_', '_data_') assert.strictEqual(error.toString(), "[XrplError(_message_, '_data_')]") }) it('NotFoundError default message', async function () { - const error = new this.client.errors.NotFoundError() + const error = new NotFoundError() assert.strictEqual(error.toString(), '[NotFoundError(Not found)]') }) }) diff --git a/test/client/getLedgerIndex.ts b/test/client/getLedgerIndex.ts new file mode 100644 index 00000000..7e729196 --- /dev/null +++ b/test/client/getLedgerIndex.ts @@ -0,0 +1,15 @@ +import { assert } from 'chai' + +import rippled from '../fixtures/rippled' +import { setupClient, teardownClient } from '../setupClient' + +describe('client.getLedgerIndex', function () { + beforeEach(setupClient) + afterEach(teardownClient) + + it('getLedgerIndex', async function () { + this.mockRippled.addResponse('ledger', rippled.ledger.normal) + const ledgerIndex = await this.client.getLedgerIndex() + assert.strictEqual(ledgerIndex, 9038214) + }) +}) diff --git a/test/client/getOrderbook.ts b/test/client/getOrderbook.ts index f296e150..69a81c79 100644 --- a/test/client/getOrderbook.ts +++ b/test/client/getOrderbook.ts @@ -1,7 +1,7 @@ import BigNumber from 'bignumber.js' import { assert } from 'chai' -import { BookOffersRequest } from '../../src' +import { BookOffersRequest, ValidationError } from '../../src' import { OfferLedgerFlags } from '../../src/models/ledger/offer' import requests from '../fixtures/requests' import responses from '../fixtures/responses' @@ -101,7 +101,7 @@ describe('client.getOrderbook', function () { invalid: 'options', }, ), - this.client.errors.ValidationError, + ValidationError, ) }) diff --git a/test/fixtures/rippled/ledger.json b/test/fixtures/rippled/ledger.json index da4b5634..13b6e364 100644 --- a/test/fixtures/rippled/ledger.json +++ b/test/fixtures/rippled/ledger.json @@ -20,6 +20,9 @@ "1FC4D12C30CE206A6E23F46FAC62BD393BE9A79A1C452C6F3A04A13BC7A5E5A3", "E25C38FDB8DD4A2429649588638EE05D055EE6D839CABAF8ABFB4BD17CFE1F3E" ] - } + }, + "ledger_hash": "1723099E269C77C4BDE86C83FA6415D71CF20AA5CB4A94E5C388ED97123FB55B", + "ledger_index": 9038214, + "validated": true } } diff --git a/test/integration/setup.ts b/test/integration/setup.ts index 63046b3f..408061e8 100644 --- a/test/integration/setup.ts +++ b/test/integration/setup.ts @@ -13,15 +13,6 @@ export async function suiteClientSetup(this: Mocha.Context): Promise { await setupClient.bind(this)(serverUrl) await ledgerAccept(this.client) this.newWallet = generateXAddress({ includeClassicAddress: true }) - // two times to give time to server to send `ledgerClosed` event - // so getLedgerVersion will return right value - await ledgerAccept(this.client) - const response = await this.client.request({ - command: 'ledger', - ledger_index: 'validated', - }) - const ledgerVersion = response.result.ledger_index - this.startLedgerVersion = ledgerVersion await teardownClient.bind(this)() }