Skip to content

Commit

Permalink
feat: Pancake Squad subgraph (pancakeswap#102)
Browse files Browse the repository at this point in the history
* 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
ChefKai authored Sep 18, 2021
1 parent b6b849c commit 7ec64c6
Show file tree
Hide file tree
Showing 8 changed files with 411 additions and 6 deletions.
14 changes: 8 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,19 @@ Currently, there are multiple subgraphs, but additional subgraphs can be added t

5. **[Pairs](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/pairs)**: Tracks all PancakeSwap Pairs and Tokens.

6. **[Prediction (v1)](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/prediction)**: Tracks all PancakeSwap Prediction (v1) with market, rounds, and bets.
6. **[Pancake Squad](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/pancake-squad)**: Tracks all Pancake Squad metrics with owners, tokens, and transactions.

7. **[Prediction (v2)](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/prediction-v2)**: Tracks all PancakeSwap Prediction (v2) with market, rounds, and bets.
7. **[Prediction (v1)](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/prediction)**: Tracks all PancakeSwap Prediction (v1) with market, rounds, and bets.

8. **[Profile](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/profile)**: Tracks all PancakeSwap Profile with teams, users, points and campaigns.
8. **[Prediction (v2)](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/prediction-v2)**: Tracks all PancakeSwap Prediction (v2) with market, rounds, and bets.

9. **[SmartChef](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/smartchef)**: Tracks all PancakeSwap SmartChef (a.k.a. Syrup Pools) with tokens and rewards.
9. **[Profile](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/profile)**: Tracks all PancakeSwap Profile with teams, users, points and campaigns.

10. **[Timelock](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/timelock)**: Tracks all PancakeSwap Timelock queued, executed, and cancelled transactions.
10. **[SmartChef](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/smartchef)**: Tracks all PancakeSwap SmartChef (a.k.a. Syrup Pools) with tokens and rewards.

11. **[Trading Competition (v1)](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/trading-competition-v1)**: Tracks all metrics for the Easter Battle (April 07—14, 2021).
11. **[Timelock](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/timelock)**: Tracks all PancakeSwap Timelock queued, executed, and cancelled transactions.

12. **[Trading Competition (v1)](https://thegraph.com/legacy-explorer/subgraph/pancakeswap/trading-competition-v1)**: Tracks all metrics for the Easter Battle (April 07—14, 2021).

## v1

Expand Down
72 changes: 72 additions & 0 deletions subgraphs/pancake-squad/abis/ERC721.json
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"
}
]
114 changes: 114 additions & 0 deletions subgraphs/pancake-squad/mappings/index.ts
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();
}
36 changes: 36 additions & 0 deletions subgraphs/pancake-squad/mappings/utils/erc-721.ts
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;
}
10 changes: 10 additions & 0 deletions subgraphs/pancake-squad/mappings/utils/index.ts
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()
);
}
13 changes: 13 additions & 0 deletions subgraphs/pancake-squad/package.json
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"
}
}
Loading

0 comments on commit 7ec64c6

Please sign in to comment.