add network id to autofill

This commit is contained in:
Denis Angell
2023-02-22 17:47:14 -05:00
parent 69c705874f
commit 294c1cb083
2 changed files with 32 additions and 1 deletions

View File

@@ -9,6 +9,7 @@ import { setTransactionFlagsToNumber } from '../models/utils/flags'
import { xrpToDrops } from '../utils'
import getFeeXrp from './getFeeXrp'
import getNetworkID from './getNetworkID'
// Expire unconfirmed transactions after 20 ledger versions, approximately 1 minute, by default
const LEDGER_OFFSET = 20
@@ -39,8 +40,10 @@ async function autofill<T extends Transaction>(
setValidAddresses(tx)
setTransactionFlagsToNumber(tx)
const promises: Array<Promise<void>> = []
if (tx.NetworkID == null) {
promises.push(setNetworkID(this, tx))
}
if (tx.Sequence == null) {
promises.push(setNextValidSequenceNumber(this, tx))
}
@@ -200,6 +203,12 @@ function scaleValue(value, multiplier): string {
return new BigNumber(value).times(multiplier).toString()
}
async function setNetworkID(client: Client, tx: Transaction): Promise<void> {
const id = await getNetworkID(client)
// eslint-disable-next-line no-param-reassign -- param reassign is safe
tx.NetworkID = id
}
async function setLatestValidatedLedgerSequence(
client: Client,
tx: Transaction,

View File

@@ -0,0 +1,22 @@
import type { Client } from '..'
import { XrplError } from '../errors'
/**
* Returns the network ID of the rippled server.
*
* @param this - The Client used to connect to the ledger.
* @param client
* @returns The network id.
*/
export default async function getNetworkID(client: Client): Promise<number> {
const response = await client.request({
command: 'server_info',
})
const networkID = response.result.info.network_id
if (networkID == null) {
throw new XrplError(
'getNetworkID: Could not get network_id from server_info',
)
}
return networkID
}