Skip to content

Commit c2968cb

Browse files
committed
adds metadata templates to the subgraph
Signed-off-by: stadolf <[email protected]>
1 parent 06417d6 commit c2968cb

File tree

5 files changed

+70
-7
lines changed

5 files changed

+70
-7
lines changed

subgraph/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"build:sepolia": "graph codegen && graph build --network sepolia",
99
"build:mainnet": "graph codegen && graph build --network mainnet",
1010
"deploy:local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 moleculeprotocol/ipnft-subgraph",
11-
"deploy:sepolia": "env-cmd -x -f ../.env graph deploy ip-nft-sepolia --version-label 1.1.1 --node https://subgraphs.alchemy.com/api/subgraphs/deploy --ipfs https://ipfs.satsuma.xyz --deploy-key \\$SATSUMA_DEPLOY_KEY",
11+
"deploy:sepolia": "env-cmd -x -f ../.env graph deploy ip-nft-sepolia --version-label 1.2.2 --node https://subgraphs.alchemy.com/api/subgraphs/deploy --ipfs https://ipfs.satsuma.xyz --deploy-key \\$SATSUMA_DEPLOY_KEY",
1212
"deploy:mainnet": "env-cmd -x -f ../.env graph deploy ip-nft-mainnet --version-label 1.1.0 --node https://subgraphs.alchemy.com/api/subgraphs/deploy --ipfs https://ipfs.satsuma.xyz --deploy-key \\$SATSUMA_DEPLOY_KEY",
1313
"create:local": "graph create --node http://localhost:8020/ moleculeprotocol/ipnft-subgraph",
1414
"remove:local": "graph remove --node http://localhost:8020/ moleculeprotocol/ipnft-subgraph",

subgraph/schema.graphql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ type Ipnft @entity {
77
listings: [Listing!] @derivedFrom(field: "ipnft")
88
readers: [CanRead!] @derivedFrom(field: "ipnft")
99
ipts: [IPT!] @derivedFrom(field: "ipnft")
10+
metadata: IpnftMetadata
11+
updatedAtTimestamp: BigInt
12+
}
13+
14+
type IpnftMetadata @entity {
15+
id: ID!
16+
name: String!
17+
image: String!
18+
description: String!
19+
externalURL: String!
1020
}
1121

1222
type IPT @entity {

subgraph/src/ipnftMapping.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,15 @@ import {
77
store
88
} from '@graphprotocol/graph-ts'
99
import {
10+
IPNFT as IPNFTContract,
1011
IPNFTMinted as IPNFTMintedEvent,
11-
Reserved as ReservedEvent,
12-
ReadAccessGranted as ReadAccessGrantedEvent,
13-
Transfer as TransferEvent,
1412
MetadataUpdate as MetadataUpdateEvent,
15-
IPNFT as IPNFTContract
13+
ReadAccessGranted as ReadAccessGrantedEvent,
14+
Reserved as ReservedEvent,
15+
Transfer as TransferEvent
1616
} from '../generated/IPNFT/IPNFT'
17-
import { Ipnft, Reservation, CanRead } from '../generated/schema'
17+
import { IpnftMetadata as IpnftMetadataTemplate } from '../generated/templates'
18+
import { CanRead, Ipnft, Reservation } from '../generated/schema'
1819

1920
export function handleTransfer(event: TransferEvent): void {
2021
if (event.params.to == Address.zero()) {
@@ -80,8 +81,14 @@ export function handleMint(event: IPNFTMintedEvent): void {
8081
ipnft.tokenURI = event.params.tokenURI
8182
ipnft.createdAt = event.block.timestamp
8283
ipnft.symbol = event.params.symbol
84+
let ipfsLocation = event.params.tokenURI.replace('ipfs://', '');
85+
ipnft.metadata = ipfsLocation
86+
ipnft.updatedAtTimestamp = event.block.timestamp
87+
IpnftMetadataTemplate.create(ipfsLocation)
88+
8389
store.remove('Reservation', event.params.tokenId.toString())
8490
ipnft.save()
91+
8592
}
8693

8794
export function handleMetadataUpdated(event: MetadataUpdateEvent): void {
@@ -94,8 +101,19 @@ export function handleMetadataUpdated(event: MetadataUpdateEvent): void {
94101
//erc4906 is not emitting the new url, we must query it ourselves
95102
let _ipnftContract = IPNFTContract.bind(event.params._event.address);
96103
let newUri = _ipnftContract.tokenURI(event.params._tokenId)
104+
if (!newUri) {
105+
log.debug("no new uri found for token, likely just minted {}", [event.params._tokenId.toString()])
106+
return
107+
}
97108

98109
ipnft.tokenURI = newUri
110+
111+
let ipfsLocation = newUri.replace('ipfs://', '');
112+
ipnft.updatedAtTimestamp = event.block.timestamp
113+
ipnft.metadata = ipfsLocation
114+
115+
IpnftMetadataTemplate.create(ipfsLocation)
116+
99117
ipnft.save()
100118
}
101119

subgraph/src/metadataMapping.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { json, Bytes, dataSource } from '@graphprotocol/graph-ts'
2+
import { IpnftMetadata } from '../generated/schema'
3+
4+
export function handleMetadata(content: Bytes): void {
5+
let ipnftMetadata = new IpnftMetadata(dataSource.stringParam())
6+
const value = json.fromBytes(content).toObject()
7+
if (value) {
8+
const image = value.get('image')
9+
const name = value.get('name')
10+
const description = value.get('description')
11+
const externalURL = value.get('external_url')
12+
13+
if (name && image && description && externalURL) {
14+
ipnftMetadata.name = name.toString()
15+
ipnftMetadata.image = image.toString()
16+
ipnftMetadata.externalURL = externalURL.toString()
17+
ipnftMetadata.description = description.toString()
18+
}
19+
20+
ipnftMetadata.save()
21+
}
22+
}

subgraph/subgraph.yaml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ dataSources:
3232
file: ./src/ipnftMapping.ts
3333
- kind: ethereum/contract
3434
name: SchmackoSwap
35-
network: foundry
35+
network: sepolia
3636
source:
3737
abi: SchmackoSwap
3838
address: "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
@@ -228,3 +228,16 @@ templates:
228228
handler: handleScheduled
229229
- event: ScheduleReleased(indexed bytes32,indexed address,uint256)
230230
handler: handleReleased
231+
- name: IpnftMetadata
232+
kind: file/ipfs
233+
mapping:
234+
apiVersion: 0.0.7
235+
language: wasm/assemblyscript
236+
file: ./src/metadataMapping.ts
237+
handler: handleMetadata
238+
entities:
239+
- IpnftMetadata
240+
abis:
241+
- name: IPNFT
242+
file: ./abis/IPNFT.json
243+
network: foundry

0 commit comments

Comments
 (0)