diff --git a/packages/xrpl/src/models/common/index.ts b/packages/xrpl/src/models/common/index.ts index bd7e1279..e8b3b76f 100644 --- a/packages/xrpl/src/models/common/index.ts +++ b/packages/xrpl/src/models/common/index.ts @@ -4,15 +4,13 @@ interface XRP { currency: 'XRP' } -interface IssuedCurrency { +export interface IssuedCurrency { currency: string issuer: string } export type Currency = IssuedCurrency | XRP -export type Issue = Currency - export interface IssuedCurrencyAmount extends IssuedCurrency { value: string } diff --git a/packages/xrpl/src/models/methods/ammInfo.ts b/packages/xrpl/src/models/methods/ammInfo.ts index 6ad5135f..c033d3af 100644 --- a/packages/xrpl/src/models/methods/ammInfo.ts +++ b/packages/xrpl/src/models/methods/ammInfo.ts @@ -1,4 +1,4 @@ -import { Amount, Issue, IssuedCurrencyAmount } from '../common' +import { Amount, IssuedCurrency, IssuedCurrencyAmount } from '../common' import { BaseRequest, BaseResponse } from './baseMethod' @@ -15,13 +15,13 @@ export interface AMMInfoRequest extends BaseRequest { * Specifies one of the pool assets (XRP or token) of the AMM instance. * Both asset and asset2 must be defined to specify an AMM instance. */ - asset?: Issue + asset?: IssuedCurrency /** * Specifies the other pool asset of the AMM instance. * Both asset and asset2 must be defined to specify an AMM instance. */ - asset2?: Issue + asset2?: IssuedCurrency } interface AuthAccount { diff --git a/packages/xrpl/src/models/transactions/AMMBid.ts b/packages/xrpl/src/models/transactions/AMMBid.ts index 1a6ec810..e5ff1a95 100644 --- a/packages/xrpl/src/models/transactions/AMMBid.ts +++ b/packages/xrpl/src/models/transactions/AMMBid.ts @@ -1,6 +1,6 @@ /* eslint-disable complexity -- required for validateAMMBid */ import { ValidationError } from '../../errors' -import { Amount, Issue } from '../common' +import { Amount, IssuedCurrency } from '../common' import { BaseTransaction, @@ -29,12 +29,12 @@ export interface AMMBid extends BaseTransaction { /** * Specifies one of the pool assets (XRP or token) of the AMM instance. */ - Asset: Issue + Asset: IssuedCurrency /** * Specifies the other pool asset of the AMM instance. */ - Asset2: Issue + Asset2: IssuedCurrency /** * This field represents the minimum price that the bidder wants to pay for the slot. diff --git a/packages/xrpl/src/models/transactions/AMMDeposit.ts b/packages/xrpl/src/models/transactions/AMMDeposit.ts index 0770d338..9177b12f 100644 --- a/packages/xrpl/src/models/transactions/AMMDeposit.ts +++ b/packages/xrpl/src/models/transactions/AMMDeposit.ts @@ -1,6 +1,6 @@ /* eslint-disable complexity -- required for validateAMMDeposit */ import { ValidationError } from '../../errors' -import { Amount, Issue, IssuedCurrencyAmount } from '../common' +import { Amount, IssuedCurrency, IssuedCurrencyAmount } from '../common' import { BaseTransaction, @@ -49,12 +49,12 @@ export interface AMMDeposit extends BaseTransaction { /** * Specifies one of the pool assets (XRP or token) of the AMM instance. */ - Asset: Issue + Asset: IssuedCurrency /** * Specifies the other pool asset of the AMM instance. */ - Asset2: Issue + Asset2: IssuedCurrency /** * Specifies the amount of shares of the AMM instance pools that the trader diff --git a/packages/xrpl/src/models/transactions/AMMVote.ts b/packages/xrpl/src/models/transactions/AMMVote.ts index ce33acfe..cd19c736 100644 --- a/packages/xrpl/src/models/transactions/AMMVote.ts +++ b/packages/xrpl/src/models/transactions/AMMVote.ts @@ -1,5 +1,5 @@ import { ValidationError } from '../../errors' -import { Issue } from '../common' +import { IssuedCurrency } from '../common' import { AMM_MAX_TRADING_FEE } from './AMMCreate' import { BaseTransaction, isIssue, validateBaseTransaction } from './common' @@ -16,12 +16,12 @@ export interface AMMVote extends BaseTransaction { /** * Specifies one of the pool assets (XRP or token) of the AMM instance. */ - Asset: Issue + Asset: IssuedCurrency /** * Specifies the other pool asset of the AMM instance. */ - Asset2: Issue + Asset2: IssuedCurrency /** * Specifies the fee, in basis point. diff --git a/packages/xrpl/src/models/transactions/AMMWithdraw.ts b/packages/xrpl/src/models/transactions/AMMWithdraw.ts index 1d8a9aa9..4a64431a 100644 --- a/packages/xrpl/src/models/transactions/AMMWithdraw.ts +++ b/packages/xrpl/src/models/transactions/AMMWithdraw.ts @@ -1,6 +1,6 @@ /* eslint-disable complexity -- required for validateAMMWithdraw */ import { ValidationError } from '../../errors' -import { Amount, Issue, IssuedCurrencyAmount } from '../common' +import { Amount, IssuedCurrency, IssuedCurrencyAmount } from '../common' import { BaseTransaction, @@ -54,12 +54,12 @@ export interface AMMWithdraw extends BaseTransaction { /** * Specifies one of the pool assets (XRP or token) of the AMM instance. */ - Asset: Issue + Asset: IssuedCurrency /** * Specifies the other pool asset of the AMM instance. */ - Asset2: Issue + Asset2: IssuedCurrency /** * Specifies the amount of shares of the AMM instance pools that the trader diff --git a/packages/xrpl/src/models/transactions/common.ts b/packages/xrpl/src/models/transactions/common.ts index bd330b7b..b685730c 100644 --- a/packages/xrpl/src/models/transactions/common.ts +++ b/packages/xrpl/src/models/transactions/common.ts @@ -4,7 +4,13 @@ import { TRANSACTION_TYPES } from 'ripple-binary-codec' import { ValidationError } from '../../errors' -import { Amount, Issue, IssuedCurrencyAmount, Memo, Signer } from '../common' +import { + Amount, + IssuedCurrency, + IssuedCurrencyAmount, + Memo, + Signer, +} from '../common' import { onlyHasFields } from '../utils' const MEMO_SIZE = 3 @@ -50,17 +56,36 @@ function isSigner(obj: unknown): boolean { ) } +const XRP_CURRENCY_SIZE = 1 +const ISSUE_SIZE = 2 const ISSUED_CURRENCY_SIZE = 3 function isRecord(value: unknown): value is Record { return value !== null && typeof value === 'object' } +/** + * Verify the form and type of an IssuedCurrency at runtime. + * + * @param input - The input to check the form and type of. + * @returns Whether the IssuedCurrency is properly formed. + */ +export function isIssue(input: unknown): input is IssuedCurrency { + return ( + isRecord(input) && + ((Object.keys(input).length === ISSUE_SIZE && + typeof input.issuer === 'string' && + typeof input.currency === 'string') || + (Object.keys(input).length === XRP_CURRENCY_SIZE && + input.currency === 'XRP')) + ) +} + /** * Verify the form and type of an IssuedCurrencyAmount at runtime. * * @param input - The input to check the form and type of. - * @returns Whether the IssuedCurrencyAmount is malformed. + * @returns Whether the IssuedCurrencyAmount is properly formed. */ export function isIssuedCurrency( input: unknown, @@ -78,31 +103,12 @@ export function isIssuedCurrency( * Verify the form and type of an Amount at runtime. * * @param amount - The object to check the form and type of. - * @returns Whether the Amount is malformed. + * @returns Whether the Amount is properly formed. */ export function isAmount(amount: unknown): amount is Amount { return typeof amount === 'string' || isIssuedCurrency(amount) } -/** - * Verify the form and type of an Issue at runtime. - * - * @param input - The object to check the form and type of. - * @returns Whether the Issue is malformed. - */ -export function isIssue(input: unknown): input is Issue { - if (!isRecord(input)) { - return false - } - const length = Object.keys(input).length - return ( - (length === 1 && input.currency === 'XRP') || - (length === 2 && - typeof input.currency === 'string' && - typeof input.issuer === 'string') - ) -} - // eslint-disable-next-line @typescript-eslint/no-empty-interface -- no global flags right now, so this is fine export interface GlobalFlags {}