-
Notifications
You must be signed in to change notification settings - Fork 8
/
mint-collection.ts
145 lines (129 loc) · 4.3 KB
/
mint-collection.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
import { createSetCollectionSizeInstruction } from '@metaplex-foundation/mpl-token-metadata';
import { debugLog, Result, Try, unixTimestamp } from '~/suite-utils';
import { Converter } from '~/converter';
import { Account } from '~/account';
import { Storage } from '~/suite-storage';
import { Validator } from '~/validator';
import { TransactionBuilder } from '~/transaction-builder';
import { InputNftMetadata } from '~/types/regular-nft';
import { Secret } from '~/types/account';
import { RegularNft as Mint } from './mint';
import { MintCollectionOptions } from '~/types/regular-nft';
import { MintStructure } from '~/types/transaction-builder';
/**
* create a collection
* This function needs only 1 call
*
* @param {Secret} owner
* @param {InputNftMetadata} input
* @param {Partial<MintCollectionOptions>} options
* @return Promise<Result<MintStructure, Error>>
*/
export namespace RegularNft {
const DEFAULT_COLLECTION_SIZE = 0;
const DEFAULT_STORAGE_TYPE = 'nftStorage';
export const mintCollection = (
owner: Secret,
input: InputNftMetadata,
options: Partial<MintCollectionOptions> = {},
): Promise<Result<MintStructure, Error>> => {
return Try(async () => {
const valid = Validator.checkAll<InputNftMetadata>(input);
if (valid.isErr) {
throw valid.error;
}
const { freezeAuthority, feePayer, collectionSize } = options;
const payer = feePayer ? feePayer : owner;
const storageType = input.storageType || DEFAULT_STORAGE_TYPE;
const ownerPublicKey = owner.toKeypair().publicKey;
//--- porperties, Upload content ---
let properties;
if (input.properties) {
properties = await Converter.Properties.intoInfra(
input.properties,
Storage.uploadFile,
storageType,
{ feePayer: payer },
);
}
input = {
...input,
properties,
};
//--- porperties, Upload content ---
const storageMetadata = Storage.toConvertOffchaindata(input, 0);
// created at by unix timestamp
storageMetadata.created_at = unixTimestamp();
let uri!: string;
if (input.filePath && input.storageType) {
const uploaded = await Storage.upload(
storageMetadata,
input.filePath,
storageType,
{ feePayer: payer },
);
debugLog('# upload content url: ', uploaded);
if (uploaded.isErr) {
throw uploaded;
}
uri = uploaded.value;
} else if (input.uri) {
const image = { image: input.uri };
const uploaded = await Storage.uploadData(
{ ...storageMetadata, ...image },
storageType,
{ feePayer: payer },
);
if (uploaded.isErr) {
throw uploaded;
}
uri = uploaded.value;
} else {
throw Error(`Must set filePath' or 'uri'`);
}
const datav2 = Converter.RegularNftMetadata.intoInfra(input, uri, 0);
const isMutable = input.isMutable === undefined ? true : input.isMutable;
debugLog('# input: ', input);
debugLog('# datav2: ', datav2);
const collectionMint = Account.Keypair.create();
const collectionMetadataAccount = Account.Pda.getMetadata(
collectionMint.pubkey,
);
const instructions = await Mint.createMint(
collectionMint.toPublicKey(),
ownerPublicKey,
datav2,
payer.toKeypair().publicKey,
isMutable,
);
// freezeAuthority
if (freezeAuthority) {
instructions.push(
Mint.createDeleagate(
collectionMint.toPublicKey(),
ownerPublicKey,
freezeAuthority.toPublicKey(),
),
);
}
const collections = {
collectionMetadata: collectionMetadataAccount,
collectionAuthority: owner.toKeypair().publicKey,
collectionMint: collectionMint.toKeypair().publicKey,
};
instructions.push(
createSetCollectionSizeInstruction(collections, {
setCollectionSizeArgs: {
size: collectionSize || DEFAULT_COLLECTION_SIZE,
},
}),
);
return new TransactionBuilder.Mint(
instructions,
[owner.toKeypair(), collectionMint.toKeypair()],
payer.toKeypair(),
collectionMint.pubkey,
);
});
};
}