Skip to content
This repository has been archived by the owner on Jul 15, 2022. It is now read-only.

Commit

Permalink
Merge pull request #899 from juan-cortes/swap-splitRateError
Browse files Browse the repository at this point in the history
Swap - Split the out of range error for rates into two
  • Loading branch information
gre authored Oct 5, 2020
2 parents 10b042c + 5fcb82e commit c628742
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 33 deletions.
8 changes: 6 additions & 2 deletions src/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
21 changes: 15 additions & 6 deletions src/swap/getExchangeRates.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
61 changes: 36 additions & 25 deletions src/swap/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down

1 comment on commit c628742

@vercel
Copy link

@vercel vercel bot commented on c628742 Oct 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.