Skip to content

Commit

Permalink
resolve comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy-babylonlabs committed Sep 10, 2024
1 parent 9654360 commit 1d655df
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 39 deletions.
62 changes: 31 additions & 31 deletions src/app/api/getDelegationPoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,59 +24,59 @@ export interface PaginatedDelegationsPoints {
pagination: Pagination;
}

export const getDelegationPoints = async (
// Get delegation points by staker BTC public key
export const getDelegationPointsByStakerBtcPk = async (
stakerBtcPk: string,
paginationKey?: string,
stakerBtcPk?: string,
stakingTxHashHexes?: string[],
): Promise<PaginatedDelegationsPoints> => {
const params: Record<string, string | string[]> = {};
const params: Record<string, string> = {
staker_btc_pk: encode(stakerBtcPk),
};

if (stakerBtcPk && stakingTxHashHexes && stakingTxHashHexes.length > 0) {
throw new Error(
"Only one of stakerBtcPk or stakingTxHashHexes should be provided",
);
if (paginationKey && paginationKey !== "") {
params.pagination_key = encode(paginationKey);
}

if (
!stakerBtcPk &&
(!stakingTxHashHexes || stakingTxHashHexes.length === 0)
) {
throw new Error(
"Either stakerBtcPk or stakingTxHashHexes must be provided",
);
}
const response = await apiWrapper(
"GET",
"/v1/points/staker/delegations",
"Error getting delegation points by staker BTC public key",
params,
);

if (stakerBtcPk) {
params.staker_btc_pk = encode(stakerBtcPk);
}
return {
data: response.data.data,
pagination: response.data.pagination,
};
};

// Get delegation points by staking transaction hash hex
export const getDelegationPointsByStakingTxHashHexes = async (
stakingTxHashHexes: string[],
paginationKey?: string,
): Promise<PaginatedDelegationsPoints> => {
let allDelegationPoints: DelegationPoints[] = [];
let nextPaginationKey = paginationKey;

do {
const currentParams = { ...params };
const currentParams: Record<string, string | string[]> = {
staking_tx_hash_hex: stakingTxHashHexes.splice(0, 10),
};

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",
"/v1/points/staking-tx/delegations",
"Error getting delegation points by staking transaction hashes",
currentParams,
);

allDelegationPoints = allDelegationPoints.concat(response.data.data);
nextPaginationKey = response.data.pagination.next_key;
} while (
nextPaginationKey ||
(stakingTxHashHexes && stakingTxHashHexes.length > 0)
);
} while (nextPaginationKey || stakingTxHashHexes.length > 0);

return {
data: allDelegationPoints,
Expand Down
8 changes: 4 additions & 4 deletions src/app/components/Staking/Staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ export const Staking: React.FC<StakingProps> = ({
isWalletConnected,
onConnect,
isLoading,
btcWallet,
btcWalletNetwork,
address,
publicKeyNoCoord,
setDelegationsLocalStorage,
btcWalletBalanceSat,
availableUTXOs,
publicKeyNoCoord,
address,
btcWallet,
btcWalletNetwork,
}) => {
// Staking form state
const [stakingAmountSat, setStakingAmountSat] = useState(0);
Expand Down
7 changes: 3 additions & 4 deletions src/app/context/api/DelegationsPointsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useQuery } from "@tanstack/react-query";
import React, { createContext, useContext, useEffect, useState } from "react";

import {
getDelegationPoints,
getDelegationPointsByStakingTxHashHexes,
PaginatedDelegationsPoints,
} from "@/app/api/getDelegationPoints";
import { Delegation } from "@/app/types/delegations";
Expand Down Expand Up @@ -50,10 +50,9 @@ export const DelegationsPointsProvider: React.FC<
);

do {
const result = await getDelegationPoints(
paginationKey,
undefined,
const result = await getDelegationPointsByStakingTxHashHexes(
stakingTxHashHexes,
paginationKey,
);
allPoints = [...allPoints, ...result.data];
paginationKey = result.pagination.next_key;
Expand Down

0 comments on commit 1d655df

Please sign in to comment.