From 720d2c2c93222b279be0d7074daef8d493f8f59c Mon Sep 17 00:00:00 2001 From: Data Nexus Date: Sat, 8 Oct 2022 14:30:15 -0400 Subject: [PATCH 1/3] Added Holder Daily Snapshot Added Holder Daily Snapshot --- schema.graphql | 67 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 62 insertions(+), 5 deletions(-) diff --git a/schema.graphql b/schema.graphql index 7441d09..3f660c5 100644 --- a/schema.graphql +++ b/schema.graphql @@ -5,29 +5,86 @@ enum TransactionType { } type TokenHolderTransaction @entity(immutable: true) { - id: ID! # -transaction-logIndex + " -transaction-logIndex " + id: ID! + + " Normalized balance as a decimal " balance: BigDecimal! + + " Block Number " block: BigInt! - date: String! # ISO 8601 format + + " Date in ISO 8601 format " + date: String! + + " (TRANSFER/MINT/BURN) " type: TransactionType! + + " Token Holder sub-entity " holder: TokenHolder! + + " Balance prior to the Transfer " previousBalance: BigDecimal! - timestamp: String! # Unix format + + " Time in unix format " + timestamp: String! + + " Transaction Hash " transaction: Bytes! + + " Transaction Event Log Index " transactionLogIndex: BigInt! + + " Value of the Transfer " value: BigDecimal! } type TokenHolder @entity { - id: ID! # -holder + " -holder " + id: ID! + + " Normalized balance as a decimal " balance: BigDecimal! + + " Holder Wallet Address " holder: Bytes! + + " Id of the Token " token: Token! } +# Snapshot of the users balance as of a day period, multiple transactions can maintain a singular day record +# If a user does not have a transaction for the day no record will exist and the prior record can be relied upon. +type HolderDailySnapshot @entity { + " { Holder ID }-{ Unix Day (timestamp/86400) } " + id: ID! + + " The Token Holder " + holder: Holder! + + " Balance at the time of the snapshot " + balance: BigDecimal! + + " Block number of this snapshot " + blockNumber: BigInt! + + " Timestamp of this snapshot " + timestamp: BigInt! + + " Day Start in Unix " + dayStart: BigInt! +} + type Token @entity { - id: ID! # token-blockchain + " token-blockchain " + id: ID! + + " Contract Address for the Token " address: Bytes! + + " Blockchain Name " blockchain: String! + + " Token Name " name: String! } From 3fb8153a9d5f5f084d1be9692a2cea81dde1c755 Mon Sep 17 00:00:00 2001 From: Data Nexus Date: Sat, 8 Oct 2022 20:23:13 -0400 Subject: [PATCH 2/3] Added Time Series in updateTokenBalance --- src/handleEvent.ts | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/handleEvent.ts b/src/handleEvent.ts index a50ef66..52d1515 100644 --- a/src/handleEvent.ts +++ b/src/handleEvent.ts @@ -190,6 +190,26 @@ function updateTokenBalance( // Update the TokenHolder tokenHolder.balance = newBalance; tokenHolder.save(); + + // The timestamp is in seconds, when we divide by 86400 we will get a integer representation for the day + let day = block.timestamp.toI32() / 86400; + + // Token Holder id - Day + let _holderDailySnapshotId = tokenHolder.id + "-" + day.toString(); + + let _holderDailySnapshot = MarketDailySnapshot.load(_holderDailySnapshotId); + + if (!_holderDailySnapshot) { + _holderDailySnapshot = new MarketDailySnapshot(_holderDailySnapshotId); + + _holderDailySnapshot.holder = tokenHolder.id + _holderDailySnapshot.dayStart = day * 86400 + } + + _holderDailySnapshot.balance = tokenHolder.balance + _holderDailySnapshot.blockNumber = block + _holderDailySnapshot.timestamp = timestamp + _holderDailySnapshot.save() } export function handleTransfer(event: Transfer): void { From 4481e8b2de9416d34e31cdd81a5147d95479ec08 Mon Sep 17 00:00:00 2001 From: Data Nexus Date: Sat, 8 Oct 2022 20:27:37 -0400 Subject: [PATCH 3/3] Fixed type and added derived field --- schema.graphql | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/schema.graphql b/schema.graphql index 3f660c5..55312df 100644 --- a/schema.graphql +++ b/schema.graphql @@ -51,6 +51,9 @@ type TokenHolder @entity { " Id of the Token " token: Token! + + " Sub entity to the daily snapshots for the TokenHolder " + dailySnapshots: [TokenHolder!]! @derivedFrom(field: "holder") } # Snapshot of the users balance as of a day period, multiple transactions can maintain a singular day record @@ -60,7 +63,7 @@ type HolderDailySnapshot @entity { id: ID! " The Token Holder " - holder: Holder! + holder: TokenHolder! " Balance at the time of the snapshot " balance: BigDecimal!