mirror of
https://github.com/Xahau/xahau.js.git
synced 2025-12-01 17:45:49 +00:00
refactor: define TrustSet transaction model (#1549)
- Defines a TypeScript type for TrustSet - Provides an optional function to users for verifying a TrustSet instance at runtime: verifyTrustSet() - Adds tests for verifyTrustSet()
This commit is contained in:
committed by
Mayukha Vadari
parent
f7b93c54ff
commit
9e16327676
@@ -7,5 +7,6 @@ export * from './checkCash'
|
||||
export * from './checkCancel'
|
||||
export * from './accountDelete'
|
||||
export * from './signerListSet'
|
||||
export * from './trustSet'
|
||||
export * from './depositPreauth'
|
||||
export * from './paymentTransaction'
|
||||
|
||||
@@ -9,6 +9,7 @@ import { OfferCancel } from "./offerCancel"
|
||||
import { OfferCreate } from "./offerCreate"
|
||||
import { PaymentTransaction } from "./paymentTransaction"
|
||||
import { SignerListSet } from "./signerListSet"
|
||||
import { TrustSet } from "./trustSet"
|
||||
|
||||
export type Transaction =
|
||||
AccountDelete
|
||||
@@ -30,7 +31,7 @@ export type Transaction =
|
||||
// | SetRegularKey
|
||||
| SignerListSet
|
||||
// | TicketCreate
|
||||
// | TrustSet
|
||||
| TrustSet
|
||||
|
||||
export interface TransactionAndMetadata {
|
||||
transaction: Transaction
|
||||
|
||||
54
src/models/transactions/trustSet.ts
Normal file
54
src/models/transactions/trustSet.ts
Normal file
@@ -0,0 +1,54 @@
|
||||
import { ValidationError } from '../../common/errors'
|
||||
import { Amount } from '../common'
|
||||
import { BaseTransaction, GlobalFlags, isAmount, verifyBaseTransaction } from './common'
|
||||
|
||||
export enum TrustSetFlagsEnum {
|
||||
tfSetfAuth = 0x00010000,
|
||||
tfSetNoRipple = 0x00020000,
|
||||
tfClearNoRipple = 0x00040000,
|
||||
tfSetFreeze = 0x00100000,
|
||||
tfClearFreeze = 0x00200000,
|
||||
}
|
||||
|
||||
export interface TrustSetFlags extends GlobalFlags {
|
||||
tfSetfAuth?: boolean
|
||||
tfSetNoRipple?: boolean
|
||||
tfClearNoRipple?: boolean
|
||||
tfSetFreeze?: boolean
|
||||
tfClearFreeze?: boolean
|
||||
}
|
||||
|
||||
export interface TrustSet extends BaseTransaction {
|
||||
TransactionType: 'TrustSet'
|
||||
LimitAmount: Amount
|
||||
QualityIn?: number
|
||||
QualityOut?: number
|
||||
Flags?: number | TrustSetFlags
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param tx A TrustSet Transaction.
|
||||
* @returns {void}
|
||||
* @throws {ValidationError} When the TrustSet is malformed.
|
||||
*/
|
||||
export function verifyTrustSet(tx: TrustSet): void {
|
||||
verifyBaseTransaction(tx)
|
||||
const { LimitAmount, QualityIn, QualityOut } = tx
|
||||
|
||||
if (LimitAmount === undefined) {
|
||||
throw new ValidationError('TrustSet: missing field LimitAmount')
|
||||
}
|
||||
|
||||
if (!isAmount(LimitAmount)) {
|
||||
throw new ValidationError('TrustSet: invalid LimitAmount')
|
||||
}
|
||||
|
||||
if (QualityIn !== undefined && typeof QualityIn !== 'number') {
|
||||
throw new ValidationError('TrustSet: QualityIn must be a number')
|
||||
}
|
||||
|
||||
if (QualityOut !== undefined && typeof QualityOut !== 'number') {
|
||||
throw new ValidationError('TrustSet: QualityOut must be a number')
|
||||
}
|
||||
}
|
||||
66
test/models/trustSet.ts
Normal file
66
test/models/trustSet.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
import { ValidationError } from 'xrpl-local/common/errors'
|
||||
import { verifyTrustSet } from './../../src/models/transactions/trustSet'
|
||||
import { assert } from 'chai'
|
||||
|
||||
/**
|
||||
* TrustSet Transaction Verification Testing
|
||||
*
|
||||
* Providing runtime verification testing for each specific transaction type
|
||||
*/
|
||||
describe('TrustSet Transaction Verification', () => {
|
||||
let trustSet
|
||||
|
||||
beforeEach(() => {
|
||||
trustSet = {
|
||||
TransactionType: 'TrustSet',
|
||||
Account: 'rUn84CUYbNjRoTQ6mSW7BVJPSVJNLb1QLo',
|
||||
LimitAmount: {
|
||||
currency: 'XRP',
|
||||
issuer: 'rcXY84C4g14iFp6taFXjjQGVeHqSCh9RX',
|
||||
value: '4329.23'
|
||||
},
|
||||
QualityIn: 1234,
|
||||
QualityOut: 4321,
|
||||
} as any
|
||||
})
|
||||
|
||||
it ('verifies valid TrustSet', () => {
|
||||
assert.doesNotThrow(() => verifyTrustSet(trustSet))
|
||||
})
|
||||
|
||||
it ('throws when LimitAmount is missing', () => {
|
||||
delete trustSet.LimitAmount
|
||||
assert.throws(
|
||||
() => verifyTrustSet(trustSet),
|
||||
ValidationError,
|
||||
'TrustSet: missing field LimitAmount'
|
||||
)
|
||||
})
|
||||
|
||||
it ('throws when LimitAmount is invalid', () => {
|
||||
trustSet.LimitAmount = 1234
|
||||
assert.throws(
|
||||
() => verifyTrustSet(trustSet),
|
||||
ValidationError,
|
||||
'TrustSet: invalid LimitAmount'
|
||||
)
|
||||
})
|
||||
|
||||
it ('throws when QualityIn is not a number', () => {
|
||||
trustSet.QualityIn = '1234'
|
||||
assert.throws(
|
||||
() => verifyTrustSet(trustSet),
|
||||
ValidationError,
|
||||
'TrustSet: QualityIn must be a number'
|
||||
)
|
||||
})
|
||||
|
||||
it ('throws when QualityOut is not a number', () => {
|
||||
trustSet.QualityOut = '4321'
|
||||
assert.throws(
|
||||
() => verifyTrustSet(trustSet),
|
||||
ValidationError,
|
||||
'TrustSet: QualityOut must be a number'
|
||||
)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user