Skip to content

Commit

Permalink
feat: modified getVault function to check vault state
Browse files Browse the repository at this point in the history
  • Loading branch information
Polybius93 committed Nov 28, 2023
1 parent 77d0217 commit 7f16405
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
9 changes: 6 additions & 3 deletions src/app/hooks/use-ethereum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { useSelector } from "react-redux";
import { customShiftValue } from "@common/utilities";
import { EthereumError } from "@models/error-types";
import { Network, ethereumNetworks } from "@models/network";
import { RawVault, Vault } from "@models/vault";
import { RawVault, Vault, VaultState } from "@models/vault";
import { RootState, store } from "@store/index";
import { accountActions } from "@store/slices/account/account.actions";
import { vaultActions } from "@store/slices/vault/vault.actions";
Expand All @@ -22,7 +22,7 @@ export interface UseEthereumReturnType {
lockedBTCBalance: number | undefined;
requestEthereumAccount: (network: Network) => Promise<void>;
getAllVaults: () => Promise<void>;
getVault: (vaultUUID: string) => Promise<void>;
getVault: (vaultUUID: string, vaultState: VaultState) => Promise<void>;
setupVault: (btcDepositAmount: number) => Promise<void>;
closeVault: (vaultUUID: string) => Promise<void>;
recommendTokenToMetamask: () => Promise<boolean>;
Expand Down Expand Up @@ -274,15 +274,18 @@ export function useEthereum(): UseEthereumReturnType {

async function getVault(
vaultUUID: string,
vaultState: VaultState,
retryInterval = 5000,
maxRetries = 5,
maxRetries = 10,
): Promise<void> {
for (let i = 0; i < maxRetries; i++) {
try {
if (!protocolContract)
throw new Error("Protocol contract not initialized");
const vault: RawVault = await protocolContract.getVault(vaultUUID);
if (!vault) throw new Error("Vault is undefined");
if (vault.status !== vaultState)
throw new Error("Vault is not in the correct state");
const formattedVault: Vault = formatVault(vault);
store.dispatch(
vaultActions.swapVault({ vaultUUID, updatedVault: formattedVault }),
Expand Down
12 changes: 7 additions & 5 deletions src/app/hooks/use-observer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { mintUnmintActions } from "@store/slices/mintunmint/mintunmint.actions";
import { AnyAction } from "redux";
import { UseEthereumReturnType } from "./use-ethereum";
import { modalActions } from "@store/slices/modal/modal.actions";
import { VaultState } from "@models/vault";

export function useObserver(
ethereum: UseEthereumReturnType,
Expand All @@ -30,7 +31,7 @@ export function useObserver(

console.log(`Vault ${vaultUUID} is ready`);

await getVault(vaultUUID).then(() => {
await getVault(vaultUUID, VaultState.READY).then(() => {
dispatch(mintUnmintActions.setMintStep(1));
});
});
Expand All @@ -44,8 +45,9 @@ export function useObserver(

console.log(`Vault ${vaultUUID} is closing`);

await getVault(vaultUUID);
dispatch(mintUnmintActions.setUnmintStep(1));
await getVault(vaultUUID, VaultState.CLOSING).then(() => {
dispatch(mintUnmintActions.setUnmintStep(1));
});
});

protocolContract.on("SetStatusFunded", async (...args) => {
Expand All @@ -57,7 +59,7 @@ export function useObserver(

console.log(`Vault ${vaultUUID} is minted`);

await getVault(vaultUUID).then(() => {
await getVault(vaultUUID, VaultState.FUNDED).then(() => {
dispatch(mintUnmintActions.setMintStep(0));
dispatch(modalActions.toggleSuccessfulFlowModalVisibility("mint"));
});
Expand All @@ -72,7 +74,7 @@ export function useObserver(

console.log(`Vault ${vaultUUID} is closed`);

await getVault(vaultUUID).then(() => {
await getVault(vaultUUID, VaultState.CLOSED).then(() => {
dispatch(mintUnmintActions.setUnmintStep(0));
dispatch(modalActions.toggleSuccessfulFlowModalVisibility("unmint"));
});
Expand Down

0 comments on commit 7f16405

Please sign in to comment.