forked from pancakeswap/pancake-subgraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(nft-market): Logic/Handlers (pancakeswap#118)
* refactor: ERC721 utils * refactor: Day(s) update utils * refactor: Collection(s) handlers * refactor: Royalties handler * refactor: Ask (update) handler * refactor: Ask (cancel) handler
- Loading branch information
Showing
6 changed files
with
126 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,44 @@ | ||
/* eslint-disable prefer-const */ | ||
import { CollectionDayData, MarketPlaceDayData } from "../../generated/schema"; | ||
import { Address, BigInt, BigDecimal, ethereum } from "@graphprotocol/graph-ts"; | ||
import { CollectionDayData, MarketPlaceDayData } from "../../generated/schema"; | ||
|
||
// BigNumber-like references | ||
let ZERO_BI = BigInt.fromI32(0); | ||
let ONE_BI = BigInt.fromI32(1); | ||
let ZERO_BD = BigDecimal.fromString("0"); | ||
|
||
export function updateCollectionDayData(collection: Address, newVolumeinBNB: BigDecimal, event: ethereum.Event): void { | ||
export function updateMarketPlaceDayData(newVolumeInBNB: BigDecimal, event: ethereum.Event): void { | ||
let timestamp = event.block.timestamp.toI32(); | ||
let dayID = timestamp / 86400; | ||
let dayStartTimestamp = dayID * 86400; | ||
|
||
let ID = dayID.toString() + "-" + collection.toHexString(); | ||
|
||
let collectionDayData = CollectionDayData.load(ID.toString()); | ||
|
||
if (collectionDayData == null) { | ||
collectionDayData = new CollectionDayData(ID.toString()); | ||
collectionDayData.date = dayStartTimestamp; | ||
collectionDayData.collection = collection.toHexString(); | ||
collectionDayData.dailyVolumeBNB = newVolumeinBNB; | ||
collectionDayData.dailyTrades = ONE_BI; | ||
} else { | ||
collectionDayData.dailyVolumeBNB = collectionDayData.dailyVolumeBNB.plus(newVolumeinBNB); | ||
collectionDayData.dailyTrades = collectionDayData.dailyTrades.plus(ONE_BI); | ||
let marketPlaceDayData = MarketPlaceDayData.load(dayID.toString()); | ||
if (marketPlaceDayData === null) { | ||
marketPlaceDayData = new MarketPlaceDayData(dayID.toString()); | ||
marketPlaceDayData.date = dayStartTimestamp; | ||
marketPlaceDayData.dailyVolumeBNB = ZERO_BD; | ||
marketPlaceDayData.dailyTrades = ZERO_BI; | ||
} | ||
|
||
collectionDayData.save(); | ||
marketPlaceDayData.dailyVolumeBNB = marketPlaceDayData.dailyVolumeBNB.plus(newVolumeInBNB); | ||
marketPlaceDayData.dailyTrades = marketPlaceDayData.dailyTrades.plus(ONE_BI); | ||
marketPlaceDayData.save(); | ||
} | ||
|
||
export function updateMarketPlaceDayData(newVolumeinBNB: BigDecimal, event: ethereum.Event): void { | ||
export function updateCollectionDayData(collection: Address, newVolumeInBNB: BigDecimal, event: ethereum.Event): void { | ||
let timestamp = event.block.timestamp.toI32(); | ||
let dayID = timestamp / 86400; | ||
let dayStartTimestamp = dayID * 86400; | ||
let ID = dayID.toString() + "-" + collection.toHex(); | ||
|
||
let marketPlaceDayData = MarketPlaceDayData.load(dayID.toString()); | ||
|
||
if (marketPlaceDayData == null) { | ||
marketPlaceDayData = new MarketPlaceDayData(dayID.toString()); | ||
marketPlaceDayData.date = dayStartTimestamp; | ||
marketPlaceDayData.dailyVolumeBNB = newVolumeinBNB; | ||
marketPlaceDayData.dailyTrades = ONE_BI; | ||
} else { | ||
marketPlaceDayData.dailyVolumeBNB = marketPlaceDayData.dailyVolumeBNB.plus(newVolumeinBNB); | ||
marketPlaceDayData.dailyTrades = marketPlaceDayData.dailyTrades.plus(ONE_BI); | ||
let collectionDayData = CollectionDayData.load(ID); | ||
if (collectionDayData === null) { | ||
collectionDayData = new CollectionDayData(ID); | ||
collectionDayData.date = dayStartTimestamp; | ||
collectionDayData.collection = collection.toHex(); | ||
collectionDayData.dailyVolumeBNB = ZERO_BD; | ||
collectionDayData.dailyTrades = ZERO_BI; | ||
} | ||
|
||
marketPlaceDayData.save(); | ||
collectionDayData.dailyVolumeBNB = collectionDayData.dailyVolumeBNB.plus(newVolumeInBNB); | ||
collectionDayData.dailyTrades = collectionDayData.dailyTrades.plus(ONE_BI); | ||
collectionDayData.save(); | ||
} |
31 changes: 15 additions & 16 deletions
31
...raphs/nft-market/mappings/utils/ERC721.ts → ...raphs/nft-market/mappings/utils/erc721.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,36 @@ | ||
/* eslint-disable prefer-const */ | ||
import { Address, BigInt } from "@graphprotocol/graph-ts"; | ||
|
||
import { IERC721 } from "../../generated/ERC721NFTMarketV1/IERC721"; | ||
|
||
export function fetchCollectionName(collectionAddress: Address): string { | ||
export function fetchName(collectionAddress: Address): string { | ||
let contract = IERC721.bind(collectionAddress); | ||
let nameValue = "unknown"; | ||
let nameResult = contract.try_name(); | ||
|
||
let nameResult = contract.try_name(); | ||
if (!nameResult.reverted) { | ||
nameValue = nameResult.value; | ||
return nameResult.value; | ||
} | ||
return nameValue; | ||
|
||
return "unknown"; | ||
} | ||
|
||
export function fetchCollectionSymbol(collectionAddress: Address): string { | ||
export function fetchSymbol(collectionAddress: Address): string { | ||
let contract = IERC721.bind(collectionAddress); | ||
let symbolValue = "unknown"; | ||
let symbolResult = contract.try_symbol(); | ||
|
||
let symbolResult = contract.try_symbol(); | ||
if (!symbolResult.reverted) { | ||
symbolValue = symbolResult.value; | ||
return symbolResult.value; | ||
} | ||
return symbolValue; | ||
|
||
return "unknown"; | ||
} | ||
|
||
export function fetchTokenURI(collectionAddress: Address, tokenId: BigInt): string { | ||
export function fetchTokenURI(collectionAddress: Address, tokenId: BigInt): string | null { | ||
let contract = IERC721.bind(collectionAddress); | ||
let tokenURIValue = "unknown"; | ||
let tokenURIResult = contract.try_tokenURI(tokenId); | ||
|
||
let tokenURIResult = contract.try_tokenURI(tokenId); | ||
if (!tokenURIResult.reverted) { | ||
tokenURIValue = tokenURIResult.value; | ||
return tokenURIResult.value; | ||
} | ||
return tokenURIValue; | ||
|
||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.