Skip to content

Commit

Permalink
No duplicate can be added to local storage (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
gbarkhatov authored Aug 6, 2024
1 parent 81518e1 commit 8cc0d8a
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 23 deletions.
39 changes: 27 additions & 12 deletions src/app/components/Delegations/Delegations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,33 @@ export const Delegations: React.FC<DelegationsProps> = ({
delegation: DelegationInterface,
newState: string,
) => {
setIntermediateDelegationsLocalStorage((delegations) => [
toLocalStorageIntermediateDelegation(
delegation.stakingTxHashHex,
publicKeyNoCoord,
delegation.finalityProviderPkHex,
delegation.stakingValueSat,
delegation.stakingTx.txHex,
delegation.stakingTx.timelock,
newState,
),
...delegations,
]);
const newTxId = delegation.stakingTxHashHex;

setIntermediateDelegationsLocalStorage((delegations) => {
// Check if an intermediate delegation with the same transaction ID already exists
const exists = delegations.some(
(existingDelegation) => existingDelegation.stakingTxHashHex === newTxId,
);

// If it doesn't exist, add the new intermediate delegation
if (!exists) {
return [
toLocalStorageIntermediateDelegation(
newTxId,
publicKeyNoCoord,
delegation.finalityProviderPkHex,
delegation.stakingValueSat,
delegation.stakingTx.txHex,
delegation.stakingTx.timelock,
newState,
),
...delegations,
];
}

// If it exists, return the existing delegations unchanged
return delegations;
});
};

// Handles unbonding requests for Active delegations that want to be withdrawn early
Expand Down
38 changes: 27 additions & 11 deletions src/app/components/Staking/Staking.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,33 @@ export const Staking: React.FC<StakingProps> = ({
signedTxHex: string,
stakingTerm: number,
) => {
setDelegationsLocalStorage((delegations) => [
toLocalStorageDelegation(
Transaction.fromHex(signedTxHex).getId(),
publicKeyNoCoord,
finalityProvider!.btcPk,
stakingAmountSat,
signedTxHex,
stakingTerm,
),
...delegations,
]);
// Get the transaction ID
const newTxId = Transaction.fromHex(signedTxHex).getId();

setDelegationsLocalStorage((delegations) => {
// Check if the delegation with the same transaction ID already exists
const exists = delegations.some(
(delegation) => delegation.stakingTxHashHex === newTxId,
);

// If it doesn't exist, add the new delegation
if (!exists) {
return [
toLocalStorageDelegation(
newTxId,
publicKeyNoCoord,
finalityProvider!.btcPk,
stakingAmountSat,
signedTxHex,
stakingTerm,
),
...delegations,
];
}

// If it exists, return the existing delegations unchanged
return delegations;
});
};

// Memoize the staking fee calculation
Expand Down

0 comments on commit 8cc0d8a

Please sign in to comment.