Skip to content

Commit

Permalink
add poi event emitting
Browse files Browse the repository at this point in the history
  • Loading branch information
nour-karoui committed Oct 24, 2024
1 parent 3587a0d commit e1f8fb4
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 18 deletions.
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions src/IPNFT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;
}

Expand Down
58 changes: 58 additions & 0 deletions subgraph/abis/IPNFT.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
1 change: 1 addition & 0 deletions subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ type Ipnft @entity {
ipts: [IPT!] @derivedFrom(field: "ipnft")
metadata: IpnftMetadata
updatedAtTimestamp: BigInt
poi: Bytes
}

type IpnftMetadata @entity {
Expand Down
50 changes: 33 additions & 17 deletions subgraph/src/ipnftMapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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()
}

2 changes: 2 additions & 0 deletions subgraph/subgraph.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e1f8fb4

Please sign in to comment.