Skip to content

Commit

Permalink
rename IssuedCurrency -> Issue to match rippled
Browse files Browse the repository at this point in the history
  • Loading branch information
mvadari committed Nov 9, 2022
1 parent 65bc14f commit 2670298
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
4 changes: 2 additions & 2 deletions packages/ripple-binary-codec/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Currency } from './currency'
import { Hash128 } from './hash-128'
import { Hash160 } from './hash-160'
import { Hash256 } from './hash-256'
import { IssuedCurrency } from './issued-currency'
import { Issue } from './issue'
import { PathSet } from './path-set'
import { STArray } from './st-array'
import { STObject } from './st-object'
Expand All @@ -31,7 +31,7 @@ const coreTypes = {
Hash128,
Hash160,
Hash256,
IssuedCurrency,
Issue,
PathSet,
STArray,
STObject,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,27 @@ import { Buffer } from 'buffer/'
/**
* Interface for JSON objects that represent amounts
*/
interface IssuedCurrencyObject extends JsonObject {
interface IssueObject extends JsonObject {
currency: string
issuer: string
}

/**
* Type guard for AmountObject
*/
function isIssuedCurrencyObject(arg): arg is IssuedCurrencyObject {
function isIssueObject(arg): arg is IssueObject {
const keys = Object.keys(arg).sort()
return keys.length === 2 && keys[0] === 'currency' && keys[1] === 'issuer'
}

/**
* Class for serializing/Deserializing Amounts
*/
class IssuedCurrency extends SerializedType {
static readonly ZERO_ISSUED_CURRENCY: IssuedCurrency = new IssuedCurrency(
Buffer.alloc(20),
)
class Issue extends SerializedType {
static readonly ZERO_ISSUED_CURRENCY: Issue = new Issue(Buffer.alloc(20))

constructor(bytes: Buffer) {
super(bytes ?? IssuedCurrency.ZERO_ISSUED_CURRENCY.bytes)
super(bytes ?? Issue.ZERO_ISSUED_CURRENCY.bytes)
}

/**
Expand All @@ -40,25 +38,23 @@ class IssuedCurrency extends SerializedType {
* representing an integer amount
* @returns An Amount object
*/
static from<T extends IssuedCurrency | IssuedCurrencyObject | string>(
value: T,
): IssuedCurrency {
if (value instanceof IssuedCurrency) {
static from<T extends Issue | IssueObject | string>(value: T): Issue {
if (value instanceof Issue) {
return value
}

if (typeof value === 'string') {
IssuedCurrency.assertXrpIsValid(value)
Issue.assertXrpIsValid(value)

const currency = Currency.from(value).toBytes()

return new IssuedCurrency(currency)
return new Issue(currency)
}

if (isIssuedCurrencyObject(value)) {
if (isIssueObject(value)) {
const currency = Currency.from(value.currency).toBytes()
const issuer = AccountID.from(value.issuer).toBytes()
return new IssuedCurrency(Buffer.concat([currency, issuer]))
return new Issue(Buffer.concat([currency, issuer]))
}

throw new Error('Invalid type to construct an Amount')
Expand All @@ -70,21 +66,21 @@ class IssuedCurrency extends SerializedType {
* @param parser BinaryParser to read the Amount from
* @returns An Amount object
*/
static fromParser(parser: BinaryParser): IssuedCurrency {
static fromParser(parser: BinaryParser): Issue {
const currency = parser.read(20)
if (new Currency(currency).toJSON() === 'XRP') {
return new IssuedCurrency(currency)
return new Issue(currency)
}
const currencyAndIssuer = [currency, parser.read(20)]
return new IssuedCurrency(Buffer.concat(currencyAndIssuer))
return new Issue(Buffer.concat(currencyAndIssuer))
}

/**
* Get the JSON representation of this Amount
*
* @returns the JSON interpretation of this.bytes
*/
toJSON(): IssuedCurrencyObject | string {
toJSON(): IssueObject | string {
const parser = new BinaryParser(this.toString())
const currency = Currency.fromParser(parser) as Currency
if (currency.toJSON() === 'XRP') {
Expand All @@ -111,4 +107,4 @@ class IssuedCurrency extends SerializedType {
}
}

export { IssuedCurrency, IssuedCurrencyObject }
export { Issue, IssueObject }
10 changes: 5 additions & 5 deletions packages/ripple-binary-codec/src/types/xchain-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ import { BinaryParser } from '../serdes/binary-parser'
import { AccountID } from './account-id'
import { JsonObject, SerializedType } from './serialized-type'
import { Buffer } from 'buffer/'
import { IssuedCurrency, IssuedCurrencyObject } from './issued-currency'
import { Issue, IssueObject } from './issue'

/**
* Interface for JSON objects that represent cross-chain bridges
*/
interface XChainBridgeObject extends JsonObject {
LockingChainDoor: string
LockingChainIssue: IssuedCurrencyObject | string
LockingChainIssue: IssueObject | string
IssuingChainDoor: string
IssuingChainIssue: IssuedCurrencyObject | string
IssuingChainIssue: IssueObject | string
}

/**
Expand Down Expand Up @@ -45,9 +45,9 @@ class XChainBridge extends SerializedType {
static readonly TYPE_ORDER: { name: string; type: typeof SerializedType }[] =
[
{ name: 'LockingChainDoor', type: AccountID },
{ name: 'LockingChainIssue', type: IssuedCurrency },
{ name: 'LockingChainIssue', type: Issue },
{ name: 'IssuingChainDoor', type: AccountID },
{ name: 'IssuingChainIssue', type: IssuedCurrency },
{ name: 'IssuingChainIssue', type: Issue },
]

constructor(bytes: Buffer) {
Expand Down

0 comments on commit 2670298

Please sign in to comment.