11import {
22 Address ,
3+ BigInt ,
34 ByteArray ,
45 crypto ,
56 ethereum ,
67 log ,
78 store
89} from '@graphprotocol/graph-ts'
910import {
11+ IPNFT as IPNFTContract ,
1012 IPNFTMinted as IPNFTMintedEvent ,
11- Reserved as ReservedEvent ,
12- ReadAccessGranted as ReadAccessGrantedEvent ,
13- Transfer as TransferEvent ,
1413 MetadataUpdate as MetadataUpdateEvent ,
15- IPNFT as IPNFTContract
14+ ReadAccessGranted as ReadAccessGrantedEvent ,
15+ Reserved as ReservedEvent ,
16+ Transfer as TransferEvent
1617} from '../generated/IPNFT/IPNFT'
17- import { Ipnft , Reservation , CanRead } from '../generated/schema'
18+ import { IpnftMetadata as IpnftMetadataTemplate } from '../generated/templates'
19+ import { CanRead , Ipnft , Reservation } from '../generated/schema'
1820
1921export function handleTransfer ( event : TransferEvent ) : void {
2022 if ( event . params . to == Address . zero ( ) ) {
@@ -73,15 +75,29 @@ export function handleReservation(event: ReservedEvent): void {
7375 reservation . save ( )
7476}
7577
78+ function updateIpnftMetadata ( ipnft : Ipnft , uri : string , timestamp : BigInt ) : void {
79+ let ipfsLocation = uri . replace ( 'ipfs://' , '' ) ;
80+ if ( ! ipfsLocation || ipfsLocation == uri ) {
81+ log . error ( "Invalid URI format for tokenId {}: {}" , [ ipnft . id , uri ] )
82+ return
83+ }
84+
85+ ipnft . tokenURI = uri
86+ ipnft . metadata = ipfsLocation
87+ ipnft . updatedAtTimestamp = timestamp
88+ IpnftMetadataTemplate . create ( ipfsLocation )
89+ }
90+
7691//the underlying parameter arrays are misaligned, hence we cannot cast or unify both events
7792export function handleMint ( event : IPNFTMintedEvent ) : void {
7893 let ipnft = new Ipnft ( event . params . tokenId . toString ( ) )
7994 ipnft . owner = event . params . owner
80- ipnft . tokenURI = event . params . tokenURI
8195 ipnft . createdAt = event . block . timestamp
8296 ipnft . symbol = event . params . symbol
97+ updateIpnftMetadata ( ipnft , event . params . tokenURI , event . block . timestamp )
8398 store . remove ( 'Reservation' , event . params . tokenId . toString ( ) )
8499 ipnft . save ( )
100+
85101}
86102
87103export function handleMetadataUpdated ( event : MetadataUpdateEvent ) : void {
@@ -94,8 +110,11 @@ export function handleMetadataUpdated(event: MetadataUpdateEvent): void {
94110 //erc4906 is not emitting the new url, we must query it ourselves
95111 let _ipnftContract = IPNFTContract . bind ( event . params . _event . address ) ;
96112 let newUri = _ipnftContract . tokenURI ( event . params . _tokenId )
97-
98- ipnft . tokenURI = newUri
113+ if ( ! newUri || newUri == "" ) {
114+ log . debug ( "no new uri found for token, likely just minted {}" , [ event . params . _tokenId . toString ( ) ] )
115+ return
116+ }
117+ updateIpnftMetadata ( ipnft , newUri , event . block . timestamp )
99118 ipnft . save ( )
100119}
101120
0 commit comments