Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: flag orders as transferred once they were transfer in status open #113

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10,000 changes: 4,086 additions & 5,914 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"test:docker": "npm test -- --docker"
},
"devDependencies": {
"@graphprotocol/graph-cli": "^0.51.2",
"@graphprotocol/graph-cli": "^0.95.0",
"@graphprotocol/graph-ts": "^0.31.0",
"matchstick-as": "^0.5.0",
"mustache": "^4.0.1",
Expand Down
1 change: 1 addition & 0 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ enum OrderStatus @entity {
open
sold
cancelled
transferred
}

# thegraph doesn't support nested property searches, so we're doing promoting properties
Expand Down
14 changes: 7 additions & 7 deletions src/entities/templates/ERC721/ERC721.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
Entity,
Bytes,
Address,
BigInt
BigInt,
} from "@graphprotocol/graph-ts";

export class Transfer extends ethereum.Event {
Expand Down Expand Up @@ -189,15 +189,15 @@ export class ERC721 extends ethereum.SmartContract {

ownerOf(_tokenId: BigInt): Address {
let result = super.call("ownerOf", "ownerOf(uint256):(address)", [
ethereum.Value.fromUnsignedBigInt(_tokenId)
ethereum.Value.fromUnsignedBigInt(_tokenId),
]);

return result[0].toAddress();
}

try_ownerOf(_tokenId: BigInt): ethereum.CallResult<Address> {
let result = super.tryCall("ownerOf", "ownerOf(uint256):(address)", [
ethereum.Value.fromUnsignedBigInt(_tokenId)
ethereum.Value.fromUnsignedBigInt(_tokenId),
]);
if (result.reverted) {
return new ethereum.CallResult();
Expand All @@ -208,15 +208,15 @@ export class ERC721 extends ethereum.SmartContract {

tokenURI(_tokenId: BigInt): string {
let result = super.call("tokenURI", "tokenURI(uint256):(string)", [
ethereum.Value.fromUnsignedBigInt(_tokenId)
ethereum.Value.fromUnsignedBigInt(_tokenId),
]);

return result[0].toString();
}

try_tokenURI(_tokenId: BigInt): ethereum.CallResult<string> {
let result = super.tryCall("tokenURI", "tokenURI(uint256):(string)", [
ethereum.Value.fromUnsignedBigInt(_tokenId)
ethereum.Value.fromUnsignedBigInt(_tokenId),
]);
if (result.reverted) {
return new ethereum.CallResult();
Expand All @@ -227,15 +227,15 @@ export class ERC721 extends ethereum.SmartContract {

balanceOf(_owner: Address): BigInt {
let result = super.call("balanceOf", "balanceOf(address):(uint256)", [
ethereum.Value.fromAddress(_owner)
ethereum.Value.fromAddress(_owner),
]);

return result[0].toBigInt();
}

try_balanceOf(_owner: Address): ethereum.CallResult<BigInt> {
let result = super.tryCall("balanceOf", "balanceOf(address):(uint256)", [
ethereum.Value.fromAddress(_owner)
ethereum.Value.fromAddress(_owner),
]);
if (result.reverted) {
return new ethereum.CallResult();
Expand Down
13 changes: 5 additions & 8 deletions src/handlers/nft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,16 @@ import {
getURNForCollectionV1,
getURNForWearableV1
} from '../modules/metadata/wearable'
import { getNFTId, getTokenURI, isMint, cancelActiveOrder, clearNFTOrderProperties } from '../modules/nft'
import { NFT, Item, Collection, Mint } from '../entities/schema'
import { getNFTId, getTokenURI, isMint, cancelActiveOrder, clearNFTOrderProperties, handleTransferOrder } from '../modules/nft'
import { NFT, Item, Collection, Mint, Order } from '../entities/schema'
import { buildCountFromNFT, buildCountFromItem } from '../modules/count'
import { Issue, Transfer, CollectionV2 as CollectionContract } from '../entities/templates/CollectionV2/CollectionV2'
import { CollectionStore } from '../entities/templates/CollectionStore/CollectionStore'
import { Transfer as ERC721Transfer, AddWearable } from '../entities/templates/ERC721/ERC721'
import { getStoreAddress } from '../modules/store'
import { MINT_SALE_TYPE, trackSale } from '../modules/analytics'
import { toLowerCase } from '../utils'
import * as status from '../modules/order'

/**
* @notice mint an NFT by a collection v2 issue event
Expand Down Expand Up @@ -127,9 +128,7 @@ export function handleTransferNFT(event: Transfer): void {
nft.updatedAt = event.block.timestamp
nft.transferredAt = event.block.timestamp

if (cancelActiveOrder(nft, event.block.timestamp)) {
nft = clearNFTOrderProperties(nft)
}
handleTransferOrder(nft, event.params.to)

createOrLoadAccount(event.params.to)

Expand Down Expand Up @@ -290,9 +289,7 @@ export function handleTransferWearableV1(event: ERC721Transfer): void {
mint.save()
} else {
let oldNFT = NFT.load(id)
if (cancelActiveOrder(oldNFT!, event.block.timestamp)) {
nft = clearNFTOrderProperties(nft)
}
handleTransferOrder(oldNFT, event.params.to)
}

createOrLoadAccount(event.params.to)
Expand Down
26 changes: 23 additions & 3 deletions src/modules/nft/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BigInt, log, Address } from '@graphprotocol/graph-ts'
import { BigInt, log, Address, Bytes } from '@graphprotocol/graph-ts'

import * as status from '../order'
import { ZERO_ADDRESS } from '../account'
Expand Down Expand Up @@ -45,8 +45,8 @@ export function cancelActiveOrder(nft: NFT, now: BigInt): boolean {
if (!nft.activeOrder) {
return false
}
let oldOrder = Order.load(nft.activeOrder!)
if (oldOrder != null && oldOrder.status == status.OPEN) {
let oldOrder = Order.load(nft.activeOrder)
if (oldOrder != null && (oldOrder.status == status.OPEN || oldOrder.status == status.TRANSFERRED)) {
// Here we are setting old orders as cancelled, because the smart contract allows new orders to be created
// and they just overwrite them in place. But the subgraph stores all orders ever
// you can also overwrite ones that are expired
Expand All @@ -73,3 +73,23 @@ export function getTokenURI(collectionAddress: Address, tokenId: BigInt): string

return tokenURI
}

export function handleTransferOrder(nft: NFT | null, to: Bytes): void {
if (nft != null && nft.activeOrder != null) {
let oldOrder = Order.load(nft.activeOrder!)
if (oldOrder != null && oldOrder.status == status.OPEN) {
oldOrder.status = status.TRANSFERRED
oldOrder.save()
nft.searchOrderStatus = status.TRANSFERRED
} else if (oldOrder != null && oldOrder.status == status.TRANSFERRED) {
let isComingBackToOrderOwner = oldOrder.owner == to
if (isComingBackToOrderOwner) {
oldOrder.status = status.OPEN
oldOrder.save()
nft.searchOrderStatus = status.OPEN
} else {
nft.searchOrderStatus = status.TRANSFERRED
}
}
}
}
3 changes: 2 additions & 1 deletion src/modules/order/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const OPEN = 'open'
export const SOLD = 'sold'
export const CANCELLED = 'cancelled'
export const CANCELLED = 'cancelled'
export const TRANSFERRED = 'transferred'