/
findNftByMint.ts
182 lines (165 loc) · 4.57 KB
/
findNftByMint.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { PublicKey } from '@solana/web3.js';
import {
toMint,
toMintAccount,
toToken,
toTokenAccount,
} from '../../tokenModule';
import {
parseOriginalOrPrintEditionAccount,
toMetadataAccount,
} from '../accounts';
import {
JsonMetadata,
Nft,
NftWithToken,
Sft,
SftWithToken,
toMetadata,
toNft,
toNftEdition,
toNftWithToken,
toSft,
toSftWithToken,
} from '../models';
import {
Operation,
OperationHandler,
OperationScope,
useOperation,
} from '@/types';
import { Metaplex } from '@/Metaplex';
// -----------------
// Operation
// -----------------
const Key = 'FindNftByMintOperation' as const;
/**
* Finds an NFT or an SFT by its mint address.
*
* ```ts
* const nft = await metaplex
* .nfts()
* .findByMint({ mintAddress };
* ```
*
* @group Operations
* @category Constructors
*/
export const findNftByMintOperation = useOperation<FindNftByMintOperation>(Key);
/**
* @group Operations
* @category Types
*/
export type FindNftByMintOperation = Operation<
typeof Key,
FindNftByMintInput,
FindNftByMintOutput
>;
/**
* @group Operations
* @category Inputs
*/
export type FindNftByMintInput = {
/** The address of the mint account. */
mintAddress: PublicKey;
/**
* The explicit token account to fetch with the NFT or SFT.
*
* If provided, and if that address is valid, the NFT or SFT returned
* will be of the type `NftWithToken` or `SftWithToken` respectively.
*
* Alternatively, you may use the `tokenOwner` parameter to fetch the
* associated token account.
*
* @defaultValue Defaults to not fetching the token account.
*/
tokenAddress?: PublicKey;
/**
* The associated token account to fetch with the NFT or SFT.
*
* If provided, and if that account exists, the NFT or SFT returned
* will be of the type `NftWithToken` or `SftWithToken` respectively.
*
* Alternatively, you may use the `tokenAddress` parameter to fetch the
* token account at an explicit address.
*
* @defaultValue Defaults to not fetching the associated token account.
*/
tokenOwner?: PublicKey;
/**
* Whether or not we should fetch the JSON Metadata for the NFT or SFT.
*
* @defaultValue `true`
*/
loadJsonMetadata?: boolean;
};
/**
* @group Operations
* @category Outputs
*/
export type FindNftByMintOutput = Nft | Sft | NftWithToken | SftWithToken;
/**
* @group Operations
* @category Handlers
*/
export const findNftByMintOperationHandler: OperationHandler<FindNftByMintOperation> =
{
handle: async (
operation: FindNftByMintOperation,
metaplex: Metaplex,
scope: OperationScope
): Promise<FindNftByMintOutput> => {
const { programs, commitment } = scope;
const {
mintAddress,
tokenAddress,
tokenOwner,
loadJsonMetadata = true,
} = operation.input;
const associatedTokenAddress = tokenOwner
? metaplex.tokens().pdas().associatedTokenAccount({
mint: mintAddress,
owner: tokenOwner,
programs,
})
: undefined;
const nftPdas = metaplex.nfts().pdas();
const accountAddresses = [
mintAddress,
nftPdas.metadata({ mint: mintAddress, programs }),
nftPdas.masterEdition({ mint: mintAddress, programs }),
tokenAddress ?? associatedTokenAddress,
].filter((address): address is PublicKey => !!address);
const accounts = await metaplex
.rpc()
.getMultipleAccounts(accountAddresses, commitment);
scope.throwIfCanceled();
const mint = toMint(toMintAccount(accounts[0]));
let metadata = toMetadata(toMetadataAccount(accounts[1]));
const editionAccount = parseOriginalOrPrintEditionAccount(accounts[2]);
const token = accounts[3] ? toToken(toTokenAccount(accounts[3])) : null;
if (loadJsonMetadata) {
try {
const json = await metaplex
.storage()
.downloadJson<JsonMetadata>(metadata.uri, scope);
metadata = { ...metadata, jsonLoaded: true, json };
} catch (error) {
metadata = { ...metadata, jsonLoaded: true, json: null };
}
}
const isNft =
editionAccount.exists &&
mint.mintAuthorityAddress &&
mint.mintAuthorityAddress.equals(editionAccount.publicKey);
if (isNft) {
const edition = toNftEdition(editionAccount);
return token
? toNftWithToken(metadata, mint, edition, token)
: toNft(metadata, mint, edition);
}
return token
? toSftWithToken(metadata, mint, token)
: toSft(metadata, mint);
},
};