From faea1abafb146f48d64dd86a3f441841dbfd995b Mon Sep 17 00:00:00 2001 From: Phu Pham Date: Mon, 5 Jun 2023 11:33:55 -0400 Subject: [PATCH] use isEarlierRippledVersion as helper for autofill --- packages/xrpl/src/sugar/autofill.ts | 59 ++++++++++++++++- packages/xrpl/src/utils/index.ts | 2 - .../xrpl/src/utils/isEarlierRippledVersion.ts | 66 ------------------- .../utils/isEarlierRippledVersion.test.ts | 22 ------- 4 files changed, 58 insertions(+), 91 deletions(-) delete mode 100644 packages/xrpl/src/utils/isEarlierRippledVersion.ts delete mode 100644 packages/xrpl/test/utils/isEarlierRippledVersion.test.ts diff --git a/packages/xrpl/src/sugar/autofill.ts b/packages/xrpl/src/sugar/autofill.ts index df6bf20d..92614248 100644 --- a/packages/xrpl/src/sugar/autofill.ts +++ b/packages/xrpl/src/sugar/autofill.ts @@ -6,7 +6,7 @@ import { ValidationError, XrplError } from '../errors' import { AccountInfoRequest, AccountObjectsRequest } from '../models/methods' import { Transaction } from '../models/transactions' import { setTransactionFlagsToNumber } from '../models/utils/flags' -import { isEarlierRippledVersion, xrpToDrops } from '../utils' +import { xrpToDrops } from '../utils' import getFeeXrp from './getFeeXrp' @@ -95,6 +95,63 @@ async function autofill( return Promise.all(promises).then(() => tx) } +// eslint-disable-next-line max-lines-per-function, max-statements -- Disable for this helper functions. +function isEarlierRippledVersion(source: string, target: string): boolean { + if (source === target) { + return false + } + const sourceDecomp = source.split('.') + const targetDecomp = target.split('.') + const sourceMajor = parseInt(sourceDecomp[0], 10) + const sourceMinor = parseInt(sourceDecomp[1], 10) + const targetMajor = parseInt(targetDecomp[0], 10) + const targetMinor = parseInt(targetDecomp[1], 10) + // Compare major version + if (sourceMajor !== targetMajor) { + return sourceMajor < targetMajor + } + // Compare minor version + if (sourceMinor !== targetMinor) { + return sourceMinor < targetMinor + } + const sourcePatch = sourceDecomp[2].split('-') + const targetPatch = targetDecomp[2].split('-') + + const sourcePatchVersion = parseInt(sourcePatch[0], 10) + const targetPatchVersion = parseInt(targetPatch[0], 10) + + // Compare patch version + if (sourcePatchVersion !== targetPatchVersion) { + return sourcePatchVersion < targetPatchVersion + } + + // Compare release version + if (sourcePatch.length !== targetPatch.length) { + return sourcePatch.length > targetPatch.length + } + + if (sourcePatch.length === 2) { + // Compare different release types + if (!sourcePatch[1][0].startsWith(targetPatch[1][0])) { + return sourcePatch[1] < targetPatch[1] + } + // Compare beta version + if (sourcePatch[1].startsWith('b')) { + return ( + parseInt(sourcePatch[1].slice(1), 10) < + parseInt(targetPatch[1].slice(1), 10) + ) + } + // Compare rc version + return ( + parseInt(sourcePatch[1].slice(2), 10) < + parseInt(targetPatch[1].slice(2), 10) + ) + } + + return false +} + function txNeedsNetworkID(client: Client): boolean { if ( client.networkID !== undefined && diff --git a/packages/xrpl/src/utils/index.ts b/packages/xrpl/src/utils/index.ts index 460fe90e..06fdb390 100644 --- a/packages/xrpl/src/utils/index.ts +++ b/packages/xrpl/src/utils/index.ts @@ -39,7 +39,6 @@ import { hashEscrow, hashPaymentChannel, } from './hashes' -import isEarlierRippledVersion from './isEarlierRippledVersion' import parseNFTokenID from './parseNFTokenID' import { percentToTransferRate, @@ -191,7 +190,6 @@ export { qualityToDecimal, isValidSecret, isValidAddress, - isEarlierRippledVersion, hashes, deriveKeypair, deriveAddress, diff --git a/packages/xrpl/src/utils/isEarlierRippledVersion.ts b/packages/xrpl/src/utils/isEarlierRippledVersion.ts deleted file mode 100644 index 42dc24b6..00000000 --- a/packages/xrpl/src/utils/isEarlierRippledVersion.ts +++ /dev/null @@ -1,66 +0,0 @@ -/** - * Determines whether a rippled version (source) is earlier than another (target). - * - * @param source - The source rippled version. - * @param target - The target rippled version. - * @returns true if source is earlier, false otherwise. - */ -// eslint-disable-next-line max-lines-per-function, max-statements -- Disable for this utils functions. -export default function isEarlierRippledVersion( - source: string, - target: string, -): boolean { - if (source === target) { - return false - } - const sourceDecomp = source.split('.') - const targetDecomp = target.split('.') - const sourceMajor = parseInt(sourceDecomp[0], 10) - const sourceMinor = parseInt(sourceDecomp[1], 10) - const targetMajor = parseInt(targetDecomp[0], 10) - const targetMinor = parseInt(targetDecomp[1], 10) - // Compare major version - if (sourceMajor !== targetMajor) { - return sourceMajor < targetMajor - } - // Compare minor version - if (sourceMinor !== targetMinor) { - return sourceMinor < targetMinor - } - const sourcePatch = sourceDecomp[2].split('-') - const targetPatch = targetDecomp[2].split('-') - - const sourcePatchVersion = parseInt(sourcePatch[0], 10) - const targetPatchVersion = parseInt(targetPatch[0], 10) - - // Compare patch version - if (sourcePatchVersion !== targetPatchVersion) { - return sourcePatchVersion < targetPatchVersion - } - - // Compare release version - if (sourcePatch.length !== targetPatch.length) { - return sourcePatch.length > targetPatch.length - } - - if (sourcePatch.length === 2) { - // Compare different release types - if (!sourcePatch[1][0].startsWith(targetPatch[1][0])) { - return sourcePatch[1] < targetPatch[1] - } - // Compare beta version - if (sourcePatch[1].startsWith('b')) { - return ( - parseInt(sourcePatch[1].slice(1), 10) < - parseInt(targetPatch[1].slice(1), 10) - ) - } - // Compare rc version - return ( - parseInt(sourcePatch[1].slice(2), 10) < - parseInt(targetPatch[1].slice(2), 10) - ) - } - - return false -} diff --git a/packages/xrpl/test/utils/isEarlierRippledVersion.test.ts b/packages/xrpl/test/utils/isEarlierRippledVersion.test.ts deleted file mode 100644 index 5e1a7ee6..00000000 --- a/packages/xrpl/test/utils/isEarlierRippledVersion.test.ts +++ /dev/null @@ -1,22 +0,0 @@ -import { isEarlierRippledVersion } from '../../src' - -describe('isEarlierRippledVersion', function () { - it('isEarlierRippledVersion compare versions correctly', () => { - expect(isEarlierRippledVersion('1.9.4', '1.9.4')).toEqual(false) - expect(isEarlierRippledVersion('0.9.2', '1.8.4')).toEqual(true) - expect(isEarlierRippledVersion('1.8.2', '1.9.4')).toEqual(true) - expect(isEarlierRippledVersion('1.9.2', '1.9.4')).toEqual(true) - expect(isEarlierRippledVersion('1.9.2', '1.9.2-b1')).toEqual(false) - expect(isEarlierRippledVersion('1.9.2', '1.9.2-rc2')).toEqual(false) - expect(isEarlierRippledVersion('1.9.4-b2', '1.9.4-rc1')).toEqual(true) - expect(isEarlierRippledVersion('1.9.4-b1', '1.9.4-b2')).toEqual(true) - expect(isEarlierRippledVersion('1.9.4-rc1', '1.9.4-rc2')).toEqual(true) - expect(isEarlierRippledVersion('1.6.2', '0.9.4')).toEqual(false) - expect(isEarlierRippledVersion('1.9.4', '1.8.6')).toEqual(false) - expect(isEarlierRippledVersion('1.9.4', '1.9.2-rc5')).toEqual(false) - expect(isEarlierRippledVersion('1.8.0-rc1', '1.8.0')).toEqual(true) - expect(isEarlierRippledVersion('1.9.4-rc1', '1.9.4-b3')).toEqual(false) - expect(isEarlierRippledVersion('1.9.4-b2', '1.9.4-b1')).toEqual(false) - expect(isEarlierRippledVersion('1.9.4-rc2', '1.9.4-rc1')).toEqual(false) - }) -})