Skip to content

Commit

Permalink
Edit Subgraph
Browse files Browse the repository at this point in the history
  • Loading branch information
ceeriil committed Mar 25, 2024
1 parent f183dd0 commit c164112
Show file tree
Hide file tree
Showing 7 changed files with 2,549 additions and 2,958 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ node_modules
graph-node
data
bin
types
types
tests
2 changes: 2 additions & 0 deletions packages/subgraph/graph-node/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ services:
ipfs: "ipfs:5001"
ethereum: "localhost:http://host.docker.internal:8545"
GRAPH_LOG: info
ETHEREUM_REORG_THRESHOLD: 1
ETHEREUM_ANCESTOR_COUNT: 1
extra_hosts:
- "host.docker.internal:host-gateway"
ipfs:
Expand Down
23 changes: 11 additions & 12 deletions packages/subgraph/networks.json
Original file line number Diff line number Diff line change
@@ -1,25 +1,24 @@
{
"sepolia": {
"LoogieAuction": {
"address": "0x210b3B8cc13720cCbb8208Ab4d328Ff488E49112",
"startBlock": 5539160
},
"localhost": {
"Loogie": {
"address": "0x83296AFF01553E6386fAe17d50118E0659f82678"
"address": "0x67d269191c92Caf3cD7723F116c85e6E9bf55933"
},
"LoogieAuction": {
"address": "0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E"
},
"WETH": {
"address": "0xf7d541135a61b54BD2F79acD7cB1b2223dC9EB8F"
"address": "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512"
}
},
"localhost": {
"sepolia": {
"Loogie": {
"address": "0x5FbDB2315678afecb367f032d93F642f64180aa3"
"address": "0x3012Ba8bA56842EC99509f014ac2BC5866e6c4D4"
},
"LoogieAuction": {
"address": "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
"address": "0xa126F5924B6D1E50F536F77502c95eEE9365A574"
},
"WETH": {
"address": "0xe7f1725E7734CE288F8367e1Bb143E90bb3F0512"
"address": "0xf7d541135a61b54BD2F79acD7cB1b2223dC9EB8F"
}
}
}
}
56 changes: 56 additions & 0 deletions packages/subgraph/src/mapping/loogie.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { LoogieCreated, Transfer, LoogieBurned } from "../types/Loogie/Loogie";
import { Loogie, TransferEvent } from "../types/schema";
import { getOrCreateAccount } from "../util/helper";
import { log } from "@graphprotocol/graph-ts";
import { BIGINT_ONE, BIGINT_ZERO } from "../util/constants";

export function handleCreatedLoogie(event: LoogieCreated): void {
let loogieId = event.params.tokenId.toString();
let loogie = new Loogie(loogieId);
loogie.chubbiness = event.params.chubbiness;
loogie.color = event.params.color;
loogie.mouthLength = event.params.mouthLength;
loogie.owner = getOrCreateAccount(event.params.minter.toHexString()).id;

loogie.save();
}

export function handleBurnedLoogie(event: LoogieBurned): void {
let loogieId = event.params.tokenId.toString();
let loogie = Loogie.load(loogieId);
if (loogie == null) {
log.error("[handleBurnedLoogie] Loogie #{} not found. Hash: {}", [
loogieId,
event.transaction.hash.toHex(),
]);
return;
}
loogie.unset(loogieId);
}
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();
// 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();

if (fromAccount.tokenBalance < BIGINT_ZERO) {
log.error("Negative balance on holder {} with balance {}", [
fromAccount.id,
fromAccount.tokenBalance.toString(),
]);
}

fromAccount.tokenBalance = fromAccount.tokenBalance.minus(BIGINT_ONE);
toAccount.tokenBalance = toAccount.tokenBalance.plus(BIGINT_ONE);
}
Original file line number Diff line number Diff line change
@@ -1,68 +1,14 @@
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 { BigInt } from "@graphprotocol/graph-ts";
import { Loogie, Bid, Auction } from "../types/schema";
import { getOrCreateAccount } from "../util/helper";
import {
AuctionBid,
AuctionCreated,
AuctionExtended,
AuctionSettled,
} from "./types/LoogieAuction/LoogieAuction";
} 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;
}

// 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();
}
import { ZERO_ADDRESS } from "../util/constants";

export function handleAuctionCreated(event: AuctionCreated): void {
const loogieId = event.params.loogieId.toString();
Expand All @@ -81,6 +27,7 @@ export function handleAuctionCreated(event: AuctionCreated): void {
auction.amount = BigInt.fromI32(0);
auction.startTime = event.params.startTime;
auction.endTime = event.params.endTime;
auction.bidder = getOrCreateAccount(ZERO_ADDRESS).id;
auction.settled = false;
auction.save();
}
Expand Down
20 changes: 11 additions & 9 deletions packages/subgraph/subgraph.yaml
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
specVersion: 0.0.4
description: "Randomly generated loogie NFTs auctioned daily."
description: Randomly generated loogie NFTs auctioned daily.
schema:
file: "./schema.graphql"
file: ./schema.graphql
dataSources:
- kind: ethereum
name: LoogieAuction
network: localhost
source:
abi: LoogieAuction
address: "0x9fE46736679d2D9a65F0992F2272dE9f3c7fa6e0"
address: "0xE6E340D132b5f46d1e472DebcD681B2aBc16e57E"
mapping:
kind: ethereum/events
apiVersion: 0.0.6
language: wasm/assemblyscript
file: "./src/mapping.ts"
file: ./src/mapping/loogieAuction.ts
entities:
- Bid
- Auction
- Loogie
- Account
abis:
- name: LoogieAuction
file: "./abis/sepolia_LoogieAuction.json"
file: ./abis/localhost_LoogieAuction.json
eventHandlers:
- event: AuctionCreated(indexed uint256,uint256,uint256)
handler: handleAuctionCreated
Expand All @@ -36,20 +36,22 @@ dataSources:
network: localhost
source:
abi: Loogie
address: "0x5FbDB2315678afecb367f032d93F642f64180aa3"
address: "0x67d269191c92Caf3cD7723F116c85e6E9bf55933"
mapping:
kind: ethereum/events
apiVersion: 0.0.6
language: wasm/assemblyscript
file: "./src/mapping.ts"
file: ./src/mapping/loogie.ts
entities:
- Loogie
- Account
abis:
- name: Loogie
file: "./abis/sepolia_Loogie.json"
file: ./abis/localhost_Loogie.json
eventHandlers:
- event: LoogieCreated(indexed uint256,bytes3,uint256,uint256)
- event: LoogieCreated(indexed uint256,bytes3,uint256,uint256,indexed address)
handler: handleCreatedLoogie
- event: LoogieBurned(indexed uint256)
handler: handleBurnedLoogie
- event: Transfer(indexed address,indexed address,indexed uint256)
handler: handleTransfer
Loading

0 comments on commit c164112

Please sign in to comment.