mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-06 17:27:59 +00:00
refactor: remove getFee from Client (#1756)
* removes getFeeXrp from client
This commit is contained in:
@@ -89,7 +89,6 @@ import {
|
||||
ensureClassicAddress,
|
||||
getLedgerIndex,
|
||||
getOrderbook,
|
||||
getFee,
|
||||
getBalances,
|
||||
getXrpBalance,
|
||||
submit,
|
||||
@@ -584,11 +583,6 @@ class Client extends EventEmitter {
|
||||
*/
|
||||
public autofill = autofill
|
||||
|
||||
/**
|
||||
* @category Fee
|
||||
*/
|
||||
public getFee = getFee
|
||||
|
||||
/**
|
||||
* @category Core
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,8 @@ import { Transaction } from '../models/transactions'
|
||||
import setTransactionFlagsToNumber from '../models/utils/flags'
|
||||
import { xrpToDrops } from '../utils'
|
||||
|
||||
import getFeeXrp from './fee'
|
||||
|
||||
// Expire unconfirmed transactions after 20 ledger versions, approximately 1 minute, by default
|
||||
const LEDGER_OFFSET = 20
|
||||
interface ClassicAccountAndTag {
|
||||
@@ -151,7 +153,7 @@ async function calculateFeePerTransactionType(
|
||||
signersCount = 0,
|
||||
): Promise<void> {
|
||||
// netFee is usually 0.00001 XRP (10 drops)
|
||||
const netFeeXRP = await client.getFee()
|
||||
const netFeeXRP = await getFeeXrp(client)
|
||||
const netFeeDrops = xrpToDrops(netFeeXRP)
|
||||
let baseFee = new BigNumber(netFeeDrops)
|
||||
|
||||
|
||||
@@ -10,23 +10,25 @@ const BASE_10 = 10
|
||||
* Calculates the current transaction fee for the ledger.
|
||||
* Note: This is a public API that can be called directly.
|
||||
*
|
||||
* @param this - The Client used to connect to the ledger.
|
||||
* @param client - The Client used to connect to the ledger.
|
||||
* @param cushion - The fee cushion to use.
|
||||
* @returns The transaction fee.
|
||||
*/
|
||||
export default async function getFee(
|
||||
this: Client,
|
||||
export default async function getFeeXrp(
|
||||
client: Client,
|
||||
cushion?: number,
|
||||
): Promise<string> {
|
||||
const feeCushion = cushion ?? this.feeCushion
|
||||
const feeCushion = cushion ?? client.feeCushion
|
||||
|
||||
const serverInfo = (await this.request({ command: 'server_info' })).result
|
||||
const serverInfo = (await client.request({ command: 'server_info' })).result
|
||||
.info
|
||||
|
||||
const baseFee = serverInfo.validated_ledger?.base_fee_xrp
|
||||
|
||||
if (baseFee == null) {
|
||||
throw new XrplError('getFee: Could not get base_fee_xrp from server_info')
|
||||
throw new XrplError(
|
||||
'getFeeXrp: Could not get base_fee_xrp from server_info',
|
||||
)
|
||||
}
|
||||
|
||||
const baseFeeXrp = new BigNumber(baseFee)
|
||||
@@ -37,7 +39,7 @@ export default async function getFee(
|
||||
let fee = baseFeeXrp.times(serverInfo.load_factor).times(feeCushion)
|
||||
|
||||
// Cap fee to `client.maxFeeXRP`
|
||||
fee = BigNumber.min(fee, this.maxFeeXRP)
|
||||
fee = BigNumber.min(fee, client.maxFeeXRP)
|
||||
// Round fee to 6 decimal places
|
||||
return new BigNumber(fee.toFixed(NUM_DECIMAL_PLACES)).toString(BASE_10)
|
||||
}
|
||||
|
||||
@@ -2,8 +2,6 @@ export { default as autofill } from './autofill'
|
||||
|
||||
export { getBalances, getXrpBalance } from './balances'
|
||||
|
||||
export { default as getFee } from './fee'
|
||||
|
||||
export { default as getLedgerIndex } from './ledgerIndex'
|
||||
|
||||
export { default as getOrderbook } from './orderbook'
|
||||
|
||||
@@ -1,28 +1,29 @@
|
||||
import { assert } from 'chai'
|
||||
|
||||
import getFeeXrp from '../../src/sugar/fee'
|
||||
import rippled from '../fixtures/rippled'
|
||||
import { setupClient, teardownClient } from '../setupClient'
|
||||
|
||||
describe('client.getFee', function () {
|
||||
describe('getFeeXrp', function () {
|
||||
beforeEach(setupClient)
|
||||
afterEach(teardownClient)
|
||||
|
||||
it('getFee', async function () {
|
||||
it('getFeeXrp', async function () {
|
||||
this.mockRippled.addResponse('server_info', rippled.server_info.normal)
|
||||
const fee = await this.client.getFee()
|
||||
const fee = await getFeeXrp(this.client)
|
||||
assert.strictEqual(fee, '0.000012')
|
||||
})
|
||||
|
||||
it('getFee - high load_factor', async function () {
|
||||
it('getFeeXrp - high load_factor', async function () {
|
||||
this.mockRippled.addResponse(
|
||||
'server_info',
|
||||
rippled.server_info.highLoadFactor,
|
||||
)
|
||||
const fee = await this.client.getFee()
|
||||
const fee = await getFeeXrp(this.client)
|
||||
assert.strictEqual(fee, '2')
|
||||
})
|
||||
|
||||
it('getFee - high load_factor with custom maxFeeXRP', async function () {
|
||||
it('getFeeXrp - high load_factor with custom maxFeeXRP', async function () {
|
||||
this.mockRippled.addResponse(
|
||||
'server_info',
|
||||
rippled.server_info.highLoadFactor,
|
||||
@@ -33,14 +34,14 @@ describe('client.getFee', function () {
|
||||
* (fee will actually be 51539.607552)
|
||||
*/
|
||||
this.client.maxFeeXRP = '51540'
|
||||
const fee = await this.client.getFee()
|
||||
const fee = await getFeeXrp(this.client)
|
||||
assert.strictEqual(fee, '51539.607552')
|
||||
})
|
||||
|
||||
it('getFee custom cushion', async function () {
|
||||
it('getFeeXrp custom cushion', async function () {
|
||||
this.mockRippled.addResponse('server_info', rippled.server_info.normal)
|
||||
this.client.feeCushion = 1.4
|
||||
const fee = await this.client.getFee()
|
||||
const fee = await getFeeXrp(this.client)
|
||||
assert.strictEqual(fee, '0.000014')
|
||||
})
|
||||
|
||||
@@ -48,16 +49,16 @@ describe('client.getFee', function () {
|
||||
* This is not recommended since it may result in attempting to pay
|
||||
* less than the base fee. However, this test verifies the existing behavior.
|
||||
*/
|
||||
it('getFee cushion less than 1.0', async function () {
|
||||
it('getFeeXrp cushion less than 1.0', async function () {
|
||||
this.mockRippled.addResponse('server_info', rippled.server_info.normal)
|
||||
this.client.feeCushion = 0.9
|
||||
const fee = await this.client.getFee()
|
||||
const fee = await getFeeXrp(this.client)
|
||||
assert.strictEqual(fee, '0.000009')
|
||||
})
|
||||
|
||||
it('getFee reporting', async function () {
|
||||
it('getFeeXrp reporting', async function () {
|
||||
this.mockRippled.addResponse('server_info', rippled.server_info.normal)
|
||||
const fee = await this.client.getFee()
|
||||
const fee = await getFeeXrp(this.client)
|
||||
assert.strictEqual(fee, '0.000012')
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user