Skip to content

Commit

Permalink
add minAmountError nandling for changeNow (#702)
Browse files Browse the repository at this point in the history
  • Loading branch information
IDIDOS authored Sep 2, 2024
2 parents f1e0aca + b8d3663 commit e11cb53
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 15 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "rubic-sdk",
"version": "5.36.2",
"version": "5.36.3",
"description": "Simplify dApp creation",
"main": "lib/index.js",
"types": "lib/index.d.ts",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import BigNumber from 'bignumber.js';
import { MaxAmountError, MinAmountError, NotSupportedTokensError } from 'src/common/errors';
import {
MaxAmountError,
MinAmountError,
NotSupportedTokensError,
RubicSdkError
} from 'src/common/errors';
import { PriceToken, PriceTokenAmount, Token } from 'src/common/tokens';
import { nativeTokensList } from 'src/common/tokens/constants/native-tokens';
import { compareAddresses } from 'src/common/utils/blockchain';
Expand Down Expand Up @@ -145,7 +150,12 @@ export class ChangenowCrossChainProvider extends CrossChainProvider {
const transit = onChainTrade ? transitCurrency! || nativeCurrency! : fromCurrency!;

try {
const toAmount = await this.getToAmount(transit, toCurrency, transitMinAmount);
const { toAmount, quoteError } = await this.getToAmount(
transit,
toCurrency,
transitMinAmount,
from.symbol
);

const to = new PriceTokenAmount({
...toToken.asStruct,
Expand Down Expand Up @@ -174,7 +184,13 @@ export class ChangenowCrossChainProvider extends CrossChainProvider {
options.providerAddress,
await this.getRoutePath(from, to)
);

if (quoteError) {
return {
trade,
error: quoteError,
tradeType: this.type
};
}
const error = await this.checkMinMaxAmounts(transitFromToken, transit, toCurrency);
if (error) {
return {
Expand Down Expand Up @@ -256,17 +272,33 @@ export class ChangenowCrossChainProvider extends CrossChainProvider {
private async getToAmount(
fromCurrency: ChangenowCurrency,
toCurrency: ChangenowCurrency,
fromAmount: BigNumber
): Promise<BigNumber> {
const res = await ChangeNowCrossChainApiService.getQuoteTx({
fromCurrency: fromCurrency.ticker,
toCurrency: toCurrency.ticker,
fromAmount: fromAmount.toFixed(),
fromNetwork: fromCurrency.network,
toNetwork: toCurrency.network
});

return new BigNumber(res.toAmount);
fromAmount: BigNumber,
fromSymbol: string
): Promise<{
toAmount: BigNumber;
quoteError?: RubicSdkError;
}> {
try {
const res = await ChangeNowCrossChainApiService.getQuoteTx({
fromCurrency: fromCurrency.ticker,
toCurrency: toCurrency.ticker,
fromAmount: fromAmount.toFixed(),
fromNetwork: fromCurrency.network,
toNetwork: toCurrency.network
});

return { toAmount: new BigNumber(res.toAmount) };
} catch (err) {
const error = err?.error;
if (error?.message?.includes('Out of min amount')) {
const minAmount = new BigNumber(error?.payload?.range?.minAmount);
return {
toAmount: new BigNumber(0),
quoteError: new MinAmountError(minAmount, fromSymbol)
};
}
throw new RubicSdkError(error?.message);
}
}

private async getMinMaxRange(
Expand Down

0 comments on commit e11cb53

Please sign in to comment.