From e1f8fb4ceee3889f26b275e5f40013a471c61f77 Mon Sep 17 00:00:00 2001 From: nour-karoui Date: Thu, 24 Oct 2024 16:32:40 +0200 Subject: [PATCH] add poi event emitting --- docker-compose.yml | 2 +- src/IPNFT.sol | 2 ++ subgraph/abis/IPNFT.json | 58 ++++++++++++++++++++++++++++++++++++ subgraph/schema.graphql | 1 + subgraph/src/ipnftMapping.ts | 50 ++++++++++++++++++++----------- subgraph/subgraph.yaml | 2 ++ 6 files changed, 97 insertions(+), 18 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index c60f2d9d..8c2242d1 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -28,7 +28,7 @@ services: postgres_pass: let-me-in postgres_db: graph-node ipfs: 'ipfs:5001' - ethereum: 'mainnet:http://anvil:8545' + ethereum: 'foundry:http://anvil:8545' GRAPH_LOG: debug GRAPH_ALLOW_NON_DETERMINISTIC_IPFS: 1 ipfs: diff --git a/src/IPNFT.sol b/src/IPNFT.sol index ebf9c9be..c339cdb6 100644 --- a/src/IPNFT.sol +++ b/src/IPNFT.sol @@ -46,6 +46,7 @@ contract IPNFT is ERC721URIStorageUpgradeable, ERC721BurnableUpgradeable, IReser event Reserved(address indexed reserver, uint256 indexed reservationId); event IPNFTMinted(address indexed owner, uint256 indexed tokenId, string tokenURI, string symbol); + event IPNFTPOI(uint256 indexed tokenId, bytes poi); event ReadAccessGranted(uint256 indexed tokenId, address indexed reader, uint256 until); event AuthorizerUpdated(address authorizer); @@ -133,6 +134,7 @@ contract IPNFT is ERC721URIStorageUpgradeable, ERC721BurnableUpgradeable, IReser _mint(to, computedTokenId); _setTokenURI(computedTokenId, _tokenURI); emit IPNFTMinted(to, computedTokenId, _tokenURI, _symbol); + emit IPNFTPOI(computedTokenId, poi); return computedTokenId; } diff --git a/subgraph/abis/IPNFT.json b/subgraph/abis/IPNFT.json index b03fe6f6..2055ed4d 100644 --- a/subgraph/abis/IPNFT.json +++ b/subgraph/abis/IPNFT.json @@ -226,6 +226,45 @@ ], "stateMutability": "payable" }, + { + "type": "function", + "name": "mintWithPOI", + "inputs": [ + { + "name": "to", + "type": "address", + "internalType": "address" + }, + { + "name": "poi", + "type": "bytes", + "internalType": "bytes" + }, + { + "name": "_tokenURI", + "type": "string", + "internalType": "string" + }, + { + "name": "_symbol", + "type": "string", + "internalType": "string" + }, + { + "name": "authorization", + "type": "bytes", + "internalType": "bytes" + } + ], + "outputs": [ + { + "name": "", + "type": "uint256", + "internalType": "uint256" + } + ], + "stateMutability": "payable" + }, { "type": "function", "name": "name", @@ -721,6 +760,25 @@ ], "anonymous": false }, + { + "type": "event", + "name": "IPNFTPOI", + "inputs": [ + { + "name": "tokenId", + "type": "uint256", + "indexed": true, + "internalType": "uint256" + }, + { + "name": "poi", + "type": "bytes", + "indexed": false, + "internalType": "bytes" + } + ], + "anonymous": false + }, { "type": "event", "name": "Initialized", diff --git a/subgraph/schema.graphql b/subgraph/schema.graphql index 07965d53..341e1100 100644 --- a/subgraph/schema.graphql +++ b/subgraph/schema.graphql @@ -9,6 +9,7 @@ type Ipnft @entity { ipts: [IPT!] @derivedFrom(field: "ipnft") metadata: IpnftMetadata updatedAtTimestamp: BigInt + poi: Bytes } type IpnftMetadata @entity { diff --git a/subgraph/src/ipnftMapping.ts b/subgraph/src/ipnftMapping.ts index d97ad381..c9d4ddf9 100644 --- a/subgraph/src/ipnftMapping.ts +++ b/subgraph/src/ipnftMapping.ts @@ -13,7 +13,8 @@ import { MetadataUpdate as MetadataUpdateEvent, ReadAccessGranted as ReadAccessGrantedEvent, Reserved as ReservedEvent, - Transfer as TransferEvent + Transfer as TransferEvent, + IPNFTPOI as IPNFTPOIEvent } from '../generated/IPNFT/IPNFT' import { IpnftMetadata as IpnftMetadataTemplate } from '../generated/templates' import { CanRead, Ipnft, Reservation } from '../generated/schema' @@ -75,17 +76,21 @@ export function handleReservation(event: ReservedEvent): void { reservation.save() } -function updateIpnftMetadata(ipnft: Ipnft, uri: string, timestamp: BigInt): void { - let ipfsLocation = uri.replace('ipfs://', ''); - if (!ipfsLocation || ipfsLocation == uri) { - log.error("Invalid URI format for tokenId {}: {}", [ipnft.id, uri]) - return - } +function updateIpnftMetadata( + ipnft: Ipnft, + uri: string, + timestamp: BigInt +): void { + let ipfsLocation = uri.replace('ipfs://', '') + if (!ipfsLocation || ipfsLocation == uri) { + log.error('Invalid URI format for tokenId {}: {}', [ipnft.id, uri]) + return + } - ipnft.tokenURI = uri - ipnft.metadata = ipfsLocation - ipnft.updatedAtTimestamp = timestamp - IpnftMetadataTemplate.create(ipfsLocation) + ipnft.tokenURI = uri + ipnft.metadata = ipfsLocation + ipnft.updatedAtTimestamp = timestamp + IpnftMetadataTemplate.create(ipfsLocation) } //the underlying parameter arrays are misaligned, hence we cannot cast or unify both events @@ -97,7 +102,17 @@ export function handleMint(event: IPNFTMintedEvent): void { updateIpnftMetadata(ipnft, event.params.tokenURI, event.block.timestamp) store.remove('Reservation', event.params.tokenId.toString()) ipnft.save() +} + +export function handlePOI(event: IPNFTPOIEvent): void { + let ipnft = Ipnft.load(event.params.tokenId.toString()) + if (!ipnft) { + log.error('ipnft {} not found', [event.params.tokenId.toString()]) + return + } + ipnft.poi = event.params.poi + ipnft.save() } export function handleMetadataUpdated(event: MetadataUpdateEvent): void { @@ -108,13 +123,14 @@ 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 _ipnftContract = IPNFTContract.bind(event.params._event.address) let newUri = _ipnftContract.tokenURI(event.params._tokenId) - if (!newUri || newUri == "") { - log.debug("no new uri found for token, likely just minted {}", [event.params._tokenId.toString()]) - return + if (!newUri || newUri == '') { + log.debug('no new uri found for token, likely just minted {}', [ + event.params._tokenId.toString() + ]) + return } - updateIpnftMetadata(ipnft, newUri, event.block.timestamp) + updateIpnftMetadata(ipnft, newUri, event.block.timestamp) ipnft.save() } - diff --git a/subgraph/subgraph.yaml b/subgraph/subgraph.yaml index 14713803..11694228 100644 --- a/subgraph/subgraph.yaml +++ b/subgraph/subgraph.yaml @@ -23,6 +23,8 @@ dataSources: handler: handleReservation - event: IPNFTMinted(indexed address,indexed uint256,string,string) handler: handleMint + - event: IPNFTPOI(indexed uint256,bytes) + handler: handlePOI - event: Transfer(indexed address,indexed address,indexed uint256) handler: handleTransfer - event: ReadAccessGranted(indexed uint256,indexed address,uint256)