mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-06 17:27:59 +00:00
add more unit tests
This commit is contained in:
@@ -6,10 +6,9 @@ import { ValidationError, XrplError } from '../errors'
|
||||
import { AccountInfoRequest, AccountObjectsRequest } from '../models/methods'
|
||||
import { Transaction } from '../models/transactions'
|
||||
import { setTransactionFlagsToNumber } from '../models/utils/flags'
|
||||
import { xrpToDrops } from '../utils'
|
||||
import { isEarlierRippledVersion, xrpToDrops } from '../utils'
|
||||
|
||||
import getFeeXrp from './getFeeXrp'
|
||||
import { isEarlierVersion } from './utils'
|
||||
|
||||
// Expire unconfirmed transactions after 20 ledger versions, approximately 1 minute, by default
|
||||
const LEDGER_OFFSET = 20
|
||||
@@ -48,7 +47,10 @@ async function autofill<T extends Transaction>(
|
||||
// autofill transaction's networkID if either the network is hooks testnet or build version is >= 1.11.0
|
||||
if (
|
||||
(this.buildVersion &&
|
||||
isEarlierVersion(REQUIRED_NETWORKID_VERSION, this.buildVersion)) ||
|
||||
isEarlierRippledVersion(
|
||||
REQUIRED_NETWORKID_VERSION,
|
||||
this.buildVersion,
|
||||
)) ||
|
||||
this.networkID === HOOKS_TESTNET_ID
|
||||
) {
|
||||
tx.NetworkID = this.networkID
|
||||
|
||||
@@ -7,6 +7,7 @@ import { xAddressToClassicAddress, isValidXAddress } from 'ripple-address-codec'
|
||||
* @returns The account's classic address.
|
||||
* @throws Error if the X-Address has an associated tag.
|
||||
*/
|
||||
// eslint-disable-next-line import/prefer-default-export -- okay for a utils file - there could be more exports later
|
||||
export function ensureClassicAddress(account: string): string {
|
||||
if (isValidXAddress(account)) {
|
||||
const { classicAddress, tag } = xAddressToClassicAddress(account)
|
||||
@@ -27,67 +28,3 @@ export function ensureClassicAddress(account: string): string {
|
||||
}
|
||||
return account
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 function isEarlierVersion(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
|
||||
}
|
||||
|
||||
@@ -40,6 +40,7 @@ import {
|
||||
hashEscrow,
|
||||
hashPaymentChannel,
|
||||
} from './hashes'
|
||||
import isEarlierRippledVersion from './isEarlierRippledVersion'
|
||||
import parseNFTokenID from './parseNFTokenID'
|
||||
import {
|
||||
percentToTransferRate,
|
||||
@@ -191,6 +192,7 @@ export {
|
||||
qualityToDecimal,
|
||||
isValidSecret,
|
||||
isValidAddress,
|
||||
isEarlierRippledVersion,
|
||||
hashes,
|
||||
deriveKeypair,
|
||||
deriveAddress,
|
||||
|
||||
66
packages/xrpl/src/utils/isEarlierRippledVersion.ts
Normal file
66
packages/xrpl/src/utils/isEarlierRippledVersion.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
@@ -19,6 +19,7 @@ const NetworkID = 1025
|
||||
const Fee = '10'
|
||||
const Sequence = 1432
|
||||
const LastLedgerSequence = 2908734
|
||||
const HOOKS_TESTNET_ID = 21338
|
||||
|
||||
describe('client.autofill', function () {
|
||||
let testContext: XrplTestContext
|
||||
@@ -46,7 +47,7 @@ describe('client.autofill', function () {
|
||||
assert.strictEqual(txResult.LastLedgerSequence, LastLedgerSequence)
|
||||
})
|
||||
|
||||
it('ignores network ID if < 1024 and its missing', async function () {
|
||||
it('ignores network ID if missing', async function () {
|
||||
const tx: Payment = {
|
||||
TransactionType: 'Payment',
|
||||
Account: 'XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi',
|
||||
@@ -63,23 +64,81 @@ describe('client.autofill', function () {
|
||||
assert.strictEqual(txResult.NetworkID, undefined)
|
||||
})
|
||||
|
||||
// it('override network ID if > 1024 and its missing', async function () {
|
||||
// testContext.client.networkID = 1025
|
||||
// const tx: Payment = {
|
||||
// TransactionType: 'Payment',
|
||||
// Account: 'XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi',
|
||||
// Amount: '1234',
|
||||
// Destination: 'X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ',
|
||||
// Fee,
|
||||
// Sequence,
|
||||
// LastLedgerSequence,
|
||||
// }
|
||||
// testContext.mockRippled!.addResponse('ledger', rippled.ledger.normal)
|
||||
it('overrides network ID if > 1024 and version is later than 1.11.0', async function () {
|
||||
testContext.client.networkID = 1025
|
||||
testContext.client.buildVersion = '1.11.1'
|
||||
const tx: Payment = {
|
||||
TransactionType: 'Payment',
|
||||
Account: 'XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi',
|
||||
Amount: '1234',
|
||||
Destination: 'X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ',
|
||||
Fee,
|
||||
Sequence,
|
||||
LastLedgerSequence,
|
||||
}
|
||||
testContext.mockRippled!.addResponse('ledger', rippled.ledger.normal)
|
||||
|
||||
// const txResult = await testContext.client.autofill(tx)
|
||||
const txResult = await testContext.client.autofill(tx)
|
||||
|
||||
// assert.strictEqual(txResult.NetworkID, 1025)
|
||||
// })
|
||||
assert.strictEqual(txResult.NetworkID, 1025)
|
||||
})
|
||||
|
||||
it('ignores network ID if > 1024 but version is earlier than 1.11.0', async function () {
|
||||
testContext.client.networkID = 1025
|
||||
testContext.client.buildVersion = '1.10.1'
|
||||
const tx: Payment = {
|
||||
TransactionType: 'Payment',
|
||||
Account: 'XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi',
|
||||
Amount: '1234',
|
||||
Destination: 'X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ',
|
||||
Fee,
|
||||
Sequence,
|
||||
LastLedgerSequence,
|
||||
}
|
||||
testContext.mockRippled!.addResponse('ledger', rippled.ledger.normal)
|
||||
|
||||
const txResult = await testContext.client.autofill(tx)
|
||||
|
||||
assert.strictEqual(txResult.NetworkID, undefined)
|
||||
})
|
||||
|
||||
it('ignores network ID if < 1024', async function () {
|
||||
testContext.client.networkID = 1023
|
||||
testContext.client.buildVersion = '1.11.1'
|
||||
const tx: Payment = {
|
||||
TransactionType: 'Payment',
|
||||
Account: 'XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi',
|
||||
Amount: '1234',
|
||||
Destination: 'X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ',
|
||||
Fee,
|
||||
Sequence,
|
||||
LastLedgerSequence,
|
||||
}
|
||||
testContext.mockRippled!.addResponse('ledger', rippled.ledger.normal)
|
||||
|
||||
const txResult = await testContext.client.autofill(tx)
|
||||
|
||||
assert.strictEqual(txResult.NetworkID, undefined)
|
||||
})
|
||||
|
||||
it('override network ID for hooks testnet', async function () {
|
||||
testContext.client.networkID = HOOKS_TESTNET_ID
|
||||
testContext.client.buildVersion = '1.10.1'
|
||||
const tx: Payment = {
|
||||
TransactionType: 'Payment',
|
||||
Account: 'XVLhHMPHU98es4dbozjVtdWzVrDjtV18pX8yuPT7y4xaEHi',
|
||||
Amount: '1234',
|
||||
Destination: 'X7AcgcsBL6XDcUb289X4mJ8djcdyKaB5hJDWMArnXr61cqZ',
|
||||
Fee,
|
||||
Sequence,
|
||||
LastLedgerSequence,
|
||||
}
|
||||
testContext.mockRippled!.addResponse('ledger', rippled.ledger.normal)
|
||||
|
||||
const txResult = await testContext.client.autofill(tx)
|
||||
|
||||
assert.strictEqual(txResult.NetworkID, HOOKS_TESTNET_ID)
|
||||
})
|
||||
|
||||
it('converts Account & Destination X-address to their classic address', async function () {
|
||||
const tx: Payment = {
|
||||
|
||||
22
packages/xrpl/test/utils/isEarlierRippledVersion.test.ts
Normal file
22
packages/xrpl/test/utils/isEarlierRippledVersion.test.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
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)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user