From 294c1cb083b5d0216c6bf6c1cf246703c29f1cb9 Mon Sep 17 00:00:00 2001 From: Denis Angell Date: Wed, 22 Feb 2023 17:47:14 -0500 Subject: [PATCH] add network id to autofill --- packages/xrpl/src/sugar/autofill.ts | 11 ++++++++++- packages/xrpl/src/sugar/getNetworkID.ts | 22 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 packages/xrpl/src/sugar/getNetworkID.ts diff --git a/packages/xrpl/src/sugar/autofill.ts b/packages/xrpl/src/sugar/autofill.ts index 05d50a96..38f45364 100644 --- a/packages/xrpl/src/sugar/autofill.ts +++ b/packages/xrpl/src/sugar/autofill.ts @@ -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( setValidAddresses(tx) setTransactionFlagsToNumber(tx) - const promises: Array> = [] + 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 { + 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, diff --git a/packages/xrpl/src/sugar/getNetworkID.ts b/packages/xrpl/src/sugar/getNetworkID.ts new file mode 100644 index 00000000..fa2651e3 --- /dev/null +++ b/packages/xrpl/src/sugar/getNetworkID.ts @@ -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 { + 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 +}