replace Issue with IssuedCurrency

This commit is contained in:
Omar Khan
2023-02-15 23:13:05 -05:00
parent d2f7fe622b
commit df75b4a908
7 changed files with 44 additions and 40 deletions

View File

@@ -4,15 +4,13 @@ interface XRP {
currency: 'XRP' currency: 'XRP'
} }
interface IssuedCurrency { export interface IssuedCurrency {
currency: string currency: string
issuer: string issuer: string
} }
export type Currency = IssuedCurrency | XRP export type Currency = IssuedCurrency | XRP
export type Issue = Currency
export interface IssuedCurrencyAmount extends IssuedCurrency { export interface IssuedCurrencyAmount extends IssuedCurrency {
value: string value: string
} }

View File

@@ -1,4 +1,4 @@
import { Amount, Issue, IssuedCurrencyAmount } from '../common' import { Amount, IssuedCurrency, IssuedCurrencyAmount } from '../common'
import { BaseRequest, BaseResponse } from './baseMethod' 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. * 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. * 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. * Specifies the other pool asset of the AMM instance.
* Both asset and asset2 must be defined to specify an AMM instance. * Both asset and asset2 must be defined to specify an AMM instance.
*/ */
asset2?: Issue asset2?: IssuedCurrency
} }
interface AuthAccount { interface AuthAccount {

View File

@@ -1,6 +1,6 @@
/* eslint-disable complexity -- required for validateAMMBid */ /* eslint-disable complexity -- required for validateAMMBid */
import { ValidationError } from '../../errors' import { ValidationError } from '../../errors'
import { Amount, Issue } from '../common' import { Amount, IssuedCurrency } from '../common'
import { import {
BaseTransaction, BaseTransaction,
@@ -29,12 +29,12 @@ export interface AMMBid extends BaseTransaction {
/** /**
* Specifies one of the pool assets (XRP or token) of the AMM instance. * 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. * 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. * This field represents the minimum price that the bidder wants to pay for the slot.

View File

@@ -1,6 +1,6 @@
/* eslint-disable complexity -- required for validateAMMDeposit */ /* eslint-disable complexity -- required for validateAMMDeposit */
import { ValidationError } from '../../errors' import { ValidationError } from '../../errors'
import { Amount, Issue, IssuedCurrencyAmount } from '../common' import { Amount, IssuedCurrency, IssuedCurrencyAmount } from '../common'
import { import {
BaseTransaction, BaseTransaction,
@@ -49,12 +49,12 @@ export interface AMMDeposit extends BaseTransaction {
/** /**
* Specifies one of the pool assets (XRP or token) of the AMM instance. * 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. * 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 * Specifies the amount of shares of the AMM instance pools that the trader

View File

@@ -1,5 +1,5 @@
import { ValidationError } from '../../errors' import { ValidationError } from '../../errors'
import { Issue } from '../common' import { IssuedCurrency } from '../common'
import { AMM_MAX_TRADING_FEE } from './AMMCreate' import { AMM_MAX_TRADING_FEE } from './AMMCreate'
import { BaseTransaction, isIssue, validateBaseTransaction } from './common' 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. * 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. * Specifies the other pool asset of the AMM instance.
*/ */
Asset2: Issue Asset2: IssuedCurrency
/** /**
* Specifies the fee, in basis point. * Specifies the fee, in basis point.

View File

@@ -1,6 +1,6 @@
/* eslint-disable complexity -- required for validateAMMWithdraw */ /* eslint-disable complexity -- required for validateAMMWithdraw */
import { ValidationError } from '../../errors' import { ValidationError } from '../../errors'
import { Amount, Issue, IssuedCurrencyAmount } from '../common' import { Amount, IssuedCurrency, IssuedCurrencyAmount } from '../common'
import { import {
BaseTransaction, BaseTransaction,
@@ -54,12 +54,12 @@ export interface AMMWithdraw extends BaseTransaction {
/** /**
* Specifies one of the pool assets (XRP or token) of the AMM instance. * 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. * 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 * Specifies the amount of shares of the AMM instance pools that the trader

View File

@@ -4,7 +4,13 @@
import { TRANSACTION_TYPES } from 'ripple-binary-codec' import { TRANSACTION_TYPES } from 'ripple-binary-codec'
import { ValidationError } from '../../errors' import { ValidationError } from '../../errors'
import { Amount, Issue, IssuedCurrencyAmount, Memo, Signer } from '../common' import {
Amount,
IssuedCurrency,
IssuedCurrencyAmount,
Memo,
Signer,
} from '../common'
import { onlyHasFields } from '../utils' import { onlyHasFields } from '../utils'
const MEMO_SIZE = 3 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 const ISSUED_CURRENCY_SIZE = 3
function isRecord(value: unknown): value is Record<string, unknown> { function isRecord(value: unknown): value is Record<string, unknown> {
return value !== null && typeof value === 'object' 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. * Verify the form and type of an IssuedCurrencyAmount at runtime.
* *
* @param input - The input to check the form and type of. * @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( export function isIssuedCurrency(
input: unknown, input: unknown,
@@ -78,31 +103,12 @@ export function isIssuedCurrency(
* Verify the form and type of an Amount at runtime. * Verify the form and type of an Amount at runtime.
* *
* @param amount - The object to check the form and type of. * @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 { export function isAmount(amount: unknown): amount is Amount {
return typeof amount === 'string' || isIssuedCurrency(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 // eslint-disable-next-line @typescript-eslint/no-empty-interface -- no global flags right now, so this is fine
export interface GlobalFlags {} export interface GlobalFlags {}