Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Allow new third parties URN #115

Merged
merged 5 commits into from
Aug 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 31 additions & 6 deletions src/lib/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,37 @@ import { computeZoom, getZoom } from './zoom'

const DEFAULT_PROFILE = 'default'
const QUANTITY_OF_PARTS_ON_SHORTENED_ITEMS_URN = 6
const THIRD_PARTY_URN_ID = "collections-thirdparty"
const THIRD_PARTY_URN_ID = 'collections-thirdparty'
const MAPPED_THIRD_PARTY_ITEM_FULL_URN_PROPERTIES_LENGTH = 10

type URNData = {
assetUrn: string
tokenId?: string
network?: string
contractAddress?: string
}

function getTokenIdAndAssetUrn(completeUrn: string): { assetUrn: string; tokenId: string | undefined } {
const lastIndex = completeUrn.lastIndexOf(':')
function getTokenIdAndAssetUrn(completeUrn: string): URNData {
const urnProperties = completeUrn.split(':')
const urnPropertiesLength = urnProperties.length

let result: URNData | undefined

if (
completeUrn.includes(THIRD_PARTY_URN_ID) &&
urnPropertiesLength === MAPPED_THIRD_PARTY_ITEM_FULL_URN_PROPERTIES_LENGTH
) {
result = {
assetUrn: urnProperties.slice(0, 7).join(':'),
tokenId: urnProperties[9],
contractAddress: urnProperties[8],
network: urnProperties[7],
}
} else if (urnPropertiesLength > QUANTITY_OF_PARTS_ON_SHORTENED_ITEMS_URN) {
result = { assetUrn: urnProperties.slice(0, -1).join(':'), tokenId: urnProperties[urnProperties.length - 1] }
LautaroPetaccio marked this conversation as resolved.
Show resolved Hide resolved
}

return lastIndex !== -1 && completeUrn.split(':').length > QUANTITY_OF_PARTS_ON_SHORTENED_ITEMS_URN && !completeUrn.includes(THIRD_PARTY_URN_ID)
? { assetUrn: completeUrn.substring(0, lastIndex), tokenId: completeUrn.substring(lastIndex + 1) }
: { assetUrn: completeUrn, tokenId: undefined }
return result ?? { assetUrn: completeUrn, tokenId: undefined }
}

async function fetchItem(urn: string, peerUrl: string) {
Expand Down Expand Up @@ -110,9 +133,11 @@ async function fetchURNs(urns: string[], peerUrl: string): Promise<[WearableDefi
return [[], []]
}

console.log('All URNs', urns)
const sanitizedAssetUrns = urns
.map((urn: string) => urn.replace(/^dcl:\/\/base-avatars\//, 'urn:decentraland:off-chain:base-avatars:'))
.map((urn: string) => getTokenIdAndAssetUrn(urn).assetUrn)
console.log('Sanitized asset urns', sanitizedAssetUrns)

return peerApi.fetchItems(sanitizedAssetUrns, peerUrl)
}
Expand Down