Parse transaction flags into map of names:booleans (#2734)

* overview logic of parsetransactionflags

* parse transaction flags works

* basic tests

* eslint and docs

* linting

* lint

* fix typing

* test fix

* revert import delete

* lint

* integration fix

* lint

* imports

* added numeric test

* add history log

* history update
This commit is contained in:
achowdhry-ripple
2024-08-07 11:44:57 -04:00
committed by GitHub
parent a46e86f17e
commit f3960c3ccc
4 changed files with 89 additions and 1 deletions

View File

@@ -2,6 +2,11 @@
Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xrpl-announce) for release announcements. We recommend that xrpl.js (ripple-lib) users stay up-to-date with the latest stable release.
## Unreleased Changes
### Added
* parseTransactionFlags as a utility function in the xrpl package to streamline transactions flags-to-map conversion
## 4.0.0 (2024-07-15)
### BREAKING CHANGES

View File

@@ -10,6 +10,7 @@ export * as LedgerEntry from './ledger'
export {
setTransactionFlagsToNumber,
parseAccountRootFlags,
parseTransactionFlags,
} from './utils/flags'
export * from './methods'
export * from './transactions'

View File

@@ -1,6 +1,5 @@
/* eslint-disable no-param-reassign -- param reassign is safe */
/* eslint-disable no-bitwise -- flags require bitwise operations */
import { ValidationError } from '../../errors'
import {
AccountRootFlagsInterface,
@@ -90,3 +89,30 @@ function convertFlagsToNumber(flags: GlobalFlags, flagEnum: any): number {
return flags[flag] ? resultFlags | flagEnum[flag] : resultFlags
}, 0)
}
/**
* Convert a Transaction flags property into a map for easy interpretation.
*
* @param tx - A transaction to parse flags for.
* @returns A map with all flags as booleans.
*/
export function parseTransactionFlags(tx: Transaction): object {
setTransactionFlagsToNumber(tx)
if (typeof tx.Flags !== 'number' || !tx.Flags || tx.Flags === 0) {
return {}
}
const flags = tx.Flags
const flagsMap = {}
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment -- safe member access
const flagEnum = txToFlag[tx.TransactionType]
Object.values(flagEnum).forEach((flag) => {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access -- safe member access
if (typeof flag === 'string' && isFlagEnabled(flags, flagEnum[flag])) {
flagsMap[flag] = true
}
})
return flagsMap
}

View File

@@ -17,6 +17,7 @@ import { isFlagEnabled } from '../../src/models/utils'
import {
setTransactionFlagsToNumber,
parseAccountRootFlags,
parseTransactionFlags,
} from '../../src/models/utils/flags'
/**
@@ -207,5 +208,60 @@ describe('Models Utils', function () {
assert.isUndefined(parsed.lsfDisallowIncomingTrustline)
assert.isUndefined(parsed.lsfAllowTrustLineClawback)
})
it('parseTransactionFlags all enabled', function () {
const tx: PaymentChannelClaim = {
Account: 'r...',
TransactionType: 'PaymentChannelClaim',
Channel:
'C1AE6DDDEEC05CF2978C0BAD6FE302948E9533691DC749DCDD3B9E5992CA6198',
Flags: {
tfRenew: true,
tfClose: false,
},
}
const expected = {
tfRenew: true,
}
const flagsMap = parseTransactionFlags(tx)
assert.notStrictEqual(flagsMap, expected)
})
it('parseTransactionFlags all false', function () {
const tx: PaymentChannelClaim = {
Account: 'r...',
TransactionType: 'PaymentChannelClaim',
Channel:
'C1AE6DDDEEC05CF2978C0BAD6FE302948E9533691DC749DCDD3B9E5992CA6198',
Flags: {
tfRenew: false,
tfClose: false,
},
}
const expected = {}
const flagsMap = parseTransactionFlags(tx)
assert.notStrictEqual(flagsMap, expected)
})
it('parseTransactionFlags flag is already numeric', function () {
const tx: PaymentChannelClaim = {
Account: 'r...',
TransactionType: 'PaymentChannelClaim',
Channel:
'C1AE6DDDEEC05CF2978C0BAD6FE302948E9533691DC749DCDD3B9E5992CA6198',
Flags: 65536,
}
const expected = {
tfRenew: true,
}
const flagsMap = parseTransactionFlags(tx)
assert.notStrictEqual(flagsMap, expected)
})
})
})