@ledgerhq/live-common
Version:
Common ground for the Ledger Live apps
201 lines • 7.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.mockPostSwapCancelled = exports.mockPostSwapAccepted = exports.mockGetStatus = exports.mockGetProviders = exports.mockInitSwap = exports.mockGetExchangeRates = void 0;
const bignumber_js_1 = require("bignumber.js");
const rxjs_1 = require("rxjs");
const account_1 = require("../../account");
const currencies_1 = require("../../currencies");
const errors_1 = require("../../errors");
const mockGetExchangeRates = async (exchange, transaction, currencyTo) => {
const { fromAccount, toAccount } = exchange;
const amount = transaction.amount;
const unitFrom = (0, account_1.getAccountCurrency)(fromAccount).units[0];
const unitTo = (currencyTo && currencyTo.units[0]) ?? (0, account_1.getAccountCurrency)(toAccount).units[0];
const tenPowMagnitude = new bignumber_js_1.BigNumber(10).pow(unitFrom.magnitude);
const amountFrom = amount.div(tenPowMagnitude);
const minAmountFrom = new bignumber_js_1.BigNumber(0.0001);
const maxAmountFrom = new bignumber_js_1.BigNumber(1000);
if (amountFrom.lte(minAmountFrom)) {
throw new errors_1.SwapExchangeRateAmountTooLow(undefined, {
minAmountFromFormatted: (0, currencies_1.formatCurrencyUnit)(unitFrom, new bignumber_js_1.BigNumber(minAmountFrom).times(tenPowMagnitude), {
alwaysShowSign: false,
disableRounding: true,
showCode: true,
}),
});
}
if (amountFrom.gte(maxAmountFrom)) {
throw new errors_1.SwapExchangeRateAmountTooHigh(undefined, {
maxAmountFromFormatted: (0, currencies_1.formatCurrencyUnit)(unitFrom, new bignumber_js_1.BigNumber(maxAmountFrom).times(tenPowMagnitude), {
alwaysShowSign: false,
disableRounding: true,
showCode: true,
}),
});
}
//Fake delay to show loading UI
await new Promise(r => setTimeout(r, 800));
const magnitudeAwareRate = new bignumber_js_1.BigNumber(1)
.div(new bignumber_js_1.BigNumber(10).pow(unitFrom.magnitude))
.times(new bignumber_js_1.BigNumber(10).pow(unitTo.magnitude));
//Mock OK, not really magnitude aware
return [
{
rate: new bignumber_js_1.BigNumber("1"),
toAmount: amount.times(magnitudeAwareRate),
magnitudeAwareRate,
rateId: "changellyRateId1",
provider: "changelly",
providerType: "CEX",
expirationDate: new Date(),
tradeMethod: "fixed",
},
{
rate: new bignumber_js_1.BigNumber("1.1"),
toAmount: amount.times(magnitudeAwareRate),
magnitudeAwareRate,
rateId: "changellyRateId2",
provider: "changelly",
providerType: "CEX",
expirationDate: new Date(),
tradeMethod: "float",
},
{
rate: new bignumber_js_1.BigNumber("0.9"),
toAmount: amount.times(magnitudeAwareRate),
magnitudeAwareRate,
rateId: "cicRateId1",
provider: "cic",
providerType: "CEX",
expirationDate: new Date(),
tradeMethod: "float",
},
{
rate: new bignumber_js_1.BigNumber("0.95"),
toAmount: amount.times(magnitudeAwareRate),
magnitudeAwareRate,
rateId: "1inchRateId1",
provider: "oneinch",
providerType: "DEX",
expirationDate: new Date(),
tradeMethod: "float",
providerURL: `https://1inch.com/swap/#/1/unified/swap/eth/usdt&sourceTokenAmount=${transaction.amount}`,
},
];
};
exports.mockGetExchangeRates = mockGetExchangeRates;
const mockInitSwap = (exchange, exchangeRate, transaction) => {
return (0, rxjs_1.of)({
type: "init-swap-result",
initSwapResult: {
transaction,
swapId: "mockedSwapId",
magnitudeAwareRate: new bignumber_js_1.BigNumber(50000),
},
});
};
exports.mockInitSwap = mockInitSwap;
// Need to understand how and why this gets used
const mockGetProviders = async () => {
//Fake delay to show loading UI
await new Promise(r => setTimeout(r, 800));
return [
{
provider: "changelly",
pairs: [
{ from: "bitcoin", to: "ethereum", tradeMethod: "float" },
{ from: "bitcoin", to: "ethereum", tradeMethod: "fixed" },
{ from: "bitcoin", to: "dogecoin", tradeMethod: "float" },
{ from: "bitcoin", to: "dogecoin", tradeMethod: "fixed" },
{ from: "ethereum", to: "bitcoin", tradeMethod: "float" },
{ from: "ethereum", to: "bitcoin", tradeMethod: "fixed" },
{ from: "ethereum", to: "ethereum/erc20/usd_tether__erc20_", tradeMethod: "float" },
],
},
{
provider: "cic",
pairs: [
{ from: "bitcoin", to: "ethereum", tradeMethod: "float" },
{ from: "bitcoin", to: "ethereum", tradeMethod: "fixed" },
{ from: "ethereum", to: "bitcoin", tradeMethod: "float" },
{ from: "ethereum", to: "bitcoin", tradeMethod: "fixed" },
],
},
];
};
exports.mockGetProviders = mockGetProviders;
// Providers using V5 schema. For some reason it doesn't work though (still requires V4)
// return {
// currencies: {
// 7: "ethereum/erc20/usd_tether__erc20_",
// 8: "bitcoin",
// 149: "ethereum",
// },
// providers: {
// changelly: [
// {
// methods: ["fixed", "float"],
// pairs: {
// 7: [8, 149],
// 8: [7, 149],
// 149: [7, 8],
// },
// },
// ],
// cic: [
// {
// methods: ["fixed", "float"],
// pairs: {
// 7: [8, 149],
// 8: [7, 149],
// 149: [7, 8],
// },
// },
// ],
// oneinch: [
// {
// methods: ["float"],
// pairs: {
// 7: [149],
// 149: [7],
// },
// },
// ],
// paraswap: [
// {
// methods: ["float"],
// pairs: {
// 7: [149],
// 149: [7],
// },
// },
// ],
// },
// };
const mockGetStatus = async (statusList) => {
//Fake delay to show loading UI
await new Promise(r => setTimeout(r, 800));
return statusList.map(s => ({ ...s, status: "finished" }));
};
exports.mockGetStatus = mockGetStatus;
const mockPostSwapAccepted = async ({
/* eslint-disable @typescript-eslint/no-unused-vars */
provider, swapId, transactionId,
/* eslint-enable */
}) => {
//Fake delay to simulate network
await new Promise(r => setTimeout(r, 800));
return null;
};
exports.mockPostSwapAccepted = mockPostSwapAccepted;
const mockPostSwapCancelled = async ({
/* eslint-disable @typescript-eslint/no-unused-vars */
provider, swapId,
/* eslint-enable */
}) => {
//Fake delay to simulate network
await new Promise(r => setTimeout(r, 800));
return null;
};
exports.mockPostSwapCancelled = mockPostSwapCancelled;
//# sourceMappingURL=mock.js.map