Skip to content

Commit

Permalink
fix: retry button on transfaction failure should check old firmware e…
Browse files Browse the repository at this point in the history
…rror for ledger (#961)

* fix: retry button on transfaction failure should check old firmware error for ledger

* fix: enable single pool delegation on tx failure re-try button click

* fix: remove eslint warning

---------

Co-authored-by: Tomislav Horaček <[email protected]>
Co-authored-by: Rhys Bartels-Waller <[email protected]>
Co-authored-by: Piotr Czeglik <[email protected]>
  • Loading branch information
4 people authored May 1, 2024
1 parent d18829e commit 00f0c52
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
25 changes: 22 additions & 3 deletions packages/staking/src/features/Drawer/TransactionFail.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable react/no-multi-comp */
import { WalletType } from '@cardano-sdk/web-extension';
import { Button, WarningBanner } from '@lace/common';
import { Box } from '@lace/ui';
import cn from 'classnames';
Expand Down Expand Up @@ -45,17 +46,20 @@ export const TransactionFailFooter = ({ popupView }: TransactionFailProps): Reac
const [isLoading, setIsLoading] = useState<boolean>(false);
const {
delegationStoreSetDelegationTxBuilder: setDelegationTxBuilder,
walletStoreWalletType: walletType,
delegationStoreDelegationTxBuilder: delegationTxBuilder,
password: { password, removePassword },
walletStoreInMemoryWallet: inMemoryWallet,
walletManagerExecuteWithPassword: executeWithPassword,
isMultidelegationSupportedByDevice,
} = useOutsideHandles();
// TODO implement analytics for the new flow
const analytics = {
// eslint-disable-next-line @typescript-eslint/no-empty-function
sendEvent: () => {},
};
const { portfolioMutators } = useDelegationPortfolioStore((store) => ({
const { portfolioMutators, draftPortfolio } = useDelegationPortfolioStore((store) => ({
draftPortfolio: store.draftPortfolio,
portfolioMutators: store.mutators,
}));

Expand All @@ -72,23 +76,38 @@ export const TransactionFailFooter = ({ popupView }: TransactionFailProps): Reac
portfolioMutators.executeCommand({ type: 'CancelDrawer' });
};

const isInMemory = walletType === WalletType.InMemory;

// TODO unify
const signAndSubmitTransaction = useCallback(async () => {
if (!delegationTxBuilder) throw new Error('Unable to submit transaction. The delegationTxBuilder not available');

const isMultidelegation = draftPortfolio && draftPortfolio.length > 1;
if (!isInMemory && isMultidelegation) {
const isSupported = await isMultidelegationSupportedByDevice(walletType);
if (!isSupported) {
throw new Error('MULTIDELEGATION_NOT_SUPPORTED');
}
}
const signedTx = await delegationTxBuilder.build().sign();
await inMemoryWallet.submitTx(signedTx);
}, [delegationTxBuilder, inMemoryWallet]);
}, [delegationTxBuilder, draftPortfolio, inMemoryWallet, isInMemory, isMultidelegationSupportedByDevice, walletType]);

const onSubmit = async () => {
setIsLoading(true);

try {
await signAndSubmitTransaction();
setIsLoading(false);
portfolioMutators.executeCommand({ type: 'DrawerContinue' });
removePassword();
} catch (error) {
} catch (error: unknown) {
console.error('failed to sign or submit tx due to:', error);
setIsLoading(false);

if (error instanceof Error && error.message === 'MULTIDELEGATION_NOT_SUPPORTED') {
portfolioMutators.executeCommand({ type: 'HwSkipToDeviceFailure' });
}
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ export type PortfolioManagementConfirmationCommand =

export type PortfolioManagementSignCommand = CancelDrawer | DrawerContinue | DrawerFailure | DrawerBack;

export type PortfolioManagementFailureCommand = CancelDrawer | DrawerContinue;
export type PortfolioManagementFailureCommand = CancelDrawer | DrawerContinue | HwSkipToDeviceFailure;

export type PortfolioManagementHwFailureCommand = CancelDrawer | DrawerBack;

Expand All @@ -206,7 +206,7 @@ export type NewPortfolioConfirmationCommand =

export type NewPortfolioSignCommand = CancelDrawer | DrawerContinue | DrawerFailure | DrawerBack;

export type NewPortfolioFailureCommand = CancelDrawer | DrawerContinue;
export type NewPortfolioFailureCommand = CancelDrawer | DrawerContinue | HwSkipToDeviceFailure;

export type NewPortfolioHwFailureCommand = CancelDrawer | DrawerBack;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,12 @@ export const processExpandedViewCases: Handler = (params) =>
activeDrawerStep: DrawerManagementStep.Success,
})
),
HwSkipToDeviceFailure: handler<HwSkipToDeviceFailure, StatePortfolioManagement, StatePortfolioManagement>(
({ state }) => ({
...state,
activeDrawerStep: DrawerManagementStep.HwDeviceFailure,
})
),
},
params.command.type,
DrawerManagementStep.Failure
Expand Down Expand Up @@ -593,6 +599,12 @@ export const processExpandedViewCases: Handler = (params) =>
...state,
activeDrawerStep: DrawerManagementStep.Success,
})),
HwSkipToDeviceFailure: handler<HwSkipToDeviceFailure, StatePortfolioManagement, StatePortfolioManagement>(
({ state }) => ({
...state,
activeDrawerStep: DrawerManagementStep.HwDeviceFailure,
})
),
},
params.command.type,
DrawerManagementStep.Failure
Expand Down

0 comments on commit 00f0c52

Please sign in to comment.