Skip to content

Commit

Permalink
Merge pull request #38 from cryptoalgebra/fix/positions-refetch
Browse files Browse the repository at this point in the history
fix/positions-refetch
  • Loading branch information
lilchizh authored Apr 17, 2024
2 parents 85aa237 + a2a2e0d commit bb73df1
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 68 deletions.
66 changes: 44 additions & 22 deletions src/components/modals/RemoveLiquidityModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ import { Button } from "@/components/ui/button";
import { Dialog, DialogContent, DialogHeader, DialogTitle, DialogTrigger } from "@/components/ui/dialog";
import { Slider } from "@/components/ui/slider";
import { usePrepareAlgebraPositionManagerMulticall } from "@/generated";
import { useTransactionAwait } from "@/hooks/common/useTransactionAwait";
import { farmingClient } from "@/graphql/clients";
import { Deposit } from "@/graphql/generated/graphql";
import { useTransitionAwait } from "@/hooks/common/useTransactionAwait";
import { usePosition, usePositions } from "@/hooks/positions/usePositions";
import { useBurnActionHandlers, useBurnState, useDerivedBurnInfo } from "@/state/burnStore";
import { TransactionType } from "@/state/pendingTransactionsStore";
Expand Down Expand Up @@ -106,8 +108,38 @@ const RemoveLiquidityModal = ({ positionId }: RemoveLiquidityModalProps) => {

useEffect(() => {
if (!isSuccess) return;
let interval: NodeJS.Timeout;

/* pool positions refetch */
Promise.all([refetchPosition(), refetchAllPositions()])
.then(() => handleCloseModal?.());

/* farming deposits refetch */
.then(() => {
handleCloseModal?.();
if(sliderValue[0] !== 100) return;
interval = setInterval(
() =>
farmingClient.refetchQueries({
include: ['Deposits'],
onQueryUpdated: (query, { result: diff }) => {
const currentPos = diff.deposits.find(
(deposit: Deposit) =>
deposit.id.toString() === positionId.toString()
);
if (!currentPos) return;

if (currentPos.eternalFarming === null) {
clearInterval(interval);
} else {
query.refetch().then();
}
},
}),
2000
);
});

return () => clearInterval(interval);
}, [isSuccess]);

return <Dialog open={isOpen} onOpenChange={setIsOpen}>
Expand All @@ -124,26 +156,15 @@ const RemoveLiquidityModal = ({ positionId }: RemoveLiquidityModalProps) => {
<h2 className="text-3xl font-bold select-none">{`${sliderValue}%`}</h2>

<div className="flex gap-2">
<Button
variant={'icon'}
className="border border-card-border"
size={'sm'}
onClick={() => setSliderValue([25])}>25%</Button>
<Button
variant={'icon'}
className="border border-card-border"
size={'sm'}
onClick={() => setSliderValue([50])}>50%</Button>
<Button
variant={'icon'}
className="border border-card-border"
size={'sm'}
onClick={() => setSliderValue([75])}>75%</Button>
<Button
variant={'icon'}
className="border border-card-border"
size={'sm'}
onClick={() => setSliderValue([100])}>100%</Button>
{[25, 50, 75, 100].map((v) => (
<Button
key={`liquidity-percent-${v}`}
disabled={isRemoveLoading}
variant={'icon'}
className="border border-card-border"
size={'sm'}
onClick={() => setSliderValue([v])}>{v}%</Button>
))}
</div>

<Slider
Expand All @@ -155,6 +176,7 @@ const RemoveLiquidityModal = ({ positionId }: RemoveLiquidityModalProps) => {
onValueChange={(v) => setSliderValue(v)}
className="[&_[role=slider]]:h-4 [&_[role=slider]]:w-4"
aria-label="Liquidity Percent"
disabled={isRemoveLoading}
/>

<Button disabled={isDisabled} onClick={() => removeLiquidity && removeLiquidity()}>
Expand Down
77 changes: 39 additions & 38 deletions src/components/modals/SelectPositionFarmModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,47 +81,48 @@ export function SelectPositionFarmModal({
style={{ borderRadius: '32px' }}
>
<DialogHeader>
<DialogTitle className="font-bold select-none mt-2 max-md:mx-auto">
<DialogTitle className="font-bold select-none my-2 max-md:mx-auto">
Select Position
</DialogTitle>
</DialogHeader>

<ul className="grid grid-cols-2 max-md:grid-cols-1 max-h-[300px] gap-4 my-4 overflow-auto">
{availablePositions.length > 0 ? (
availablePositions.map((position) => {
const currentFormattedPosition = positionsData.find(
(fposition) =>
Number(fposition.id) === Number(position.id)
);
if (!currentFormattedPosition) return;
return (
<FarmingPositionCard
key={position.id}
className={cn(
'w-full row-span-1 col-span-1',
selectedPosition?.id === position.id
? 'border-primary-button hover:border-primary-button'
: ''
)}
onClick={() =>
handleSelectPosition(position)
}
position={position}
status={
currentFormattedPosition.outOfRange
? 'Out of range'
: 'In range'
}
/>
);
})
) : (
<h3 className="mx-auto col-span-2">
You don't have available positions for this pool
</h3>
)}
</ul>
<div className="w-full flex gap-4">
<div className='py-1'>
<ul className="grid grid-cols-2 max-md:grid-cols-1 max-h-[300px] gap-4 overflow-auto">
{availablePositions.length > 0 ? (
availablePositions.map((position) => {
const currentFormattedPosition = positionsData.find(
(fposition) =>
Number(fposition.id) === Number(position.id)
);
if (!currentFormattedPosition) return;
return (
<FarmingPositionCard
key={position.id}
className={cn(
'w-full row-span-1 col-span-1',
selectedPosition?.id === position.id
? 'border-primary-button hover:border-primary-button'
: ''
)}
onClick={() =>
handleSelectPosition(position)
}
position={position}
status={
currentFormattedPosition.outOfRange
? 'Out of range'
: 'In range'
}
/>
);
})
) : (
<h3 className="mx-auto col-span-2">
You don't have available positions for this pool
</h3>
)}
</ul>
</div>
<div className="w-full flex gap-4 mt-2">
{isApproving ? (
<Button disabled className="w-full">
Checking Approval...
Expand Down
12 changes: 4 additions & 8 deletions src/hooks/farming/useFarmStake.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,8 @@ export function useFarmStake({
if (!currentPos) return;

if (currentPos.eternalFarming !== null) {
query.refetch().then(() => {
setIsQueryLoading(false);
clearInterval(interval);
});
setIsQueryLoading(false);
clearInterval(interval);
} else {
query.refetch().then();
}
Expand Down Expand Up @@ -176,10 +174,8 @@ export function useFarmUnstake({
if (!currentPos) return;

if (currentPos.eternalFarming === null) {
query.refetch().then(() => {
setIsQueryLoading(false);
clearInterval(interval);
});
setIsQueryLoading(false);
clearInterval(interval);
} else {
query.refetch().then();
}
Expand Down

0 comments on commit bb73df1

Please sign in to comment.