Skip to content

Commit

Permalink
Merge pull request #2952 from input-output-hk/chore/ddw-1034-refactor…
Browse files Browse the repository at this point in the history
…-error-handling-in-rewards-only-wallets

[DDW-1034] Refactor error handling for reward only wallets
  • Loading branch information
danielmain committed Jul 26, 2022
2 parents d4c5122 + a5aee21 commit d8a96ec
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 84 deletions.
96 changes: 12 additions & 84 deletions source/renderer/app/api/api.ts
Expand Up @@ -228,6 +228,7 @@ import { getAssets } from './assets/requests/getAssets';
import { getAccountPublicKey } from './wallets/requests/getAccountPublicKey';
import { doesWalletRequireAdaToRemainToSupportTokens } from './utils/apiHelpers';
import { AssetLocalData } from '../types/localDataTypes';
import { handleNotEnoughMoneyError } from './errors';

export default class AdaApi {
config: RequestConfig;
Expand Down Expand Up @@ -1061,51 +1062,14 @@ export default class AdaApi {
minimumAda,
};
} catch (error) {
let notEnoughMoneyError;

if (walletBalance.gt(availableBalance)) {
// 1. Amount exceeds availableBalance due to pending transactions:
// - walletBalance > availableBalance
// = show "Cannot calculate fees while there are pending transactions."
notEnoughMoneyError = 'canNotCalculateTransactionFees';
} else if (
!walletBalance.isZero() &&
walletBalance.isEqualTo(rewardsBalance)
) {
// 2. Wallet contains only rewards:
// - walletBalance === rewardsBalance
// = show "Cannot send from a wallet that contains only rewards balances."
notEnoughMoneyError = 'inputsDepleted';
} else {
// 3. Amount exceeds walletBalance:
// - walletBalance === availableBalance
// = show "Not enough Ada. Try sending a smaller amount."
notEnoughMoneyError = 'notEnoughFundsForTransaction';
}

// ApiError with logging showcase
throw new ApiError(error, {
// @ts-ignore ts-migrate(2322) FIXME: Type 'boolean' is not assignable to type 'Record<s... Remove this comment to see the full error message
logError: true,
msg: 'AdaApi::calculateTransactionFee error',
})
.set(notEnoughMoneyError, true)
.where('code', 'not_enough_money')
.set('utxoTooSmall', true, {
// @ts-ignore ts-migrate(2339) FIXME: Property 'exec' does not exist on type '{}'.
minimumAda: get(
/(Expected min coin value: +)([0-9]+.[0-9]+)/.exec(error.message),
2,
0
),
})
.where('code', 'utxo_too_small')
.set('invalidAddress')
.where('code', 'bad_request')
.inc('message', 'Unable to decode Address')
.result();
handleNotEnoughMoneyError(error, {
walletBalance,
availableBalance,
rewardsBalance,
});
}
};

selectCoins = async (request: {
walletId: string;
walletBalance: BigNumber;
Expand Down Expand Up @@ -1270,48 +1234,12 @@ export default class AdaApi {
logger.error('AdaApi::selectCoins error', {
error,
});
let notEnoughMoneyError;

if (walletBalance.gt(availableBalance)) {
// 1. Amount exceeds availableBalance due to pending transactions:
// - walletBalance > availableBalance
// = show "Cannot calculate fees while there are pending transactions."
notEnoughMoneyError = 'canNotCalculateTransactionFees';
} else if (
!walletBalance.isZero() &&
walletBalance.isEqualTo(rewardsBalance)
) {
// 2. Wallet contains only rewards:
// - walletBalance === rewardsBalance
// = show "Cannot send from a wallet that contains only rewards balances."
notEnoughMoneyError = 'inputsDepleted';
} else {
// 3. Amount exceeds walletBalance:
// - walletBalance === availableBalance
// = show "Not enough Ada. Try sending a smaller amount."
notEnoughMoneyError = 'notEnoughFundsForTransaction';
}

// ApiError with logging showcase
throw new ApiError(error, {
// @ts-ignore ts-migrate(2322) FIXME: Type 'boolean' is not assignable to type 'Record<s... Remove this comment to see the full error message
logError: true,
msg: 'AdaApi::calculateTransactionFee error',
})
.set(notEnoughMoneyError, true)
.where('code', 'not_enough_money')
.set('utxoTooSmall', true, {
minimumAda: get(
/(Expected min coin value: +)([0-9]+.[0-9]+)/.exec(error.message),
2,
0
),
})
.where('code', 'utxo_too_small')
.set('invalidAddress')
.where('code', 'bad_request')
.inc('message', 'Unable to decode Address')
.result();
handleNotEnoughMoneyError(error, {
walletBalance,
availableBalance,
rewardsBalance,
});
}
};
createExternalTransaction = async (
Expand Down
60 changes: 60 additions & 0 deletions source/renderer/app/api/errors.ts
@@ -1,4 +1,7 @@
import BigNumber from 'bignumber.js';
import get from 'lodash/get';
import { defineMessages } from 'react-intl';
import ApiError, { ErrorType } from '../domains/ApiError';

export const messages = defineMessages({
// common
Expand Down Expand Up @@ -119,3 +122,60 @@ export const messages = defineMessages({
'"Balance after transaction would not leave enough ada in the wallet to support tokens remaining in wallet',
},
});

type Balances = {
walletBalance: BigNumber;
availableBalance: BigNumber;
rewardsBalance: BigNumber;
};

export const handleNotEnoughMoneyError = (
error: ErrorType,
balance: Balances
) => {
let notEnoughMoneyError;

const { walletBalance, availableBalance, rewardsBalance } = balance;

if (walletBalance.gt(availableBalance)) {
// 1. Amount exceeds availableBalance due to pending transactions:
// - walletBalance > availableBalance
// = show "Cannot calculate fees while there are pending transactions."
notEnoughMoneyError = 'canNotCalculateTransactionFees';
} else if (
!walletBalance.isZero() &&
walletBalance.isEqualTo(rewardsBalance)
) {
// 2. Wallet contains only rewards:
// - walletBalance === rewardsBalance
// = show "Cannot send from a wallet that contains only rewards balances."
notEnoughMoneyError = 'inputsDepleted';
} else {
// 3. Amount exceeds walletBalance:
// - walletBalance === availableBalance
// = show "Not enough Ada. Try sending a smaller amount."
notEnoughMoneyError = 'notEnoughFundsForTransaction';
}

// ApiError with logging showcase
throw new ApiError(error, {
// @ts-ignore ts-migrate(2322) FIXME: Type 'boolean' is not assignable to type 'Record<s... Remove this comment to see the full error message
logError: true,
msg: 'AdaApi::calculateTransactionFee error',
})
.set(notEnoughMoneyError, true)
.where('code', 'not_enough_money')
.set('utxoTooSmall', true, {
// @ts-ignore ts-migrate(2339) FIXME: Property 'exec' does not exist on type '{}'.
minimumAda: get(
/(Expected min coin value: +)([0-9]+.[0-9]+)/.exec(error.message),
2,
0
),
})
.where('code', 'utxo_too_small')
.set('invalidAddress')
.where('code', 'bad_request')
.inc('message', 'Unable to decode Address')
.result();
};

0 comments on commit d8a96ec

Please sign in to comment.