Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Prepare subgraph for The Decentralized Graph Network #7

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"prepare:xdai": "mustache config/xdai.json subgraph.template.yaml > subgraph.yaml",
"prepare:sokol": "mustache config/sokol.json subgraph.template.yaml > subgraph.yaml",

"deploy:studio": "graph deploy --studio poap-ethereum-mainnet",
"deploy:mainnet": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ poap-xyz/poap",
"deploy:ropsten": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ poap-xyz/poap-ropsten",
"deploy:kovan": "graph deploy --node https://api.thegraph.com/deploy/ --ipfs https://api.thegraph.com/ipfs/ poap-xyz/poap-kovan",
Expand All @@ -22,8 +23,8 @@
"deploy-local": "graph deploy --node http://localhost:8020/ --ipfs http://localhost:5001 poap-xyz/poap"
},
"dependencies": {
"@graphprotocol/graph-cli": "0.18.0",
"@graphprotocol/graph-ts": "0.18.0"
"@graphprotocol/graph-cli": "0.26.0",
"@graphprotocol/graph-ts": "0.24.1"
},
"devDependencies": {
"mustache": "^4.0.1"
Expand Down
37 changes: 21 additions & 16 deletions src/mapping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@ export function handleEventToken(ev: EventTokenEvent): void
{
let event = Event.load(ev.params.eventId.toString());
// This handler always run after the transfer handler
let token = Token.load(ev.params.tokenId.toString());
let token = Token.load(ev.params.tokenId.toString())!;
if (event == null) {
event = new Event(ev.params.eventId.toString());
event.tokenCount = BigInt.fromI32(0);
event.transferCount = BigInt.fromI32(0);
event.created = ev.block.timestamp
}

event.tokenCount += BigInt.fromI32(1);
event.transferCount += BigInt.fromI32(1);
event.tokenCount = event.tokenCount.plus(BigInt.fromI32(1));
event.transferCount = event.transferCount.plus(BigInt.fromI32(1));
token.event = event.id;
event.save();
token.save();
Expand All @@ -55,15 +55,15 @@ export function handleTransfer(ev: TransferEvent): void {
// Don't subtracts from the ZERO_ADDRESS (it's the one that mint the token)
// Avoid negative values
if(from.id != ZERO_ADDRESS) {
from.tokensOwned -= BigInt.fromI32(1);
from.tokensOwned = from.tokensOwned.minus(BigInt.fromI32(1));
}
from.save();

if (to == null) {
to = new Account(ev.params.to.toHex());
to.tokensOwned = BigInt.fromI32(0);
}
to.tokensOwned += BigInt.fromI32(1);
to.tokensOwned = to.tokensOwned.plus(BigInt.fromI32(1));
to.save();

if (token == null) {
Expand All @@ -72,24 +72,29 @@ export function handleTransfer(ev: TransferEvent): void {
token.created = ev.block.timestamp
}
token.owner = to.id;
token.transferCount += BigInt.fromI32(1);
token.transferCount = token.transferCount.plus(BigInt.fromI32(1));
token.save();

let event = Event.load(token.event);

if(event != null) {
// Add one transfer
event.transferCount += BigInt.fromI32(1);
if (token.event != null) {
let event = Event.load(token.event as string);

// Burning the token
if(to.id == ZERO_ADDRESS) {
event.tokenCount -= BigInt.fromI32(1);
// Subtract all the transfers from the burned token
event.transferCount -= token.transferCount;
if(event != null) {
// Add one transfer
event.transferCount = event.transferCount.plus(BigInt.fromI32(1));

// Burning the token
if(to.id == ZERO_ADDRESS) {
event.tokenCount = event.tokenCount.minus(BigInt.fromI32(1));
// Subtract all the transfers from the burned token
event.transferCount = event.transferCount.minus(token.transferCount);
}
event.save();
}
event.save();
}



transfer.token = token.id;
transfer.from = from.id;
transfer.to = to.id;
Expand Down
2 changes: 1 addition & 1 deletion subgraph.template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dataSources:
startBlock: {{ startBlock }}
mapping:
kind: ethereum/events
apiVersion: 0.0.4
apiVersion: 0.0.5
language: wasm/assemblyscript
entities:
- EventToken
Expand Down