use isEarlierRippledVersion as helper for autofill

This commit is contained in:
Phu Pham
2023-06-05 11:33:55 -04:00
parent cabe79d4c0
commit faea1abafb
4 changed files with 58 additions and 91 deletions

View File

@@ -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<T extends Transaction>(
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 &&

View File

@@ -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,

View File

@@ -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
}

View File

@@ -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)
})
})