refactor: Define PaymentChannelFund transaction model (#1535)

refactor: Define PaymentChannelFund transaction model (#1535)
This commit is contained in:
Nathan Nichols
2021-08-19 11:21:06 -05:00
committed by Mayukha Vadari
parent db8f7c1bcb
commit d324056203
4 changed files with 122 additions and 2 deletions

View File

@@ -13,6 +13,7 @@ export * from './offerCreate'
export * from './paymentTransaction'
export * from './paymentChannelClaim'
export * from './paymentChannelCreate'
export * from './paymentChannelFund'
export * from './signerListSet'
export * from './ticketCreate'
export * from './trustSet'

View File

@@ -0,0 +1,35 @@
import { ValidationError } from "../../common/errors";
import { BaseTransaction, verifyBaseTransaction } from "./common";
export interface PaymentChannelFund extends BaseTransaction {
TransactionType: "PaymentChannelFund";
Channel: string;
Amount: string;
Expiration?: number;
}
/**
* Verify the form and type of an PaymentChannelFund at runtime.
*
* @param tx - An PaymentChannelFund Transaction
* @returns - Void.
* @throws - When the PaymentChannelFund is Malformed.
*/
export function verifyPaymentChannelFund(tx: PaymentChannelFund): void {
verifyBaseTransaction(tx)
if (tx.Channel === undefined)
throw new ValidationError("PaymentChannelFund: missing Channel")
if (typeof tx.Channel !== 'string')
throw new ValidationError("PaymentChannelFund: Channel must be a string")
if (tx.Amount === undefined)
throw new ValidationError("PaymentChannelFund: missing Amount")
if (typeof tx.Amount !== 'string')
throw new ValidationError("PaymentChannelFund: Amount must be a string")
if (tx.Expiration !== undefined && typeof tx.Expiration !== 'number')
throw new ValidationError("PaymentChannelFund: Expiration must be a number")
}

View File

@@ -11,8 +11,9 @@ import { EscrowFinish } from "./escrowFinish"
import { OfferCancel } from "./offerCancel"
import { OfferCreate } from "./offerCreate"
import { PaymentTransaction } from "./paymentTransaction"
import { PaymentChannelCreate } from "./paymentChannelCreate"
import { PaymentChannelClaim } from './paymentChannelClaim'
import { PaymentChannelCreate } from "./paymentChannelCreate"
import { PaymentChannelFund } from "./paymentChannelFund"
import { SignerListSet } from "./signerListSet"
import { TicketCreate } from "./ticketCreate"
import { TrustSet } from "./trustSet"
@@ -32,7 +33,7 @@ export type Transaction =
| PaymentTransaction
| PaymentChannelClaim
| PaymentChannelCreate
// | PaymentChannelFund
| PaymentChannelFund
// | SetRegularKey
| SignerListSet
| TicketCreate

View File

@@ -0,0 +1,83 @@
import { ValidationError } from 'xrpl-local/common/errors'
import { verifyPaymentChannelFund } from './../../src/models/transactions/paymentChannelFund'
import { assert } from 'chai'
/**
* PaymentChannelFund Transaction Verification Testing
*
* Providing runtime verification testing for each specific transaction type
*/
describe('PaymentChannelFund Transaction Verification', function () {
let channel
beforeEach(() => {
channel = {
"Account": "rf1BiGeXwwQoi8Z2ueFYTEXSwuJYfV2Jpn",
"TransactionType": "PaymentChannelFund",
"Channel": "C1AE6DDDEEC05CF2978C0BAD6FE302948E9533691DC749DCDD3B9E5992CA6198",
"Amount": "200000",
"Expiration": 543171558
}
})
it (`verifies valid PaymentChannelFund`, () => {
assert.doesNotThrow(() => verifyPaymentChannelFund(channel))
})
it (`verifies valid PaymentChannelFund w/o optional`, () => {
delete channel.Expiration
assert.doesNotThrow(() => verifyPaymentChannelFund(channel))
})
it (`throws w/ missing Amount`, () => {
delete channel.Amount
assert.throws(
() => verifyPaymentChannelFund(channel),
ValidationError,
"PaymentChannelFund: missing Amount"
)
})
it (`throws w/ missing Channel`, () => {
delete channel.Channel
assert.throws(
() => verifyPaymentChannelFund(channel),
ValidationError,
"PaymentChannelFund: missing Channel"
)
})
it (`throws w/ invalid Amount`, () => {
channel.Amount = 100
assert.throws(
() => verifyPaymentChannelFund(channel),
ValidationError,
"PaymentChannelFund: Amount must be a string"
)
})
it (`throws w/ invalid Channel`, () => {
channel.Channel = 1000
assert.throws(
() => verifyPaymentChannelFund(channel),
ValidationError,
"PaymentChannelFund: Channel must be a string"
)
})
it (`throws w/ invalid Expiration`, () => {
channel.Expiration = "1000"
assert.throws(
() => verifyPaymentChannelFund(channel),
ValidationError,
"PaymentChannelFund: Expiration must be a number"
)
})
})