refactor: define typescript type for AccountSet transaction (#1515)

* define typescript type for AccountSet transaction
This commit is contained in:
Mukul Jangid
2021-08-18 11:06:48 -04:00
committed by Mayukha Vadari
parent b2d03363a6
commit e9d0d40db5
8 changed files with 206 additions and 141 deletions

View File

@@ -0,0 +1,69 @@
import { ValidationError } from "../../common/errors"
import { BaseTransaction, verifyBaseTransaction } from "./common"
enum AccountSetFlagEnum {
asfRequireDest = 1,
asfRequireAuth = 2,
asfDisallowXRP = 3,
asfDisableMaster = 4,
asfAccountTxnID = 5,
asfNoFreeze = 6,
asfGlobalFreeze = 7,
asfDefaultRipple = 8,
asfDepositAuth = 9,
}
export interface AccountSet extends BaseTransaction {
TransactionType: "AccountSet"
ClearFlag?: number
Domain?: string
EmailHash?: string
MessageKey?: string
SetFlag?: AccountSetFlagEnum
TransferRate?: number
TickSize?: number
}
/**
* Verify the form and type of an AccountSet at runtime.
*
* @param tx - An AccountSet Transaction
* @returns - Void.
* @throws - When the AccountSet is Malformed.
*/
export function verifyAccountSet(tx: AccountSet): void {
verifyBaseTransaction(tx)
if (tx.ClearFlag !== undefined){
if (typeof tx.ClearFlag !== 'number')
throw new ValidationError("AccountSet: invalid ClearFlag")
if (!Object.values(AccountSetFlagEnum).includes(tx.ClearFlag))
throw new ValidationError("AccountSet: invalid ClearFlag")
}
if (tx.Domain !== undefined && typeof tx.Domain !== 'string')
throw new ValidationError("AccountSet: invalid Domain")
if (tx.EmailHash !== undefined && typeof tx.EmailHash !== 'string')
throw new ValidationError("AccountSet: invalid EmailHash")
if (tx.MessageKey !== undefined && typeof tx.MessageKey !== 'string')
throw new ValidationError("AccountSet: invalid MessageKey")
if (tx.SetFlag !== undefined){
if (typeof tx.SetFlag !== 'number')
throw new ValidationError("AccountSet: invalid SetFlag")
if (!Object.values(AccountSetFlagEnum).includes(tx.SetFlag))
throw new ValidationError("AccountSet: invalid SetFlag")
}
if (tx.TransferRate !== undefined && typeof tx.TransferRate !== 'number')
throw new ValidationError("AccountSet: invalid TransferRate")
if (tx.TickSize !== undefined){
if (typeof tx.TickSize !== 'number')
throw new ValidationError("AccountSet: invalid TickSize")
if (tx.TickSize !== 0 && (3 > tx.TickSize || tx.TickSize > 15))
throw new ValidationError("AccountSet: invalid TickSize")
}
}

View File

@@ -1,7 +1,7 @@
export * from './transaction'
export * from './offerCreate'
export * from './offerCancel'
export * from './accountSet'
export * from './checkCreate'
export * from './checkCash'
export * from './checkCancel'
export * from './signerListSet'
export * from './signerListSet'

View File

@@ -1,24 +0,0 @@
import { ValidationError } from "../../common/errors"
import { BaseTransaction, verifyBaseTransaction } from "./common"
export interface OfferCancel extends BaseTransaction {
TransactionType: "OfferCancel"
OfferSequence: number
}
/**
* Verify the form and type of an OfferCancel at runtime.
*
* @param tx - An OfferCancel Transaction
* @returns - Void.
* @throws - When the OfferCancel is Malformed.
*/
export function verifyOfferCancel(tx: OfferCancel): void {
verifyBaseTransaction(tx)
if (tx.OfferSequence === undefined)
throw new ValidationError("OfferCancel: missing field OfferSequence")
if (typeof tx.OfferSequence !== 'number')
throw new ValidationError("OfferCancel: invalid OfferSequence")
}

View File

@@ -1,25 +1,21 @@
import Metadata from "../common/metadata"
import { OfferCreate } from "./offerCreate"
import { OfferCancel } from "./offerCancel"
import { CheckCash } from "./checkCash"
import { CheckCancel } from "./checkCancel"
import { CheckCreate } from "./checkCreate"
import { SignerListSet } from "./signerListSet"
import Metadata from "../common/metadata";
import { OfferCreate } from "./offerCreate";
import { CheckCash } from "./checkCash";
import { CheckCancel } from "./checkCancel";
import { CheckCreate } from "./checkCreate";
import { SignerListSet } from "./signerListSet";
import { AccountSet } from "./accountSet";
export type Transaction =
// AccountSet
AccountSet
// | AccountDelete
// | CheckCancel
// | CheckCash
// | CheckCreate
CheckCancel
| CheckCancel
| CheckCash
| CheckCreate
// | DepositPreauth
// | EscrowCancel
// | EscrowCreate
// | EscrowFinish
| OfferCancel
// | OfferCancel
| OfferCreate
// | PaymentTransaction