diff --git a/src/app/api/getDelegationPoints.ts b/src/app/api/getDelegationPoints.ts index 34a69072..586b2383 100644 --- a/src/app/api/getDelegationPoints.ts +++ b/src/app/api/getDelegationPoints.ts @@ -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 => { - const params: Record = {}; + const params: Record = { + 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 => { let allDelegationPoints: DelegationPoints[] = []; let nextPaginationKey = paginationKey; do { - const currentParams = { ...params }; + const currentParams: Record = { + 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, diff --git a/src/app/components/Staking/Staking.tsx b/src/app/components/Staking/Staking.tsx index 0950be55..edddf005 100644 --- a/src/app/components/Staking/Staking.tsx +++ b/src/app/components/Staking/Staking.tsx @@ -74,13 +74,13 @@ export const Staking: React.FC = ({ isWalletConnected, onConnect, isLoading, + btcWallet, + btcWalletNetwork, + address, + publicKeyNoCoord, setDelegationsLocalStorage, btcWalletBalanceSat, availableUTXOs, - publicKeyNoCoord, - address, - btcWallet, - btcWalletNetwork, }) => { // Staking form state const [stakingAmountSat, setStakingAmountSat] = useState(0); diff --git a/src/app/context/api/DelegationsPointsProvider.tsx b/src/app/context/api/DelegationsPointsProvider.tsx index 733c5eb8..267904cd 100644 --- a/src/app/context/api/DelegationsPointsProvider.tsx +++ b/src/app/context/api/DelegationsPointsProvider.tsx @@ -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"; @@ -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;