Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ledger entry types for NFTokens #2349

Merged
merged 5 commits into from
Jun 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/xrpl/src/models/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export interface NFTOffer {
}

/**
* One NFToken that might be returned from either an {@link NFTInfoResponse}
* One NFToken that might be returned from a {@link NFTInfoResponse}
ckniffen marked this conversation as resolved.
Show resolved Hide resolved
*
* @category Responses
*/
Expand Down
16 changes: 16 additions & 0 deletions packages/xrpl/src/models/ledger/NFTokenOffer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Amount } from '../common'

import BaseLedgerEntry from './BaseLedgerEntry'

export interface NFTokenOffer extends BaseLedgerEntry {
LedgerEntryType: 'NFTokenOffer'
Amount: Amount
Destination?: string
Expiration: number
Flags: number
NFTokenOfferNode?: string
Owner: string
OwnerNode?: string
PreviousTxnID: string
PreviousTxnLgrSeq: number
}
30 changes: 30 additions & 0 deletions packages/xrpl/src/models/ledger/NFTokenPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ModifiedNode, Node } from '../transactions/metadata'

import BaseLedgerEntry from './BaseLedgerEntry'

export interface NFToken {
Flags: number
Issuer: string
NFTokenID: string
NFTokenTaxon: number
URI?: string
}

export interface NFTokenWrapper {
NFToken: NFToken
}

export interface NFTokenPage extends BaseLedgerEntry {
LedgerEntryType: 'NFTokenPage'
NextPageMin?: string
NFTokens: NFTokenWrapper[]
PreviousPageMin?: string
PreviousTxnID?: string
PreviousTxnLgrSeq?: number
}

export function isNFTokenPage(
ledgerEntry: BaseLedgerEntry,
): ledgerEntry is NFTokenPage {
return Object.prototype.hasOwnProperty.call(ledgerEntry, `NFTokens`)
}
5 changes: 5 additions & 0 deletions packages/xrpl/src/models/ledger/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import Ledger from './Ledger'
import LedgerEntry from './LedgerEntry'
import LedgerHashes from './LedgerHashes'
import NegativeUNL from './NegativeUNL'
import { NFTokenOffer } from './NFTokenOffer'
import { NFToken, NFTokenPage } from './NFTokenPage'
import Offer, { OfferFlags } from './Offer'
import PayChannel from './PayChannel'
import RippleState, { RippleStateFlags } from './RippleState'
Expand All @@ -32,6 +34,9 @@ export {
LedgerEntry,
LedgerHashes,
NegativeUNL,
NFTokenOffer,
NFTokenPage,
NFToken,
Offer,
OfferFlags,
PayChannel,
Expand Down
3 changes: 1 addition & 2 deletions packages/xrpl/src/models/methods/nftInfo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import { LedgerIndex, NFToken } from '../common'
import { BaseRequest, BaseResponse } from './baseMethod'

/**
* The `nft_info` method retrieves information about NFToken
* NFToken.
* The `nft_info` method retrieves information about a NFToken.
ckniffen marked this conversation as resolved.
Show resolved Hide resolved
*
* @category Requests
*/
Expand Down
14 changes: 4 additions & 10 deletions packages/xrpl/src/utils/getNFTokenID.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import flatMap from 'lodash/flatMap'

import { NFTokenWrapper } from '../models/ledger/NFTokenPage'
import {
CreatedNode,
isCreatedNode,
Expand All @@ -8,13 +9,6 @@ import {
TransactionMetadata,
} from '../models/transactions/metadata'

interface NFToken {
NFToken: {
NFTokenID: string
URI: string
}
}

/**
* Gets the NFTokenID for an NFT recently minted with NFTokenMint.
*
Expand Down Expand Up @@ -62,7 +56,7 @@ export default function getNFTokenID(
const previousTokenIDSet = new Set(
flatMap(affectedNodes, (node) => {
const nftokens = isModifiedNode(node)
? (node.ModifiedNode.PreviousFields?.NFTokens as NFToken[])
? (node.ModifiedNode.PreviousFields?.NFTokens as NFTokenWrapper[])
: []
return nftokens.map((token) => token.NFToken.NFTokenID)
}).filter((id) => Boolean(id)),
Expand All @@ -72,8 +66,8 @@ export default function getNFTokenID(
const finalTokenIDs = flatMap(affectedNodes, (node) =>
(
(((node as ModifiedNode).ModifiedNode?.FinalFields?.NFTokens ??
(node as CreatedNode).CreatedNode?.NewFields?.NFTokens) as NFToken[]) ??
[]
(node as CreatedNode).CreatedNode?.NewFields
?.NFTokens) as NFTokenWrapper[]) ?? []
).map((token) => token.NFToken.NFTokenID),
).filter((nftokenID) => Boolean(nftokenID))
/* eslint-enable @typescript-eslint/consistent-type-assertions -- Necessary for parsing metadata */
Expand Down