-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.ts
More file actions
495 lines (438 loc) · 13.6 KB
/
program.ts
File metadata and controls
495 lines (438 loc) · 13.6 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
import { AnchorProvider, Program, BN } from "@coral-xyz/anchor";
import { Connection, PublicKey, SystemProgram, SYSVAR_RENT_PUBKEY } from "@solana/web3.js";
import {
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID,
getAssociatedTokenAddress
} from "@solana/spl-token";
import { Marketplace } from "./marketplace";
import idl from "./marketplace.json";
import { PROGRAM_ID, PLACEHOLDER_IMAGE } from "./constants";
export type { Marketplace };
// Get the program instance
export function getProgram(provider: AnchorProvider): Program<Marketplace> {
return new Program(idl as Marketplace, provider);
}
// Derive marketplace PDA
export function getMarketplacePDA(admin: PublicKey): [PublicKey, number] {
return PublicKey.findProgramAddressSync(
[Buffer.from("marketplace"), admin.toBuffer()],
PROGRAM_ID
);
}
// Derive listing PDA
export function getListingPDA(nftMint: PublicKey): [PublicKey, number] {
return PublicKey.findProgramAddressSync(
[Buffer.from("listing"), nftMint.toBuffer()],
PROGRAM_ID
);
}
// Derive escrow token account PDA
export function getEscrowPDA(nftMint: PublicKey): [PublicKey, number] {
return PublicKey.findProgramAddressSync(
[Buffer.from("escrow"), nftMint.toBuffer()],
PROGRAM_ID
);
}
// Initialize marketplace (one-time setup)
export async function initializeMarketplace(
program: Program<Marketplace>,
feePercentage: number = 200 // Default 2% fee (200 basis points)
): Promise<string> {
const admin = program.provider.publicKey!;
const [marketplacePDA] = getMarketplacePDA(admin);
const tx = await program.methods
.initializeMarketplace(feePercentage)
.accountsPartial({
marketplace: marketplacePDA,
admin,
systemProgram: SystemProgram.programId,
})
.rpc();
return tx;
}
// Find marketplace for a given listing (by finding admin from listing data)
export async function findMarketplaceForListing(
program: Program<Marketplace>,
nftMint: PublicKey
): Promise<{ marketplacePDA: PublicKey; admin: PublicKey; feePercent: number } | null> {
try {
// Fetch all marketplace accounts
const marketplaces = await program.account.marketplace.all();
if (marketplaces.length > 0) {
const mp = marketplaces[0];
const admin = mp.account.admin as PublicKey;
const [marketplacePDA] = getMarketplacePDA(admin);
return {
marketplacePDA,
admin,
feePercent: mp.account.feePercentage as number,
};
}
return null;
} catch (error) {
console.error("Error finding marketplace:", error);
return null;
}
}
// Get marketplace by admin
export async function getMarketplace(
program: Program<Marketplace>,
admin: PublicKey
): Promise<{ admin: PublicKey; feePercent: number; totalListings: number } | null> {
try {
const [marketplacePDA] = getMarketplacePDA(admin);
const marketplace = await program.account.marketplace.fetch(marketplacePDA);
return {
admin: marketplace.admin as PublicKey,
feePercent: marketplace.feePercentage as number,
totalListings: (marketplace.totalListings as BN).toNumber(),
};
} catch (error) {
// Try to find any marketplace
try {
const marketplaces = await program.account.marketplace.all();
if (marketplaces.length > 0) {
const mp = marketplaces[0].account;
return {
admin: mp.admin as PublicKey,
feePercent: mp.feePercentage as number,
totalListings: (mp.totalListings as BN).toNumber(),
};
}
} catch (e) {
console.error("Error fetching marketplaces:", e);
}
return null;
}
}
// Listing account data type
export interface ListingAccount {
seller: PublicKey;
nftMint: PublicKey;
price: BN;
bump: number;
isActive: boolean;
feePercent?: number; // Added from marketplace for convenience
}
// Marketplace account data type
export interface MarketplaceAccount {
admin: PublicKey;
feePercentage: number;
totalListings: BN;
bump: number;
}
// NFT metadata type
export interface NFTMetadata {
name: string;
symbol: string;
description: string;
image: string;
attributes?: Array<{
trait_type: string;
value: string;
}>;
collection?: {
name: string;
family?: string;
};
}
// Extended listing with metadata
export interface ListingWithMetadata {
publicKey: PublicKey;
account: ListingAccount;
metadata?: NFTMetadata;
}
// Fetch all active listings
export async function fetchAllListings(
program: Program<Marketplace>
): Promise<ListingWithMetadata[]> {
try {
const listings = await program.account.listing.all();
// Get fee percent from marketplace
let feePercent = 2; // Default fee
try {
const marketplaces = await program.account.marketplace.all();
if (marketplaces.length > 0) {
feePercent = marketplaces[0].account.feePercentage as number;
}
} catch {
// Use default fee
}
return listings
.filter((l) => l.account.isActive)
.map((l) => ({
publicKey: l.publicKey,
account: {
...(l.account as unknown as ListingAccount),
feePercent,
},
}));
} catch (error) {
console.error("Error fetching listings:", error);
return [];
}
}
// Fetch single listing
export async function fetchListing(
program: Program<Marketplace>,
nftMint: PublicKey
): Promise<ListingWithMetadata | null> {
try {
const [listingPDA] = getListingPDA(nftMint);
const listing = await program.account.listing.fetch(listingPDA);
// Get fee percent from marketplace
let feePercent = 2; // Default fee
try {
const marketplaces = await program.account.marketplace.all();
if (marketplaces.length > 0) {
feePercent = marketplaces[0].account.feePercentage as number;
}
} catch {
// Use default fee
}
return {
publicKey: listingPDA,
account: {
...(listing as unknown as ListingAccount),
feePercent,
},
};
} catch (error) {
console.error("Error fetching listing:", error);
return null;
}
}
// List NFT for sale (auto-initializes marketplace if needed)
export async function listNft(
program: Program<Marketplace>,
nftMint: PublicKey,
priceInLamports: number
): Promise<string> {
const seller = program.provider.publicKey!;
// Find or initialize the marketplace
let mpInfo = await findMarketplaceForListing(program, nftMint);
if (!mpInfo) {
if (process.env.NODE_ENV === "development") {
console.log("No marketplace found, initializing...");
}
try {
await initializeMarketplace(program);
// Fetch the newly created marketplace
mpInfo = await findMarketplaceForListing(program, nftMint);
if (!mpInfo) {
throw new Error("Failed to initialize marketplace");
}
} catch (error) {
if (process.env.NODE_ENV === "development") {
console.error("Error initializing marketplace:", error);
}
throw new Error("Failed to initialize marketplace. Please try again.");
}
}
// Get seller's token account
const sellerTokenAccount = await getAssociatedTokenAddress(nftMint, seller);
const tx = await program.methods
.listNft(new BN(priceInLamports))
.accountsPartial({
marketplace: mpInfo.marketplacePDA,
seller,
sellerTokenAccount,
nftMint,
})
.rpc();
return tx;
}
// Buy NFT (simplified - auto-derives PDAs)
export async function buyNft(
program: Program<Marketplace>,
nftMint: PublicKey
): Promise<string> {
const buyer = program.provider.publicKey!;
// Get listing info
const listing = await fetchListing(program, nftMint);
if (!listing) {
throw new Error("Listing not found");
}
// Find marketplace
const mpInfo = await findMarketplaceForListing(program, nftMint);
if (!mpInfo) {
throw new Error("Marketplace not found");
}
const tx = await program.methods
.buyNft()
.accountsPartial({
marketplace: mpInfo.marketplacePDA,
buyer,
seller: listing.account.seller,
admin: mpInfo.admin,
nftMint,
})
.rpc();
return tx;
}
// Cancel listing (simplified - auto-derives PDAs)
export async function cancelListing(
program: Program<Marketplace>,
nftMint: PublicKey
): Promise<string> {
const seller = program.provider.publicKey!;
// Find marketplace
const mpInfo = await findMarketplaceForListing(program, nftMint);
if (!mpInfo) {
throw new Error("Marketplace not found");
}
// Get seller's token account
const sellerTokenAccount = await getAssociatedTokenAddress(nftMint, seller);
const tx = await program.methods
.cancelListing()
.accountsPartial({
marketplace: mpInfo.marketplacePDA,
seller,
sellerTokenAccount,
nftMint,
})
.rpc();
return tx;
}
// Update listing price
export async function updateListingPrice(
program: Program<Marketplace>,
nftMint: PublicKey,
newPriceInLamports: number
): Promise<string> {
const tx = await program.methods
.updateListingPrice(new BN(newPriceInLamports))
.accountsPartial({
seller: program.provider.publicKey,
nftMint,
})
.rpc();
return tx;
}
// Metaplex metadata constants
const METADATA_PROGRAM_ID = new PublicKey("metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s");
// Derive Metaplex metadata PDA
function getMetadataPDA(mint: PublicKey): PublicKey {
const [pda] = PublicKey.findProgramAddressSync(
[
Buffer.from("metadata"),
METADATA_PROGRAM_ID.toBuffer(),
mint.toBuffer(),
],
METADATA_PROGRAM_ID
);
return pda;
}
// Fetch NFT metadata from on-chain using Metaplex standard
export async function fetchNFTMetadataFromChain(
connection: Connection,
mint: PublicKey
): Promise<NFTMetadata | undefined> {
try {
// Get metadata account PDA
const metadataPDA = getMetadataPDA(mint);
// Fetch the metadata account
const metadataAccount = await connection.getAccountInfo(metadataPDA);
if (!metadataAccount) {
return undefined;
}
// Parse the metadata account data
// Metaplex metadata layout: key(1) + update_authority(32) + mint(32) + name(36) + symbol(14) + uri(204) + ...
const data = metadataAccount.data;
// Skip key (1 byte)
let offset = 1;
// Skip update authority (32 bytes)
offset += 32;
// Skip mint (32 bytes)
offset += 32;
// Read name (4 bytes length + string)
const nameLength = data.readUInt32LE(offset);
offset += 4;
const nameBytes = data.slice(offset, offset + nameLength);
const name = nameBytes.toString('utf8').replace(/\0/g, '').trim();
offset += 32; // Name field is padded to 32 bytes
// Read symbol (4 bytes length + string)
const symbolLength = data.readUInt32LE(offset);
offset += 4;
const symbolBytes = data.slice(offset, offset + symbolLength);
const symbol = symbolBytes.toString('utf8').replace(/\0/g, '').trim();
offset += 10; // Symbol field is padded to 10 bytes
// Read URI (4 bytes length + string)
const uriLength = data.readUInt32LE(offset);
offset += 4;
const uriBytes = data.slice(offset, offset + uriLength);
const uri = uriBytes.toString('utf8').replace(/\0/g, '').trim();
// If URI is a data URI, parse it directly
if (uri.startsWith('data:application/json')) {
try {
const base64Data = uri.split(',')[1];
const jsonString = atob(base64Data);
const metadata = JSON.parse(jsonString);
return {
name: metadata.name || name,
symbol: metadata.symbol || symbol,
description: metadata.description || '',
image: metadata.image || PLACEHOLDER_IMAGE,
attributes: metadata.attributes || [],
collection: metadata.collection,
};
} catch (err) {
console.error('Error parsing data URI:', err);
}
} else if (uri.startsWith('http')) {
// Try to fetch external metadata JSON
try {
const response = await fetch(uri);
if (!response.ok) {
console.error(`Failed to fetch metadata: ${response.status} ${response.statusText}`);
throw new Error(`HTTP ${response.status}`);
}
const contentType = response.headers.get('content-type');
// Try to parse as JSON first (Irys returns application/octet-stream for JSON)
const text = await response.text();
try {
const metadata = JSON.parse(text);
// If it parses as JSON, it's metadata
if (metadata && typeof metadata === 'object') {
return {
name: metadata.name || name,
symbol: metadata.symbol || symbol,
description: metadata.description || '',
image: metadata.image || PLACEHOLDER_IMAGE,
attributes: metadata.attributes || [],
collection: metadata.collection,
};
}
} catch (jsonErr) {
// Not JSON, treat as direct image URL
return {
name,
symbol,
description: '',
image: uri,
};
}
} catch (err) {
console.error('Error fetching external metadata:', err);
// Fallback: assume URI is a direct image link
return {
name,
symbol,
description: '',
image: uri,
};
}
}
// Fallback: return basic metadata from on-chain data
return {
name,
symbol,
description: '',
image: PLACEHOLDER_IMAGE,
};
} catch (error) {
console.error('Error fetching metadata from chain:', error);
return undefined;
}
}