Skip to content

Commit 6b5aac4

Browse files
authored
fix(nami): [LW-11758] error not displayed setting collateral with insufficient balance (#1493)
* fix(nami): reset collateral data on modal close * fix(nami): correctly await setting collateral * fix(nami): init collateral explicitly checks available utxos * fix(nami): clear pw secrets on collateral modal close * fixup! fix(nami): init collateral explicitly checks available utxos
1 parent 8115e9b commit 6b5aac4

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

packages/nami/src/adapters/collateral.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,12 @@ describe('useCollateral', () => {
112112
const unspendable$ = of({
113113
coins: BigInt(4999),
114114
});
115+
mockWithSignTxConfirmation.mockImplementation(
116+
async (mockSubmitCollateralTx, password) => {
117+
await mockSubmitCollateralTx();
118+
},
119+
);
120+
115121
const { result } = renderHook(() =>
116122
useCollateral({
117123
inMemoryWallet: {
@@ -132,10 +138,12 @@ describe('useCollateral', () => {
132138
await result.current.submitCollateral(password);
133139

134140
expect(mockWithSignTxConfirmation).toBeCalledWith(
135-
mockSubmitCollateralTx,
141+
expect.any(Function),
136142
password,
137143
);
144+
138145
expect(mockWithSignTxConfirmation).toBeCalledTimes(1);
146+
expect(mockSubmitCollateralTx).toBeCalledTimes(1);
139147
});
140148

141149
describe('getCollateralUtxo', () => {

packages/nami/src/adapters/collateral.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,11 @@ export const useCollateral = ({
3232
await inMemoryWallet.utxo.setUnspendable([]);
3333
}, [inMemoryWallet.utxo]);
3434
const submitCollateral = useCallback(
35-
async (password: string) =>
36-
withSignTxConfirmation(submitCollateralTx, password),
35+
async (password: string) => {
36+
await withSignTxConfirmation(async () => {
37+
await submitCollateralTx();
38+
}, password);
39+
},
3740
[submitCollateralTx, withSignTxConfirmation],
3841
);
3942

packages/nami/src/ui/app/components/transactionBuilder.tsx

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ import ConfirmModal from './confirmModal';
4444
import UnitDisplay from './unitDisplay';
4545

4646
import type { ConfirmModalRef } from './confirmModal';
47+
import { firstValueFrom } from 'rxjs';
4748

4849
type States = 'DONE' | 'EDITING' | 'ERROR' | 'LOADING';
4950
const PoolStates: Record<States, States> = {
@@ -117,13 +118,12 @@ const TransactionBuilder = (undefined, ref) => {
117118
isInitializingCollateral,
118119
initializeCollateralTx: initializeCollateral,
119120
collateralFee,
120-
121121
buildDelegation,
122122
setSelectedStakePool,
123123
delegationTxFee,
124124
isBuildingTx,
125125
stakingError,
126-
passwordUtil: { setPassword },
126+
passwordUtil: { setPassword, clearSecrets },
127127
signAndSubmitTransaction,
128128
getStakePoolInfo,
129129
submitCollateralTx,
@@ -248,8 +248,19 @@ const TransactionBuilder = (undefined, ref) => {
248248
}
249249
collateralRef.current?.openModal();
250250

251+
const available = await firstValueFrom(
252+
inMemoryWallet.balance.utxo.available$,
253+
);
254+
251255
try {
252-
await initializeCollateral();
256+
if (available.coins < BigInt(5_000_000)) {
257+
setData(d => ({
258+
...d,
259+
error: 'Transaction not possible (maybe insufficient balance)',
260+
}));
261+
} else {
262+
await initializeCollateral();
263+
}
253264
} catch {
254265
setData(d => ({
255266
...d,
@@ -573,7 +584,7 @@ const TransactionBuilder = (undefined, ref) => {
573584
/>
574585
<ConfirmModal
575586
isPopup={true}
576-
ready={!isInitializingCollateral}
587+
ready={!isInitializingCollateral && !data.error}
577588
title={
578589
<Box display="flex" alignItems="center">
579590
<Icon as={FaRegFileCode} mr="2" /> <Box>Collateral</Box>
@@ -587,9 +598,11 @@ const TransactionBuilder = (undefined, ref) => {
587598
}}
588599
onCloseBtn={() => {
589600
capture(Events.SettingsCollateralXClick);
601+
clearSecrets();
590602
}}
591603
onConfirm={(status, error) => {
592604
if (status) {
605+
clearSecrets();
593606
capture(Events.SettingsCollateralConfirmClick);
594607
toast({
595608
title: 'Collateral added',

0 commit comments

Comments
 (0)