Skip to content

Commit

Permalink
Add nftoken_id, nftoken_ids, offer_id to meta fields
Browse files Browse the repository at this point in the history
  • Loading branch information
tequdev committed Aug 29, 2023
1 parent b76d5b2 commit ed06e1a
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 12 deletions.
2 changes: 1 addition & 1 deletion packages/xrpl/src/models/methods/tx.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ export interface TxResponse<T extends BaseTransaction = Transaction>
ledger_index?: number
/** Transaction metadata, which describes the results of the transaction.
* Can be undefined if a transaction has not been validated yet. */
meta?: TransactionMetadata | string
meta?: TransactionMetadata<T> | string
/**
* If true, this data comes from a validated ledger version; if omitted or.
* Set to false, this data is not final.
Expand Down
36 changes: 34 additions & 2 deletions packages/xrpl/src/models/transactions/metadata.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import { Amount } from '../common'

import { BaseTransaction } from './common'
import { NFTokenAcceptOffer } from './NFTokenAcceptOffer'
import { NFTokenCancelOffer } from './NFTokenCancelOffer'
import { NFTokenCreateOffer } from './NFTokenCreateOffer'
import { NFTokenMint } from './NFTokenMint'
import { Payment } from './payment'
import type { Transaction } from './transaction'

export interface CreatedNode {
CreatedNode: {
LedgerEntryType: string
Expand Down Expand Up @@ -59,11 +67,35 @@ export function isDeletedNode(node: Node): node is DeletedNode {
return Object.prototype.hasOwnProperty.call(node, `DeletedNode`)
}

export interface TransactionMetadata {
export type TransactionMetadata<T extends BaseTransaction = Transaction> = {
AffectedNodes: Node[]
DeliveredAmount?: Amount
// "unavailable" possible for transactions before 2014-01-20
delivered_amount?: Amount | 'unavailable'
TransactionIndex: number
TransactionResult: string
}
} & (T extends Payment
? {
delivered_amount?: Amount | 'unavailable'
}
: T extends NFTokenMint
? {
// rippled 1.11.0 or later
nftoken_id?: string
}
: T extends NFTokenCreateOffer
? {
// rippled 1.11.0 or later
offer_id?: string
}
: T extends NFTokenAcceptOffer
? {
// rippled 1.11.0 or later
nftoken_id?: string
}
: T extends NFTokenCancelOffer
? {
// rippled 1.11.0 or later
nftoken_ids?: string[]
}
: Record<string, never>)
10 changes: 6 additions & 4 deletions packages/xrpl/src/models/transactions/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { CheckCancel, validateCheckCancel } from './checkCancel'
import { CheckCash, validateCheckCash } from './checkCash'
import { CheckCreate, validateCheckCreate } from './checkCreate'
import { Clawback, validateClawback } from './clawback'
import { isIssuedCurrency } from './common'
import { BaseTransaction, isIssuedCurrency } from './common'
import { DepositPreauth, validateDepositPreauth } from './depositPreauth'
import { EscrowCancel, validateEscrowCancel } from './escrowCancel'
import { EscrowCreate, validateEscrowCreate } from './escrowCreate'
Expand Down Expand Up @@ -97,9 +97,11 @@ export type Transaction =
/**
* @category Transaction Models
*/
export interface TransactionAndMetadata {
transaction: Transaction
metadata: TransactionMetadata
export interface TransactionAndMetadata<
T extends BaseTransaction = Transaction,
> {
transaction: T
metadata: TransactionMetadata<T>
}

/**
Expand Down
2 changes: 1 addition & 1 deletion packages/xrpl/test/integration/requests/accountTx.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ describe('account_tx', function () {
assert.equal(response.type, expected.type)
assert.equal(response.result.account, expected.result.account)
assert.equal(
(response.result.transactions[0].meta as TransactionMetadata)
(response.result.transactions[0].meta as TransactionMetadata<Payment>)
.TransactionResult,
'tesSUCCESS',
)
Expand Down
14 changes: 10 additions & 4 deletions packages/xrpl/test/integration/transactions/nftokenMint.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { assert } from 'chai'
import { TransactionMetadata, TxRequest } from 'xrpl'

import { convertStringToHex, getNFTokenID, NFTokenMint } from '../../../src'
import {
convertStringToHex,
getNFTokenID,
NFTokenMint,
TransactionMetadata,
TxRequest,
} from '../../../src'
import { hashSignedTx } from '../../../src/utils/hashes'
import serverUrl from '../serverUrl'
import {
Expand Down Expand Up @@ -55,8 +60,9 @@ describe('NFTokenMint', function () {
})

const nftokenID =
getNFTokenID(txResponse.result.meta as TransactionMetadata) ??
'undefined'
getNFTokenID(
txResponse.result.meta as TransactionMetadata<NFTokenMint>,
) ?? 'undefined'

const accountHasNFT = accountNFTs.result.account_nfts.some(
(value) => value.NFTokenID === nftokenID,
Expand Down

0 comments on commit ed06e1a

Please sign in to comment.