Skip to content

Commit

Permalink
Set ERC721 token owner to 0 by default to avoid possible issue when
Browse files Browse the repository at this point in the history
updating URI for a non existing token
  • Loading branch information
Amxx committed Jan 16, 2024
1 parent 62abf7f commit da9cda4
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
39 changes: 26 additions & 13 deletions src/datasources/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 ?
}
}
}
10 changes: 6 additions & 4 deletions src/fetch/erc721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down

0 comments on commit da9cda4

Please sign in to comment.