diff --git a/packages/subgraph/README.md b/packages/subgraph/README.md index 41cb8bd..7c00563 100644 --- a/packages/subgraph/README.md +++ b/packages/subgraph/README.md @@ -3,24 +3,29 @@ ## Development ```bash -# copy env and add the access token from TheGraph dashboard -cp .env.test .env -# Generate subgraph.yaml manifest based on contract addresses from config directory +# Step 1: Compile abis from contracts package +cd ../contracts +npm install +npx hardhat compile + +# Step 2: In subgraph directory, install dependencies +npm install + +# Step 3: Generate subgraph.yaml manifest annd subgraph types based on contract addresses from config directory, generated contract abis, and user defined schema (schema.graphql) npm run prepare:mumbai npm run prepare:polygon -# Run a test build +# Step 4: Run a test build of your subgraph mappings npm run build -# To re-generate contract abis -cd ../contracts -npx hardhat compile - # TheGraph currently breaks when trying to deploy a contract using custom error :/ -# To deploy you have to manually remove all abi elements with "type": "error" in ../contracts/artifacts/contracts/TimeCollection.sol/TimeCollection.json +# To deploy you have to manually remove all abi elements with "type": "error" in ../contracts/artifacts/contracts/NonFungibleTimeCollection.sol/NonFungibleTimeCollection.json + +# Step 5: copy .env.test to .env and add your subgraph access token from TheGraph dashboard +cp .env.test .env -# Deploy to production, subgraph deployment slug set in package.json (wearenewt/tokenized-time-mumbai) +# Step 6: Deploy to production, subgraph deployment slug set in package.json (wearenewt/tokenized-time-mumbai) npm run deploy:hosted:mumbai npm run deploy:hosted:polygon ``` diff --git a/packages/subgraph/src/mapping/tokenizedtime.ts b/packages/subgraph/src/mapping/tokenizedtime.ts index 16a725c..fa7fa76 100644 --- a/packages/subgraph/src/mapping/tokenizedtime.ts +++ b/packages/subgraph/src/mapping/tokenizedtime.ts @@ -19,7 +19,7 @@ import { Address, log } from '@graphprotocol/graph-ts'; import { ERC721 } from '../../generated/TimeCollection/ERC721'; import { ERC20 } from '../../generated/TimeCollection/ERC20'; -export function getTokenURI(event: Transfer): string { +export function getTokenURI(event: Transfer | TokenRedeemed): string { const erc721 = ERC721.bind(event.address); const tokenURICallResult = erc721.try_tokenURI(event.params.tokenId); @@ -109,6 +109,16 @@ export function handleTokenRedeemed(event: TokenRedeemed): void { const from = event.transaction.from ? event.transaction.from.toHexString() : ''; redeemed.redeemedBy = from; redeemed.creator = nftParams.value5.toHexString(); + const nft = Nft.load(event.params.tokenId.toString()); + if (nft) { + const uri = getTokenURI(event); + nft.tokenURI = uri; + nft.save(); + } else { + log.warning('Token redeem for nft unregistered to subgraph {}', [ + event.params.tokenId.toString(), + ]); + } redeemed.save(); } else { log.warning(`Token redeem event for non-existant tokenId {}`, [ diff --git a/packages/subgraph/subgraph.template.yaml b/packages/subgraph/subgraph.template.yaml index e36b621..171bb66 100644 --- a/packages/subgraph/subgraph.template.yaml +++ b/packages/subgraph/subgraph.template.yaml @@ -25,9 +25,9 @@ dataSources: - PaymentToken abis: - name: TimeCollection - file: ../contracts/artifacts/contracts/TimeCollection.sol/TimeCollection.json + file: ../contracts/artifacts/contracts/NonFungibleTimeCollection.sol/NonFungibleTimeCollection.json - name: ERC721 - file: ../contracts/artifacts/@openzeppelin/contracts/token/ERC721/ERC721.sol/ERC721.json + file: ../contracts/artifacts/@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol/ERC721Upgradeable.json - name: ERC20 file: ../contracts/artifacts/@openzeppelin/contracts/token/ERC20/ERC20.sol/ERC20.json eventHandlers: