@@ -228,6 +228,7 @@ import Asset from '../domains/Asset';
228228import { getAssets } from './assets/requests/getAssets' ;
229229import { getAccountPublicKey } from './wallets/requests/getAccountPublicKey' ;
230230import { doesWalletRequireAdaToRemainToSupportTokens } from './utils/apiHelpers' ;
231+ import { handleNotEnoughMoneyError } from './errors' ;
231232
232233export 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- / ( E x p e c t e d m i n c o i n v a l u e : + ) ( [ 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- / ( E x p e c t e d m i n c o i n v a l u e : + ) ( [ 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 (
0 commit comments