Skip to content

Commit

Permalink
refactor(nft-market): Logic/Handlers (pancakeswap#118)
Browse files Browse the repository at this point in the history
* 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
ChefKai authored Sep 22, 2021
1 parent b163f5f commit d9e5f90
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 132 deletions.
138 changes: 66 additions & 72 deletions subgraphs/nft-market/mappings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,43 +11,43 @@ import {
RevenueClaim,
Trade,
} from "../generated/ERC721NFTMarketV1/ERC721NFTMarketV1";

import { toBigDecimal } from "./utils";
import { updateCollectionDayData, updateMarketPlaceDayData } from "./utils/dayUpdates";
import { fetchCollectionName, fetchCollectionSymbol, fetchTokenURI } from "./utils/ERC721";
import { fetchName, fetchSymbol, fetchTokenURI } from "./utils/erc721";

// Constants
let ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";

// BigNumber-like references
let ZERO_BI = BigInt.fromI32(0);
let ONE_BI = BigInt.fromI32(1);
let ZERO_BD = BigDecimal.fromString("0");
let ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";

/**
* COLLECTION
* COLLECTION(S)
*/

export function handleCollectionNew(event: CollectionNew): void {
let collection = Collection.load(event.params.collection.toHex());
if (collection == null) {
collection = new Collection(event.params.collection.toHex());
collection.name = fetchCollectionName(event.params.collection);
collection.symbol = fetchCollectionSymbol(event.params.collection);
collection.name = fetchName(event.params.collection);
collection.symbol = fetchSymbol(event.params.collection);
collection.active = true;
collection.totalTrades = ZERO_BI;
collection.totalVolumeBNB = ZERO_BD;
collection.numberTokensListed = ZERO_BI;
collection.creatorAddress = event.params.creator.toHex();
collection.tradingFee = toBigDecimal(event.params.tradingFee, 2);
collection.creatorFee = toBigDecimal(event.params.creatorFee, 2);
collection.whitelistChecker = event.params.whitelistChecker.toHex();
} else {
// Collection has existed, was closed, but is re-listed
collection.active = true;
collection.creatorAddress = event.params.creator.toHex();
collection.creatorAddress = event.params.creator;
collection.tradingFee = toBigDecimal(event.params.tradingFee, 2);
collection.creatorFee = toBigDecimal(event.params.creatorFee, 2);
collection.whitelistChecker = event.params.whitelistChecker.toHex();
collection.whitelistChecker = event.params.whitelistChecker;
collection.save();
}
collection.active = true;
collection.creatorAddress = event.params.creator;
collection.tradingFee = toBigDecimal(event.params.tradingFee, 2);
collection.creatorFee = toBigDecimal(event.params.creatorFee, 2);
collection.whitelistChecker = event.params.whitelistChecker;
collection.save();
}

Expand All @@ -62,10 +62,10 @@ export function handleCollectionClose(event: CollectionClose): void {
export function handleCollectionUpdate(event: CollectionUpdate): void {
let collection = Collection.load(event.params.collection.toHex());
if (collection !== null) {
collection.creatorAddress = event.params.creator.toHex();
collection.creatorAddress = event.params.creator;
collection.tradingFee = toBigDecimal(event.params.tradingFee, 2);
collection.creatorFee = toBigDecimal(event.params.creatorFee, 2);
collection.whitelistChecker = event.params.whitelistChecker.toHex();
collection.whitelistChecker = event.params.whitelistChecker;
collection.save();
}
}
Expand Down Expand Up @@ -93,7 +93,6 @@ export function handleAskNew(event: AskNew): void {

// 2. Collection
let collection = Collection.load(event.params.collection.toHex());

collection.numberTokensListed = collection.numberTokensListed.plus(ONE_BI);

// 3. Token
Expand Down Expand Up @@ -122,7 +121,7 @@ export function handleAskNew(event: AskNew): void {
order.timestamp = event.block.timestamp;
order.collection = event.params.collection.toHex();
order.nft = event.params.collection.toHexString() + "-" + event.params.tokenId.toString();
order.orderType = "NEW";
order.orderType = "New";
order.askPrice = toBigDecimal(event.params.askPrice, 18);
order.seller = event.params.seller.toHex();

Expand All @@ -133,59 +132,57 @@ export function handleAskNew(event: AskNew): void {
}

export function handleAskCancel(event: AskCancel): void {
// 1. User
let user = User.load(event.params.seller.toHex());
user.numberTokensListed = user.numberTokensListed.minus(ONE_BI);
if (user !== null) {
user.numberTokensListed = user.numberTokensListed.minus(ONE_BI);
user.save();
}

// 2. Collection
let collection = Collection.load(event.params.collection.toHex());
collection.numberTokensListed = collection.numberTokensListed.minus(ONE_BI);

// 3. Token
let tokenConcatId = event.params.collection.toHexString() + "-" + event.params.tokenId.toString();
let token = NFT.load(tokenConcatId);

token.currentSeller = ZERO_ADDRESS;
token.updatedAt = event.block.timestamp;
token.currentAskPrice = ZERO_BD;
token.isTradable = false;
if (collection != null) {
collection.numberTokensListed = collection.numberTokensListed.minus(ONE_BI);
collection.save();
}

// 4. Ask Order
let order = new AskOrder(event.transaction.hash.toHexString());
order.block = event.block.number;
order.timestamp = event.block.timestamp;
order.collection = event.params.collection.toHex();
order.nft = event.params.collection.toHexString() + "-" + event.params.tokenId.toString();
order.orderType = "CANCEL";
order.askPrice = toBigDecimal(ZERO_BI, 18);
order.seller = event.params.seller.toHex();
let token = NFT.load(event.params.collection.toHex() + "-" + event.params.tokenId.toString());
if (token !== null) {
token.currentSeller = ZERO_ADDRESS;
token.updatedAt = event.block.timestamp;
token.currentAskPrice = ZERO_BD;
token.isTradable = false;
token.save();
}

user.save();
collection.save();
token.save();
order.save();
if (token !== null && collection !== null) {
let order = new AskOrder(event.transaction.hash.toHex());
order.block = event.block.number;
order.timestamp = event.block.timestamp;
order.collection = ZERO_ADDRESS;
order.nft = ZERO_ADDRESS;
order.orderType = "Cancel";
order.askPrice = toBigDecimal(ZERO_BI, 18);
order.seller = event.params.seller.toHex();
order.save();
}
}

export function handleAskUpdate(event: AskUpdate): void {
// 1. Token
let tokenConcatId = event.params.collection.toHexString() + "-" + event.params.tokenId.toString();
let token = NFT.load(tokenConcatId);

token.updatedAt = event.block.timestamp;
token.currentAskPrice = toBigDecimal(event.params.askPrice, 18);

// 2. Order
let order = new AskOrder(event.transaction.hash.toHexString());
order.block = event.block.number;
order.timestamp = event.block.timestamp;
order.collection = event.params.collection.toHex();
order.nft = event.params.collection.toHexString() + "-" + event.params.tokenId.toString();
order.orderType = "MODIFY";
order.askPrice = toBigDecimal(event.params.askPrice, 18);
order.seller = event.params.seller.toHex();

token.save();
order.save();
let token = NFT.load(event.params.collection.toHex() + "-" + event.params.tokenId.toString());
if (token !== null) {
token.updatedAt = event.block.timestamp;
token.currentAskPrice = toBigDecimal(event.params.askPrice, 18);
token.save();

let order = new AskOrder(event.transaction.hash.toHex());
order.block = event.block.number;
order.timestamp = event.block.timestamp;
order.collection = token.collection;
order.nft = event.params.collection.toHex() + "-" + event.params.tokenId.toString();
order.orderType = "Modify";
order.askPrice = toBigDecimal(event.params.askPrice, 18);
order.seller = event.params.seller.toHex();
order.save();
}
}

/**
Expand Down Expand Up @@ -258,31 +255,28 @@ export function handleTrade(event: Trade): void {
seller.save();
token.save();

// 5. Update day data for collection and marketplace
updateCollectionDayData(event.params.collection, toBigDecimal(event.params.askPrice, 18), event);
updateMarketPlaceDayData(toBigDecimal(event.params.askPrice, 18), event);
}

/**
* REVENUE CLAIMS BY CREATOR/TREASURY
* ROYALTIES
*/

export function handleRevenueClaim(event: RevenueClaim): void {
let user = User.load(event.params.claimer.toHex());

if (user == null) {
if (user === null) {
user = new User(event.params.claimer.toHex());
user.numberTokensListed = ZERO_BI;
user.numberTokensPurchased = ZERO_BI;
user.numberTokensSold = ZERO_BI;
user.totalVolumeInBNBTokensPurchased = ZERO_BD;
user.totalVolumeInBNBTokensSold = ZERO_BD;
user.totalFeesCollectedInBNB = toBigDecimal(event.params.amount, 18);
user.totalFeesCollectedInBNB = ZERO_BD;
user.averageTokenPriceInBNBPurchased = ZERO_BD;
user.averageTokenPriceInBNBSold = ZERO_BD;
} else {
user.totalFeesCollectedInBNB = user.totalFeesCollectedInBNB.plus(toBigDecimal(event.params.amount, 18));
user.save();
}

user.totalFeesCollectedInBNB = user.totalFeesCollectedInBNB.plus(toBigDecimal(event.params.amount, 18));
user.save();
}
55 changes: 25 additions & 30 deletions subgraphs/nft-market/mappings/utils/dayUpdates.ts
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();
}
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;
}
2 changes: 1 addition & 1 deletion subgraphs/nft-market/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@
"scripts": {
"codegen": "graph codegen subgraph.yaml",
"build": "graph build subgraph.yaml",
"deploy": "graph deploy --product hosted-service --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ pancakeswap/nft-market subgraph.yaml"
"deploy": "graph deploy --product hosted-service --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ chefkai/pancake subgraph.yaml"
}
}
Loading

0 comments on commit d9e5f90

Please sign in to comment.