-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ac2bbd0
commit 21185c8
Showing
10 changed files
with
415 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import { encode } from "url-safe-base64"; | ||
|
||
import { Pagination } from "../types/api"; | ||
|
||
import { apiWrapper } from "./apiWrapper"; | ||
|
||
export interface DelegationPoints { | ||
staking_tx_hash_hex: string; | ||
staker: { | ||
pk: string; | ||
points: number; | ||
}; | ||
finality_provider: { | ||
pk: string; | ||
points: number; | ||
}; | ||
staking_height: number; | ||
unbonding_height: number | null; | ||
expiry_height: number; | ||
} | ||
|
||
export interface PaginatedDelegationsPoints { | ||
data: DelegationPoints[]; | ||
pagination: Pagination; | ||
} | ||
|
||
export const getDelegationPoints = async ( | ||
paginationKey?: string, | ||
stakerBtcPk?: string, | ||
stakingTxHashHexes?: string[], | ||
): Promise<PaginatedDelegationsPoints> => { | ||
const params: Record<string, string | string[]> = {}; | ||
|
||
if (stakerBtcPk && stakingTxHashHexes && stakingTxHashHexes.length > 0) { | ||
throw new Error( | ||
"Only one of stakerBtcPk or stakingTxHashHexes should be provided", | ||
); | ||
} | ||
|
||
if ( | ||
!stakerBtcPk && | ||
(!stakingTxHashHexes || stakingTxHashHexes.length === 0) | ||
) { | ||
throw new Error( | ||
"Either stakerBtcPk or stakingTxHashHexes must be provided", | ||
); | ||
} | ||
|
||
if (stakerBtcPk) { | ||
params.staker_btc_pk = encode(stakerBtcPk); | ||
} | ||
|
||
let allDelegationPoints: DelegationPoints[] = []; | ||
let nextPaginationKey = paginationKey; | ||
|
||
do { | ||
const currentParams = { ...params }; | ||
if (nextPaginationKey && nextPaginationKey !== "") { | ||
currentParams.pagination_key = encode(nextPaginationKey); | ||
} | ||
|
||
if (stakingTxHashHexes && stakingTxHashHexes.length > 0) { | ||
currentParams.staking_tx_hash_hex = stakingTxHashHexes.slice(0, 10); | ||
stakingTxHashHexes = stakingTxHashHexes.slice(10); | ||
} | ||
|
||
const response = await apiWrapper( | ||
"GET", | ||
"/v1/points/staker/delegations", | ||
"Error getting delegation points", | ||
currentParams, | ||
); | ||
|
||
allDelegationPoints = allDelegationPoints.concat(response.data.data); | ||
nextPaginationKey = response.data.pagination.next_key; | ||
} while ( | ||
nextPaginationKey || | ||
(stakingTxHashHexes && stakingTxHashHexes.length > 0) | ||
); | ||
|
||
return { | ||
data: allDelegationPoints, | ||
pagination: { next_key: nextPaginationKey || "" }, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { encode } from "url-safe-base64"; | ||
|
||
import { apiWrapper } from "./apiWrapper"; | ||
|
||
export interface StakerPoints { | ||
staker_btc_pk: string; | ||
points: number; | ||
} | ||
|
||
export const getStakersPoints = async ( | ||
stakerBtcPk: string[], | ||
): Promise<StakerPoints[]> => { | ||
const params: Record<string, string> = {}; | ||
|
||
params.staker_btc_pk = | ||
stakerBtcPk.length > 1 | ||
? stakerBtcPk.map(encode).join(",") | ||
: encode(stakerBtcPk[0]); | ||
|
||
const response = await apiWrapper( | ||
"GET", | ||
"/v1/points/stakers", | ||
"Error getting staker points", | ||
params, | ||
); | ||
|
||
return response.data; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import React from "react"; | ||
|
||
import { useDelegationsPoints } from "@/app/context/api/DelegationsPointsProvider"; | ||
|
||
interface DelegationPointsProps { | ||
stakingTxHash: string; | ||
} | ||
|
||
export const DelegationPoints: React.FC<DelegationPointsProps> = ({ | ||
stakingTxHash, | ||
}) => { | ||
const { delegationPoints, isLoading } = useDelegationsPoints(); | ||
|
||
const points = delegationPoints.get(stakingTxHash); | ||
|
||
if (isLoading) { | ||
return ( | ||
<div className="flex items-center justify-end gap-1"> | ||
<div className="h-5 w-12 animate-pulse rounded bg-gray-300 dark:bg-gray-700"></div> | ||
</div> | ||
); | ||
} | ||
|
||
return ( | ||
<div className="flex items-center justify-end gap-1"> | ||
<p className="whitespace-nowrap font-semibold"> | ||
{points !== undefined ? points : 0} | ||
</p> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { useQuery } from "@tanstack/react-query"; | ||
import React from "react"; | ||
|
||
import { getStakersPoints } from "@/app/api/getStakersPoints"; | ||
import { satoshiToBtc } from "@/utils/btcConversions"; | ||
import { maxDecimals } from "@/utils/maxDecimals"; | ||
|
||
interface StakerPointsProps { | ||
publicKeyNoCoord: string; | ||
} | ||
|
||
export const StakerPoints: React.FC<StakerPointsProps> = ({ | ||
publicKeyNoCoord, | ||
}) => { | ||
const { data: stakerPoints, isLoading } = useQuery({ | ||
queryKey: ["stakerPoints", publicKeyNoCoord], | ||
queryFn: () => getStakersPoints([publicKeyNoCoord]), | ||
enabled: !!publicKeyNoCoord, | ||
refetchInterval: 60000, // Refresh every minute | ||
}); | ||
|
||
if (isLoading) { | ||
return ( | ||
<div className="flex items-center justify-end gap-1"> | ||
<div className="h-5 w-20 animate-pulse rounded bg-gray-300 dark:bg-gray-700"></div> | ||
</div> | ||
); | ||
} | ||
|
||
const points = stakerPoints?.[0]?.points; | ||
|
||
return ( | ||
<div className="flex items-center justify-end gap-1"> | ||
<p className="whitespace-nowrap font-semibold"> | ||
{points !== undefined ? maxDecimals(satoshiToBtc(points), 8) : 0} | ||
</p> | ||
</div> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.