Skip to content

Commit

Permalink
fix amount float convertation
Browse files Browse the repository at this point in the history
  • Loading branch information
AntonKozAllB committed Sep 9, 2024
1 parent e24e753 commit 08a376c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 48 deletions.
81 changes: 44 additions & 37 deletions rest-api/src/controller/rest.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -374,7 +372,7 @@ export class RestController {
);
}
const sendParams = {
amount: amountFloat.toString(),
amount: convertGt0IntAmountToFloat(amount, sourceTokenObj.decimals),
destinationToken: destinationTokenObj,
fromAccountAddress: sender,
sourceToken: sourceTokenObj,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -962,7 +960,7 @@ export class RestController {
);
}
const params = {
amount: amount.toString(),
amount: convertGt0IntAmountToFloat(amount, tokenAddressObj.decimals),
accountAddress: ownerAddress,
token: tokenAddressObj,
};
Expand Down Expand Up @@ -1037,7 +1035,7 @@ export class RestController {
);
}
const params = {
amount: amount.toString(),
amount: convertGt0IntAmountToFloat(amount, tokenAddressObj.decimals),
accountAddress: ownerAddress,
token: tokenAddressObj,
};
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -1332,7 +1326,7 @@ export class RestController {
}
try {
return await this.sdkService.getAmountToBeDeposited(
amount,
convertGt0IntAmountToFloat(amount, tokenAddressObj.decimals),
tokenAddressObj,
);
} catch (e) {
Expand Down Expand Up @@ -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,
);
Expand All @@ -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();
}
24 changes: 13 additions & 11 deletions rest-api/src/service/sdk.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {
AmountFormatted,
ChainDetailsMap,
ChainSymbol,
CheckAddressResponse,
CheckAllowanceParams,
ExtraGasMaxLimitResponse,
GasBalanceResponse,
Expand Down Expand Up @@ -270,7 +269,7 @@ export class SDKService {
}

async getAmountToBeReceived(
amount: string,
amountFloat: string,
sourceToken: TokenWithChainDetails,
destinationToken: TokenWithChainDetails,
messenger: Messenger,
Expand All @@ -282,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 (
Expand All @@ -309,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,
Expand All @@ -330,7 +332,7 @@ export class SDKService {
}
let amount;
try {
const roundedAmountReceived = Big(amountReceived)
const roundedAmountReceived = Big(amountReceivedFloat)
.round(destinationToken.decimals)
.toFixed();
if (
Expand All @@ -354,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)
Expand All @@ -363,7 +365,7 @@ export class SDKService {
amountInFloat: amountInFloat
.round(sourceToken.decimals, Big.roundUp)
.toFixed(),
amountReceivedInFloat: amountReceived,
amountReceivedInFloat: amountReceivedFloat,
};
}

Expand Down
16 changes: 16 additions & 0 deletions rest-api/src/utils/calculation.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 08a376c

Please sign in to comment.