Skip to content

Commit

Permalink
Merge pull request #11877 from Gamboster/fix/amountScientificNotation…
Browse files Browse the repository at this point in the history
…Requests

Fix: avoid scientific notation in token swap requests
  • Loading branch information
cmgustavo authored Sep 15, 2021
2 parents 2633cb6 + 3215edc commit 038dd6a
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 5 deletions.
10 changes: 9 additions & 1 deletion src/pages/exchange-crypto/exchange-crypto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -846,16 +846,24 @@ export class ExchangeCryptoPage {
);
})[0];

// workaround to prevent scientific notation
const _amount: number = this.amountFrom * 10 ** this.fromToken.decimals;
const minUnitAmount: string = _amount.toLocaleString('fullwide', {
useGrouping: false,
maximumFractionDigits: 0
});

const data: any = {
fromTokenAddress: this.fromToken.address,
toTokenAddress: this.toToken.address,
amount: this.amountFrom * 10 ** this.fromToken.decimals // amount in minimum unit
amount: minUnitAmount // amount in minimum unit
};

if (this.referrerFee) {
data.fee = this.referrerFee; // taking this fee from BWS. This percentage of fromTokenAddress token amount will be sent to referrerAddress, the rest will be used as input for a swap | min: 0; max: 3; default: 0;
}

this.logger.debug('quoteRequestData: ', data);
this.oneInchProvider
.getQuote1inch(data)
.then(data => {
Expand Down
20 changes: 16 additions & 4 deletions src/pages/token-swap/token-swap-checkout/token-swap-checkout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,20 +133,32 @@ export class TokenSwapCheckoutPage {
// Low values increase chances that transaction will fail, high values increase chances of front running. Set values in the range from 0 to 50
const slippage = 0.5;

let minUnitAmount;
if (this.useSendMax && this.fromTokenBalance) {
minUnitAmount = this.fromTokenBalance;
} else {
// workaround to prevent scientific notation
const _amount: number =
this.amountFrom * 10 ** this.fromToken.decimals;
minUnitAmount = _amount.toLocaleString('fullwide', {
useGrouping: false,
maximumFractionDigits: 0
});
}

let swapRequestData = {
fromTokenAddress: this.fromToken.address,
toTokenAddress: this.toToken.address,
amount:
this.useSendMax && this.fromTokenBalance
? this.fromTokenBalance
: this.amountFrom * 10 ** this.fromToken.decimals, // amount in minimum unit
amount: minUnitAmount, // amount in minimum unit
fromAddress, // we can use '0x0000000000000000000000000000000000000000' for testing purposes
slippage: this.navParams.data.slippage
? this.navParams.data.slippage
: slippage,
destReceiver: toAddress
};

this.logger.debug('swapRequestData: ', swapRequestData);

this.oneInchProvider
.getSwap1inch(this.fromWalletSelected, swapRequestData)
.then(data => {
Expand Down

0 comments on commit 038dd6a

Please sign in to comment.