-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathpage.tsx
More file actions
527 lines (482 loc) · 27.4 KB
/
page.tsx
File metadata and controls
527 lines (482 loc) · 27.4 KB
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
'use client';
import { useState } from 'react';
import { PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js';
import Link from 'next/link';
import Image from 'next/image';
import { useLazorkitWalletConnect } from '@/hooks/useLazorkitWalletConnect';
import { useThemeClasses } from '@/hooks/useThemeClasses';
import { getConnection, shortenAddress } from '@/lib/solana-utils';
import {
NFT_NAME_MAX_LENGTH,
NFT_DESCRIPTION_MAX_LENGTH,
REGULAR_NFT_SYMBOL,
REGULAR_NFT_IMAGE_PATH,
MintedRegularNft,
buildMetaplexInstructions,
addSmartWalletToInstructions,
storeNftMetadata,
generateMintId,
validateNftMetadata,
} from '@/lib/nft-utils';
import {
TOKEN_PROGRAM_ID,
createInitializeMintInstruction,
createAssociatedTokenAccountInstruction,
createMintToInstruction,
getAssociatedTokenAddress,
MINT_SIZE,
} from '@solana/spl-token';
export default function Recipe06() {
const { isConnected, wallet, connect, connecting, signAndSendTransaction } = useLazorkitWalletConnect();
const theme = useThemeClasses();
const [name, setName] = useState('');
const [description, setDescription] = useState('');
const [minting, setMinting] = useState(false);
const [mintedNft, setMintedNft] = useState<MintedRegularNft | null>(null);
const [error, setError] = useState('');
const handleMint = async () => {
if (!wallet) return;
const validation = validateNftMetadata(name, description);
if (!validation.valid) {
setError(validation.error || 'Invalid input');
return;
}
setMinting(true);
setError('');
setMintedNft(null);
try {
const connection = getConnection();
const walletPubkey = new PublicKey(wallet.smartWallet);
// Generate a unique seed for the mint account
const seed = generateMintId('nft').replace(/-/g, '').slice(0, 32);
// Derive mint address deterministically from smart wallet + seed
const mintPubkey = await PublicKey.createWithSeed(
walletPubkey,
seed,
TOKEN_PROGRAM_ID
);
// Store metadata on our API first
const metadataUri = await storeNftMetadata(mintPubkey.toBase58(), {
name: name.trim(),
description: description.trim(),
});
// Get rent exemption for mint account
const lamports = await connection.getMinimumBalanceForRentExemption(MINT_SIZE);
// Derive ATA - Smart wallet is a PDA (off-curve)
const associatedTokenAddress = await getAssociatedTokenAddress(
mintPubkey,
walletPubkey,
true // allowOwnerOffCurve
);
// Build token instructions
const instructions: TransactionInstruction[] = [];
// 1. Create mint account using createAccountWithSeed
instructions.push(
SystemProgram.createAccountWithSeed({
fromPubkey: walletPubkey,
basePubkey: walletPubkey,
seed,
newAccountPubkey: mintPubkey,
lamports,
space: MINT_SIZE,
programId: TOKEN_PROGRAM_ID,
})
);
// 2. Initialize mint (0 decimals for NFT)
instructions.push(
createInitializeMintInstruction(
mintPubkey,
0,
walletPubkey,
walletPubkey
)
);
// 3. Create associated token account
instructions.push(
createAssociatedTokenAccountInstruction(
walletPubkey,
associatedTokenAddress,
walletPubkey,
mintPubkey
)
);
// 4. Mint 1 token
instructions.push(
createMintToInstruction(
mintPubkey,
associatedTokenAddress,
walletPubkey,
1
)
);
// 5 & 6. Create metadata and master edition using Umi
const metaplexInstructions = await buildMetaplexInstructions(
wallet.smartWallet,
mintPubkey.toBase58(),
name.trim(),
metadataUri
);
instructions.push(...metaplexInstructions);
// Add smart wallet to instructions for LazorKit validation
addSmartWalletToInstructions(instructions, wallet.smartWallet);
// Send transaction via LazorKit
const signature = await signAndSendTransaction({
instructions,
transactionOptions: {
computeUnitLimit: 400_000,
},
});
setMintedNft({
mintAddress: mintPubkey.toBase58(),
name: name.trim(),
description: description.trim(),
signature,
});
setName('');
setDescription('');
} catch (err: any) {
console.error('Minting error:', err);
setError(err.message || 'Failed to mint NFT');
} finally {
setMinting(false);
}
};
return (
<div className={`min-h-screen ${theme.bgPage}`}>
<div className="container mx-auto px-4 py-8 max-w-7xl">
{/* Header */}
<div className="mb-8">
<Link
href="/"
className={`${theme.textAccent} hover:opacity-80 mb-4 inline-block`}
>
← Back to Home
</Link>
<div className="flex items-start gap-3 mb-2">
<span className="text-4xl">🎨</span>
<div>
<h1 className={`text-4xl font-bold ${theme.textPrimary}`}>
Regular Metaplex NFT Minting
</h1>
</div>
</div>
<p className={theme.textMuted}>
Mint standard Metaplex NFTs with Token Metadata and Master Edition
</p>
</div>
<div className="grid lg:grid-cols-2 gap-8">
{/* Left Panel - Info */}
<div className="space-y-6">
{/* Integration Highlight */}
<div className={`${theme.bgCardAlt} rounded-2xl p-6`}>
<h2 className={`text-2xl font-bold ${theme.textPrimary} mb-4 flex items-center gap-2`}>
<span>🤝</span> LazorKit x Metaplex
</h2>
<p className={`text-sm ${theme.textSecondary} mb-4`}>
Standard Metaplex NFT with full on-chain metadata and Master Edition.
</p>
<div className="space-y-2 text-sm">
<div className="flex items-start gap-2">
<span className="text-green-500 mt-0.5 flex-shrink-0">✓</span>
<span className={theme.textSecondary}>Full on-chain metadata account</span>
</div>
<div className="flex items-start gap-2">
<span className="text-green-500 mt-0.5 flex-shrink-0">✓</span>
<span className={theme.textSecondary}>Master Edition (1/1 NFT)</span>
</div>
<div className="flex items-start gap-2">
<span className="text-green-500 mt-0.5 flex-shrink-0">✓</span>
<span className={theme.textSecondary}>Compatible with all marketplaces</span>
</div>
<div className="flex items-start gap-2">
<span className={theme.infoYellowTitle}>!</span>
<span className={theme.textSecondary}>Requires ~0.02 SOL for rent (from wallet)</span>
</div>
</div>
</div>
{/* Making Metaplex Work with LazorKit */}
<div className={`${theme.bgCard} rounded-2xl p-6`}>
<h2 className={`text-xl font-bold ${theme.textPrimary} mb-4`}>Making Metaplex Work with LazorKit</h2>
<div className={`space-y-4 text-sm ${theme.textSecondary}`}>
<div>
<div className="flex items-start gap-2 mb-2">
<span className={theme.infoYellowTitle}>1.</span>
<span className={`font-semibold ${theme.textPrimary}`}>Create Dummy Signer for Umi</span>
</div>
<p className={`ml-5 ${theme.textMuted} mb-2`}>
Umi requires a signer, but LazorKit handles signing via passkey:
</p>
<div className={`${theme.codeBlock} rounded-lg p-3 overflow-x-auto`}>
<pre className="text-xs text-gray-100 whitespace-pre-wrap">
{`// From lib/nft-utils.ts
function createDummySigner(walletAddress: string): Signer {
return {
publicKey: umiPublicKey(walletAddress),
signMessage: async () => new Uint8Array(64),
signTransaction: async (tx) => tx,
signAllTransactions: async (txs) => txs,
};
}`}
</pre>
</div>
</div>
<div>
<div className="flex items-start gap-2 mb-2">
<span className={theme.infoYellowTitle}>2.</span>
<span className={`font-semibold ${theme.textPrimary}`}>Convert Umi to Web3.js Instructions</span>
</div>
<p className={`ml-5 ${theme.textMuted} mb-2`}>
Use the adapter to get instructions LazorKit can execute:
</p>
<div className={`${theme.codeBlock} rounded-lg p-3 overflow-x-auto`}>
<pre className="text-xs text-gray-100 whitespace-pre-wrap">
{`// From lib/nft-utils.ts
import { toWeb3JsInstruction } from
'@metaplex-foundation/umi-web3js-adapters';
const metadataIxs = createMetadataAccountV3(umi, {...})
.getInstructions();
for (const ix of metadataIxs) {
instructions.push(toWeb3JsInstruction(ix));
}`}
</pre>
</div>
</div>
<div>
<div className="flex items-start gap-2 mb-2">
<span className={theme.infoYellowTitle}>3.</span>
<span className={`font-semibold ${theme.textPrimary}`}>Add Smart Wallet to Instructions</span>
</div>
<p className={`ml-5 ${theme.textMuted} mb-2`}>
LazorKit validation requires wallet in all instructions:
</p>
<div className={`${theme.codeBlock} rounded-lg p-3 overflow-x-auto`}>
<pre className="text-xs text-gray-100 whitespace-pre-wrap">
{`// From lib/nft-utils.ts
function addSmartWalletToInstructions(
instructions: TransactionInstruction[],
smartWalletAddress: string
): void {
instructions.forEach((ix) => {
const hasSmartWallet = ix.keys.some(
k => k.pubkey.toBase58() === smartWalletAddress
);
if (!hasSmartWallet) {
ix.keys.push({
pubkey: walletPubkey,
isSigner: false,
isWritable: false
});
}
});
}`}
</pre>
</div>
</div>
<div>
<div className="flex items-start gap-2 mb-2">
<span className={theme.infoYellowTitle}>4.</span>
<span className={`font-semibold ${theme.textPrimary}`}>Sign & Send via LazorKit</span>
</div>
<p className={`ml-5 ${theme.textMuted} mb-2`}>
LazorKit handles signing with passkey and gas sponsorship:
</p>
<div className={`${theme.codeBlock} rounded-lg p-3 overflow-x-auto`}>
<pre className="text-xs text-gray-100 whitespace-pre-wrap">
{`// From page.tsx
const { signAndSendTransaction } = useWallet();
const signature = await signAndSendTransaction({
instructions,
transactionOptions: {
computeUnitLimit: 400_000,
},
});`}
</pre>
</div>
</div>
</div>
</div>
{/* What You'll Learn */}
<div className={`${theme.bgCard} rounded-2xl p-6`}>
<h2 className={`text-xl font-bold ${theme.textPrimary} mb-4`}>What You'll Learn</h2>
<ul className={`space-y-3 text-sm ${theme.textSecondary}`}>
<li className="flex items-start gap-3">
<span className="text-green-500 mt-1 flex-shrink-0">✓</span>
<span>Use Metaplex Umi with LazorKit smart wallets</span>
</li>
<li className="flex items-start gap-3">
<span className="text-green-500 mt-1 flex-shrink-0">✓</span>
<span>Create Token Metadata & Master Edition accounts</span>
</li>
<li className="flex items-start gap-3">
<span className="text-green-500 mt-1 flex-shrink-0">✓</span>
<span>Handle PDA wallets with createAccountWithSeed</span>
</li>
<li className="flex items-start gap-3">
<span className="text-green-500 mt-1 flex-shrink-0">✓</span>
<span>Convert Umi instructions to Web3.js format</span>
</li>
</ul>
</div>
</div>
{/* Right Panel - Minting Interface */}
<div className="space-y-6">
<div className={`${theme.bgCard} rounded-2xl p-8`}>
{!isConnected ? (
<div className="text-center space-y-6">
<div>
<h3 className={`text-2xl font-bold ${theme.textPrimary} mb-2`}>Connect Your Wallet</h3>
<p className={`${theme.textMuted} text-sm`}>
Use LazorKit smart wallet to mint NFTs
</p>
</div>
<button
onClick={connect}
disabled={connecting}
className={`w-full ${theme.btnPrimary} disabled:opacity-50 disabled:cursor-not-allowed`}
>
{connecting ? 'Connecting...' : '🔑 Connect Wallet'}
</button>
</div>
) : (
<div className="space-y-6">
<div>
<h3 className={`text-2xl font-bold ${theme.textPrimary} mb-2`}>Mint Regular NFT</h3>
<p className={`${theme.textMuted} text-sm`}>
{shortenAddress(wallet?.smartWallet || '', 4)}
</p>
</div>
{/* Name Input */}
<div className="space-y-2">
<div className="flex justify-between">
<label className={`text-sm ${theme.textSecondary}`}>NFT Name</label>
<span className={`text-xs ${theme.textMuted}`}>
{name.length}/{NFT_NAME_MAX_LENGTH}
</span>
</div>
<input
type="text"
placeholder="My Awesome NFT"
value={name}
onChange={(e) => setName(e.target.value.slice(0, NFT_NAME_MAX_LENGTH))}
className={`w-full ${theme.bgInput} rounded-xl px-4 py-3 ${theme.textPrimary} placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:border-purple-500`}
disabled={minting}
/>
</div>
{/* Description Input */}
<div className="space-y-2">
<div className="flex justify-between">
<label className={`text-sm ${theme.textSecondary}`}>Description</label>
<span className={`text-xs ${theme.textMuted}`}>
{description.length}/{NFT_DESCRIPTION_MAX_LENGTH}
</span>
</div>
<textarea
placeholder="Describe your NFT..."
value={description}
onChange={(e) => setDescription(e.target.value.slice(0, NFT_DESCRIPTION_MAX_LENGTH))}
rows={3}
className={`w-full ${theme.bgInput} rounded-xl px-4 py-3 ${theme.textPrimary} placeholder-gray-400 dark:placeholder-gray-500 focus:outline-none focus:border-purple-500 resize-none`}
disabled={minting}
/>
</div>
{/* Cost Notice */}
<div className={`${theme.infoYellow} rounded-xl p-3 text-sm`}>
<span className={theme.infoYellowText}>
<strong className={theme.infoYellowTitle}>Note:</strong> Regular NFTs require ~0.02 SOL for account rent.
This is paid from your smart wallet.
</span>
</div>
{/* Error Message */}
{error && (
<div className="bg-red-100 dark:bg-red-500/10 border border-red-300 dark:border-red-500/30 rounded-xl p-3 text-sm text-red-700 dark:text-red-200">
{error}
</div>
)}
{/* Mint Button */}
<div className={`${theme.infoBlue} rounded-lg p-4 mb-4`}>
<div className="flex items-start gap-2">
<span className={`${theme.infoBlueTitle} text-lg`}>ℹ️</span>
<div className={`text-xs ${theme.infoBlueText}`}>
<p className={`font-semibold mb-1 ${theme.infoBlueTitle}`}>About the Transaction Preview</p>
<p className="mb-2">
Your wallet may display unusual token amounts (like large negative numbers) during transaction simulation.
This is normal and happens because the mint account doesn't exist yet when the wallet tries to preview the transaction.
</p>
<p>
<strong>Why this happens:</strong> To optimize for gasless UX, we create the mint account, initialize it,
and add metadata all in a single transaction. The alternative would be splitting this into 2-3 separate
transactions (each requiring Face ID), which would be slower and less user-friendly.
</p>
<p className="mt-2">
<strong>Rest assured:</strong> The transaction will execute correctly and your NFT will mint successfully!
</p>
</div>
</div>
</div>
<button
onClick={handleMint}
disabled={minting || !name.trim() || !description.trim()}
className="w-full bg-gradient-to-r from-green-500 to-emerald-500 hover:from-green-600 hover:to-emerald-600 text-white font-semibold py-4 px-6 rounded-xl transition-all disabled:opacity-50 disabled:cursor-not-allowed"
>
{minting ? 'Minting...' : '🎨 Mint Regular NFT'}
</button>
<div className={`text-xs ${theme.textMuted} text-center`}>
Creates 4 accounts • ~0.02 SOL rent • Full Metaplex standard
</div>
</div>
)}
</div>
{/* Minted NFT Display */}
{mintedNft && (
<div className="bg-green-100 dark:bg-green-500/10 border border-green-300 dark:border-green-500/30 rounded-2xl p-6">
<h2 className={`text-xl font-bold ${theme.textPrimary} mb-4 flex items-center gap-2`}>
<span>✅</span> NFT Minted Successfully!
</h2>
<div className="space-y-4">
{/* NFT Card */}
<div className={`${theme.bgCard} rounded-xl p-4 flex gap-4`}>
<div className="relative w-20 h-20 rounded-lg overflow-hidden flex-shrink-0">
<Image
src={REGULAR_NFT_IMAGE_PATH}
alt={mintedNft.name}
fill
className="object-cover"
/>
</div>
<div className="flex-1 min-w-0">
<h3 className={`${theme.textPrimary} font-semibold truncate`}>{mintedNft.name}</h3>
<p className={`${theme.textMuted} text-sm line-clamp-2`}>{mintedNft.description}</p>
<p className={`${theme.textAccent} text-xs mt-1`}>{REGULAR_NFT_SYMBOL}</p>
</div>
</div>
{/* Links */}
<div className="space-y-2">
<a
href={`https://orbmarkets.io/address/${mintedNft.mintAddress}?network=devnet&cluster=devnet`}
target="_blank"
rel="noopener noreferrer"
className={`block w-full text-center ${theme.bgInput} ${theme.textAccent} border ${theme.borderAccent} hover:bg-opacity-80 py-2 px-4 rounded-lg text-sm transition-colors`}
>
View NFT on Orb Explorer →
</a>
<a
href={`https://orbmarkets.io/tx/${mintedNft.signature}?advanced=true&tab=summary&cluster=devnet&network=devnet`}
target="_blank"
rel="noopener noreferrer"
className={`block w-full text-center ${theme.bgInput} hover:opacity-80 ${theme.textSecondary} py-2 px-4 rounded-lg text-sm transition-colors`}
>
View Transaction →
</a>
</div>
<p className={`text-xs ${theme.textMuted} text-center`}>
Mint: {shortenAddress(mintedNft.mintAddress, 8)}
</p>
</div>
</div>
)}
</div>
</div>
</div>
</div>
);
}