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.
feat: Pancake Squad subgraph (pancakeswap#102)
* feat: CAKE Club subgraph * fix(cake-club): Import statement * fix(cake-club): Gas Price as BigDecimal * feat(cake-club): Owner full-text search * chore(cake-club): Rebranding to Pancake Squad * feat(cake-club): Improve entities saving * chore(cake-club): Comments * feat(cake-club): Add Creator property * feat(cake-club): Add Total Tokens Minted property * feat(cake-club): Modify createdAt/updatedAt properties * chore(cake-club): Rename Creator to Minter * feat(cake-club): Add Transaction Hash property * fix(cake-club): Total Tokens property * feat(cake-club): Add Name/Symbol/TokenUri properties
- Loading branch information
Showing
8 changed files
with
411 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,72 @@ | ||
[ | ||
{ | ||
"inputs": [], | ||
"name": "name", | ||
"outputs": [ | ||
{ | ||
"internalType": "string", | ||
"name": "", | ||
"type": "string" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [], | ||
"name": "symbol", | ||
"outputs": [ | ||
{ | ||
"internalType": "string", | ||
"name": "", | ||
"type": "string" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"inputs": [ | ||
{ | ||
"internalType": "uint256", | ||
"name": "tokenId", | ||
"type": "uint256" | ||
} | ||
], | ||
"name": "tokenURI", | ||
"outputs": [ | ||
{ | ||
"internalType": "string", | ||
"name": "", | ||
"type": "string" | ||
} | ||
], | ||
"stateMutability": "view", | ||
"type": "function" | ||
}, | ||
{ | ||
"anonymous": false, | ||
"inputs": [ | ||
{ | ||
"indexed": true, | ||
"internalType": "address", | ||
"name": "from", | ||
"type": "address" | ||
}, | ||
{ | ||
"indexed": true, | ||
"internalType": "address", | ||
"name": "to", | ||
"type": "address" | ||
}, | ||
{ | ||
"indexed": true, | ||
"internalType": "uint256", | ||
"name": "tokenId", | ||
"type": "uint256" | ||
} | ||
], | ||
"name": "Transfer", | ||
"type": "event" | ||
} | ||
] |
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 |
---|---|---|
@@ -0,0 +1,114 @@ | ||
/* eslint-disable prefer-const */ | ||
import { Address, BigInt } from "@graphprotocol/graph-ts"; | ||
import { Contract, Owner, Token, Transaction } from "../generated/schema"; | ||
import { Transfer } from "../generated/ERC721/ERC721"; | ||
import { toBigDecimal } from "./utils"; | ||
import { fetchName, fetchSymbol, fetchTokenUri } from "./utils/erc-721"; | ||
|
||
// Constants | ||
let CONTRACT_ADDRESS = "0x0000000000000000000000000000000000001000"; | ||
let ZERO_ADDRESS = "0x0000000000000000000000000000000000000000"; | ||
|
||
// BigNumber-like references | ||
let ZERO_BI = BigInt.fromI32(0); | ||
let ONE_BI = BigInt.fromI32(1); | ||
|
||
export function handleTransfer(event: Transfer): void { | ||
let contract = Contract.load(CONTRACT_ADDRESS); | ||
if (contract === null) { | ||
// Contract | ||
contract = new Contract(CONTRACT_ADDRESS); | ||
contract.name = fetchName(); | ||
contract.symbol = fetchSymbol(); | ||
contract.totalTokens = ZERO_BI; | ||
contract.totalOwners = ZERO_BI; | ||
contract.totalTransactions = ZERO_BI; | ||
contract.save(); | ||
} | ||
contract.totalTransactions = contract.totalTransactions.plus(ONE_BI); | ||
contract.save(); | ||
|
||
let from = Owner.load(event.params.from.toHex()); | ||
if (from === null) { | ||
// Owner - as Sender | ||
from = new Owner(event.params.from.toHex()); | ||
from.totalTokens = ZERO_BI; | ||
from.totalTokensMinted = ZERO_BI; | ||
from.totalTransactions = ZERO_BI; | ||
from.block = event.block.number; | ||
from.createdAt = event.block.timestamp; | ||
from.updatedAt = event.block.timestamp; | ||
from.save(); | ||
|
||
// Contract | ||
contract.totalOwners = contract.totalOwners.plus(ONE_BI); | ||
contract.save(); | ||
} | ||
from.totalTokens = event.params.from.equals(Address.fromString(ZERO_ADDRESS)) | ||
? from.totalTokens | ||
: from.totalTokens.minus(ONE_BI); | ||
from.totalTransactions = from.totalTransactions.plus(ONE_BI); | ||
from.updatedAt = event.block.timestamp; | ||
from.save(); | ||
|
||
let to = Owner.load(event.params.to.toHex()); | ||
if (to === null) { | ||
// Owner - as Receiver | ||
to = new Owner(event.params.to.toHex()); | ||
to.totalTokens = ZERO_BI; | ||
to.totalTokensMinted = ZERO_BI; | ||
to.totalTransactions = ZERO_BI; | ||
to.block = event.block.number; | ||
to.createdAt = event.block.timestamp; | ||
to.updatedAt = event.block.timestamp; | ||
to.save(); | ||
|
||
// Contract | ||
contract.totalOwners = contract.totalOwners.plus(ONE_BI); | ||
contract.save(); | ||
} | ||
to.totalTokens = to.totalTokens.plus(ONE_BI); | ||
to.totalTransactions = to.totalTransactions.plus(ONE_BI); | ||
to.updatedAt = event.block.timestamp; | ||
to.save(); | ||
|
||
let token = Token.load(event.params.tokenId.toString()); | ||
if (token === null) { | ||
// Token | ||
token = new Token(event.params.tokenId.toString()); | ||
token.minter = to.id; | ||
token.owner = to.id; | ||
token.burned = false; | ||
token.uri = fetchTokenUri(event.params.tokenId); | ||
token.totalTransactions = ZERO_BI; | ||
token.block = event.block.number; | ||
token.createdAt = event.block.timestamp; | ||
token.updatedAt = event.block.timestamp; | ||
token.save(); | ||
|
||
// Owner - as Receiver | ||
to.totalTokensMinted = to.totalTokensMinted.plus(ONE_BI); | ||
to.save(); | ||
|
||
// Contract | ||
contract.totalTokens = contract.totalTokens.plus(ONE_BI); | ||
contract.save(); | ||
} | ||
token.owner = to.id; | ||
token.burned = event.params.to.equals(Address.fromString(ZERO_ADDRESS)); | ||
token.totalTransactions = token.totalTransactions.plus(ONE_BI); | ||
token.updatedAt = event.block.timestamp; | ||
token.save(); | ||
|
||
// Transaction | ||
let transaction = new Transaction(event.transaction.hash.toHex()); | ||
transaction.hash = event.transaction.hash; | ||
transaction.from = from.id; | ||
transaction.to = to.id; | ||
transaction.token = token.id; | ||
transaction.gasUsed = event.transaction.gasUsed; | ||
transaction.gasPrice = toBigDecimal(event.transaction.gasPrice, 9); | ||
transaction.block = event.block.number; | ||
transaction.timestamp = event.block.timestamp; | ||
transaction.save(); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/* eslint-disable prefer-const */ | ||
import { BigInt, dataSource } from "@graphprotocol/graph-ts"; | ||
import { ERC721 } from "../../generated/ERC721/ERC721"; | ||
|
||
export function fetchName(): string { | ||
let contract = ERC721.bind(dataSource.address()); | ||
|
||
let nameResult = contract.try_name(); | ||
if (!nameResult.reverted) { | ||
return nameResult.value; | ||
} | ||
|
||
return "unknown"; | ||
} | ||
|
||
export function fetchSymbol(): string { | ||
let contract = ERC721.bind(dataSource.address()); | ||
|
||
let symbolResult = contract.try_symbol(); | ||
if (!symbolResult.reverted) { | ||
return symbolResult.value; | ||
} | ||
|
||
return "unknown"; | ||
} | ||
|
||
export function fetchTokenUri(tokenID: BigInt): string | null { | ||
let contract = ERC721.bind(dataSource.address()); | ||
|
||
let uriResult = contract.try_tokenURI(tokenID); | ||
if (!uriResult.reverted) { | ||
return uriResult.value; | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
/* eslint-disable prefer-const */ | ||
import { BigDecimal, BigInt } from "@graphprotocol/graph-ts"; | ||
|
||
export function toBigDecimal(quantity: BigInt, decimals: i32 = 18): BigDecimal { | ||
return quantity.divDecimal( | ||
BigInt.fromI32(10) | ||
.pow(decimals as u8) | ||
.toBigDecimal() | ||
); | ||
} |
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
{ | ||
"name": "pancake-squad", | ||
"description": "PancakeSwap Subgraph", | ||
"version": "1.0.0", | ||
"repository": "[email protected]:pancakeswap/pancake-subgraph.git", | ||
"author": "PancakeSwap", | ||
"license": "MIT", | ||
"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/pancake-squad subgraph.yaml" | ||
} | ||
} |
Oops, something went wrong.