From 36121e8c9cb1d2d91dc20efbfcb67046f35294ba Mon Sep 17 00:00:00 2001 From: Johnpaul Date: Wed, 20 Nov 2024 19:42:08 +0100 Subject: [PATCH] fix highlight ingestor --- src/ingestors/highlight/index.ts | 74 +++++++++++--------- src/ingestors/highlight/offchain-metadata.ts | 26 +++++-- src/ingestors/highlight/onchain-metadata.ts | 5 +- src/ingestors/highlight/types.ts | 2 + test/ingestors/highlight.test.ts | 14 ++-- 5 files changed, 74 insertions(+), 47 deletions(-) diff --git a/src/ingestors/highlight/index.ts b/src/ingestors/highlight/index.ts index 46e1715..1fb5c88 100644 --- a/src/ingestors/highlight/index.ts +++ b/src/ingestors/highlight/index.ts @@ -1,48 +1,49 @@ 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 { getHighlightMetadata, getHighlightMintPriceInWei } from './onchain-metadata'; import { getHighlightCollectionByAddress, getHighlightCollectionById, getHighlightCollectionOwnerDetails, + getHighlightUrlForAddress, getHighlightVectorId, } from './offchain-metadata'; +import { getHighlightMetadata, getHighlightMintPriceInWei } from './onchain-metadata'; + import { MINT_CONTRACT_ABI } from './abi'; +import { MintTemplateBuilder } from '../../lib/builder/mint-template-builder'; const CONTRACT_ADDRESS = '0x8087039152c472Fa74F47398628fF002994056EA'; export class HighlightIngestor implements MintIngestor { async supportsUrl(resources: MintIngestorResources, url: string): Promise { - return false; - // const id = url.split('/').pop(); - // if (!id) { - // return false; - // } - - // const collection = await getHighlightCollectionById(resources, id); - - // if (!collection || collection.chainId !== 8453) { - // return false; - // } - - // const urlPattern = /^https:\/\/highlight\.xyz\/mint\/[a-f0-9]{24}$/; - // return ( - // new URL(url).hostname === 'www.highlight.xyz' || new URL(url).hostname === 'highlight.xyz' || urlPattern.test(url) - // ); + const match = url.match(/\/mint\/([^\/]+)/); + const id = match ? match[1] : null; + if (!id) { + return false; + } + // accept + const collection = await getHighlightCollectionById(resources, id); + + if (!collection || collection.chainId !== 8453) { + return false; + } + + const urlPattern = /^https:\/\/highlight\.xyz\/mint\/[a-f0-9]{24}$/; + return ( + new URL(url).hostname === 'www.highlight.xyz' || new URL(url).hostname === 'highlight.xyz' || urlPattern.test(url) + ); } async supportsContract(resources: MintIngestorResources, contractOptions: MintContractOptions): Promise { - return false; - // if (contractOptions.chainId !== 8453) { - // return false; - // } - // const collection = await getHighlightCollectionByAddress(resources, contractOptions); - // if (!collection) { - // return false; - // } - // return true; + if (contractOptions.chainId !== 8453) { + return false; + } + const collection = await getHighlightCollectionByAddress(resources, contractOptions); + if (!collection) { + return false; + } + return true; } async createMintForContract( @@ -82,7 +83,10 @@ export class HighlightIngestor implements MintIngestor { throw new MintIngestorError(MintIngestionErrorName.MissingRequiredData, 'Error finding creator'); } - const collectionId = collection.highlightCollection?.id || collection.id; + const url = await getHighlightUrlForAddress(resources, contractOptions.contractAddress); + + const match = url.match(/\/mint\/([^\/]+)/); + const collectionId = match ? match[1] : null; if (!collectionId) { throw new MintIngestorError(MintIngestionErrorName.MissingRequiredData, 'Collection id not available'); @@ -95,7 +99,7 @@ export class HighlightIngestor implements MintIngestor { imageUrl: creator?.creatorAccountSettings?.displayAvatar, }); - mintBuilder.setMintOutputContract({ chainId: 8453, address: collection.highlightCollection.address }); + mintBuilder.setMintOutputContract({ chainId: 8453, address: collection.primaryContract }); const vectorId = await getHighlightVectorId(resources, collectionId); @@ -143,9 +147,10 @@ export class HighlightIngestor implements MintIngestor { } // Example URL: https://highlight.xyz/mint/665fa33f07b3436991e55632 - const splits = url.split('/'); - const id = splits.pop(); - const chain = splits.pop(); + const match = url.match(/\/mint\/([^\/]+)/); + const id = match ? match[1] : null; + + const tokenId = url.split('/t/')[1]; if (!id) { throw new MintIngestorError(MintIngestionErrorName.CouldNotResolveMint, 'Url error'); @@ -160,7 +165,10 @@ export class HighlightIngestor implements MintIngestor { return this.createMintForContract(resources, { chainId: collection.chainId, contractAddress: collection.address, - url, + url: `https://highlight.xyz/mint/base:${collection.address}${ + collection.editionId == '1' ? ':' + collection.editionId : '' + }${tokenId ? '/t/' + tokenId : ''}`, + tokenId, }); } } diff --git a/src/ingestors/highlight/offchain-metadata.ts b/src/ingestors/highlight/offchain-metadata.ts index 2ddccd2..a02aac9 100644 --- a/src/ingestors/highlight/offchain-metadata.ts +++ b/src/ingestors/highlight/offchain-metadata.ts @@ -1,5 +1,5 @@ -import { MintContractOptions, MintIngestorResources } from 'src/lib'; import { Collection, CollectionByAddress, CollectionByAddress1 } from './types'; +import { MintContractOptions, MintIngestorResources } from 'src/lib'; export const getHighlightCollectionById = async ( resources: MintIngestorResources, @@ -31,6 +31,7 @@ export const getHighlightCollectionById = async ( status baseUri onChainBaseUri + editionId } } `, @@ -68,7 +69,10 @@ export const getHighlightCollectionByAddress = async ( } catch (error) {} }; -export const getHighlightVectorId = async (resources: MintIngestorResources, id: string): Promise => { +export const getHighlightVectorId = async ( + resources: MintIngestorResources, + id: string, +): Promise => { const url = 'https://api.highlight.xyz:8080/'; const headers = { @@ -114,7 +118,8 @@ export const getHighlightVectorId = async (resources: MintIngestorResources, id: ).onchainMintVectorId; const vectorId = vectorString.split(':').pop(); return vectorId; - } catch (error) {} + } catch (error) { + } }; export const getHighlightCollectionOwnerDetails = async (resources: MintIngestorResources, id: string) => { @@ -152,8 +157,21 @@ export const getHighlightCollectionOwnerDetails = async (resources: MintIngestor try { const resp = await resources.fetcher.post(url, data, { headers }); if (resp.data.errors) { - throw new Error("Error fetching owner"); + throw new Error('Error fetching owner'); } return resp.data.data.getPublicCollectionDetails; } catch (error) {} }; + +export const getHighlightUrlForAddress = async (resources: MintIngestorResources, address: string) => { + // try to get it by edition 1 or edition 0 + let edition1Id = `base:${address}:1`; + let edition0Id = `base:${address}`; + + let promises = [getHighlightCollectionById(resources, edition1Id), getHighlightCollectionById(resources, edition0Id)]; + + let data = (await Promise.all(promises)) as Collection[]; + let record = data.filter((d) => !!d)[0]; + + return `https://highlight.xyz/mint/base:${record.address}${record.editionId === '1' ? ':1' : ''}`; +}; diff --git a/src/ingestors/highlight/onchain-metadata.ts b/src/ingestors/highlight/onchain-metadata.ts index 4ce5524..8b7c834 100644 --- a/src/ingestors/highlight/onchain-metadata.ts +++ b/src/ingestors/highlight/onchain-metadata.ts @@ -1,4 +1,5 @@ import { Alchemy, Contract } from 'alchemy-sdk'; + import { MINT_CONTRACT_ABI } from './abi'; const CONTRACT_ADDRESS = '0x8087039152c472Fa74F47398628fF002994056EA'; @@ -19,9 +20,7 @@ export const getHighlightMintPriceInWei = async (vectorId: number, alchemy: Alch const totalFee = parseInt(pricePerToken.toString()) + fee; return `${totalFee}`; - } catch (error) { - console.log(error); - } + } catch (error) {} }; export const getHighlightMetadata = async ( diff --git a/src/ingestors/highlight/types.ts b/src/ingestors/highlight/types.ts index 32d9a72..6b6d707 100644 --- a/src/ingestors/highlight/types.ts +++ b/src/ingestors/highlight/types.ts @@ -10,6 +10,7 @@ export type Collection = { chainId: number; status: string; baseUri: string; + editionId: string; }; export type CollectionByAddress1 = { @@ -24,6 +25,7 @@ export type CollectionByAddress1 = { description: string; sampleImages: string[]; creator: string; + primaryContract: string; }; export type CollectionByAddress2 = { diff --git a/test/ingestors/highlight.test.ts b/test/ingestors/highlight.test.ts index 6a774d2..6a8188e 100644 --- a/test/ingestors/highlight.test.ts +++ b/test/ingestors/highlight.test.ts @@ -1,13 +1,13 @@ -import { expect } from 'chai'; -import { HighlightIngestor } from '../../src/ingestors/highlight'; -import { mintIngestorResources } from '../../src/lib/resources'; import { EVMMintInstructions } from '../../src/lib/types/mint-template'; +import { HighlightIngestor } from '../../src/ingestors/highlight'; import { MintTemplateBuilder } from '../../src/lib/builder/mint-template-builder'; import { basicIngestorTests } from '../shared/basic-ingestor-tests'; +import { expect } from 'chai'; +import { mintIngestorResources } from '../../src/lib/resources'; const resources = mintIngestorResources(); -describe.skip('highlight', function () { +describe('highlight', function () { basicIngestorTests( new HighlightIngestor(), resources, @@ -52,7 +52,7 @@ describe.skip('highlight', function () { it('createMintTemplateForUrl: Returns a mint template for a supported URL', async function () { const ingestor = new HighlightIngestor(); - const url = 'https://highlight.xyz/mint/66856628ff8a01fdccc132f4'; + const url = 'https://highlight.xyz/mint/base:0x0E5DDe3De7cf2761d8a81Ee68F48410425e2dBbA'; const resources = mintIngestorResources(); const template = await ingestor.createMintTemplateForUrl(resources, url); @@ -65,7 +65,7 @@ describe.skip('highlight', function () { const mintInstructions = template.mintInstructions as EVMMintInstructions; expect(mintInstructions.contractAddress).to.equal('0x8087039152c472Fa74F47398628fF002994056EA'); - expect(template.mintOutputContract?.address).to.equal('0x0E5DDe3De7cf2761d8a81Ee68F48410425e2dBbA'); + expect(template.mintOutputContract?.address).to.equal('0x0e5dde3de7cf2761d8a81ee68f48410425e2dbba'); expect(mintInstructions.contractMethod).to.equal('vectorMint721'); expect(mintInstructions.contractParams).to.equal('[1176, quantity, address]'); expect(mintInstructions.priceWei).to.equal('800000000000000'); @@ -89,7 +89,7 @@ describe.skip('highlight', function () { it.skip('createMintTemplateForUrl: Returns a mint template for a supported URL with free price', async function () { const ingestor = new HighlightIngestor(); - const url = 'https://highlight.xyz/mint/66744e64e610ed36adeb1a64'; + const url = 'https://highlight.xyz/mint/base:0x30d745EDC90E92b4fdCE50c5239962Cf3407B12B'; const resources = mintIngestorResources(); const template = await ingestor.createMintTemplateForUrl(resources, url);