Skip to content

Commit

Permalink
tidy up
Browse files Browse the repository at this point in the history
  • Loading branch information
subject026 committed Aug 7, 2023
1 parent 22222a6 commit 39e6754
Show file tree
Hide file tree
Showing 13 changed files with 298 additions and 248 deletions.
53 changes: 13 additions & 40 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,12 @@ type Token @entity {
transfers: BigInt!
minted: BigInt!
burned: BigInt!
dailySnapshots: [TokenDailySnapshot!]! @derivedFrom(field: "token")
}

type Account @entity {
id: ID!
balances: [AccountBalance!]! @derivedFrom(field: "account")
}

type TokenDailySnapshot @entity {
Expand Down Expand Up @@ -165,44 +171,6 @@ type TokenDailySnapshot @entity {
timestamp: BigInt!
}

type TokenWeeklySnapshot @entity {
" { Token Address }-{ # of days since Unix epoch time } "
id: ID!

" Token this snapshot is associated with "
token: Token!

" Daily total Supply of the token "
weeklyTotalSupply: BigInt!

" Total number of events occurred in a day "
weeklyEventCount: Int!

" Total number of transfers in a day "
weeklyTransferCount: Int!

" Total number of token transfered in a day "
weeklyTransferAmount: BigInt!

" Block number of this snapshot "
blockNumber: BigInt!

" Timestamp of this snapshot "
timestamp: BigInt!
}

type Account @entity {
" Address of the account "
id: ID!

" Token balances that this account holds "
balances: [AccountBalance!]! @derivedFrom(field: "account")

" Token balances snapshot that this account holds "
balanceDailySnapshots: [AccountBalanceDailySnapshot!]!
@derivedFrom(field: "account")
}

type AccountBalance @entity {
" { Address Of the Account }-{ Address of the Token }"
id: ID!
Expand All @@ -213,6 +181,9 @@ type AccountBalance @entity {
" Token address "
token: Token!

" Token balances snapshot that this account holds "
dailySnapshots: [AccountBalanceDailySnapshot!]! @derivedFrom(field: "balance")

" Current account balance "
amount: BigInt!

Expand All @@ -224,7 +195,7 @@ type AccountBalance @entity {
}

type AccountBalanceDailySnapshot @entity {
" { Address Of the Account }-{ Address of the Token }-{ # of hours since Unix epoch time } "
" { Address Of the Account }-{ Address of the Token }-{ date string eg. 2-4-2023 } "
id: ID!

" Account address "
Expand All @@ -233,7 +204,9 @@ type AccountBalanceDailySnapshot @entity {
" Token address "
token: Token!

" Current account balance "
balance: AccountBalance!

" Snapshot account balance "
amount: BigInt!

" Block number in which the balance was last modified "
Expand Down
6 changes: 3 additions & 3 deletions src/modules/AccountBalance.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { BigInt } from "@graphprotocol/graph-ts";

import { Account, AccountBalance, Token } from "../../generated/schema";
import { AccountBalance } from "../../generated/schema";
import { BIGINT_ZERO } from "../constants";

export function createAccountBalanceId(
accountId: string,
tokenId: string
): string {
return accountId + "-" + tokenId;
return tokenId + "-" + accountId;
}

export function getOrCreateAccountBalance(
Expand All @@ -18,7 +18,7 @@ export function getOrCreateAccountBalance(
let previousBalance = AccountBalance.load(balanceId);

if (previousBalance != null) {
return previousBalance as AccountBalance;
return previousBalance;
}

let newBalance = new AccountBalance(balanceId);
Expand Down
5 changes: 3 additions & 2 deletions src/modules/AccountBalanceDailySnapshot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export function createAccountBalanceSnapshotId(
block: ethereum.Block,
accountId: string
): string {
let date = new Date(block.timestamp.toI64());
let date = new Date(block.timestamp.toI64() * 1000);

let day = date.getUTCDate().toString();
let month = (date.getUTCMonth() + 1).toString();
let month = date.getUTCMonth().toString();
let year = date.getUTCFullYear().toString();

let datestring = day + "-" + month + "-" + year;
Expand All @@ -36,6 +36,7 @@ export function getOrCreateAccountBalanceDailySnapshot(

let newSnapshot = new AccountBalanceDailySnapshot(snapshotId);
newSnapshot.account = accountId;
newSnapshot.balance = balanceId;
newSnapshot.token = tokenId;

return newSnapshot;
Expand Down
83 changes: 29 additions & 54 deletions src/modules/Token.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
import { Address, ethereum, BigInt } from "@graphprotocol/graph-ts";
import {
Token,
TokenDailySnapshot,
TokenWeeklySnapshot,
} from "../../generated/schema";
import { Token, TokenDailySnapshot } from "../../generated/schema";
import {
BIGINT_ZERO,
CONTRACT_ADDRESS,
Expand Down Expand Up @@ -43,22 +39,41 @@ export function decreaseTokenSupply(token: Token, amount: BigInt): Token {
return token;
}

/**
* use block.timestamp.toI64 to get number
* @param id string
* @param timestamp number
* @returns id: string
*/
export function createSnapshotID(id: string, block: ethereum.Block): string {
return id + "-" + (block.timestamp.toI64() / SECONDS_PER_DAY).toString();
// /**
// * use block.timestamp.toI64 to get number
// * @param id string
// * @param timestamp number
// * @returns id: string
// */
// export function createSnapshotID(id: string, block: ethereum.Block): string {
// return id + "-" + (block.timestamp.toI64() / SECONDS_PER_DAY).toString();
// }

export function createSnapshotID(
block: ethereum.Block,
accountId: string
): string {
// block timestamp is in seconds!?!?!?
let date = new Date(block.timestamp.toI64() * 1000);

let day = date.getUTCDate().toString();
let month = date.getUTCMonth().toString();
let year = date.getUTCFullYear().toString();

let datestring = day + "-" + month + "-" + year;
// log.debug("generating snapshot id.....!\n", []);
// log.debug("timestamp: " + block.timestamp.toString() + "\n", []);
// log.debug("datestring: " + datestring + "\n", []);

return accountId + "-" + datestring;
}

export function getOrCreateTokenDailySnapshot(
block: ethereum.Block,
tokenId: string,
supply: BigInt
): TokenDailySnapshot {
let snapshotId = createSnapshotID(tokenId, block);
let snapshotId = createSnapshotID(block, tokenId);
let previousSnapshot = TokenDailySnapshot.load(snapshotId);

if (previousSnapshot != null) {
Expand All @@ -75,46 +90,6 @@ export function getOrCreateTokenDailySnapshot(
return newSnapshot;
}

export function getOrCreateTokenWeeklySnapshot(
block: ethereum.Block,
tokenId: string,
supply: BigInt
): TokenWeeklySnapshot {
let snapshotId = createSnapshotID(tokenId, block);
let previousSnapshot = TokenWeeklySnapshot.load(snapshotId);

if (previousSnapshot != null) {
return previousSnapshot as TokenWeeklySnapshot;
}

let newSnapshot = new TokenWeeklySnapshot(snapshotId);
newSnapshot.token = tokenId;
newSnapshot.weeklyTotalSupply = supply;
newSnapshot.weeklyEventCount = 0;
newSnapshot.weeklyTransferCount = 0;
newSnapshot.weeklyTransferAmount = BIGINT_ZERO;

return newSnapshot;
}

export function updateTokenWeeklySnapshot(
block: ethereum.Block,
amount: BigInt,
supply: BigInt,
tokenId: string
): void {
let weeklySnapshot = getOrCreateTokenWeeklySnapshot(block, tokenId, supply);
weeklySnapshot.weeklyEventCount += 1;
weeklySnapshot.weeklyTransferCount += 1;
weeklySnapshot.weeklyTransferAmount = weeklySnapshot.weeklyTransferAmount.plus(
amount
);
weeklySnapshot.blockNumber = block.number;
weeklySnapshot.timestamp = block.timestamp;

weeklySnapshot.save();
}

export function updateTokenDailySnapshot(
block: ethereum.Block,
tokenId: string,
Expand Down
10 changes: 5 additions & 5 deletions src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
updateTokenDailySnapshot,
} from "./Token";
import { TransferEvent } from "../../generated/schema";
import { CONTRACT_ADDRESS } from "../constants";
import { getOrCreateAccount } from "./Account";

export function handleMint(event: Transfer): void {
Expand Down Expand Up @@ -41,8 +40,8 @@ export function handleMint(event: Transfer): void {
token.id
);

account.save();
balance.save();
account.save();
token.save();
}

Expand Down Expand Up @@ -70,8 +69,8 @@ export function handleBurn(event: Transfer): void {
token.id
);

account.save();
balance.save();
account.save();
token.save();
}

Expand Down Expand Up @@ -129,8 +128,9 @@ export function handleTransfer(event: Transfer): void {
);

transferEvent.save();
toAccount.save();
fromAccount.save();
newToBalance.save();
newFromBalance.save();
toAccount.save();
fromAccount.save();
token.save();
}
Empty file added tests/Burn.test.ts
Empty file.
64 changes: 64 additions & 0 deletions tests/Contract.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { logStore } from "matchstick-as/assembly/store";

import { assert, describe, test } from "matchstick-as/assembly/index";
import { Address, BigInt, log } from "@graphprotocol/graph-ts";
import { Account, Token } from "../generated/schema";

import { handleTransferEvent } from "../src/contract";
import { FROM_ADDRESS, TO_ADDRESS, createTransferEvent } from "./test-utils";
import { CONTRACT_ADDRESS } from "../src/constants";
import { createSnapshotID } from "../src/modules/Token";

/**
* Events
*
*
Approval
Burned
ClaimedRewards
ClaimedYield
Minted
OwnershipTransferred
Transfer
*/

// TODO assert for relevant things after full transfer event

// 1. create token
// 2. create to/from accounts
// 2. create to/from balances
// 2. create to/from daily snapshots
describe("Transfer Event", () => {
test("handle transfer event", () => {
const value = "80000000000000000000";

let event = createTransferEvent(
Address.fromString(TO_ADDRESS),
Address.fromString(FROM_ADDRESS),
BigInt.fromString(value)
);

assert.addressEquals(Address.fromString(CONTRACT_ADDRESS), event.address);

let id = event.transaction.hash
.concatI32(event.logIndex.toI32())
.toHexString();

assert.entityCount("Transfer", 0);
assert.entityCount("Token", 0);

handleTransferEvent(event);

assert.entityCount("Token", 1);
assert.entityCount("Transfer", 1);

// should have 2 of everything now
assert.entityCount("Account", 2);
assert.entityCount("AccountBalance", 2);

// logStore();

return;
});
});
50 changes: 50 additions & 0 deletions tests/Mint.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { logStore } from "matchstick-as/assembly/store";
import { assert, describe, test } from "matchstick-as/assembly/index";
import { Address, BigInt, log } from "@graphprotocol/graph-ts";

import { Account, Token } from "../generated/schema";

import { handleTransferEvent } from "../src/contract";
import { FROM_ADDRESS, TO_ADDRESS, createTransferEvent } from "./test-utils";
import { CONTRACT_ADDRESS, GENESIS_ADDRESS } from "../src/constants";
import { createSnapshotID } from "../src/modules/Token";
import { createAccountBalanceId } from "../src/modules/AccountBalance";

describe("Mint Event", () => {
test("handleTransferEvent", () => {
const value = 80000000000000000000;

let event = createTransferEvent(
Address.fromString(GENESIS_ADDRESS),
Address.fromString(TO_ADDRESS),
BigInt.fromU64(value)
);

assert.entityCount("Transfer", 0);
assert.entityCount("Token", 0);

handleTransferEvent(event);

// assert.entityCount("Token", 1);
assert.entityCount("Transfer", 1);

// to account should exist
let toAccount = Account.load(TO_ADDRESS);

// assert.assertNotNull(toAccount);

// should have 1 each as don't track genesis address account/balances
assert.entityCount("Account", 1);
assert.entityCount("AccountBalance", 1);
// assert.fieldEquals(
// "AccountBalance",
// createAccountBalanceId(TO_ADDRESS, CONTRACT_ADDRESS),
// "amount",
// value.toString()
// );

logStore();

return;
});
});
Loading

0 comments on commit 39e6754

Please sign in to comment.