diff --git a/src/models/transactions/index.ts b/src/models/transactions/index.ts index 5808d223..38f6e4e4 100644 --- a/src/models/transactions/index.ts +++ b/src/models/transactions/index.ts @@ -5,6 +5,7 @@ export * from './checkCancel' export * from './checkCash' export * from './checkCreate' export * from './signerListSet' +export * from './ticketCreate' export * from './depositPreauth' export * from './escrowCancel' export * from './escrowFinish' diff --git a/src/models/transactions/ticketCreate.ts b/src/models/transactions/ticketCreate.ts new file mode 100644 index 00000000..fd2e8779 --- /dev/null +++ b/src/models/transactions/ticketCreate.ts @@ -0,0 +1,30 @@ +import { ValidationError } from '../../common/errors' +import { BaseTransaction, verifyBaseTransaction } from './common' + +export interface TicketCreate extends BaseTransaction { + TransactionType: 'TicketCreate' + TicketCount: number +} + +/** + * + * @param {TicketCreate} tx A TicketCreate Transaction. + * @returns {void} + * @throws {ValidationError} When the TicketCreate is malformed. + */ + export function verifyTicketCreate(tx: TicketCreate): void { + verifyBaseTransaction(tx) + const { TicketCount } = tx + + if (TicketCount === undefined) { + throw new ValidationError('TicketCreate: missing field TicketCount') + } + + if (typeof TicketCount !== 'number') { + throw new ValidationError('TicketCreate: TicketCount must be a number') + } + + if (!Number.isInteger(TicketCount) || TicketCount < 1 || TicketCount > 250) { + throw new ValidationError('TicketCreate: TicketCount must be an integer from 1 to 250') + } +} diff --git a/src/models/transactions/transaction.ts b/src/models/transactions/transaction.ts index 76370bb9..67c027e6 100644 --- a/src/models/transactions/transaction.ts +++ b/src/models/transactions/transaction.ts @@ -11,6 +11,7 @@ import { OfferCancel } from "./offerCancel" import { OfferCreate } from "./offerCreate" import { PaymentTransaction } from "./paymentTransaction" import { SignerListSet } from "./signerListSet" +import { TicketCreate } from "./ticketCreate" import { TrustSet } from "./trustSet" export type Transaction = @@ -31,7 +32,7 @@ export type Transaction = // | PaymentChannelFund // | SetRegularKey | SignerListSet -// | TicketCreate + | TicketCreate | TrustSet export interface TransactionAndMetadata { diff --git a/test/models/ticketCreate.ts b/test/models/ticketCreate.ts new file mode 100644 index 00000000..74a2f8df --- /dev/null +++ b/test/models/ticketCreate.ts @@ -0,0 +1,69 @@ +import { ValidationError } from 'xrpl-local/common/errors' +import { verifyTicketCreate } from './../../src/models/transactions/ticketCreate' +import { assert } from 'chai' + +/** + * TicketCreate Transaction Verification Testing + * + * Providing runtime verification testing for each specific transaction type + */ +describe('TicketCreate Transaction Verification', () => { + let ticketCreate + + beforeEach(() => { + ticketCreate = { + TransactionType: 'TicketCreate', + Account: 'rUn84CUYbNjRoTQ6mSW7BVJPSVJNLb1QLo', + TicketCount: 150, + } as any + }) + + it ('verifies valid TicketCreate', () => { + assert.doesNotThrow(() => verifyTicketCreate(ticketCreate)) + }) + + it ('throws when TicketCount is missing', () => { + delete ticketCreate.TicketCount + assert.throws( + () => verifyTicketCreate(ticketCreate), + ValidationError, + 'TicketCreate: missing field TicketCount' + ) + }) + + it ('throws when TicketCount is not a number', () => { + ticketCreate.TicketCount = '150' + assert.throws( + () => verifyTicketCreate(ticketCreate), + ValidationError, + 'TicketCreate: TicketCount must be a number' + ) + }) + + it ('throws when TicketCount is not an integer', () => { + ticketCreate.TicketCount = 12.5 + assert.throws( + () => verifyTicketCreate(ticketCreate), + ValidationError, + 'TicketCreate: TicketCount must be an integer from 1 to 250' + ) + }) + + it ('throws when TicketCount is < 1', () => { + ticketCreate.TicketCount = 0 + assert.throws( + () => verifyTicketCreate(ticketCreate), + ValidationError, + 'TicketCreate: TicketCount must be an integer from 1 to 250' + ) + }) + + it ('throws when TicketCount is > 250', () => { + ticketCreate.TicketCount = 251 + assert.throws( + () => verifyTicketCreate(ticketCreate), + ValidationError, + 'TicketCreate: TicketCount must be an integer from 1 to 250' + ) + }) +})