mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-06 17:27:59 +00:00
add network id to autofill
This commit is contained in:
@@ -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,
|
||||
|
||||
22
packages/xrpl/src/sugar/getNetworkID.ts
Normal file
22
packages/xrpl/src/sugar/getNetworkID.ts
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user