stats for ubi and profit

This commit is contained in:
JulesCrown 2024-05-15 19:21:35 +02:00
parent ac708963c8
commit 8ff75f4cc3
6 changed files with 222 additions and 575 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View file

@ -1,12 +1,12 @@
{
"sepolia": {
"Harb": {
"address": "0xcd02666582a2057085edabc55c5120155ba4e93c",
"startBlock": 5510934
"address": "0xaf0a83ae80be549d32f94b74a4bdcda9090c4c8b",
"startBlock": 5899691
},
"Stake": {
"address": "0x4256777543814d66f9f66390f6bb33c55a24c331",
"startBlock": 5510934
"address": "0x4a7acb6b7318b775dccf00e378449290162cf382",
"startBlock": 5899691
}
}
}

View file

@ -6,6 +6,25 @@ type Stats @entity {
id: Bytes!
outstandingSupply: BigInt! # uint256
activeSupply: BigInt! # uint256
totalUbiClaimed: BigInt! # field to track total UBI claimed
ubiClaimedLastWeek: BigInt!
ubiClaimedLastDay: BigInt! # field to track UBI claimed in the last 24
ubiClaimedLastHour: BigInt!
ringBuffer: [BigInt!]! # Ring buffer to store daily totals
ringBufferPointer: Int! # Pointer to the current day in the ring buffer
lastUpdatedHour: Int! # The last updated day boundary (timestamp in days)
totalMinted: BigInt!
mintedLastWeek: BigInt!
mintedLastDay: BigInt!
mintedLastHour: BigInt!
totalBurned: BigInt!
burnedLastWeek: BigInt!
burnedLastDay: BigInt!
burnedLastHour: BigInt!
}
enum PositionStatus {

View file

@ -1,19 +1,198 @@
import {
Approval as ApprovalEvent,
EIP712DomainChanged as EIP712DomainChangedEvent,
Transfer as TransferEvent
} from "../generated/Harb/Harb"
Transfer as TransferEvent,
UbiClaimed as UbiClaimedEvent
} from "../generated/Harb/Harb";
import { BigInt, Bytes, ethereum, Address, log } from "@graphprotocol/graph-ts";
import { Stats } from "../generated/schema"
export function handleApproval(event: ApprovalEvent): void {
// Helper function to get or create Stats entity
function getOrCreateStats(): Stats {
let stats = Stats.load(Bytes.fromHexString("0x01") as Bytes);
if (stats == null) {
stats = new Stats(Bytes.fromHexString("0x01") as Bytes);
stats.outstandingSupply = BigInt.zero();
stats.activeSupply = BigInt.zero();
stats.totalUbiClaimed = BigInt.zero();
stats.ubiClaimedLastWeek = BigInt.zero();
stats.ubiClaimedLastDay = BigInt.zero();
stats.ubiClaimedLastHour = BigInt.zero();
stats.totalMinted = BigInt.zero();
stats.mintedLastWeek = BigInt.zero();
stats.mintedLastDay = BigInt.zero();
stats.mintedLastHour = BigInt.zero();
stats.totalBurned = BigInt.zero();
stats.burnedLastWeek = BigInt.zero();
stats.burnedLastDay = BigInt.zero();
stats.burnedLastHour = BigInt.zero();
stats.ringBuffer = new Array<BigInt>(168 * 3).fill(BigInt.zero());
stats.ringBufferPointer = 0;
stats.lastUpdatedHour = 0;
}
return stats;
}
export function handleEIP712DomainChanged(
event: EIP712DomainChangedEvent
): void {
}
export function handleTransfer(event: TransferEvent): void {
let ZERO_ADDRESS = Address.fromString("0x0000000000000000000000000000000000000000");
// Update Stats entity
let stats = getOrCreateStats();
// Get a copy of the ring buffer
let ringBuffer = stats.ringBuffer;
// Determine if it's a mint or burn event
if (event.params.from == ZERO_ADDRESS) {
// Mint event
stats.totalMinted = stats.totalMinted.plus(event.params.value);
// Add the minted amount to the current hour's total in the ring buffer
let mintBufferIndex = (stats.ringBufferPointer * 3) + 1; // Minted tokens are at index 1
ringBuffer[mintBufferIndex] = ringBuffer[mintBufferIndex].plus(event.params.value);
// Calculate the elapsed time in the current hour
let currentTimestamp = event.block.timestamp.toI32();
let startOfHour = (currentTimestamp / 3600) * 3600;
let elapsedSeconds = currentTimestamp - startOfHour;
// Project the current hour's total based on the average rate of minted tokens in the current hour
let projectedTotal = ringBuffer[mintBufferIndex].times(BigInt.fromI32(3600)).div(BigInt.fromI32(elapsedSeconds));
// Calculate the medium between the previous hour and the projection
let previousHourTotal = ringBuffer[((stats.ringBufferPointer - 1 + 168) % 168) * 3 + 1];
stats.mintedLastHour = previousHourTotal.plus(projectedTotal).div(BigInt.fromI32(2));
log.info("transfer handler: Recalculated totals. projected total: {}. previous hour total: {}. medium: {}.", [
projectedTotal.toString(),
previousHourTotal.toString(),
stats.burnedLastHour.toString(),
]);
} else if (event.params.to == ZERO_ADDRESS) {
// Burn event
stats.totalBurned = stats.totalBurned.plus(event.params.value);
// Add the burned amount to the current hour's total in the ring buffer
let burnBufferIndex = (stats.ringBufferPointer * 3) + 2; // Burned tokens are at index 2
ringBuffer[burnBufferIndex] = ringBuffer[burnBufferIndex].plus(event.params.value);
// Calculate the elapsed time in the current hour
let currentTimestamp = event.block.timestamp.toI32();
let startOfHour = (currentTimestamp / 3600) * 3600;
let elapsedSeconds = currentTimestamp - startOfHour;
// Project the current hour's total based on the average rate of burned tokens in the current hour
let projectedTotal = ringBuffer[burnBufferIndex].times(BigInt.fromI32(3600)).div(BigInt.fromI32(elapsedSeconds));
// Calculate the medium between the previous hour and the projection
let previousHourTotal = ringBuffer[((stats.ringBufferPointer - 1 + 168) % 168) * 3 + 2];
stats.burnedLastHour = previousHourTotal.plus(projectedTotal).div(BigInt.fromI32(2));
}
// Update the ring buffer in the stats entity
stats.ringBuffer = ringBuffer;
// Save the updated Stats entity
stats.save();
}
export function handleUbiClaimed(event: UbiClaimedEvent): void {
let stats = getOrCreateStats();
// Get a copy of the ring buffer
let ringBuffer = stats.ringBuffer;
// Update total UBI claimed
stats.totalUbiClaimed = stats.totalUbiClaimed.plus(event.params.ubiAmount);
// Add the UBI amount to the current hour's total in the ring buffer
let ubiBufferIndex = (stats.ringBufferPointer * 3) + 0; // UBI is at index 0
ringBuffer[ubiBufferIndex] = ringBuffer[ubiBufferIndex].plus(event.params.ubiAmount);
// Calculate the elapsed time in the current hour
let currentTimestamp = event.block.timestamp.toI32();
let startOfHour = (currentTimestamp / 3600) * 3600;
let elapsedSeconds = currentTimestamp - startOfHour;
// Project the current hour's total based on the average rate of UBI claims in the current hour
let projectedTotal = ringBuffer[ubiBufferIndex].times(BigInt.fromI32(3600)).div(BigInt.fromI32(elapsedSeconds));
// Calculate the medium between the previous hour and the projection
let previousHourTotal = ringBuffer[((stats.ringBufferPointer + 167) % 168) * 3 + 0];
stats.ubiClaimedLastHour = previousHourTotal.plus(projectedTotal).div(BigInt.fromI32(2));
// Update the ring buffer in the stats entity
stats.ringBuffer = ringBuffer;
// Save the updated Stats entity
stats.save();
}
// Block handler to aggregate stats
export function handleBlock(block: ethereum.Block): void {
// Update Stats entity
let stats = getOrCreateStats();
// Get a copy of the ring buffer
let ringBuffer = stats.ringBuffer;
// Calculate the current hour
let currentHour = block.timestamp.toI32() / 3600;
// Check if we've moved to a new hour
if (currentHour > stats.lastUpdatedHour) {
// Move the ring buffer pointer forward
stats.ringBufferPointer = (stats.ringBufferPointer + 1) % 168;
// Reset the new current hour in the ring buffer
let baseIndex = stats.ringBufferPointer * 3;
ringBuffer[baseIndex] = BigInt.zero(); // UBI claimed
ringBuffer[baseIndex + 1] = BigInt.zero(); // Minted tokens
ringBuffer[baseIndex + 2] = BigInt.zero(); // Burned tokens
// Update the last updated hour
stats.lastUpdatedHour = currentHour;
// Recalculate the sum of the last 7 days and 24 hours
let totalLast7Days = BigInt.zero();
let totalMintedLast7Days = BigInt.zero();
let totalBurnedLast7Days = BigInt.zero();
let totalLast24Hours = BigInt.zero();
let totalMintedLast24Hours = BigInt.zero();
let totalBurnedLast24Hours = BigInt.zero();
for (let i = 0; i < 168; i++) {
let index = ((stats.ringBufferPointer - i + 168) % 168) * 3;
totalLast7Days = totalLast7Days.plus(ringBuffer[index]);
totalMintedLast7Days = totalMintedLast7Days.plus(ringBuffer[index + 1]);
totalBurnedLast7Days = totalBurnedLast7Days.plus(ringBuffer[index + 2]);
if (i < 24) {
totalLast24Hours = totalLast24Hours.plus(ringBuffer[index]);
totalMintedLast24Hours = totalMintedLast24Hours.plus(ringBuffer[index + 1]);
totalBurnedLast24Hours = totalBurnedLast24Hours.plus(ringBuffer[index + 2]);
}
}
stats.ubiClaimedLastWeek = totalLast7Days;
stats.mintedLastWeek = totalMintedLast7Days;
stats.burnedLastWeek = totalBurnedLast7Days;
stats.ubiClaimedLastDay = totalLast24Hours;
stats.mintedLastDay = totalMintedLast24Hours;
stats.burnedLastDay = totalBurnedLast24Hours;
// Update the ring buffer in the stats entity
stats.ringBuffer = ringBuffer;
// Save the updated Stats entity
stats.save();
}
}

View file

@ -8,33 +8,34 @@ dataSources:
name: Harb
network: sepolia
source:
address: "0xf3dc6b3fdda9d1cfe16b93e6a6482b63869f7d35"
address: "0x7517db0f2b24223f2f0e3567149ca180e204da8a"
abi: Harb
startBlock: 5708443
startBlock: 5909060
mapping:
kind: ethereum/events
apiVersion: 0.0.7
language: wasm/assemblyscript
entities:
- Stats
- UbiClaim
abis:
- name: Harb
file: ./abis/Harb.json
eventHandlers:
- event: Approval(indexed address,indexed address,uint256)
handler: handleApproval
- event: EIP712DomainChanged()
handler: handleEIP712DomainChanged
- event: Transfer(indexed address,indexed address,uint256)
handler: handleTransfer
- event: UbiClaimed(indexed address,uint256)
handler: handleUbiClaimed
blockHandlers:
- handler: handleBlock
file: ./src/harb.ts
- kind: ethereum
name: Stake
network: sepolia
source:
address: "0x035ad87485cbd3794fe891caf4219bad7dc929ed"
address: "0x00b4d656b8182d0c2f4841b7a6f1429b94f73a66"
abi: Stake
startBlock: 5708444
startBlock: 5909060
mapping:
kind: ethereum/events
apiVersion: 0.0.7