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 all commits
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: 2 additions & 0 deletions packages/xrpl/HISTORY.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ Subscribe to [the **xrpl-announce** mailing list](https://groups.google.com/g/xr
* Add `ledger_hash` and `ledger_index` to `account_nfts`,
`nft_buy_offers`, and `nft_sell_offers` requests.
* Add `nft_page` to `ledger_entry` request.
* Add types for `NFTokenPage` and `NFTokenOffer` LedgerEntries.
* Add type for NFToken object that is stored on a `NFTokenPage`.

## 2.8.0 (2023-06-13)

Expand Down
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 an {@link NFTInfoResponse}
*
* @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
}
22 changes: 22 additions & 0 deletions packages/xrpl/src/models/ledger/NFTokenPage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
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
}
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 { NFToken } from '../common'
import { BaseRequest, BaseResponse, LookupByLedgerRequest } from './baseMethod'

/**
* The `nft_info` method retrieves information about NFToken
* NFToken.
* The `nft_info` method retrieves information about an NFToken.
*
* @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,6 +1,7 @@
import flatMap from 'lodash/flatMap'
import { decode } from 'ripple-binary-codec'

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

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

/**
* Ensures that the metadata is in a deserialized format to parse.
*
Expand Down Expand Up @@ -80,7 +74,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 @@ -90,8 +84,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