From b3a72588ea590ca24a517b3d71196eef2d7a040d Mon Sep 17 00:00:00 2001 From: Nathan Nichols Date: Fri, 24 Sep 2021 12:09:54 -0700 Subject: [PATCH] Autofills AccountDelete Fee w/ `reserve_inc_xrp` (#1639) * fix: fetch Owner Reserves from ledger --- src/sugar/autofill.ts | 17 +++++++++++++---- test/client/autofill.ts | 13 ++++++++++++- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/sugar/autofill.ts b/src/sugar/autofill.ts index db2730b1..bc8ca01d 100644 --- a/src/sugar/autofill.ts +++ b/src/sugar/autofill.ts @@ -12,10 +12,8 @@ import { Transaction } from '../models/transactions' import setTransactionFlagsToNumber from '../models/utils/flags' import { xrpToDrops } from '../utils' -// 20 drops +// Expire unconfirmed transactions after 20 ledger versions, approximately 1 minute, by default const LEDGER_OFFSET = 20 -// 5 XRP -const ACCOUNT_DELETE_FEE = 5000000 interface ClassicAccountAndTag { classicAccount: string tag: number | false | undefined @@ -139,6 +137,17 @@ async function setNextValidSequenceNumber( tx.Sequence = data.result.account_data.Sequence } +async function fetchAccountDeleteFee(client: Client): Promise { + const response = await client.request({ command: 'server_state' }) + const fee = response.result.state.validated_ledger?.reserve_inc + + if (fee == null) { + return Promise.reject(new Error('Could not fetch Owner Reserve.')) + } + + return new BigNumber(fee) +} + async function calculateFeePerTransactionType( client: Client, tx: Transaction, @@ -162,7 +171,7 @@ async function calculateFeePerTransactionType( // AccountDelete Transaction if (tx.TransactionType === 'AccountDelete') { - baseFee = new BigNumber(ACCOUNT_DELETE_FEE) + baseFee = await fetchAccountDeleteFee(client) } // Multi-signed Transaction diff --git a/test/client/autofill.ts b/test/client/autofill.ts index 7b021ccf..4dbb8bdb 100644 --- a/test/client/autofill.ts +++ b/test/client/autofill.ts @@ -140,6 +140,17 @@ describe('client.autofill', function () { } this.mockRippled.addResponse('account_info', rippled.account_info.normal) this.mockRippled.addResponse('ledger', rippled.ledger.normal) + this.mockRippled.addResponse('server_state', { + status: 'success', + type: 'response', + result: { + state: { + validated_ledger: { + reserve_inc: 2000000, + }, + }, + }, + }) this.mockRippled.addResponse('server_info', rippled.server_info.normal) this.mockRippled.addResponse( 'account_objects', @@ -147,7 +158,7 @@ describe('client.autofill', function () { ) const txResult = await this.client.autofill(tx) - assert.strictEqual(txResult.Fee, '5000000') + assert.strictEqual(txResult.Fee, '2000000') }) it('should autofill Fee of an EscrowFinish transaction with signersCount', async function () {