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
This commit is contained in:
Mayukha Vadari
2021-09-24 18:40:03 -04:00
committed by GitHub
parent b939a1d5ba
commit 97049e1120
9 changed files with 47 additions and 32 deletions

View File

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

View File

@@ -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<void> {
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
}

View File

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

15
src/sugar/ledgerIndex.ts Normal file
View File

@@ -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<number> {
const ledgerResponse = await this.request({
command: 'ledger',
ledger_index: 'validated',
})
return ledgerResponse.result.ledger_index
}

View File

@@ -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)]')
})
})

View File

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

View File

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

View File

@@ -20,6 +20,9 @@
"1FC4D12C30CE206A6E23F46FAC62BD393BE9A79A1C452C6F3A04A13BC7A5E5A3",
"E25C38FDB8DD4A2429649588638EE05D055EE6D839CABAF8ABFB4BD17CFE1F3E"
]
}
},
"ledger_hash": "1723099E269C77C4BDE86C83FA6415D71CF20AA5CB4A94E5C388ED97123FB55B",
"ledger_index": 9038214,
"validated": true
}
}

View File

@@ -13,15 +13,6 @@ export async function suiteClientSetup(this: Mocha.Context): Promise<void> {
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)()
}