From f3960c3cccc26f7e595fc4365a68fdd52db03be3 Mon Sep 17 00:00:00 2001 From: achowdhry-ripple Date: Wed, 7 Aug 2024 11:44:57 -0400 Subject: [PATCH] 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 --- packages/xrpl/HISTORY.md | 5 +++ packages/xrpl/src/models/index.ts | 1 + packages/xrpl/src/models/utils/flags.ts | 28 ++++++++++++- packages/xrpl/test/models/utils.test.ts | 56 +++++++++++++++++++++++++ 4 files changed, 89 insertions(+), 1 deletion(-) diff --git a/packages/xrpl/HISTORY.md b/packages/xrpl/HISTORY.md index d473adfb..1f198c02 100644 --- a/packages/xrpl/HISTORY.md +++ b/packages/xrpl/HISTORY.md @@ -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 diff --git a/packages/xrpl/src/models/index.ts b/packages/xrpl/src/models/index.ts index 44625848..5c98faf8 100644 --- a/packages/xrpl/src/models/index.ts +++ b/packages/xrpl/src/models/index.ts @@ -10,6 +10,7 @@ export * as LedgerEntry from './ledger' export { setTransactionFlagsToNumber, parseAccountRootFlags, + parseTransactionFlags, } from './utils/flags' export * from './methods' export * from './transactions' diff --git a/packages/xrpl/src/models/utils/flags.ts b/packages/xrpl/src/models/utils/flags.ts index 15f1cbb2..81fba1ae 100644 --- a/packages/xrpl/src/models/utils/flags.ts +++ b/packages/xrpl/src/models/utils/flags.ts @@ -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 +} diff --git a/packages/xrpl/test/models/utils.test.ts b/packages/xrpl/test/models/utils.test.ts index ec82ce11..c4313610 100644 --- a/packages/xrpl/test/models/utils.test.ts +++ b/packages/xrpl/test/models/utils.test.ts @@ -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) + }) }) })