Skip to content

Commit

Permalink
fix: retry state improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
k0beLeenders committed Nov 27, 2024
1 parent 9550f61 commit 77d81a3
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ export const LendBox = ({
<ActionMessage
_actionMessage={actionMessage}
retry={refreshSimulation}
isRetrying={isSimulating.status === SimulationStatus.SIMULATING}
isRetrying={isSimulating.isLoading}
/>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -421,7 +421,7 @@ export const LoopBox = ({
<ActionMessage
_actionMessage={actionMessage}
retry={refreshSimulation}
isRetrying={isSimulating.status === SimulationStatus.SIMULATING}
isRetrying={isSimulating.isLoading}
/>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,10 +409,7 @@ export const RepayCollatBox = ({
<ActionMessage
_actionMessage={actionMessage}
retry={refreshSimulation}
isRetrying={
isSimulating.status === SimulationStatus.SIMULATING ||
isSimulating.status === SimulationStatus.PREPARING
}
isRetrying={isSimulating.isLoading}
/>
</div>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
import React from "react";

import { LAMPORTS_PER_SOL, Transaction, TransactionMessage, VersionedTransaction } from "@solana/web3.js";
import { createJupiterApiClient } from "@jup-ag/api";
import { Transaction, VersionedTransaction } from "@solana/web3.js";

import { makeBundleTipIx, makeUnwrapSolIx, MarginfiAccountWrapper, MarginfiClient } from "@mrgnlabs/marginfi-client-v2";
import { MarginfiClient } from "@mrgnlabs/marginfi-client-v2";
import { ExtendedBankInfo, ActionType } from "@mrgnlabs/marginfi-v2-ui-state";
import {
ActionMessageType,
deserializeInstruction,
extractErrorString,
getSwapQuoteWithRetry,
LstData,
StakeActionTxns,
STATIC_SIMULATION_ERRORS,
usePrevious,
} from "@mrgnlabs/mrgn-utils";
import {
LST_MINT,
LUT_PROGRAM_AUTHORITY_INDEX,
NATIVE_MINT as SOL_MINT,
SolanaTransaction,
TransactionBroadcastType,
uiToNative,
} from "@mrgnlabs/mrgn-common";

import { createStakeLstTx, createUnstakeLstTx, getSimulationResult } from "../utils";
import { SimulationStatus } from "../../../utils/simulation.utils";
Expand Down Expand Up @@ -57,7 +46,6 @@ export function useStakeSimulation({
setIsLoading,
}: StakeSimulationProps) {
const [slippageBps, platformFeeBps] = useActionBoxStore((state) => [state.slippageBps, state.platformFeeBps]);
const [simulationStatus, setSimulationStatus] = React.useState<SimulationStatus>(SimulationStatus.IDLE);
const prevDebouncedAmount = usePrevious(debouncedAmount);
const [hasUserInteracted, setHasUserInteracted] = React.useState(false);

Expand Down Expand Up @@ -104,7 +92,6 @@ export function useStakeSimulation({
const fetchTxs = React.useCallback(
async (amount: number, actionType: ActionType) => {
setHasUserInteracted(true);
setSimulationStatus(SimulationStatus.PREPARING);
const connection = marginfiClient?.provider.connection;

if (amount === 0 || !selectedBank || !connection || !lstData) {
Expand Down Expand Up @@ -180,11 +167,7 @@ export function useStakeSimulation({
if (prevDebouncedAmount !== debouncedAmount) {
// Only set to PREPARING if we're actually going to simulate
if (debouncedAmount > 0) {
setSimulationStatus(SimulationStatus.PREPARING);
fetchTxs(debouncedAmount, actionMode);
} else {
// If amount is 0, move back to idle
setSimulationStatus(SimulationStatus.IDLE);
}
}
}, [prevDebouncedAmount, debouncedAmount, fetchTxs, actionMode, hasUserInteracted]);
Expand All @@ -204,5 +187,5 @@ export function useStakeSimulation({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [actionTxns]);

return { handleSimulation, refreshSimulation, simulationStatus };
return { handleSimulation, refreshSimulation };
}
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ export const StakeBox = ({
};
}, [refreshState]);

const { handleSimulation, refreshSimulation, simulationStatus } = useStakeSimulation({
const { handleSimulation, refreshSimulation } = useStakeSimulation({
debouncedAmount: debouncedAmount ?? 0,
selectedBank,
actionMode,
Expand Down Expand Up @@ -374,7 +374,7 @@ export const StakeBox = ({
<ActionMessage
_actionMessage={actionMessage}
retry={refreshSimulation}
isRetrying={isSimulating.status === SimulationStatus.SIMULATING}
isRetrying={isSimulating.isLoading}
/>
</div>
)
Expand All @@ -393,7 +393,7 @@ export const StakeBox = ({

<div className="flex items-center justify-between">
<ActionSimulationStatus
simulationStatus={simulationStatus}
simulationStatus={isSimulating.status}
hasErrorMessages={additionalActionMessages.length > 0}
isActive={selectedBank && amount > 0 ? true : false}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ const ActionSimulationStatus = ({
// clearTimeout(timeoutRef.current);
// }

if (hasErrorMessages && !isNewSimulation) {
setSimulationCompleteStatus(SimulationCompleteStatus.ERROR);
} else if (simulationStatus === SimulationStatus.SIMULATING || simulationStatus === SimulationStatus.PREPARING) {
if (simulationStatus === SimulationStatus.SIMULATING || simulationStatus === SimulationStatus.PREPARING) {
setSimulationCompleteStatus(SimulationCompleteStatus.LOADING);
setIsNewSimulation(false);
} else if (hasErrorMessages && !isNewSimulation) {
setSimulationCompleteStatus(SimulationCompleteStatus.ERROR);
} else if (simulationStatus === SimulationStatus.COMPLETE && !isNewSimulation) {
setSimulationCompleteStatus(SimulationCompleteStatus.SUCCESS);
}
Expand Down

0 comments on commit 77d81a3

Please sign in to comment.