From 1d9f9ae7c5e1851224a36b015a992e5134a7fcc9 Mon Sep 17 00:00:00 2001 From: Phu Pham Date: Thu, 1 Jun 2023 17:45:09 -0400 Subject: [PATCH] make a txNeedsNetworkID helper function --- packages/xrpl/src/sugar/autofill.ts | 38 +++++++++++++++++------------ 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/packages/xrpl/src/sugar/autofill.ts b/packages/xrpl/src/sugar/autofill.ts index e9153634..54309b6e 100644 --- a/packages/xrpl/src/sugar/autofill.ts +++ b/packages/xrpl/src/sugar/autofill.ts @@ -12,7 +12,7 @@ import getFeeXrp from './getFeeXrp' // Expire unconfirmed transactions after 20 ledger versions, approximately 1 minute, by default const LEDGER_OFFSET = 20 -// Sidechains are expected to have network IDs above this. +// Sidechains are expected to have network IDs above this. // Mainnet and testnet are exceptions. More context: https://github.com/XRPLF/rippled/pull/4370 const RESTRICTED_NETWORKS = 1024 const REQUIRED_NETWORKID_VERSION = '1.11.0' @@ -76,20 +76,8 @@ async function autofill( setTransactionFlagsToNumber(tx) const promises: Array> = [] - if (this.networkID !== undefined && this.networkID > RESTRICTED_NETWORKS) { - // autofill transaction's networkID if either the network is hooks testnet or build version is >= 1.11.0 - if ( - (this.buildVersion && - isEarlierRippledVersion( - REQUIRED_NETWORKID_VERSION, - this.buildVersion, - )) || - this.networkID === HOOKS_TESTNET_ID - ) { - tx.NetworkID = this.networkID - } else { - tx.NetworkID = undefined - } + if (tx.NetworkID == null) { + tx.NetworkID = txNeedsNetworkID(this) ? this.networkID : undefined } if (tx.Sequence == null) { promises.push(setNextValidSequenceNumber(this, tx)) @@ -107,6 +95,26 @@ async function autofill( return Promise.all(promises).then(() => tx) } +function txNeedsNetworkID(client: Client): boolean { + if ( + client.networkID !== undefined && + client.networkID > RESTRICTED_NETWORKS + ) { + // transaction needs networkID if either the network is hooks testnet or build version is >= 1.11.0 + if ( + (client.buildVersion && + isEarlierRippledVersion( + REQUIRED_NETWORKID_VERSION, + client.buildVersion, + )) || + client.networkID === HOOKS_TESTNET_ID + ) { + return true + } + } + return false +} + function setValidAddresses(tx: Transaction): void { validateAccountAddress(tx, 'Account', 'SourceTag') // eslint-disable-next-line @typescript-eslint/dot-notation -- Destination can exist on Transaction