-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
332 lines (308 loc) · 8.09 KB
/
utils.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { AnchorProvider } from "@coral-xyz/anchor";
import {
Connection,
PublicKey,
VersionedTransaction,
TransactionMessage,
TransactionInstruction,
Signer,
Commitment,
SystemProgram,
} from "@solana/web3.js";
import {
TYPE_SIZE,
LENGTH_SIZE,
getMintLen,
ExtensionType,
createInitializeMetadataPointerInstruction,
TOKEN_2022_PROGRAM_ID,
createInitializeMint2Instruction,
createMintToCheckedInstruction,
createAssociatedTokenAccountInstruction,
getMinimumBalanceForRentExemptMint,
TOKEN_PROGRAM_ID,
} from "@solana/spl-token";
import {
TokenMetadata,
pack,
createInitializeInstruction,
} from "@solana/spl-token-metadata";
import { ASSOCIATED_PROGRAM_ID } from "@coral-xyz/anchor/dist/cjs/utils/token";
import { faker } from "@faker-js/faker";
export const MANAGER_SEED = Buffer.from("manager");
export const GROUP_ACCOUNT_SEED = Buffer.from("group");
export const MEMBER_ACCOUNT_SEED = Buffer.from("member");
export const MARKETPLACE = Buffer.from("marketplace");
export const SALE = Buffer.from("sale");
export const LISTING = Buffer.from("listing");
export const getExtraMetasAccountPda = (
mint: PublicKey,
programId: PublicKey,
) => {
const [extraMetasAccount] = PublicKey.findProgramAddressSync(
[Buffer.from("extra-account-metas"), mint.toBuffer()],
programId,
);
return extraMetasAccount;
};
export const getApproveAccountPda = (mint: PublicKey, programId: PublicKey) => {
const [approveAccount] = PublicKey.findProgramAddressSync(
[Buffer.from("approve-account"), mint.toBuffer()],
programId,
);
return approveAccount;
};
export const getManagerAccountPda = (programId: PublicKey) => {
const [managerAccount] = PublicKey.findProgramAddressSync(
[Buffer.from("manager")],
programId,
);
return managerAccount;
};
export const getGroupAccountPda = (mint: PublicKey, programId: PublicKey) => {
const [groupAccount] = PublicKey.findProgramAddressSync(
[Buffer.from("group"), mint.toBuffer()],
programId,
);
return groupAccount;
};
export const getMemberAccountPda = (mint: PublicKey, programId: PublicKey) => {
const [memberAccount] = PublicKey.findProgramAddressSync(
[Buffer.from("member"), mint.toBuffer()],
programId,
);
return memberAccount;
};
export const getDistributionAccountPda = (
group: PublicKey,
paymentMint: PublicKey,
programId: PublicKey,
) => {
const [distributionAccount] = PublicKey.findProgramAddressSync(
[group.toBuffer(), paymentMint.toBuffer()],
programId,
);
return distributionAccount;
};
export const getListingAccountPda = (
seller: PublicKey,
mint: PublicKey,
programId: PublicKey,
) => {
const [listingAccount] = PublicKey.findProgramAddressSync(
[MARKETPLACE, LISTING, seller.toBuffer(), mint.toBuffer()],
programId,
);
return listingAccount;
};
export async function airdrop(
connection: Connection,
address: PublicKey,
airdropLamports: number,
commitment: Commitment = "confirmed",
) {
const signature = await connection.requestAirdrop(address, airdropLamports);
const { blockhash, lastValidBlockHeight } =
await connection.getLatestBlockhash(commitment);
await connection.confirmTransaction(
{
blockhash,
lastValidBlockHeight,
signature,
},
commitment,
);
}
export async function getMinRentForWNSMint(
connection: Connection,
metaData: TokenMetadata,
type: string,
) {
// Size of MetadataExtension 2 bytes for type, 2 bytes for length
const metadataExtension = TYPE_SIZE + LENGTH_SIZE;
// Size of metadata
const metadataLen = pack(metaData).length;
// Size of Mint Account with extensions
const mintLen = getMintLen(
[
ExtensionType.MintCloseAuthority,
ExtensionType.MetadataPointer,
ExtensionType.TransferHook,
ExtensionType.PermanentDelegate,
].concat(
type === "member"
? [ExtensionType.GroupMemberPointer]
: [ExtensionType.GroupPointer],
),
);
// Minimum lamports required for Mint Account
return connection.getMinimumBalanceForRentExemption(
mintLen + metadataExtension + metadataLen,
);
}
export async function createMintTokenKegIx(
connection: Connection,
mint: PublicKey,
authority: PublicKey,
payer: PublicKey,
) {
const space = getMintLen([]);
const rent = await getMinimumBalanceForRentExemptMint(
connection,
"confirmed",
);
return {
ixs: [
SystemProgram.createAccount({
fromPubkey: payer,
newAccountPubkey: mint,
programId: TOKEN_PROGRAM_ID,
space,
lamports: rent,
}),
createInitializeMint2Instruction(mint, 6, authority, authority),
],
};
}
export async function createMint2022Ix(
connection: Connection,
mint: PublicKey,
authority: PublicKey,
payer: PublicKey,
) {
// Size of MetadataExtension 2 bytes for type, 2 bytes for length
const metadataExtension = TYPE_SIZE + LENGTH_SIZE;
const metadata: TokenMetadata = {
mint,
name: faker.finance.currencyName(),
symbol: faker.finance.currencyCode(),
uri: faker.image.urlPicsumPhotos(),
additionalMetadata: [],
updateAuthority: authority,
};
// Size of metadata
const metadataLen = pack(metadata).length;
const mintLen = getMintLen([ExtensionType.MetadataPointer]);
// Minimum lamports required for Mint Account
const mintRent = await connection.getMinimumBalanceForRentExemption(
mintLen + metadataExtension + metadataLen,
);
return {
ixs: [
SystemProgram.createAccount({
fromPubkey: payer,
newAccountPubkey: mint,
programId: TOKEN_2022_PROGRAM_ID,
space: mintLen,
lamports: mintRent,
}),
createInitializeMetadataPointerInstruction(
mint,
authority,
mint,
TOKEN_2022_PROGRAM_ID,
),
createInitializeMint2Instruction(
mint,
6,
authority,
authority,
TOKEN_2022_PROGRAM_ID,
),
createInitializeInstruction({
metadata: mint,
mint,
mintAuthority: authority,
programId: TOKEN_2022_PROGRAM_ID,
updateAuthority: authority,
...metadata,
}),
],
};
}
export function mintToBuyerSellerIx(
mint: PublicKey,
authority: PublicKey,
payer: PublicKey,
buyer: PublicKey,
buyerTokenAccount: PublicKey,
seller: PublicKey,
sellerTokenAccount: PublicKey,
tokenProgram: PublicKey = TOKEN_PROGRAM_ID,
) {
return {
ixs: [
createAssociatedTokenAccountInstruction(
payer,
buyerTokenAccount,
buyer,
mint,
tokenProgram,
ASSOCIATED_PROGRAM_ID,
),
createMintToCheckedInstruction(
mint,
buyerTokenAccount,
authority,
10_000 * 10 ** 6,
6,
[],
tokenProgram,
),
createAssociatedTokenAccountInstruction(
payer,
sellerTokenAccount,
seller,
mint,
tokenProgram,
ASSOCIATED_PROGRAM_ID,
),
createMintToCheckedInstruction(
mint,
sellerTokenAccount,
authority,
10_000 * 10 ** 6,
6,
[],
tokenProgram,
),
],
};
}
export async function sendAndConfirmWNSTransaction(
connection: Connection,
instructions: TransactionInstruction[],
provider: AnchorProvider,
skipPreflight = true,
additionalSigners: Signer[] = [],
) {
const transaction = new VersionedTransaction(
new TransactionMessage({
instructions,
payerKey: provider.wallet.publicKey,
recentBlockhash: (await connection.getLatestBlockhash("confirmed"))
.blockhash,
}).compileToV0Message(),
);
const signedTx = await provider.wallet.signTransaction(transaction);
signedTx.sign(additionalSigners);
try {
const signature = await connection.sendTransaction(signedTx, {
preflightCommitment: "confirmed",
skipPreflight,
});
const { blockhash, lastValidBlockHeight } =
await connection.getLatestBlockhash("confirmed");
await connection.confirmTransaction(
{
signature,
lastValidBlockHeight,
blockhash,
},
"confirmed",
);
return signature;
} catch (err) {
throw err;
}
}