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

Added Time Series #4

Open
wants to merge 3 commits into
base: main
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
70 changes: 65 additions & 5 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,89 @@ enum TransactionType {
}

type TokenHolderTransaction @entity(immutable: true) {
id: ID! # <tokenholder ID>-transaction-logIndex
" <tokenholder ID>-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! # <token ID>-holder
" <token ID>-holder "
id: ID!

" Normalized balance as a decimal "
balance: BigDecimal!

" Holder Wallet Address "
holder: Bytes!

" 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
# 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: TokenHolder!

" 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!
}
20 changes: 20 additions & 0 deletions src/handleEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down