Skip to content

Commit

Permalink
deduplicate metadata construction code
Browse files Browse the repository at this point in the history
only adds metadata relation if metadata is good

Signed-off-by: stadolf <[email protected]>
  • Loading branch information
elmariachi111 committed Oct 8, 2024
1 parent 9130b99 commit bd86bfe
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 19 deletions.
35 changes: 19 additions & 16 deletions subgraph/src/ipnftMapping.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
Address,
BigInt,
ByteArray,
crypto,
ethereum,
Expand Down Expand Up @@ -74,18 +75,28 @@ export function handleReservation(event: ReservedEvent): void {
reservation.save()
}

function updateIpnftMetadata(ipnft: Ipnft, uri: string, timestamp: BigInt): Ipnft {
let ipfsLocation = uri.replace('ipfs://', '');
if (!ipfsLocation || ipfsLocation == uri) {
log.error("Invalid URI format for tokenId {}: {}", [ipnft.id, uri]);
return ipnft;
}

ipnft.tokenURI = uri;
ipnft.metadata = ipfsLocation;
ipnft.updatedAtTimestamp = timestamp;
IpnftMetadataTemplate.create(ipfsLocation);

return ipnft;
}

//the underlying parameter arrays are misaligned, hence we cannot cast or unify both events
export function handleMint(event: IPNFTMintedEvent): void {
let ipnft = new Ipnft(event.params.tokenId.toString())
ipnft.owner = event.params.owner
ipnft.tokenURI = event.params.tokenURI
ipnft.createdAt = event.block.timestamp
ipnft.symbol = event.params.symbol
let ipfsLocation = event.params.tokenURI.replace('ipfs://', '');
ipnft.metadata = ipfsLocation
ipnft.updatedAtTimestamp = event.block.timestamp
IpnftMetadataTemplate.create(ipfsLocation)

ipnft = updateIpnftMetadata(ipnft, event.params.tokenURI, event.block.timestamp)
store.remove('Reservation', event.params.tokenId.toString())
ipnft.save()

Expand All @@ -101,19 +112,11 @@ export function handleMetadataUpdated(event: MetadataUpdateEvent): void {
//erc4906 is not emitting the new url, we must query it ourselves
let _ipnftContract = IPNFTContract.bind(event.params._event.address);
let newUri = _ipnftContract.tokenURI(event.params._tokenId)
if (!newUri) {
if (!newUri || newUri == "") {
log.debug("no new uri found for token, likely just minted {}", [event.params._tokenId.toString()])
return
}

ipnft.tokenURI = newUri

let ipfsLocation = newUri.replace('ipfs://', '');
ipnft.updatedAtTimestamp = event.block.timestamp
ipnft.metadata = ipfsLocation

IpnftMetadataTemplate.create(ipfsLocation)

ipnft = updateIpnftMetadata(ipnft, newUri, event.block.timestamp)
ipnft.save()
}

6 changes: 3 additions & 3 deletions subgraph/src/metadataMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,21 @@ import { json, Bytes, dataSource } from '@graphprotocol/graph-ts'
import { IpnftMetadata } from '../generated/schema'

export function handleMetadata(content: Bytes): void {
let ipnftMetadata = new IpnftMetadata(dataSource.stringParam())
const value = json.fromBytes(content).toObject()
if (value) {
const image = value.get('image')
const name = value.get('name')
const description = value.get('description')
const externalURL = value.get('external_url')

if (name && image && description && externalURL) {
let ipnftMetadata = new IpnftMetadata(dataSource.stringParam())
ipnftMetadata.name = name.toString()
ipnftMetadata.image = image.toString()
ipnftMetadata.externalURL = externalURL.toString()
ipnftMetadata.description = description.toString()
ipnftMetadata.save()
}

ipnftMetadata.save()
}
}

0 comments on commit bd86bfe

Please sign in to comment.