-
Notifications
You must be signed in to change notification settings - Fork 2
feat: add total locked token metrics #101
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
Changes from all commits
063672b
d3e54d3
cc1e294
65291c1
54f78ee
3d90978
fc4a45b
8cc8493
2ae2acd
802f016
01449bc
9da1ba8
c883b87
6939010
32a5612
2551433
f8cae7d
8b779bb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import { useQuery } from "@tanstack/react-query"; | ||
| import { getPublicClient } from "@/services/viem/client"; | ||
| import useNetwork from "./useNetwork"; | ||
|
|
||
| interface UseBlockNumberOptions { | ||
| refetchInterval?: number | false; | ||
| } | ||
|
|
||
| /** | ||
| * Custom hook to fetch the current block number using viem's public client. | ||
| * | ||
| * @returns Query result containing the current block number | ||
| * | ||
| * @example | ||
| * ```tsx | ||
| * const { data: blockNumber, isLoading } = useBlockNumber(); | ||
| * ``` | ||
| */ | ||
| export function useBlockNumber(options?: UseBlockNumberOptions) { | ||
| const { network } = useNetwork(); | ||
|
|
||
| return useQuery({ | ||
| queryKey: ["blockNumber", network], | ||
| queryFn: async () => { | ||
| const publicClient = getPublicClient(network); | ||
| const blockNumber = await publicClient.getBlockNumber(); | ||
| return blockNumber; | ||
| }, | ||
| refetchInterval: options?.refetchInterval || false, | ||
| placeholderData: (previousData) => previousData, | ||
| }); | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import { createPublicClient, http, type PublicClient } from "viem"; | ||
| import { getChain } from "@/constants/chains"; | ||
| import type { Network } from "@/types"; | ||
|
|
||
| const clients = new Map<Network, PublicClient>(); | ||
|
|
||
| export function getPublicClient(network: Network): PublicClient { | ||
| const existing = clients.get(network); | ||
| if (existing) { | ||
| return existing; | ||
| } | ||
|
|
||
| const chain = getChain(network); | ||
| const client = createPublicClient({ | ||
| chain, | ||
| transport: http(), | ||
| }); | ||
|
|
||
| clients.set(network, client); | ||
| return client; | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * Calculate total lockup for a token using the current block number. | ||
| * | ||
| * Formula: lockupCurrent + lockupRate * (blockNumber - lockupLastSettledUntilEpoch) | ||
| * | ||
| * @param lockupCurrent - The current fixed lockup amount | ||
| * @param lockupRate - The rate at which lockup increases per block | ||
| * @param lockupLastSettledUntilEpoch - The block number when lockup was last settled | ||
| * @param blockNumber - The current block number | ||
| * @returns The total lockup amount as a string, or "0" if calculation fails | ||
| * | ||
| * @example | ||
| * ```ts | ||
| * const totalLockup = calculateTotalLockup( | ||
| * "1000", | ||
| * "10", | ||
| * "100", | ||
| * 150n | ||
| * ); | ||
| * // Returns: "1500" (1000 + 10 * (150 - 100)) | ||
| * ``` | ||
| */ | ||
| export function calculateTotalLockup( | ||
| lockupCurrent: bigint | string | undefined, | ||
| lockupRate: bigint | string | undefined, | ||
| lockupLastSettledUntilEpoch: bigint | string | undefined, | ||
| blockNumber: bigint | string | undefined, | ||
| ): string { | ||
| // Handle missing data | ||
| if ( | ||
| lockupCurrent === undefined || | ||
| lockupRate === undefined || | ||
| lockupLastSettledUntilEpoch === undefined || | ||
| blockNumber === undefined | ||
| ) { | ||
| return "0"; | ||
| } | ||
|
|
||
| try { | ||
| lockupCurrent = BigInt(lockupCurrent); | ||
| lockupRate = BigInt(lockupRate); | ||
| lockupLastSettledUntilEpoch = BigInt(lockupLastSettledUntilEpoch); | ||
| blockNumber = BigInt(blockNumber); | ||
|
|
||
| // If no streaming lockup (rate is 0), return only fixed lockup | ||
| if (lockupRate === 0n) { | ||
| return lockupCurrent.toString(); | ||
| } | ||
|
|
||
| // If current block is before or equal to last settled, return only fixed lockup | ||
| if (blockNumber <= lockupLastSettledUntilEpoch) { | ||
| return lockupCurrent.toString(); | ||
| } | ||
|
|
||
| // Calculate streaming lockup: rate * (blockNumber - lastSettled) | ||
| const blockDelta = blockNumber - lockupLastSettledUntilEpoch; | ||
| const streamingLockup = lockupRate * blockDelta; | ||
|
|
||
| // Total lockup = fixed + streaming | ||
| const totalLockup = lockupCurrent + streamingLockup; | ||
|
|
||
| // Ensure non-negative result | ||
| return totalLockup >= 0n ? totalLockup.toString() : "0"; | ||
| } catch (error) { | ||
| console.error("Error calculating total lockup:", error); | ||
| return "0"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,6 +24,9 @@ type Token @entity(immutable: false) { | |
| totalUsers: BigInt! | ||
| userFunds: BigInt! # same as sum of all UserToken.funds | ||
| operatorCommission: BigInt! | ||
| lockupCurrent: BigInt! | ||
| lockupRate: BigInt! | ||
| lockupLastSettledUntilEpoch: BigInt! | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a global property for a token, but it's actually a per-rail property. Will this be updated on every payment rail settlement?
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, on rail settlement, |
||
| userTokens: [UserToken!]! @derivedFrom(field: "token") | ||
| } | ||
|
|
||
|
|
@@ -128,8 +131,8 @@ type Rail @entity(immutable: false) { | |
|
|
||
| type RateChangeQueue @entity(immutable: false) { | ||
| id: Bytes! # railId,startEpoch | ||
| startEpoch: BigInt! | ||
| untilEpoch: BigInt! | ||
| startEpoch: BigInt! # exclusive: rate applies starting at `startEpoch + 1` | ||
| untilEpoch: BigInt! # inclusive: rate applies through `untilEpoch` | ||
| rate: BigInt! | ||
| rail: Rail! | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ditto for removal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was using this query before the merging the current upstream branch, after which I've seen that it's no longer used so I decided to delete it. YAGNI.