Skip to content

Commit

Permalink
Merge pull request #8626 from LedgerHQ/bugfix/ada-staking-delegation
Browse files Browse the repository at this point in the history
fix: display the selected validator
  • Loading branch information
qperrot authored Dec 13, 2024
2 parents 82d464f + cd37e2c commit 4941eec
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 15 deletions.
6 changes: 6 additions & 0 deletions .changeset/pretty-hairs-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ledgerhq/coin-cardano": minor
"ledger-live-desktop": minor
---

Fix ADA Changing current delegation does not display the validator
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,9 @@ type Props = {
};

const ValidatorField = ({ account, delegation, onChangeValidator, selectedPoolId }: Props) => {
const [ledgerPools, setLedgerPools] = useState<Array<StakePool>>([]);
const [currentPool, setCurrentPool] = useState<Array<StakePool>>([]);
const unit = useAccountUnit(account);

const [showAll, setShowAll] = useState(
LEDGER_POOL_IDS.length === 0 ||
(LEDGER_POOL_IDS.length === 1 && delegation?.poolId === LEDGER_POOL_IDS[0]),
);

const [showAll, setShowAll] = useState(false);
const [ledgerPoolsLoading, setLedgerPoolsLoading] = useState(false);
const { pools, searchQuery, setSearchQuery, onScrollEndReached, isSearching, isPaginating } =
useCardanoFamilyPools(account.currency);
Expand All @@ -50,13 +45,14 @@ const ValidatorField = ({ account, delegation, onChangeValidator, selectedPoolId
useEffect(() => {
if (LEDGER_POOL_IDS.length) {
setLedgerPoolsLoading(true);
fetchPoolDetails(account.currency, LEDGER_POOL_IDS)
const delegationPoolId = delegation?.poolId ? [delegation.poolId] : [];
fetchPoolDetails(account.currency, delegationPoolId, LEDGER_POOL_IDS)
.then((apiRes: { pools: Array<StakePool> }) => {
setCurrentPool(apiRes.pools);
const filteredLedgerPools = apiRes.pools.filter(
pool => pool.poolId !== delegation?.poolId,
);
if (filteredLedgerPools.length) {
setLedgerPools(filteredLedgerPools);
onChangeValidator(filteredLedgerPools[0]);
}
})
Expand All @@ -66,6 +62,17 @@ const ValidatorField = ({ account, delegation, onChangeValidator, selectedPoolId
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);

useEffect(() => {
const selectedPool = pools.find(p => p.poolId === selectedPoolId);
if (selectedPool) {
setCurrentPool([selectedPool]);
onChangeValidator(selectedPool);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [selectedPoolId]);

const onSearch = useCallback(
(evt: React.ChangeEvent<HTMLInputElement>) => setSearchQuery(evt.target.value),
[setSearchQuery],
Expand All @@ -88,16 +95,26 @@ const ValidatorField = ({ account, delegation, onChangeValidator, selectedPoolId
{showAll && <ValidatorSearchInput noMargin={true} search={searchQuery} onSearch={onSearch} />}
<ValidatorsFieldContainer>
<Box p={1} data-testid="validator-list">
{(showAll && isSearching) || (!showAll && ledgerPoolsLoading) ? (
{(showAll && isSearching) ||
(!showAll && ledgerPoolsLoading) ||
(!showAll && !pools.length) ? (
<Box flex={1} py={3} alignItems="center" justifyContent="center">
<BigSpinner size={35} />
</Box>
) : (
<ScrollLoadingList
data={
showAll
? pools.filter(p => !poolIdsToFilterFromAllPools.includes(p.poolId))
: ledgerPools
? [
currentPool[0],
...pools.filter(
p =>
p &&
!poolIdsToFilterFromAllPools.includes(p.poolId) &&
p !== currentPool[0],
),
]
: currentPool
}
style={{
flex: showAll ? "1 0 256px" : "1 0 64px",
Expand Down
11 changes: 8 additions & 3 deletions libs/coin-modules/coin-cardano/src/api/getPools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ export async function fetchPoolList(
export async function fetchPoolDetails(
currency: CryptoCurrency,
poolIds: Array<string>,
ledgerPoolIds?: Array<string>,
): Promise<APIGetPoolsDetail> {
const res = await network({
const currentPoolIds = poolIds.length == 0 ? ledgerPoolIds : poolIds;
const { data } = await network<APIGetPoolsDetail>({
method: "GET",
url: isTestnet(currency)
? `${CARDANO_TESTNET_API_ENDPOINT}/v1/pool/detail`
: `${CARDANO_API_ENDPOINT}/v1/pool/detail`,
params: { poolIds },
params: { poolIds: currentPoolIds },
});
return res && (res.data as APIGetPoolsDetail);
if (data.pools.length === 0 && ledgerPoolIds) {
return fetchPoolDetails(currency, ledgerPoolIds);
}
return data;
}

0 comments on commit 4941eec

Please sign in to comment.