Skip to content

Commit

Permalink
Edit Mapping and Schema
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeriil committed Mar 24, 2024
1 parent 00b041b commit e4315d8
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,8 @@ node_modules
.vscode
.idea
.vercel

graph-node
data
bin
types
24 changes: 12 additions & 12 deletions packages/subgraph/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ Uses a subgraph from The Graph to index and query blockchain data.

[The Graph](https://thegraph.com/) is a protocol for building decentralized applications (dApps) quickly on Ethereum and IPFS using GraphQL.

- 🗃️ **Decentralized Indexing**: The Graph enables open APIs ("subgraphs") to efficiently index and organize blockchain data.
- 🔎 **Efficient Querying**: The protocol uses GraphQL for streamlined querying blockchain data.
- 🙌 **Community Ecosystem**: The Graph fosters collaboration by empowering developers to build, deploy, and share subgraphs!
- 🗃️ **Decentralized Indexing**: The Graph enables open APIs ("subgraphs") to efficiently index and organize blockchain data.
- 🔎 **Efficient Querying**: The protocol uses GraphQL for streamlined querying blockchain data.
- 🙌 **Community Ecosystem**: The Graph fosters collaboration by empowering developers to build, deploy, and share subgraphs!

For detailed instructions and more context, check out the [Getting Started Guide](https://thegraph.com/docs/en/cookbook/quick-start).

Expand All @@ -18,10 +18,10 @@ For detailed instructions and more context, check out the [Getting Started Guide

Before you begin, you need to install the following tools:

- [Node.js](https://nodejs.org/en/download/)
- Yarn ([v1](https://classic.yarnpkg.com/en/docs/install/) or [v2+](https://yarnpkg.com/getting-started/install))
- [Git](https://git-scm.com/downloads)
- [Docker](https://docs.docker.com/get-docker/)
- [Node.js](https://nodejs.org/en/download/)
- Yarn ([v1](https://classic.yarnpkg.com/en/docs/install/) or [v2+](https://yarnpkg.com/getting-started/install))
- [Git](https://git-scm.com/downloads)
- [Docker](https://docs.docker.com/get-docker/)

 

Expand Down Expand Up @@ -195,11 +195,11 @@ yarn local-ship

> This command does the following all in one… 🚀🚀🚀
- Copies the contracts ABI from the hardhat/deployments folder
- Generates the networks.json file
- Generates AssemblyScript types from the subgraph schema and the contract ABIs.
- Compiles and checks the mapping functions.
- … and deploy a local subgraph!
- Copies the contracts ABI from the hardhat/deployments folder
- Generates the networks.json file
- Generates AssemblyScript types from the subgraph schema and the contract ABIs.
- Compiles and checks the mapping functions.
- … and deploy a local subgraph!

> If you get an error ts-node you can install it with the following command
Expand Down
31 changes: 31 additions & 0 deletions packages/subgraph/abis/localhost_Loogie.json
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,37 @@
"name": "ApprovalForAll",
"type": "event"
},
{
"anonymous": false,
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes3",
"name": "color",
"type": "bytes3"
},
{
"indexed": false,
"internalType": "uint256",
"name": "chubbiness",
"type": "uint256"
},
{
"indexed": false,
"internalType": "uint256",
"name": "mouthLength",
"type": "uint256"
}
],
"name": "LoogieCreated",
"type": "event"
},
{
"anonymous": false,
"inputs": [
Expand Down
10 changes: 8 additions & 2 deletions packages/subgraph/abis/sepolia_Loogie.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,18 +65,24 @@
"inputs": [
{
"indexed": true,
"internalType": "uint256",
"name": "tokenId",
"type": "uint256"
},
{
"indexed": false,
"internalType": "bytes3",
"name": "color",
"type": "bytes3"
},
{
"indexed": true,
"indexed": false,
"internalType": "uint256",
"name": "chubbiness",
"type": "uint256"
},
{
"indexed": true,
"indexed": false,
"internalType": "uint256",
"name": "mouthLength",
"type": "uint256"
Expand Down
22 changes: 20 additions & 2 deletions packages/subgraph/networks.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,25 @@
{
"sepolia": {
"LoogieAuction": {
"address": "0x210b3B8cc13720cCbb8208Ab4d328Ff488E49112",
"startBlock": 5539160
},
"Loogie": {
"address": "0x83296AFF01553E6386fAe17d50118E0659f82678"
},
"WETH": {
"address": "0xf7d541135a61b54BD2F79acD7cB1b2223dC9EB8F"
}
},
"localhost": {
"YourContract": {
"Loogie": {
"address": "0x5FbDB2315678afecb367f032d93F642f64180aa3"
},
"LoogieAuction": {
"address": "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
},
"WETH": {
"address": "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512"
}
}
}
}
4 changes: 2 additions & 2 deletions packages/subgraph/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
"type": "module",
"scripts": {
"abi-copy": "node --loader ts-node/esm --experimental-specifier-resolution=node scripts/abi_copy.ts",
"codegen": "graph codegen",
"codegen": "graph codegen --output-dir src/types/",
"build": "graph build",
"deploy": "graph deploy --node https://api.studio.thegraph.com/deploy/ loogie",
"deploy": " graph auth --studio cb2365b3c5f1e9ef2f3b6fcace14c9ba && graph deploy --node https://api.studio.thegraph.com/deploy/ dailyloogies",
"local-create": "graph create --node http://localhost:8020/ loogie",
"local-remove": "graph remove --node http://localhost:8020/ loogie",
"local-deploy": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 loogie",
Expand Down
33 changes: 31 additions & 2 deletions packages/subgraph/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ type Bid @entity {
id: ID!

"The Noun being bid on"
loogie: Noun!
loogie: Loogie!

"Bid amount"
amount: BigInt!
Expand All @@ -28,6 +28,9 @@ type Auction @entity {
"Auction ID"
id: ID!

"The Loogie"
loogie: Loogie!

"Total amount of the auction"
amount: BigInt!

Expand All @@ -53,15 +56,41 @@ type Loogie @entity {

"The owner of the Loogie`s"
owner: Account!

color: Bytes!

chubbiness: BigInt!

mouthLength: BigInt!
}

type Account @entity {
"An Account is any address that holds any amount of Loogies, the id used is the blockchain address."
id: ID!

"Loogie`s balance of this address expressed as a BigInt normalized value for the Nouns ERC721 Token"
"Loogie`s balance of this address expressed as a BigInt normalized value for the ERC721 Token"
tokenBalance: BigInt!

"The Loogies owned by this account"
loogies: [Loogie!]!
}

type TransferEvent @entity {
"The txn hash of this event"
id: ID!

"The Loogie being transfered"
loogie: Loogie!

"Previous holder address"
previousHolder: Account!

"New holder address"
newHolder: Account!

"Block number of the event"
blockNumber: BigInt!

"The timestamp of the block the event is in"
blockTimestamp: BigInt!
}
168 changes: 141 additions & 27 deletions packages/subgraph/src/mapping.ts
Original file line number Diff line number Diff line change
@@ -1,35 +1,149 @@
import { BigInt, Address } from "@graphprotocol/graph-ts";
import { LoogieCreated, Transfer } from "./types/Loogie/Loogie";
import { Loogie, TransferEvent, Bid, Auction, Account } from "./types/schema";
import { getOrCreateAccount } from "./util/helper";
import {
YourContract,
GreetingChange,
} from "../generated/YourContract/YourContract";
import { Greeting, Sender } from "../generated/schema";

export function handleGreetingChange(event: GreetingChange): void {
let senderString = event.params.greetingSetter.toHexString();

let sender = Sender.load(senderString);

if (sender === null) {
sender = new Sender(senderString);
sender.address = event.params.greetingSetter;
sender.createdAt = event.block.timestamp;
sender.greetingCount = BigInt.fromI32(1);
} else {
sender.greetingCount = sender.greetingCount.plus(BigInt.fromI32(1));
AuctionBid,
AuctionCreated,
AuctionExtended,
AuctionSettled,
} from "./types/LoogieAuction/LoogieAuction";
import { log } from "@graphprotocol/graph-ts";

export function handleCreatedLoogie(event: LoogieCreated): void {
let loogieId = event.params.tokenId.toString();

let loogie = Loogie.load(loogieId);
if (loogie == null) {
log.critical("[handleCreatedLoogie] Loogie #{} not found. Hash: {}", [
loogieId,
event.transaction.hash.toHex(),
]);
return;
}
loogie.chubbiness = event.params.chubbiness;
loogie.color = event.params.color;
loogie.mouthLength = event.params.mouthLength;
loogie.save();
}
export function handleTransfer(event: Transfer): void {
// Get the sender and receiver accounts
let fromAccount = getOrCreateAccount(event.params.from.toHexString());
let toAccount = getOrCreateAccount(event.params.to.toHexString());

// Get the transferred Loogie ID
let transferredLoogieId = event.params.tokenId.toString();

// Load the Loogie entity
let loogie = Loogie.load(transferredLoogieId);

// Check if the Loogie exists
if (loogie == null) {
log.error("[handleTransfer] Loogie #{} not found. Hash: {}", [
transferredLoogieId,
event.transaction.hash.toHex(),
]);
return;
}

let greeting = new Greeting(
event.transaction.hash.toHex() + "-" + event.logIndex.toString()
// Update ownership status
loogie.owner = toAccount.id;

// Save changes to the Loogie entity
loogie.save();

// Create a new TransferEvent entity
let transferEvent = new TransferEvent(
event.transaction.hash.toHexString() + "_" + transferredLoogieId
);
transferEvent.blockNumber = event.block.number;
transferEvent.blockTimestamp = event.block.timestamp;
transferEvent.loogie = transferredLoogieId;
transferEvent.previousHolder = fromAccount.id;
transferEvent.newHolder = toAccount.id;
transferEvent.save();
}

export function handleAuctionCreated(event: AuctionCreated): void {
const loogieId = event.params.loogieId.toString();

let loogie = Loogie.load(loogieId);
if (loogie == null) {
log.error("[handleAuctionCreated] Loogie #{} not found. Hash: {}", [
loogieId,
event.transaction.hash.toHex(),
]);
return;
}

let auction = new Auction(loogieId);
auction.loogie = loogie.id;
auction.amount = BigInt.fromI32(0);
auction.startTime = event.params.startTime;
auction.endTime = event.params.endTime;
auction.settled = false;
auction.save();
}

greeting.greeting = event.params.newGreeting;
greeting.sender = senderString;
greeting.premium = event.params.premium;
greeting.value = event.params.value;
greeting.createdAt = event.block.timestamp;
greeting.transactionHash = event.transaction.hash.toHex();
export function handleAuctionBid(event: AuctionBid): void {
let loogieId = event.params.loogieId.toString();
let bidderAddress = event.params.sender.toHex();

let bidder = getOrCreateAccount(bidderAddress);

let auction = Auction.load(loogieId);
if (auction == null) {
log.error("[handleAuctionBid] Auction not found for Loogie #{}. Hash: {}", [
loogieId,
event.transaction.hash.toHex(),
]);
return;
}

auction.amount = event.params.value;
auction.bidder = bidder.id;
auction.save();

// Save Bid
let bid = new Bid(event.transaction.hash.toHex());
bid.bidder = bidder.id;
bid.amount = auction.amount;
bid.loogie = auction.loogie;
bid.txIndex = event.transaction.index;
bid.blockNumber = event.block.number;
bid.blockTimestamp = event.block.timestamp;
bid.auction = auction.id;
bid.save();
}

export function handleAuctionExtended(event: AuctionExtended): void {
let loogieId = event.params.loogieId.toString();

let auction = Auction.load(loogieId);
if (auction == null) {
log.error(
"[handleAuctionExtended] Auction not found for Loogie #{}. Hash: {}",
[loogieId, event.transaction.hash.toHex()]
);
return;
}

auction.endTime = event.params.endTime;
auction.save();
}

export function handleAuctionSettled(event: AuctionSettled): void {
let loogieId = event.params.loogieId.toString();

let auction = Auction.load(loogieId);
if (auction == null) {
log.error(
"[handleAuctionSettled] Auction not found for Noun #{}. Hash: {}",
[loogieId, event.transaction.hash.toHex()]
);
return;
}

greeting.save();
sender.save();
auction.settled = true;
auction.save();
}
Loading

0 comments on commit e4315d8

Please sign in to comment.