-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathindex.ts
More file actions
192 lines (154 loc) · 6.51 KB
/
Copy pathindex.ts
File metadata and controls
192 lines (154 loc) · 6.51 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
import { MintContractOptions, MintIngestor, MintIngestorResources } from '../../lib/types/mint-ingestor';
import { MintIngestionErrorName, MintIngestorError } from '../../lib/types/mint-ingestor-error';
import { MintInstructionType, MintTemplate } from '../../lib/types/mint-template';
import { MintTemplateBuilder } from '../../lib/builder/mint-template-builder';
import {
getHighlightCollectionByAddress,
getHighlightCollectionById,
getHighlightCollectionIdByOnChainId,
getHighlightMintContractAddress,
getHighlightMintDataForCollection,
getHighlightMintVectorId,
getHighlightVectorPriceInWei,
isSupportedHighlightChain,
normalizeHighlightOnChainCollectionId,
} from './offchain-metadata';
import { MINT_CONTRACT_ABI } from './abi';
import { CollectionByAddress } from './types';
const HIGHLIGHT_HOSTS = new Set(['highlight.xyz', 'www.highlight.xyz']);
const DEFAULT_END_TIMESTAMP_SECONDS = 1893456000;
const getHighlightMintIdFromUrl = (url: string): string | undefined => {
try {
const parsedUrl = new URL(url);
if (!HIGHLIGHT_HOSTS.has(parsedUrl.hostname)) {
return undefined;
}
const [, mintPath, id] = parsedUrl.pathname.split('/');
if (mintPath !== 'mint' || !id) {
return undefined;
}
return decodeURIComponent(id);
} catch (error) {}
};
const getTimestampSeconds = (value: string, fieldName: string): number => {
const timestamp = Date.parse(value);
if (Number.isNaN(timestamp)) {
throw new MintIngestorError(MintIngestionErrorName.MissingRequiredData, `${fieldName} not available`);
}
return Math.floor(timestamp / 1000);
};
export class HighlightIngestor implements MintIngestor {
async supportsUrl(resources: MintIngestorResources, url: string): Promise<boolean> {
const collection = await this.getCollectionFromUrl(resources, url);
if (!collection) {
return false;
}
return !!getHighlightMintDataForCollection(collection);
}
async supportsContract(resources: MintIngestorResources, contractOptions: MintContractOptions): Promise<boolean> {
if (!isSupportedHighlightChain(contractOptions.chainId)) {
return false;
}
const collection = await getHighlightCollectionByAddress(resources, contractOptions);
if (!collection) {
return false;
}
return true;
}
async createMintForContract(
resources: MintIngestorResources,
contractOptions: MintContractOptions,
): Promise<MintTemplate> {
const collection = await getHighlightCollectionByAddress(resources, contractOptions);
if (!collection) {
throw new MintIngestorError(MintIngestionErrorName.CouldNotResolveMint, 'Collection not found');
}
return this.buildMintTemplate(collection, contractOptions.url);
}
async createMintTemplateForUrl(resources: MintIngestorResources, url: string): Promise<MintTemplate> {
const collection = await this.getCollectionFromUrl(resources, url);
const mintData = collection ? getHighlightMintDataForCollection(collection) : undefined;
if (!mintData) {
throw new MintIngestorError(MintIngestionErrorName.IncompatibleUrl, 'Incompatible URL');
}
return this.buildMintTemplate(mintData, url);
}
private async getCollectionFromUrl(resources: MintIngestorResources, url: string) {
const id = getHighlightMintIdFromUrl(url);
if (!id) {
return undefined;
}
if (/^[a-f0-9]{24}$/i.test(id)) {
return getHighlightCollectionById(resources, id);
}
const onChainId = normalizeHighlightOnChainCollectionId(id);
if (!onChainId) {
return undefined;
}
const collectionId = await getHighlightCollectionIdByOnChainId(resources, onChainId);
if (!collectionId) {
return undefined;
}
return getHighlightCollectionById(resources, collectionId);
}
private buildMintTemplate(collection: CollectionByAddress, url: string | undefined): MintTemplate {
const mintBuilder = new MintTemplateBuilder()
.setMintInstructionType(MintInstructionType.EVM_MINT)
.setPartnerName('Highlight');
if (url) {
mintBuilder.setMarketingUrl(url);
}
const description = collection?.description;
mintBuilder.setName(collection.name).setDescription(description).setFeaturedImageUrl(collection.image.split('?')[0]);
if (collection.sampleImages.length) {
collection.sampleImages.forEach((url, index) => {
mintBuilder.addImage(url, `Sample image #${index}`);
});
}
if (!collection.creator) {
throw new MintIngestorError(MintIngestionErrorName.MissingRequiredData, 'Error finding creator');
}
const collectionId = collection.id;
if (!collectionId) {
throw new MintIngestorError(MintIngestionErrorName.MissingRequiredData, 'Collection id not available');
}
mintBuilder.setCreator({
name: collection.creatorAccountSettings?.displayName || '',
walletAddress: collection.creator,
imageUrl: collection.creatorAccountSettings?.displayAvatar,
});
mintBuilder.setMintOutputContract({ chainId: collection.chainId, address: collection.primaryContract });
const vectorId = getHighlightMintVectorId(collection.mintVector);
if (!vectorId) {
throw new MintIngestorError(MintIngestionErrorName.MissingRequiredData, 'Id not available');
}
const totalPriceWei = getHighlightVectorPriceInWei(collection.mintVector);
if (!totalPriceWei) {
throw new MintIngestorError(MintIngestionErrorName.MissingRequiredData, 'Price not available');
}
const mintContractAddress = getHighlightMintContractAddress(collection.mintVector);
if (!mintContractAddress) {
throw new MintIngestorError(MintIngestionErrorName.MissingRequiredData, 'Mint contract not available');
}
mintBuilder.setMintInstructions({
chainId: collection.chainId,
contractAddress: mintContractAddress,
contractMethod: 'vectorMint721',
contractParams: `[${vectorId}, quantity, address]`,
abi: MINT_CONTRACT_ABI,
priceWei: totalPriceWei,
supportsQuantity: true,
});
const { mintVector } = collection;
const startTimestamp = getTimestampSeconds(mintVector.start, 'Start date');
const endTimestamp = mintVector.end
? getTimestampSeconds(mintVector.end, 'End date')
: DEFAULT_END_TIMESTAMP_SECONDS;
const liveDate = +new Date() > startTimestamp * 1000 ? new Date() : new Date(startTimestamp * 1000);
mintBuilder
.setAvailableForPurchaseStart(new Date(startTimestamp * 1000))
.setAvailableForPurchaseEnd(new Date(endTimestamp * 1000))
.setLiveDate(liveDate);
return mintBuilder.build();
}
}