diff --git a/src/errors.js b/src/errors.js index db0c98f2c9..17452e8222 100644 --- a/src/errors.js +++ b/src/errors.js @@ -127,8 +127,12 @@ export const SwapNoAvailableProviders = createCustomErrorClass( "SwapNoAvailableProviders" ); -export const SwapExchangeRateOutOfBounds = createCustomErrorClass( - "SwapExchangeRateOutOfBounds" +export const SwapExchangeRateAmountTooLow = createCustomErrorClass( + "SwapExchangeRateAmountTooLow" +); + +export const SwapExchangeRateAmountTooHigh = createCustomErrorClass( + "SwapExchangeRateAmountTooHigh" ); export const SwapUnknownSwapId = createCustomErrorClass("SwapUnknownSwapId"); diff --git a/src/swap/getExchangeRates.js b/src/swap/getExchangeRates.js index d1972536fd..75bb1ae9f7 100644 --- a/src/swap/getExchangeRates.js +++ b/src/swap/getExchangeRates.js @@ -8,7 +8,10 @@ import network from "../network"; import { getSwapAPIBaseURL } from "./"; import { getEnv } from "../env"; import { BigNumber } from "bignumber.js"; -import { SwapExchangeRateOutOfBounds } from "../errors"; +import { + SwapExchangeRateAmountTooLow, + SwapExchangeRateAmountTooHigh, +} from "../errors"; const getExchangeRates: GetExchangeRates = async ( exchange: Exchange, @@ -40,11 +43,17 @@ const getExchangeRates: GetExchangeRates = async ( return res.data.map( ({ rate, rateId, provider, minAmountFrom, maxAmountFrom }) => { if (!rate || !rateId) { - throw new SwapExchangeRateOutOfBounds(null, { - unit: unitFrom.code, - minAmountFrom, - maxAmountFrom, - }); + const isTooSmall = BigNumber(apiAmount).lt(minAmountFrom); + + throw isTooSmall + ? new SwapExchangeRateAmountTooLow(null, { + unit: unitFrom.code, + minAmountFrom, + }) + : new SwapExchangeRateAmountTooHigh(null, { + unit: unitFrom.code, + maxAmountFrom, + }); } // NB Allows us to simply multiply satoshi values from/to diff --git a/src/swap/mock.js b/src/swap/mock.js index 515662b4f8..f766704273 100644 --- a/src/swap/mock.js +++ b/src/swap/mock.js @@ -8,44 +8,55 @@ import type { GetProviders, SwapRequestEvent, } from "./types"; -import { getAccountCurrency, getAccountUnit } from "../account"; +import { getAccountUnit } from "../account"; import type { Transaction } from "../types"; -import { SwapExchangeRateOutOfBounds } from "../errors"; +import { + SwapExchangeRateAmountTooLow, + SwapExchangeRateAmountTooHigh, +} from "../errors"; import { Observable, of } from "rxjs"; export const mockGetExchangeRates = async ( exchange: Exchange, transaction: Transaction ) => { - const { fromAccount, toAccount } = exchange; + const { fromAccount } = exchange; const amount = transaction.amount; - const from = getAccountCurrency(fromAccount).id; - const to = getAccountCurrency(toAccount).id; const unitFrom = getAccountUnit(fromAccount); const amountFrom = amount.div(BigNumber(10).pow(unitFrom.magnitude)); + const minAmountFrom = BigNumber(0.0001); + const maxAmountFrom = BigNumber(1000); + const apiAmount = BigNumber(amountFrom).div( + BigNumber(10).pow(unitFrom.magnitude) + ); - if (amountFrom.gte(0.00001) && amountFrom.lte(100000)) { - //Fake delay to show loading UI - await new Promise((r) => setTimeout(r, 800)); - //Mock OK, not really magnitude aware - return [ - { - rate: BigNumber("4.2"), - magnitudeAwareRate: BigNumber("4.2"), - rateId: "mockedRateId", - provider: "changelly", - expirationDate: new Date(), - }, - ]; - } else { - //Mock KO - throw new SwapExchangeRateOutOfBounds(null, { - from, - to, - minAmountFrom: 0.00001, - maxAmountFrom: 10, + if (apiAmount.lte(minAmountFrom)) { + throw new SwapExchangeRateAmountTooLow(null, { + unit: unitFrom.code, + minAmountFrom, }); } + + if (apiAmount.gte(maxAmountFrom)) { + throw new SwapExchangeRateAmountTooHigh(null, { + unit: unitFrom.code, + maxAmountFrom, + }); + } + + //Fake delay to show loading UI + await new Promise((r) => setTimeout(r, 800)); + + //Mock OK, not really magnitude aware + return [ + { + rate: BigNumber("4.2"), + magnitudeAwareRate: BigNumber("4.2"), + rateId: "mockedRateId", + provider: "changelly", + expirationDate: new Date(), + }, + ]; }; export const mockInitSwap = (