Skip to content

Commit

Permalink
Parse transaction flags into map of names:booleans (#2734)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
achowdhry-ripple authored Aug 7, 2024
1 parent a46e86f commit f3960c3
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/xrpl/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/xrpl/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * as LedgerEntry from './ledger'
export {
setTransactionFlagsToNumber,
parseAccountRootFlags,
parseTransactionFlags,
} from './utils/flags'
export * from './methods'
export * from './transactions'
Expand Down
28 changes: 27 additions & 1 deletion packages/xrpl/src/models/utils/flags.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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
}
56 changes: 56 additions & 0 deletions packages/xrpl/test/models/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { isFlagEnabled } from '../../src/models/utils'
import {
setTransactionFlagsToNumber,
parseAccountRootFlags,
parseTransactionFlags,
} from '../../src/models/utils/flags'

/**
Expand Down Expand Up @@ -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)
})
})
})

0 comments on commit f3960c3

Please sign in to comment.