From da9cda4e012a87508943c6f8372fcb855cd32f1a Mon Sep 17 00:00:00 2001 From: Hadrien Croubois Date: Tue, 16 Jan 2024 10:51:26 +0100 Subject: [PATCH] Set ERC721 token owner to 0 by default to avoid possible issue when updating URI for a non existing token --- src/datasources/erc721.ts | 39 ++++++++++++++++++++++++++------------- src/fetch/erc721.ts | 10 ++++++---- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/datasources/erc721.ts b/src/datasources/erc721.ts index 71d141b..f28f7d8 100644 --- a/src/datasources/erc721.ts +++ b/src/datasources/erc721.ts @@ -135,27 +135,40 @@ export function handleApprovalForAll(event: ApprovalForAllEvent): void { } export function handleMetadataUpdate(event: MetadataUpdateEvent) : void { + let erc721 = IERC721.bind(event.address) let contract = fetchERC721(event.address) - if (contract != null) { - let erc721 = IERC721.bind(Address.fromBytes(contract.id)) - let token = fetchERC721Token(contract, event.params._tokenId) - let try_tokenURI = erc721.try_tokenURI(event.params._tokenId) - token.uri = try_tokenURI.reverted ? '' : try_tokenURI.value - token.save() + if (contract != null) { + if (contract.supportsMetadata) { + let token = fetchERC721Token(contract, event.params._tokenId) + let try_tokenURI = erc721.try_tokenURI(event.params._tokenId) + token.uri = try_tokenURI.reverted ? '' : try_tokenURI.value + token.save() + } else { + // add a warning ? + } } } export function handleBatchMetadataUpdate(event: BatchMetadataUpdateEvent) : void { + let erc721 = IERC721.bind(event.address) let contract = fetchERC721(event.address) - if (contract != null) { - let erc721 = IERC721.bind(Address.fromBytes(contract.id)) - for (let tokenId = event.params._fromTokenId.toU64(); tokenId <= event.params._toTokenId.toU64(); ++tokenId) { - let token = fetchERC721Token(contract, BigInt.fromU64(tokenId)) - let try_tokenURI = erc721.try_tokenURI(BigInt.fromU64(tokenId)) - token.uri = try_tokenURI.reverted ? '' : try_tokenURI.value - token.save() + if (contract != null) { + if (contract.supportsMetadata) { + const from = event.params._fromTokenId.toU64(); + const to = event.params._toTokenId.toU64(); + // Updates of blocks larger than 5000 tokens may DoS the subgraph, we skip them + if (to - from <= 5000) { + for (let tokenId =from; tokenId <= to; ++tokenId) { + let token = fetchERC721Token(contract, BigInt.fromU64(tokenId)) + let try_tokenURI = erc721.try_tokenURI(BigInt.fromU64(tokenId)) + token.uri = try_tokenURI.reverted ? '' : try_tokenURI.value + token.save() + } + } + } else { + // add a warning ? } } } diff --git a/src/fetch/erc721.ts b/src/fetch/erc721.ts index 4b9f028..af864a6 100644 --- a/src/fetch/erc721.ts +++ b/src/fetch/erc721.ts @@ -71,10 +71,12 @@ export function fetchERC721Token(contract: ERC721Contract, identifier: BigInt): let token = ERC721Token.load(id) if (token == null) { - token = new ERC721Token(id) - token.contract = contract.id - token.identifier = identifier - token.approval = fetchAccount(Address.zero()).id + const AddressZero = fetchAccount(Address.zero()) + token = new ERC721Token(id) + token.contract = contract.id + token.identifier = identifier + token.owner = AddressZero.id + token.approval = AddressZero.id if (contract.supportsMetadata) { let erc721 = IERC721.bind(Address.fromBytes(contract.id))