refactor: reorganize/rename flags interfaces (#1700)

* refactor: reorganize/rename flags interfaces
This commit is contained in:
Mukul Jangid
2021-10-06 17:14:02 -04:00
committed by GitHub
parent e5f6e4a825
commit 5e515a9105
17 changed files with 61 additions and 72 deletions

View File

@@ -9,7 +9,7 @@ import type {
TxResponse, TxResponse,
} from '..' } from '..'
import type { Amount } from '../models/common' import type { Amount } from '../models/common'
import { PaymentTransactionFlags, Transaction } from '../models/transactions' import { PaymentFlags, Transaction } from '../models/transactions'
import type TransactionMetadata from '../models/transactions/metadata' import type TransactionMetadata from '../models/transactions/metadata'
import { isFlagEnabled } from '../models/utils' import { isFlagEnabled } from '../models/utils'
@@ -54,7 +54,7 @@ function isPartialPayment(
const tfPartial = const tfPartial =
typeof tx.Flags === 'number' typeof tx.Flags === 'number'
? isFlagEnabled(tx.Flags, PaymentTransactionFlags.tfPartialPayment) ? isFlagEnabled(tx.Flags, PaymentFlags.tfPartialPayment)
: tx.Flags?.tfPartialPayment : tx.Flags?.tfPartialPayment
if (!tfPartial) { if (!tfPartial) {

View File

@@ -19,7 +19,7 @@ export default interface AccountRoot extends BaseLedgerEntry {
TransferRate?: number TransferRate?: number
} }
export enum AccountRootLedgerFlags { export enum AccountRootFlags {
lsfPasswordSpent = 0x00010000, lsfPasswordSpent = 0x00010000,
lsfRequireDestTag = 0x00020000, lsfRequireDestTag = 0x00020000,
lsfRequireAuth = 0x00040000, lsfRequireAuth = 0x00040000,

View File

@@ -1,5 +1,5 @@
/* eslint-disable import/no-unused-modules -- Needs to export all ledger objects */ /* eslint-disable import/no-unused-modules -- Needs to export all ledger objects */
import AccountRoot, { AccountRootLedgerFlags } from './accountRoot' import AccountRoot, { AccountRootFlags } from './accountRoot'
import Amendments from './amendments' import Amendments from './amendments'
import Check from './check' import Check from './check'
import DepositPreauth from './depositPreauth' import DepositPreauth from './depositPreauth'
@@ -10,15 +10,15 @@ import Ledger from './ledger'
import LedgerEntry from './ledgerEntry' import LedgerEntry from './ledgerEntry'
import LedgerHashes from './ledgerHashes' import LedgerHashes from './ledgerHashes'
import NegativeUNL from './negativeUNL' import NegativeUNL from './negativeUNL'
import Offer, { OfferLedgerFlags } from './offer' import Offer, { OfferFlags } from './offer'
import PayChannel from './payChannel' import PayChannel from './payChannel'
import RippleState, { RippleStateLedgerFlags } from './rippleState' import RippleState, { RippleStateFlags } from './rippleState'
import SignerList, { SignerListLedgerFlags } from './signerList' import SignerList, { SignerListFlags } from './signerList'
import Ticket from './ticket' import Ticket from './ticket'
export { export {
AccountRoot, AccountRoot,
AccountRootLedgerFlags, AccountRootFlags,
Amendments, Amendments,
Check, Check,
DepositPreauth, DepositPreauth,
@@ -30,11 +30,11 @@ export {
LedgerHashes, LedgerHashes,
NegativeUNL, NegativeUNL,
Offer, Offer,
OfferLedgerFlags, OfferFlags,
PayChannel, PayChannel,
RippleState, RippleState,
RippleStateLedgerFlags, RippleStateFlags,
SignerList, SignerList,
SignerListLedgerFlags, SignerListFlags,
Ticket, Ticket,
} }

View File

@@ -17,7 +17,7 @@ export default interface Offer extends BaseLedgerEntry {
Expiration?: number Expiration?: number
} }
export enum OfferLedgerFlags { export enum OfferFlags {
lsfPassive = 0x00010000, lsfPassive = 0x00010000,
lsfSell = 0x00020000, lsfSell = 0x00020000,
} }

View File

@@ -18,7 +18,7 @@ export default interface RippleState extends BaseLedgerEntry {
HighQualityOut?: number HighQualityOut?: number
} }
export enum RippleStateLedgerFlags { export enum RippleStateFlags {
// True, if entry counts toward reserve. // True, if entry counts toward reserve.
lsfLowReserve = 0x00010000, lsfLowReserve = 0x00010000,
lsfHighReserve = 0x00020000, lsfHighReserve = 0x00020000,

View File

@@ -18,7 +18,7 @@ export default interface SignerList extends BaseLedgerEntry {
SignerQuorum: number SignerQuorum: number
} }
export enum SignerListLedgerFlags { export enum SignerListFlags {
// True, uses only one OwnerCount // True, uses only one OwnerCount
lsfOneOwnerCount = 0x00010000, lsfOneOwnerCount = 0x00010000,
} }

View File

@@ -3,7 +3,7 @@ import { ValidationError } from '../../errors'
import { BaseTransaction, validateBaseTransaction } from './common' import { BaseTransaction, validateBaseTransaction } from './common'
export enum AccountSetFlags { export enum AccountSetAsfFlags {
asfRequireDest = 1, asfRequireDest = 1,
asfRequireAuth = 2, asfRequireAuth = 2,
asfDisallowXRP = 3, asfDisallowXRP = 3,
@@ -15,7 +15,7 @@ export enum AccountSetFlags {
asfDepositAuth = 9, asfDepositAuth = 9,
} }
export enum AccountSetTransactionFlags { export enum AccountSetTfFlags {
tfRequireDestTag = 0x00010000, tfRequireDestTag = 0x00010000,
tfOptionalDestTag = 0x00020000, tfOptionalDestTag = 0x00020000,
tfRequireAuth = 0x00040000, tfRequireAuth = 0x00040000,
@@ -40,7 +40,7 @@ export interface AccountSet extends BaseTransaction {
Domain?: string Domain?: string
EmailHash?: string EmailHash?: string
MessageKey?: string MessageKey?: string
SetFlag?: AccountSetFlags SetFlag?: AccountSetAsfFlags
TransferRate?: number TransferRate?: number
TickSize?: number TickSize?: number
} }
@@ -62,7 +62,7 @@ export function validateAccountSet(tx: Record<string, unknown>): void {
if (typeof tx.ClearFlag !== 'number') { if (typeof tx.ClearFlag !== 'number') {
throw new ValidationError('AccountSet: invalid ClearFlag') throw new ValidationError('AccountSet: invalid ClearFlag')
} }
if (!Object.values(AccountSetFlags).includes(tx.ClearFlag)) { if (!Object.values(AccountSetAsfFlags).includes(tx.ClearFlag)) {
throw new ValidationError('AccountSet: invalid ClearFlag') throw new ValidationError('AccountSet: invalid ClearFlag')
} }
} }
@@ -83,7 +83,7 @@ export function validateAccountSet(tx: Record<string, unknown>): void {
if (typeof tx.SetFlag !== 'number') { if (typeof tx.SetFlag !== 'number') {
throw new ValidationError('AccountSet: invalid SetFlag') throw new ValidationError('AccountSet: invalid SetFlag')
} }
if (!Object.values(AccountSetFlags).includes(tx.SetFlag)) { if (!Object.values(AccountSetAsfFlags).includes(tx.SetFlag)) {
throw new ValidationError('AccountSet: invalid SetFlag') throw new ValidationError('AccountSet: invalid SetFlag')
} }
} }

View File

@@ -9,7 +9,7 @@ import {
isAmount, isAmount,
} from './common' } from './common'
export enum OfferCreateTransactionFlags { export enum OfferCreateFlags {
tfPassive = 0x00010000, tfPassive = 0x00010000,
tfImmediateOrCancel = 0x00020000, tfImmediateOrCancel = 0x00020000,
tfFillOrKill = 0x00040000, tfFillOrKill = 0x00040000,

View File

@@ -10,7 +10,7 @@ import {
validateBaseTransaction, validateBaseTransaction,
} from './common' } from './common'
export enum PaymentTransactionFlags { export enum PaymentFlags {
tfNoDirectRipple = 0x00010000, tfNoDirectRipple = 0x00010000,
tfPartialPayment = 0x00020000, tfPartialPayment = 0x00020000,
tfLimitQuality = 0x00040000, tfLimitQuality = 0x00040000,
@@ -95,7 +95,7 @@ function checkPartialPayment(tx: Record<string, unknown>): void {
const flags = tx.Flags as number | PaymentFlagsInterface const flags = tx.Flags as number | PaymentFlagsInterface
const isTfPartialPayment = const isTfPartialPayment =
typeof flags === 'number' typeof flags === 'number'
? isFlagEnabled(flags, PaymentTransactionFlags.tfPartialPayment) ? isFlagEnabled(flags, PaymentFlags.tfPartialPayment)
: flags.tfPartialPayment ?? false : flags.tfPartialPayment ?? false
if (!isTfPartialPayment) { if (!isTfPartialPayment) {

View File

@@ -3,7 +3,7 @@ import { ValidationError } from '../../errors'
import { BaseTransaction, GlobalFlags, validateBaseTransaction } from './common' import { BaseTransaction, GlobalFlags, validateBaseTransaction } from './common'
export enum PaymentChannelClaimTransactionFlags { export enum PaymentChannelClaimFlags {
tfRenew = 0x00010000, tfRenew = 0x00010000,
tfClose = 0x00020000, tfClose = 0x00020000,
} }

View File

@@ -11,8 +11,8 @@ import { AccountDelete, validateAccountDelete } from './accountDelete'
import { import {
AccountSet, AccountSet,
validateAccountSet, validateAccountSet,
AccountSetFlags, AccountSetAsfFlags,
AccountSetTransactionFlags, AccountSetTfFlags,
} from './accountSet' } from './accountSet'
import { CheckCancel, validateCheckCancel } from './checkCancel' import { CheckCancel, validateCheckCancel } from './checkCancel'
import { CheckCash, validateCheckCash } from './checkCash' import { CheckCash, validateCheckCash } from './checkCash'
@@ -26,13 +26,13 @@ import { OfferCancel, validateOfferCancel } from './offerCancel'
import { import {
OfferCreate, OfferCreate,
validateOfferCreate, validateOfferCreate,
OfferCreateTransactionFlags, OfferCreateFlags,
} from './offerCreate' } from './offerCreate'
import { Payment, validatePayment, PaymentTransactionFlags } from './payment' import { Payment, validatePayment, PaymentFlags } from './payment'
import { import {
PaymentChannelClaim, PaymentChannelClaim,
validatePaymentChannelClaim, validatePaymentChannelClaim,
PaymentChannelClaimTransactionFlags, PaymentChannelClaimFlags,
} from './paymentChannelClaim' } from './paymentChannelClaim'
import { import {
PaymentChannelCreate, PaymentChannelCreate,
@@ -45,11 +45,7 @@ import {
import { SetRegularKey, validateSetRegularKey } from './setRegularKey' import { SetRegularKey, validateSetRegularKey } from './setRegularKey'
import { SignerListSet, validateSignerListSet } from './signerListSet' import { SignerListSet, validateSignerListSet } from './signerListSet'
import { TicketCreate, validateTicketCreate } from './ticketCreate' import { TicketCreate, validateTicketCreate } from './ticketCreate'
import { import { TrustSet, validateTrustSet, TrustSetFlags } from './trustSet'
TrustSet,
validateTrustSet,
TrustSetTransactionFlags,
} from './trustSet'
export type Transaction = export type Transaction =
| AccountDelete | AccountDelete
@@ -188,10 +184,10 @@ export function validate(transaction: Record<string, unknown>): void {
} }
export { export {
AccountSetFlags, AccountSetAsfFlags,
AccountSetTransactionFlags, AccountSetTfFlags,
OfferCreateTransactionFlags, OfferCreateFlags,
PaymentTransactionFlags, PaymentFlags,
PaymentChannelClaimTransactionFlags, PaymentChannelClaimFlags,
TrustSetTransactionFlags, TrustSetFlags,
} }

View File

@@ -8,7 +8,7 @@ import {
validateBaseTransaction, validateBaseTransaction,
} from './common' } from './common'
export enum TrustSetTransactionFlags { export enum TrustSetFlags {
tfSetfAuth = 0x00010000, tfSetfAuth = 0x00010000,
tfSetNoRipple = 0x00020000, tfSetNoRipple = 0x00020000,
tfClearNoRipple = 0x00040000, tfClearNoRipple = 0x00040000,

View File

@@ -4,26 +4,20 @@
import { ValidationError } from '../../errors' import { ValidationError } from '../../errors'
import { import {
AccountSetFlagsInterface, AccountSetFlagsInterface,
AccountSetTransactionFlags, AccountSetTfFlags,
} from '../transactions/accountSet' } from '../transactions/accountSet'
import { GlobalFlags } from '../transactions/common' import { GlobalFlags } from '../transactions/common'
import { import {
OfferCreateFlagsInterface, OfferCreateFlagsInterface,
OfferCreateTransactionFlags, OfferCreateFlags,
} from '../transactions/offerCreate' } from '../transactions/offerCreate'
import { import { PaymentFlagsInterface, PaymentFlags } from '../transactions/payment'
PaymentFlagsInterface,
PaymentTransactionFlags,
} from '../transactions/payment'
import { import {
PaymentChannelClaimFlagsInterface, PaymentChannelClaimFlagsInterface,
PaymentChannelClaimTransactionFlags, PaymentChannelClaimFlags,
} from '../transactions/paymentChannelClaim' } from '../transactions/paymentChannelClaim'
import type { Transaction } from '../transactions/transaction' import type { Transaction } from '../transactions/transaction'
import { import { TrustSetFlagsInterface, TrustSetFlags } from '../transactions/trustSet'
TrustSetFlagsInterface,
TrustSetTransactionFlags,
} from '../transactions/trustSet'
/** /**
* Sets a transaction's flags to its numeric representation. * Sets a transaction's flags to its numeric representation.
@@ -63,29 +57,29 @@ export default function setTransactionFlagsToNumber(tx: Transaction): void {
function convertAccountSetFlagsToNumber( function convertAccountSetFlagsToNumber(
flags: AccountSetFlagsInterface, flags: AccountSetFlagsInterface,
): number { ): number {
return reduceFlags(flags, AccountSetTransactionFlags) return reduceFlags(flags, AccountSetTfFlags)
} }
function convertOfferCreateFlagsToNumber( function convertOfferCreateFlagsToNumber(
flags: OfferCreateFlagsInterface, flags: OfferCreateFlagsInterface,
): number { ): number {
return reduceFlags(flags, OfferCreateTransactionFlags) return reduceFlags(flags, OfferCreateFlags)
} }
function convertPaymentChannelClaimFlagsToNumber( function convertPaymentChannelClaimFlagsToNumber(
flags: PaymentChannelClaimFlagsInterface, flags: PaymentChannelClaimFlagsInterface,
): number { ): number {
return reduceFlags(flags, PaymentChannelClaimTransactionFlags) return reduceFlags(flags, PaymentChannelClaimFlags)
} }
function convertPaymentTransactionFlagsToNumber( function convertPaymentTransactionFlagsToNumber(
flags: PaymentFlagsInterface, flags: PaymentFlagsInterface,
): number { ): number {
return reduceFlags(flags, PaymentTransactionFlags) return reduceFlags(flags, PaymentFlags)
} }
function convertTrustSetFlagsToNumber(flags: TrustSetFlagsInterface): number { function convertTrustSetFlagsToNumber(flags: TrustSetFlagsInterface): number {
return reduceFlags(flags, TrustSetTransactionFlags) return reduceFlags(flags, TrustSetFlags)
} }
// eslint-disable-next-line @typescript-eslint/no-explicit-any -- added ValidationError check for flagEnum // eslint-disable-next-line @typescript-eslint/no-explicit-any -- added ValidationError check for flagEnum

View File

@@ -4,7 +4,7 @@ import _ from 'lodash'
import type { Client } from '../client' import type { Client } from '../client'
import { LedgerIndex } from '../models/common' import { LedgerIndex } from '../models/common'
import { OfferLedgerFlags } from '../models/ledger/offer' import { OfferFlags } from '../models/ledger/offer'
import { import {
BookOffer, BookOffer,
BookOffersRequest, BookOffersRequest,
@@ -80,7 +80,7 @@ async function getOrderbook(
const sell: BookOffer[] = [] const sell: BookOffer[] = []
orders.forEach((order) => { orders.forEach((order) => {
// eslint-disable-next-line no-bitwise -- necessary for flags check // eslint-disable-next-line no-bitwise -- necessary for flags check
if ((order.Flags & OfferLedgerFlags.lsfSell) === 0) { if ((order.Flags & OfferFlags.lsfSell) === 0) {
buy.push(order) buy.push(order)
} else { } else {
sell.push(order) sell.push(order)

View File

@@ -2,7 +2,7 @@ import BigNumber from 'bignumber.js'
import { assert } from 'chai' import { assert } from 'chai'
import { BookOffersRequest, ValidationError } from '../../src' import { BookOffersRequest, ValidationError } from '../../src'
import { OfferLedgerFlags } from '../../src/models/ledger/offer' import { OfferFlags } from '../../src/models/ledger/offer'
import requests from '../fixtures/requests' import requests from '../fixtures/requests'
import responses from '../fixtures/responses' import responses from '../fixtures/responses'
import rippled from '../fixtures/rippled' import rippled from '../fixtures/rippled'
@@ -169,11 +169,11 @@ describe('client.getOrderbook', function () {
requests.getOrderbook.normal.takerGets, requests.getOrderbook.normal.takerGets,
) )
assert.strictEqual( assert.strictEqual(
response.buy.every((item) => item.Flags !== OfferLedgerFlags.lsfSell), response.buy.every((item) => item.Flags !== OfferFlags.lsfSell),
true, true,
) )
assert.strictEqual( assert.strictEqual(
response.sell.every((item) => item.Flags === OfferLedgerFlags.lsfSell), response.sell.every((item) => item.Flags === OfferFlags.lsfSell),
true, true,
) )
}) })

View File

@@ -3,7 +3,7 @@ import { assert } from 'chai'
import { import {
validatePayment, validatePayment,
validate, validate,
PaymentTransactionFlags, PaymentFlags,
ValidationError, ValidationError,
} from 'xrpl-local' } from 'xrpl-local'
@@ -158,7 +158,7 @@ describe('Payment', function () {
it(`verifies valid DeliverMin with tfPartialPayment flag set as a number`, function () { it(`verifies valid DeliverMin with tfPartialPayment flag set as a number`, function () {
paymentTransaction.DeliverMin = '10000' paymentTransaction.DeliverMin = '10000'
paymentTransaction.Flags = PaymentTransactionFlags.tfPartialPayment paymentTransaction.Flags = PaymentFlags.tfPartialPayment
assert.doesNotThrow(() => validatePayment(paymentTransaction)) assert.doesNotThrow(() => validatePayment(paymentTransaction))
assert.doesNotThrow(() => validate(paymentTransaction)) assert.doesNotThrow(() => validate(paymentTransaction))
}) })

View File

@@ -4,13 +4,13 @@ import { assert } from 'chai'
import { import {
DepositPreauth, DepositPreauth,
OfferCreate, OfferCreate,
OfferCreateTransactionFlags, OfferCreateFlags,
PaymentChannelClaim, PaymentChannelClaim,
PaymentChannelClaimTransactionFlags, PaymentChannelClaimFlags,
Payment, Payment,
PaymentTransactionFlags, PaymentFlags,
TrustSet, TrustSet,
TrustSetTransactionFlags, TrustSetFlags,
} from 'xrpl-local' } from 'xrpl-local'
import { isFlagEnabled } from 'xrpl-local/models/utils' import { isFlagEnabled } from 'xrpl-local/models/utils'
import setTransactionFlagsToNumber from 'xrpl-local/models/utils/flags' import setTransactionFlagsToNumber from 'xrpl-local/models/utils/flags'
@@ -63,7 +63,7 @@ describe('Models Utils', function () {
}, },
} }
const { tfPassive, tfFillOrKill } = OfferCreateTransactionFlags const { tfPassive, tfFillOrKill } = OfferCreateFlags
const expected: number = tfPassive | tfFillOrKill const expected: number = tfPassive | tfFillOrKill
setTransactionFlagsToNumber(tx) setTransactionFlagsToNumber(tx)
@@ -82,7 +82,7 @@ describe('Models Utils', function () {
}, },
} }
const { tfRenew } = PaymentChannelClaimTransactionFlags const { tfRenew } = PaymentChannelClaimFlags
const expected: number = tfRenew const expected: number = tfRenew
setTransactionFlagsToNumber(tx) setTransactionFlagsToNumber(tx)
@@ -102,7 +102,7 @@ describe('Models Utils', function () {
}, },
} }
const { tfPartialPayment, tfLimitQuality } = PaymentTransactionFlags const { tfPartialPayment, tfLimitQuality } = PaymentFlags
const expected: number = tfPartialPayment | tfLimitQuality const expected: number = tfPartialPayment | tfLimitQuality
setTransactionFlagsToNumber(tx) setTransactionFlagsToNumber(tx)
@@ -129,8 +129,7 @@ describe('Models Utils', function () {
}, },
} }
const { tfSetfAuth, tfClearNoRipple, tfClearFreeze } = const { tfSetfAuth, tfClearNoRipple, tfClearFreeze } = TrustSetFlags
TrustSetTransactionFlags
const expected: number = tfSetfAuth | tfClearNoRipple | tfClearFreeze const expected: number = tfSetfAuth | tfClearNoRipple | tfClearFreeze
setTransactionFlagsToNumber(tx) setTransactionFlagsToNumber(tx)