diff --git a/rest-api/src/controller/rest.controller.ts b/rest-api/src/controller/rest.controller.ts index e97fcbd..2c262f5 100644 --- a/rest-api/src/controller/rest.controller.ts +++ b/rest-api/src/controller/rest.controller.ts @@ -33,6 +33,8 @@ import { SwapCalcInfo, } from '../service/sdk.service'; import { httpException } from '../error/errors'; +import Big from 'big.js'; +import { convertIntAmountToFloat } from '../utils/calculation'; type RawTransaction = | VersionedTransaction @@ -172,13 +174,13 @@ export class RestController { HttpStatus.BAD_REQUEST, ); } - const floatAmount = parseFloat(amount) / 10 ** sourceTokenObj.decimals; - if (isNaN(floatAmount)) { - throw new HttpException('Invalid amount', HttpStatus.BAD_REQUEST); - } - const minimumReceiveAmountFloat = - parseFloat(minimumReceiveAmount) / 10 ** destinationTokenObj.decimals; - if (isNaN(minimumReceiveAmountFloat)) { + let minimumReceiveAmountFloat: string; + try { + minimumReceiveAmountFloat = convertIntAmountToFloat( + minimumReceiveAmount, + destinationTokenObj.decimals, + ).toFixed(); + } catch (e) { throw new HttpException( 'Invalid minimumReceiveAmount', HttpStatus.BAD_REQUEST, @@ -194,11 +196,11 @@ export class RestController { ); } const params = { - amount: floatAmount.toString(), + amount: convertGt0IntAmountToFloat(amount, sourceTokenObj.decimals), destinationToken: destinationTokenObj, fromAccountAddress: sender, minimumReceiveAmount: minimumReceiveAmount - ? minimumReceiveAmountFloat.toString() + ? minimumReceiveAmountFloat : undefined, sourceToken: sourceTokenObj, toAccountAddress: recipient, @@ -360,10 +362,6 @@ export class RestController { } const feePaymentMethodEnum = FeePaymentMethod[feePaymentMethod as keyof typeof FeePaymentMethod]; - const amountFloat = parseFloat(amount) / 10 ** sourceTokenObj.decimals; - if (isNaN(amountFloat) || amountFloat <= 0) { - throw new HttpException('Invalid amount', HttpStatus.BAD_REQUEST); - } if ( !!solanaTxFeeParams && !Object.keys(SolanaTxFeeParamsMethod).includes(solanaTxFeeParams) @@ -374,7 +372,7 @@ export class RestController { ); } const sendParams = { - amount: amountFloat.toString(), + amount: convertGt0IntAmountToFloat(amount, sourceTokenObj.decimals), destinationToken: destinationTokenObj, fromAccountAddress: sender, sourceToken: sourceTokenObj, @@ -833,14 +831,14 @@ export class RestController { } const messengerEnum = Messenger[messenger as keyof typeof Messenger]; const feeFloat = relayerFeeInStables - ? ( - parseFloat(relayerFeeInStables) / - 10 ** sourceTokenObj.decimals - ).toString() + ? convertIntAmountToFloat( + relayerFeeInStables, + sourceTokenObj.decimals, + ).toFixed() : undefined; try { return await this.sdkService.getAmountToBeReceived( - amount, + convertGt0IntAmountToFloat(amount, sourceTokenObj.decimals), sourceTokenObj, destinationTokenObj, messengerEnum, @@ -896,14 +894,14 @@ export class RestController { } const messengerEnum = Messenger[messenger as keyof typeof Messenger]; const feeFloat = relayerFeeInStables - ? ( - parseFloat(relayerFeeInStables) / - 10 ** sourceTokenObj.decimals - ).toString() + ? convertIntAmountToFloat( + relayerFeeInStables, + sourceTokenObj.decimals, + ).toFixed() : undefined; try { return await this.sdkService.getAmountToSend( - amount, + convertGt0IntAmountToFloat(amount, destinationTokenObj.decimals), sourceTokenObj, destinationTokenObj, messengerEnum, @@ -962,7 +960,7 @@ export class RestController { ); } const params = { - amount: amount.toString(), + amount: convertGt0IntAmountToFloat(amount, tokenAddressObj.decimals), accountAddress: ownerAddress, token: tokenAddressObj, }; @@ -1037,7 +1035,7 @@ export class RestController { ); } const params = { - amount: amount.toString(), + amount: convertGt0IntAmountToFloat(amount, tokenAddressObj.decimals), accountAddress: ownerAddress, token: tokenAddressObj, }; @@ -1183,13 +1181,9 @@ export class RestController { } const feePaymentMethodEnum = FeePaymentMethod[feePaymentMethod as keyof typeof FeePaymentMethod]; - const floatAmount = parseFloat(amount) / 10 ** tokenAddressObj.decimals; - if (isNaN(floatAmount) || floatAmount <= 0) { - throw new HttpException('Invalid amount', HttpStatus.BAD_REQUEST); - } try { return await this.sdkService.checkAllowance({ - amount: floatAmount.toString(), + amount: convertGt0IntAmountToFloat(amount, tokenAddressObj.decimals), owner: ownerAddress, token: tokenAddressObj, gasFeePaymentMethod: feePaymentMethodEnum, @@ -1332,7 +1326,7 @@ export class RestController { } try { return await this.sdkService.getAmountToBeDeposited( - amount, + convertGt0IntAmountToFloat(amount, tokenAddressObj.decimals), tokenAddressObj, ); } catch (e) { @@ -1362,13 +1356,9 @@ export class RestController { if (!tokenAddressObj) { throw new HttpException('Token not found', HttpStatus.BAD_REQUEST); } - const floatAmount = parseFloat(amount) / 10 ** tokenAddressObj.decimals; - if (isNaN(floatAmount) || floatAmount <= 0) { - throw new HttpException('Invalid amount', HttpStatus.BAD_REQUEST); - } try { return await this.sdkService.getAmountToBeWithdrawn( - floatAmount.toString(), + convertGt0IntAmountToFloat(amount, tokenAddressObj.decimals), ownerAddress, tokenAddressObj, ); @@ -1377,3 +1367,20 @@ export class RestController { } } } + +export function convertGt0IntAmountToFloat( + amount: string, + decimals: number, + exceptionMsg: string = 'Invalid amount', +): string { + let amountFloatBig: Big; + try { + amountFloatBig = convertIntAmountToFloat(amount, decimals); + } catch (e) { + throw new HttpException(exceptionMsg, HttpStatus.BAD_REQUEST); + } + if (amountFloatBig.lte(0)) { + throw new HttpException(exceptionMsg, HttpStatus.BAD_REQUEST); + } + return amountFloatBig.toFixed(); +} diff --git a/rest-api/src/service/sdk.service.ts b/rest-api/src/service/sdk.service.ts index 374378a..52f3fa1 100644 --- a/rest-api/src/service/sdk.service.ts +++ b/rest-api/src/service/sdk.service.ts @@ -269,7 +269,7 @@ export class SDKService { } async getAmountToBeReceived( - amount: string, + amountFloat: string, sourceToken: TokenWithChainDetails, destinationToken: TokenWithChainDetails, messenger: Messenger, @@ -281,13 +281,13 @@ export class SDKService { } try { const amountToSendFloat = stableFeeInFloat - ? Big(amount) + ? Big(amountFloat) .minus(stableFeeInFloat) .round(sourceToken.decimals) .toFixed() - : amount; + : amountFloat; if (Big(amountToSendFloat).lte(0)) { - return { amountInFloat: amount, amountReceivedInFloat: '' }; + return { amountInFloat: amountFloat, amountReceivedInFloat: '' }; } let amountReceived: string; if ( @@ -308,16 +308,19 @@ export class SDKService { messenger, ); } - return { amountInFloat: amount, amountReceivedInFloat: amountReceived }; + return { + amountInFloat: amountFloat, + amountReceivedInFloat: amountReceived, + }; } catch (e: any) { const errorCode = e.errorCode; console.error('errorCode', errorCode); - return { amountInFloat: amount, amountReceivedInFloat: '' }; + return { amountInFloat: amountFloat, amountReceivedInFloat: '' }; } } async getAmountToSend( - amountReceived: string, + amountReceivedFloat: string, sourceToken: TokenWithChainDetails, destinationToken: TokenWithChainDetails, messenger?: Messenger, @@ -329,7 +332,7 @@ export class SDKService { } let amount; try { - const roundedAmountReceived = Big(amountReceived) + const roundedAmountReceived = Big(amountReceivedFloat) .round(destinationToken.decimals) .toFixed(); if ( @@ -353,7 +356,7 @@ export class SDKService { } catch (e: any) { const errorCode = e.errorCode; console.error('errorCode', errorCode); - return { amountInFloat: '', amountReceivedInFloat: amountReceived }; + return { amountInFloat: '', amountReceivedInFloat: amountReceivedFloat }; } const amountInFloat = stableFeeInFloat ? Big(amount).plus(stableFeeInFloat) @@ -362,7 +365,7 @@ export class SDKService { amountInFloat: amountInFloat .round(sourceToken.decimals, Big.roundUp) .toFixed(), - amountReceivedInFloat: amountReceived, + amountReceivedInFloat: amountReceivedFloat, }; } diff --git a/rest-api/src/utils/calculation.ts b/rest-api/src/utils/calculation.ts new file mode 100644 index 0000000..48a2d50 --- /dev/null +++ b/rest-api/src/utils/calculation.ts @@ -0,0 +1,16 @@ +import { Big, BigSource } from 'big.js'; + +export function convertIntAmountToFloat( + amountInt: BigSource, + decimals: number, +): Big { + const amountValue = Big(amountInt); + if (amountValue.eq(0)) { + return Big(0); + } + return Big(amountValue).div(toPowBase10(decimals)); +} + +export function toPowBase10(decimals: number): Big { + return Big(10).pow(decimals); +}