Skip to content

Commit 916fa6d

Browse files
committed
[DDW 1034]: Refactor error handling for reward only wallets
1 parent ce56d89 commit 916fa6d

File tree

3 files changed

+110
-122
lines changed

3 files changed

+110
-122
lines changed

source/renderer/app/api/api.ts

Lines changed: 12 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@ import Asset from '../domains/Asset';
228228
import { getAssets } from './assets/requests/getAssets';
229229
import { getAccountPublicKey } from './wallets/requests/getAccountPublicKey';
230230
import { doesWalletRequireAdaToRemainToSupportTokens } from './utils/apiHelpers';
231+
import { handleNotEnoughMoneyError } from './errors';
231232

232233
export default class AdaApi {
233234
config: RequestConfig;
@@ -1059,51 +1060,14 @@ export default class AdaApi {
10591060
minimumAda,
10601061
};
10611062
} catch (error) {
1062-
let notEnoughMoneyError;
1063-
1064-
if (walletBalance.gt(availableBalance)) {
1065-
// 1. Amount exceeds availableBalance due to pending transactions:
1066-
// - walletBalance > availableBalance
1067-
// = show "Cannot calculate fees while there are pending transactions."
1068-
notEnoughMoneyError = 'canNotCalculateTransactionFees';
1069-
} else if (
1070-
!walletBalance.isZero() &&
1071-
walletBalance.isEqualTo(rewardsBalance)
1072-
) {
1073-
// 2. Wallet contains only rewards:
1074-
// - walletBalance === rewardsBalance
1075-
// = show "Cannot send from a wallet that contains only rewards balances."
1076-
notEnoughMoneyError = 'inputsDepleted';
1077-
} else {
1078-
// 3. Amount exceeds walletBalance:
1079-
// - walletBalance === availableBalance
1080-
// = show "Not enough Ada. Try sending a smaller amount."
1081-
notEnoughMoneyError = 'notEnoughFundsForTransaction';
1082-
}
1083-
1084-
// ApiError with logging showcase
1085-
throw new ApiError(error, {
1086-
// @ts-ignore ts-migrate(2322) FIXME: Type 'boolean' is not assignable to type 'Record<s... Remove this comment to see the full error message
1087-
logError: true,
1088-
msg: 'AdaApi::calculateTransactionFee error',
1089-
})
1090-
.set(notEnoughMoneyError, true)
1091-
.where('code', 'not_enough_money')
1092-
.set('utxoTooSmall', true, {
1093-
// @ts-ignore ts-migrate(2339) FIXME: Property 'exec' does not exist on type '{}'.
1094-
minimumAda: get(
1095-
/(Expected min coin value: +)([0-9]+.[0-9]+)/.exec(error.message),
1096-
2,
1097-
0
1098-
),
1099-
})
1100-
.where('code', 'utxo_too_small')
1101-
.set('invalidAddress')
1102-
.where('code', 'bad_request')
1103-
.inc('message', 'Unable to decode Address')
1104-
.result();
1063+
handleNotEnoughMoneyError(error, {
1064+
walletBalance,
1065+
availableBalance,
1066+
rewardsBalance,
1067+
});
11051068
}
11061069
};
1070+
11071071
selectCoins = async (request: {
11081072
walletId: string;
11091073
walletBalance: BigNumber;
@@ -1267,48 +1231,12 @@ export default class AdaApi {
12671231
logger.error('AdaApi::selectCoins error', {
12681232
error,
12691233
});
1270-
let notEnoughMoneyError;
12711234

1272-
if (walletBalance.gt(availableBalance)) {
1273-
// 1. Amount exceeds availableBalance due to pending transactions:
1274-
// - walletBalance > availableBalance
1275-
// = show "Cannot calculate fees while there are pending transactions."
1276-
notEnoughMoneyError = 'canNotCalculateTransactionFees';
1277-
} else if (
1278-
!walletBalance.isZero() &&
1279-
walletBalance.isEqualTo(rewardsBalance)
1280-
) {
1281-
// 2. Wallet contains only rewards:
1282-
// - walletBalance === rewardsBalance
1283-
// = show "Cannot send from a wallet that contains only rewards balances."
1284-
notEnoughMoneyError = 'inputsDepleted';
1285-
} else {
1286-
// 3. Amount exceeds walletBalance:
1287-
// - walletBalance === availableBalance
1288-
// = show "Not enough Ada. Try sending a smaller amount."
1289-
notEnoughMoneyError = 'notEnoughFundsForTransaction';
1290-
}
1291-
1292-
// ApiError with logging showcase
1293-
throw new ApiError(error, {
1294-
// @ts-ignore ts-migrate(2322) FIXME: Type 'boolean' is not assignable to type 'Record<s... Remove this comment to see the full error message
1295-
logError: true,
1296-
msg: 'AdaApi::calculateTransactionFee error',
1297-
})
1298-
.set(notEnoughMoneyError, true)
1299-
.where('code', 'not_enough_money')
1300-
.set('utxoTooSmall', true, {
1301-
minimumAda: get(
1302-
/(Expected min coin value: +)([0-9]+.[0-9]+)/.exec(error.message),
1303-
2,
1304-
0
1305-
),
1306-
})
1307-
.where('code', 'utxo_too_small')
1308-
.set('invalidAddress')
1309-
.where('code', 'bad_request')
1310-
.inc('message', 'Unable to decode Address')
1311-
.result();
1235+
handleNotEnoughMoneyError(error, {
1236+
walletBalance,
1237+
availableBalance,
1238+
rewardsBalance,
1239+
});
13121240
}
13131241
};
13141242
createExternalTransaction = async (

source/renderer/app/api/errors.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
1+
import BigNumber from 'bignumber.js';
12
import { defineMessages } from 'react-intl';
23

4+
import ApiError, { ErrorType } from '../domains/ApiError';
5+
36
export const messages = defineMessages({
47
// common
58
wrongEncryptionPassphrase: {
@@ -119,3 +122,60 @@ export const messages = defineMessages({
119122
'"Balance after transaction would not leave enough ada in the wallet to support tokens remaining in wallet',
120123
},
121124
});
125+
126+
type Balances = {
127+
walletBalance: BigNumber;
128+
availableBalance: BigNumber;
129+
rewardsBalance: BigNumber;
130+
};
131+
132+
export const handleNotEnoughMoneyError = (
133+
error: ErrorType,
134+
balance: Balances
135+
) => {
136+
let notEnoughMoneyError;
137+
138+
const { walletBalance, availableBalance, rewardsBalance } = balance;
139+
140+
if (walletBalance.gt(availableBalance)) {
141+
// 1. Amount exceeds availableBalance due to pending transactions:
142+
// - walletBalance > availableBalance
143+
// = show "Cannot calculate fees while there are pending transactions."
144+
notEnoughMoneyError = 'canNotCalculateTransactionFees';
145+
} else if (
146+
!walletBalance.isZero() &&
147+
walletBalance.isEqualTo(rewardsBalance)
148+
) {
149+
// 2. Wallet contains only rewards:
150+
// - walletBalance === rewardsBalance
151+
// = show "Cannot send from a wallet that contains only rewards balances."
152+
notEnoughMoneyError = 'inputsDepleted';
153+
} else {
154+
// 3. Amount exceeds walletBalance:
155+
// - walletBalance === availableBalance
156+
// = show "Not enough Ada. Try sending a smaller amount."
157+
notEnoughMoneyError = 'notEnoughFundsForTransaction';
158+
}
159+
160+
// ApiError with logging showcase
161+
throw new ApiError(error, {
162+
// @ts-ignore ts-migrate(2322) FIXME: Type 'boolean' is not assignable to type 'Record<s... Remove this comment to see the full error message
163+
logError: true,
164+
msg: 'AdaApi::calculateTransactionFee error',
165+
})
166+
.set(notEnoughMoneyError, true)
167+
.where('code', 'not_enough_money')
168+
.set('utxoTooSmall', true, {
169+
// @ts-ignore ts-migrate(2339) FIXME: Property 'exec' does not exist on type '{}'.
170+
minimumAda: get(
171+
/(Expected min coin value: +)([0-9]+.[0-9]+)/.exec(error.message),
172+
2,
173+
0
174+
),
175+
})
176+
.where('code', 'utxo_too_small')
177+
.set('invalidAddress')
178+
.where('code', 'bad_request')
179+
.inc('message', 'Unable to decode Address')
180+
.result();
181+
};

0 commit comments

Comments
 (0)