Skip to content

Commit

Permalink
fix handling the tx array
Browse files Browse the repository at this point in the history
lint fix
  • Loading branch information
vidvidvid committed Dec 4, 2024
1 parent 0ad7a2c commit fc62b28
Show file tree
Hide file tree
Showing 9 changed files with 249 additions and 130 deletions.
15 changes: 11 additions & 4 deletions packages/ui/app/_components/dialogs/manage/BorrowTab.tsx
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
import { useEffect, useMemo } from 'react';

import { Info } from 'lucide-react';
import { formatUnits } from 'viem';

import { Alert, AlertDescription } from '@ui/components/ui/alert';
import { Button } from '@ui/components/ui/button';
import {
HFPStatus,
TransactionType,
useManageDialogContext
} from '@ui/context/ManageDialogContext';
import { useBorrow } from '@ui/hooks/market/useBorrow';
import { useHealth } from '@ui/hooks/market/useHealth';

import Amount from './Amount';
import StatusAlerts from './StatusAlerts';
import TransactionStepsHandler from './TransactionStepsHandler';
import ResultHandler from '../../ResultHandler';
import MemoizedUtilizationStats from '../../UtilizationStats';
import { useHealth } from '@ui/hooks/market/useHealth';
import { useEffect } from 'react';

interface BorrowTabProps {
maxAmount: bigint;
Expand All @@ -31,13 +33,13 @@ interface BorrowTabProps {
const BorrowTab = ({ maxAmount, isLoadingMax, totalStats }: BorrowTabProps) => {
const {
selectedMarketData,
transactionSteps,
resetTransactionSteps,
chainId,
isLoadingUpdatedAssets,
updatedValues,
comptrollerAddress,
setPredictionAmount
setPredictionAmount,
getStepsForTypes
} = useManageDialogContext();

const {
Expand Down Expand Up @@ -77,8 +79,13 @@ const BorrowTab = ({ maxAmount, isLoadingMax, totalStats }: BorrowTabProps) => {

useEffect(() => {
setPredictionAmount(amountAsBInt);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [amountAsBInt]);

const transactionSteps = useMemo(() => {
return getStepsForTypes(TransactionType.BORROW);
}, [getStepsForTypes]);

return (
<div className="space-y-4 pt-4">
<Amount
Expand Down
15 changes: 11 additions & 4 deletions packages/ui/app/_components/dialogs/manage/RepayTab.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { useEffect, useMemo } from 'react';

import { formatUnits } from 'viem';

import { Button } from '@ui/components/ui/button';
import {
HFPStatus,
TransactionType,
useManageDialogContext
} from '@ui/context/ManageDialogContext';
import { useHealth } from '@ui/hooks/market/useHealth';
import { useRepay } from '@ui/hooks/market/useRepay';

import Amount from './Amount';
import StatusAlerts from './StatusAlerts';
import TransactionStepsHandler from './TransactionStepsHandler';
import ResultHandler from '../../ResultHandler';
import MemoizedUtilizationStats from '../../UtilizationStats';
import { useHealth } from '@ui/hooks/market/useHealth';
import { useEffect } from 'react';

interface RepayTabProps {
maxAmount: bigint;
Expand All @@ -29,13 +31,13 @@ interface RepayTabProps {
const RepayTab = ({ maxAmount, isLoadingMax, totalStats }: RepayTabProps) => {
const {
selectedMarketData,
transactionSteps,
resetTransactionSteps,
chainId,
isLoadingUpdatedAssets,
updatedValues,
comptrollerAddress,
setPredictionAmount
setPredictionAmount,
getStepsForTypes
} = useManageDialogContext();

const {
Expand Down Expand Up @@ -65,8 +67,13 @@ const RepayTab = ({ maxAmount, isLoadingMax, totalStats }: RepayTabProps) => {

useEffect(() => {
setPredictionAmount(amountAsBInt);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [amountAsBInt]);

const transactionSteps = useMemo(() => {
return getStepsForTypes(TransactionType.REPAY);
}, [getStepsForTypes]);

return (
<div className="space-y-4 pt-4">
<Amount
Expand Down
15 changes: 9 additions & 6 deletions packages/ui/app/_components/dialogs/manage/SupplyTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@ import {
TooltipContent,
TooltipTrigger
} from '@ui/components/ui/tooltip';
import { useManageDialogContext } from '@ui/context/ManageDialogContext';
import {
TransactionType,
useManageDialogContext
} from '@ui/context/ManageDialogContext';
import { useCollateralToggle } from '@ui/hooks/market/useCollateralToggle';
import { useSupply } from '@ui/hooks/market/useSupply';

Expand Down Expand Up @@ -45,7 +48,8 @@ const SupplyTab = ({
updatedValues,
isLoadingUpdatedAssets,
refetchUsedQueries,
setPredictionAmount
setPredictionAmount,
getStepsForTypes
} = useManageDialogContext();

const {
Expand Down Expand Up @@ -77,13 +81,12 @@ const SupplyTab = ({

useEffect(() => {
setPredictionAmount(amountAsBInt);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [amountAsBInt]);

console.log('collateralTxSteps', collateralTxSteps);
console.log('supplyTxSteps', supplyTxSteps);
const combinedTransactionSteps = useMemo(() => {
return [...supplyTxSteps, ...collateralTxSteps];
}, [supplyTxSteps, collateralTxSteps]);
return getStepsForTypes(TransactionType.SUPPLY, TransactionType.COLLATERAL);
}, [getStepsForTypes]);

const isDisabled = !amount || amountAsBInt === 0n;
const hasActiveTransactions = combinedTransactionSteps.length > 0;
Expand Down
15 changes: 11 additions & 4 deletions packages/ui/app/_components/dialogs/manage/WithdrawTab.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
import { useEffect, useMemo } from 'react';

import { formatUnits } from 'viem';

import { Button } from '@ui/components/ui/button';
import {
HFPStatus,
TransactionType,
useManageDialogContext
} from '@ui/context/ManageDialogContext';
import { useHealth } from '@ui/hooks/market/useHealth';
import { useWithdraw } from '@ui/hooks/market/useWithdraw';

import Amount from './Amount';
import StatusAlerts from './StatusAlerts';
import TransactionStepsHandler from './TransactionStepsHandler';
import ResultHandler from '../../ResultHandler';
import MemoizedUtilizationStats from '../../UtilizationStats';
import { useHealth } from '@ui/hooks/market/useHealth';
import { useEffect } from 'react';

interface WithdrawTabProps {
maxAmount: bigint;
Expand All @@ -33,13 +35,13 @@ const WithdrawTab = ({
}: WithdrawTabProps) => {
const {
selectedMarketData,
transactionSteps,
resetTransactionSteps,
chainId,
updatedValues,
isLoadingUpdatedAssets,
comptrollerAddress,
setPredictionAmount
setPredictionAmount,
getStepsForTypes // Add this from context
} = useManageDialogContext();

const {
Expand Down Expand Up @@ -77,8 +79,13 @@ const WithdrawTab = ({

useEffect(() => {
setPredictionAmount(amountAsBInt);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [amountAsBInt]);

const transactionSteps = useMemo(() => {
return getStepsForTypes(TransactionType.WITHDRAW);
}, [getStepsForTypes]);

return (
<div className="space-y-4 pt-4">
<Amount
Expand Down
82 changes: 64 additions & 18 deletions packages/ui/context/ManageDialogContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,22 @@ export enum HFPStatus {
WARNING = 'WARNING'
}

export enum TransactionType {
SUPPLY = 'SUPPLY',
COLLATERAL = 'COLLATERAL',
BORROW = 'BORROW',
REPAY = 'REPAY',
WITHDRAW = 'WITHDRAW'
}

interface TransactionStepsState {
[TransactionType.SUPPLY]: TransactionStep[];
[TransactionType.COLLATERAL]: TransactionStep[];
[TransactionType.BORROW]: TransactionStep[];
[TransactionType.REPAY]: TransactionStep[];
[TransactionType.WITHDRAW]: TransactionStep[];
}

interface ManageDialogContextType {
active: ActiveTab;
setActive: (tab: ActiveTab) => void;
Expand All @@ -59,16 +75,18 @@ interface ManageDialogContextType {
};
isLoadingUpdatedAssets: boolean;
setPredictionAmount: (amount: bigint) => void;
transactionSteps: TransactionStep[];
addStepsForAction: (steps: TransactionStep[]) => void;
upsertTransactionStep: (
transactionSteps: TransactionStepsState;
addStepsForType: (type: TransactionType, steps: TransactionStep[]) => void;
upsertStepForType: (
type: TransactionType,
update:
| {
index: number;
transactionStep: TransactionStep;
}
| undefined
) => void;
getStepsForTypes: (...types: TransactionType[]) => TransactionStep[];
}

const formatBalance = (value: bigint | undefined, decimals: number): string => {
Expand Down Expand Up @@ -99,9 +117,14 @@ export const ManageDialogProvider: React.FC<{
[]
);
const [predictionAmount, setPredictionAmount] = useState<bigint>(0n);
const [transactionSteps, setTransactionSteps] = useState<TransactionStep[]>(
[]
);
const [transactionSteps, setTransactionSteps] =
useState<TransactionStepsState>({
[TransactionType.SUPPLY]: [],
[TransactionType.COLLATERAL]: [],
[TransactionType.BORROW]: [],
[TransactionType.REPAY]: [],
[TransactionType.WITHDRAW]: []
});

useEffect(() => {
setCurrentFundOperation(operationMap[active]);
Expand Down Expand Up @@ -226,19 +249,23 @@ export const ManageDialogProvider: React.FC<{
};
}, [chainId, updatedAsset, selectedMarketData, updatedAssets, currentSdk]);

const upsertTransactionStep = useCallback(
const upsertStepForType = useCallback(
(
type: TransactionType,
updatedStep:
| { index: number; transactionStep: TransactionStep }
| undefined
) => {
if (!updatedStep) {
setTransactionSteps([]);
setTransactionSteps((prev) => ({
...prev,
[type]: []
}));
return;
}

setTransactionSteps((prevState) => {
const currentSteps = prevState.slice();
setTransactionSteps((prev) => {
const currentSteps = prev[type].slice();
currentSteps[updatedStep.index] = {
...currentSteps[updatedStep.index],
...updatedStep.transactionStep
Expand All @@ -256,24 +283,42 @@ export const ManageDialogProvider: React.FC<{
}
}

return currentSteps;
return {
...prev,
[type]: currentSteps
};
});
},
[]
);

const addStepsForAction = useCallback(
(steps: TransactionStep[]) => {
const addStepsForType = useCallback(
(type: TransactionType, steps: TransactionStep[]) => {
steps.forEach((step, i) =>
upsertTransactionStep({ index: i, transactionStep: step })
upsertStepForType(type, { index: i, transactionStep: step })
);
},
[upsertTransactionStep]
[upsertStepForType]
);

const getStepsForTypes = useCallback(
(...types: TransactionType[]) => {
return types.reduce<TransactionStep[]>((acc, type) => {
return [...acc, ...transactionSteps[type]];
}, []);
},
[transactionSteps]
);

const resetTransactionSteps = useCallback(() => {
refetchUsedQueries();
setTransactionSteps([]); // Clear steps directly
setTransactionSteps({
[TransactionType.SUPPLY]: [],
[TransactionType.COLLATERAL]: [],
[TransactionType.BORROW]: [],
[TransactionType.REPAY]: [],
[TransactionType.WITHDRAW]: []
});
}, [refetchUsedQueries]);

return (
Expand All @@ -290,8 +335,9 @@ export const ManageDialogProvider: React.FC<{
updatedValues,
setPredictionAmount,
transactionSteps,
addStepsForAction,
upsertTransactionStep
addStepsForType,
upsertStepForType,
getStepsForTypes
}}
>
{children}
Expand Down
Loading

0 comments on commit fc62b28

Please sign in to comment.