/
migrateToSizedCollectionNft.ts
187 lines (171 loc) · 4.97 KB
/
migrateToSizedCollectionNft.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
183
184
185
186
187
import { createSetCollectionSizeInstruction } from '@metaplex-foundation/mpl-token-metadata';
import { PublicKey } from '@solana/web3.js';
import { SendAndConfirmTransactionResponse } from '../../rpcModule';
import { Metaplex } from '@/Metaplex';
import {
BigNumber,
Operation,
OperationHandler,
OperationScope,
Signer,
useOperation,
} from '@/types';
import { TransactionBuilder, TransactionBuilderOptions } from '@/utils';
// -----------------
// Operation
// -----------------
const Key = 'MigrateToSizedCollectionNftOperation' as const;
/**
* Migrates a legacy Collection NFT to a sized Collection NFT.
* Both can act as a Collection for NFTs but only the latter
* keeps track of the size of the collection on chain.
*
* ```ts
* await metaplex
* .nfts()
* .migrateToSizedCollection({ mintAddress, size: toBigNumber(10000) };
* ```
*
* @group Operations
* @category Constructors
*/
export const migrateToSizedCollectionNftOperation =
useOperation<MigrateToSizedCollectionNftOperation>(Key);
/**
* @group Operations
* @category Types
*/
export type MigrateToSizedCollectionNftOperation = Operation<
typeof Key,
MigrateToSizedCollectionNftInput,
MigrateToSizedCollectionNftOutput
>;
/**
* @group Operations
* @category Inputs
*/
export type MigrateToSizedCollectionNftInput = {
/** The address of the mint account. */
mintAddress: PublicKey;
/**
* An authority that can update the Collection NFT at the
* given mint address. This can either be the update authority
* for that Collection NFT or an approved delegate authority.
*
* @defaultValue `metaplex.identity()`
*/
collectionAuthority?: Signer;
/**
* The current size of all **verified** NFTs and/or SFTs within
* the Collection.
*
* **Warning, once set, this size can no longer be updated.**
*/
size: BigNumber;
/**
* Whether or not the provided `collectionAuthority` is a delegated
* collection authority, i.e. it was approved by the update authority
* using `metaplex.nfts().approveCollectionAuthority()`.
*
* @defaultValue `false`
*/
isDelegated?: boolean;
};
/**
* @group Operations
* @category Outputs
*/
export type MigrateToSizedCollectionNftOutput = {
/** The blockchain response from sending and confirming the transaction. */
response: SendAndConfirmTransactionResponse;
};
/**
* @group Operations
* @category Handlers
*/
export const migrateToSizedCollectionNftOperationHandler: OperationHandler<MigrateToSizedCollectionNftOperation> =
{
handle: async (
operation: MigrateToSizedCollectionNftOperation,
metaplex: Metaplex,
scope: OperationScope
): Promise<MigrateToSizedCollectionNftOutput> => {
return migrateToSizedCollectionNftBuilder(
metaplex,
operation.input,
scope
).sendAndConfirm(metaplex, scope.confirmOptions);
},
};
// -----------------
// Builder
// -----------------
/**
* @group Transaction Builders
* @category Inputs
*/
export type MigrateToSizedCollectionNftBuilderParams = Omit<
MigrateToSizedCollectionNftInput,
'confirmOptions'
> & {
/** A key to distinguish the instruction that sets the collection size. */
instructionKey?: string;
};
/**
* Migrates a legacy Collection NFT to a sized Collection NFT.
* Both can act as a Collection for NFTs but only the latter
* keeps track of the size of the collection on chain.
*
* ```ts
* const transactionBuilder = metaplex
* .nfts()
* .builders()
* .migrateToSizedCollection({ mintAddress, size: toBigNumber(10000) });
* ```
*
* @group Transaction Builders
* @category Constructors
*/
export const migrateToSizedCollectionNftBuilder = (
metaplex: Metaplex,
params: MigrateToSizedCollectionNftBuilderParams,
options: TransactionBuilderOptions = {}
): TransactionBuilder => {
const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
const {
mintAddress,
collectionAuthority = metaplex.identity(),
size,
isDelegated = false,
} = params;
const tokenMetadataProgram = metaplex.programs().getTokenMetadata(programs);
const nftPdas = metaplex.nfts().pdas();
return (
TransactionBuilder.make()
.setFeePayer(payer)
// Update the metadata account.
.add({
instruction: createSetCollectionSizeInstruction(
{
collectionMetadata: nftPdas.metadata({
mint: mintAddress,
programs,
}),
collectionAuthority: collectionAuthority.publicKey,
collectionMint: mintAddress,
collectionAuthorityRecord: isDelegated
? nftPdas.collectionAuthorityRecord({
mint: mintAddress,
collectionAuthority: collectionAuthority.publicKey,
programs,
})
: undefined,
},
{ setCollectionSizeArgs: { size } },
tokenMetadataProgram.address
),
signers: [collectionAuthority],
key: params.instructionKey ?? 'setCollectionSize',
})
);
};