@ledgerhq/live-common 21.5.1-btcjs.0 → 21.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/account/addAccounts.d.ts.map +1 -1
- package/lib/account/addAccounts.js +1 -2
- package/lib/account/addAccounts.js.map +1 -1
- package/lib/account/support.d.ts.map +1 -1
- package/lib/account/support.js +1 -24
- package/lib/account/support.js.map +1 -1
- package/lib/api/explorerConfig/index.js +15 -15
- package/lib/bot/index.d.ts +1 -2
- package/lib/bot/index.d.ts.map +1 -1
- package/lib/bot/index.js +15 -19
- package/lib/bot/index.js.map +1 -1
- package/lib/bot/test.bot.js +1 -4
- package/lib/bot/test.bot.js.map +1 -1
- package/lib/bridge/jsHelpers.d.ts +1 -6
- package/lib/bridge/jsHelpers.d.ts.map +1 -1
- package/lib/bridge/jsHelpers.js +3 -11
- package/lib/bridge/jsHelpers.js.map +1 -1
- package/lib/env.js +1 -1
- package/lib/env.js.map +1 -1
- package/lib/exchange/swap/getExchangeRates.d.ts.map +1 -1
- package/lib/exchange/swap/getExchangeRates.js +9 -8
- package/lib/exchange/swap/getExchangeRates.js.map +1 -1
- package/lib/exchange/swap/hooks/index.d.ts +11 -0
- package/lib/exchange/swap/hooks/index.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/index.js +23 -0
- package/lib/exchange/swap/hooks/index.js.map +1 -0
- package/lib/exchange/swap/hooks/useFromState.d.ts +15 -0
- package/lib/exchange/swap/hooks/useFromState.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useFromState.js +67 -0
- package/lib/exchange/swap/hooks/useFromState.js.map +1 -0
- package/lib/exchange/swap/hooks/useFromState.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useFromState.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useFromState.test.js +141 -0
- package/lib/exchange/swap/hooks/useFromState.test.js.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.d.ts +5 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.js +37 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.js.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.test.js +185 -0
- package/lib/exchange/swap/hooks/usePickDefaultAccount.test.js.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.d.ts +3 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.js +17 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.js.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.test.js +73 -0
- package/lib/exchange/swap/hooks/usePickDefaultCurrency.test.js.map +1 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.d.ts +12 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.js +105 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.js.map +1 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.test.js +78 -0
- package/lib/exchange/swap/hooks/usePollKYCStatus.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useProviderRates.d.ts +20 -0
- package/lib/exchange/swap/hooks/useProviderRates.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useProviderRates.js +174 -0
- package/lib/exchange/swap/hooks/useProviderRates.js.map +1 -0
- package/lib/exchange/swap/hooks/useProviderRates.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useProviderRates.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useProviderRates.test.js +261 -0
- package/lib/exchange/swap/hooks/useProviderRates.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.d.ts +15 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.js +35 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.js.map +1 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.test.js +79 -0
- package/lib/exchange/swap/hooks/useReverseAccounts.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.d.ts +5 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.js +43 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.js.map +1 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.test.js +32 -0
- package/lib/exchange/swap/hooks/useSelectableCurrencies.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useSwapProviders.d.ts +14 -0
- package/lib/exchange/swap/hooks/useSwapProviders.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSwapProviders.js +104 -0
- package/lib/exchange/swap/hooks/useSwapProviders.js.map +1 -0
- package/lib/exchange/swap/hooks/useSwapProviders.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useSwapProviders.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSwapProviders.test.js +174 -0
- package/lib/exchange/swap/hooks/useSwapProviders.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.d.ts +53 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.js +109 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.js.map +1 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.test.js +50 -0
- package/lib/exchange/swap/hooks/useSwapTransaction.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useToState.d.ts +12 -0
- package/lib/exchange/swap/hooks/useToState.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useToState.js +73 -0
- package/lib/exchange/swap/hooks/useToState.js.map +1 -0
- package/lib/exchange/swap/hooks/useToState.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useToState.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useToState.test.js +107 -0
- package/lib/exchange/swap/hooks/useToState.test.js.map +1 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.d.ts +15 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.d.ts.map +1 -0
- package/lib/{families/bitcoin/getAccountNetworkInfo.js → exchange/swap/hooks/useUpdateMaxAmount.js} +47 -65
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.js.map +1 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.test.d.ts +2 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.test.d.ts.map +1 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.test.js +217 -0
- package/lib/exchange/swap/hooks/useUpdateMaxAmount.test.js.map +1 -0
- package/lib/exchange/swap/mock.d.ts +6 -2
- package/lib/exchange/swap/mock.d.ts.map +1 -1
- package/lib/exchange/swap/mock.js +21 -6
- package/lib/exchange/swap/mock.js.map +1 -1
- package/lib/exchange/swap/types.d.ts +1 -1
- package/lib/exchange/swap/types.d.ts.map +1 -1
- package/lib/exchange/swap/utils/index.d.ts +16 -0
- package/lib/exchange/swap/utils/index.d.ts.map +1 -0
- package/lib/exchange/swap/utils/index.js +49 -0
- package/lib/exchange/swap/utils/index.js.map +1 -0
- package/lib/exchange/swap/utils/index.test.d.ts +2 -0
- package/lib/exchange/swap/utils/index.test.d.ts.map +1 -0
- package/lib/exchange/swap/utils/index.test.js +238 -0
- package/lib/exchange/swap/utils/index.test.js.map +1 -0
- package/lib/families/bitcoin/account.d.ts.map +1 -1
- package/lib/families/bitcoin/account.js +2 -3
- package/lib/families/bitcoin/account.js.map +1 -1
- package/lib/families/bitcoin/datasets/bitcoin.js +6 -6
- package/lib/families/bitcoin/datasets/bitcoin.js.map +1 -1
- package/lib/families/bitcoin/datasets/litecoin.js +3 -3
- package/lib/families/bitcoin/datasets/litecoin.js.map +1 -1
- package/lib/families/bitcoin/serialization.d.ts +2 -2
- package/lib/families/bitcoin/serialization.d.ts.map +1 -1
- package/lib/families/bitcoin/serialization.js +4 -8
- package/lib/families/bitcoin/serialization.js.map +1 -1
- package/lib/families/bitcoin/specs.d.ts +1 -0
- package/lib/families/bitcoin/specs.d.ts.map +1 -1
- package/lib/families/bitcoin/specs.js +17 -8
- package/lib/families/bitcoin/specs.js.map +1 -1
- package/lib/families/bitcoin/speculos-deviceActions.js +2 -2
- package/lib/families/bitcoin/speculos-deviceActions.js.map +1 -1
- package/lib/families/bitcoin/test-dataset.js +1 -1
- package/lib/families/bitcoin/test-dataset.js.map +1 -1
- package/lib/families/bitcoin/transaction.d.ts +3 -3
- package/lib/families/bitcoin/transaction.d.ts.map +1 -1
- package/lib/families/bitcoin/transaction.js +61 -64
- package/lib/families/bitcoin/transaction.js.map +1 -1
- package/lib/families/bitcoin/types.d.ts +36 -66
- package/lib/families/bitcoin/types.d.ts.map +1 -1
- package/lib/families/bitcoin/types.js +1 -16
- package/lib/families/bitcoin/types.js.map +1 -1
- package/lib/families/crypto_org/datasets/croeseid.d.ts.map +1 -1
- package/lib/families/crypto_org/datasets/croeseid.js +2 -6
- package/lib/families/crypto_org/datasets/croeseid.js.map +1 -1
- package/lib/families/elrond/api/apiCalls.d.ts +0 -4
- package/lib/families/elrond/api/apiCalls.d.ts.map +1 -1
- package/lib/families/elrond/api/apiCalls.js +3 -37
- package/lib/families/elrond/api/apiCalls.js.map +1 -1
- package/lib/families/elrond/api/sdk.js +2 -2
- package/lib/families/elrond/api/sdk.js.map +1 -1
- package/lib/families/elrond/js-transaction.d.ts +2 -0
- package/lib/families/elrond/js-transaction.d.ts.map +1 -1
- package/lib/families/elrond/types.d.ts +2 -0
- package/lib/families/elrond/types.d.ts.map +1 -1
- package/lib/families/elrond/types.js.map +1 -1
- package/lib/families/tezos/bridge/js.js.map +1 -1
- package/lib/generated/bridge/js.d.ts +0 -4
- package/lib/generated/bridge/js.d.ts.map +1 -1
- package/lib/generated/bridge/js.js +18 -20
- package/lib/generated/bridge/js.js.map +1 -1
- package/lib/generated/specs.d.ts +1 -0
- package/lib/generated/specs.d.ts.map +1 -1
- package/lib/migrations/accounts-dataset.d.ts.map +1 -1
- package/lib/migrations/accounts-dataset.js +2 -4
- package/lib/migrations/accounts-dataset.js.map +1 -1
- package/lib/migrations/accounts.d.ts.map +1 -1
- package/lib/migrations/accounts.js +0 -17
- package/lib/migrations/accounts.js.map +1 -1
- package/lib/mock/account.d.ts +2 -1
- package/lib/mock/account.d.ts.map +1 -1
- package/lib/mock/account.js +5 -4
- package/lib/mock/account.js.map +1 -1
- package/package.json +12 -13
- package/src/.DS_Store +0 -0
- package/src/__tests__/.DS_Store +0 -0
- package/src/__tests__/__snapshots__/.DS_Store +0 -0
- package/src/__tests__/__snapshots__/all.libcore.ts.snap +10 -10
- package/src/account/addAccounts.ts +1 -2
- package/src/account/support.ts +1 -24
- package/src/api/explorerConfig/index.ts +15 -15
- package/src/bot/index.ts +3 -8
- package/src/bot/test.bot.ts +1 -6
- package/src/bridge/jsHelpers.ts +2 -22
- package/src/env.ts +1 -1
- package/src/exchange/swap/getExchangeRates.ts +8 -5
- package/src/exchange/swap/hooks/index.ts +10 -0
- package/src/exchange/swap/hooks/useFromState.test.ts +134 -0
- package/src/exchange/swap/hooks/useFromState.ts +84 -0
- package/src/exchange/swap/hooks/usePickDefaultAccount.test.ts +167 -0
- package/src/exchange/swap/hooks/usePickDefaultAccount.ts +47 -0
- package/src/exchange/swap/hooks/usePickDefaultCurrency.test.ts +107 -0
- package/src/exchange/swap/hooks/usePickDefaultCurrency.ts +20 -0
- package/src/exchange/swap/hooks/usePollKYCStatus.test.ts +89 -0
- package/src/exchange/swap/hooks/usePollKYCStatus.ts +43 -0
- package/src/exchange/swap/hooks/useProviderRates.test.ts +200 -0
- package/src/exchange/swap/hooks/useProviderRates.ts +130 -0
- package/src/exchange/swap/hooks/useReverseAccounts.test.ts +83 -0
- package/src/exchange/swap/hooks/useReverseAccounts.ts +56 -0
- package/src/exchange/swap/hooks/useSelectableCurrencies.test.ts +39 -0
- package/src/exchange/swap/hooks/useSelectableCurrencies.ts +26 -0
- package/src/exchange/swap/hooks/useSwapProviders.test.ts +113 -0
- package/src/exchange/swap/hooks/useSwapProviders.ts +57 -0
- package/src/exchange/swap/hooks/useSwapTransaction.test.ts +54 -0
- package/src/exchange/swap/hooks/useSwapTransaction.ts +170 -0
- package/src/exchange/swap/hooks/useToState.test.ts +53 -0
- package/src/exchange/swap/hooks/useToState.ts +86 -0
- package/src/exchange/swap/hooks/useUpdateMaxAmount.test.ts +132 -0
- package/src/exchange/swap/hooks/useUpdateMaxAmount.ts +67 -0
- package/src/exchange/swap/mock.ts +22 -3
- package/src/exchange/swap/types.ts +2 -1
- package/src/exchange/swap/utils/index.test.ts +224 -0
- package/src/exchange/swap/utils/index.ts +67 -0
- package/src/families/bitcoin/account.ts +1 -2
- package/src/families/bitcoin/datasets/bitcoin.ts +6 -6
- package/src/families/bitcoin/datasets/litecoin.ts +6 -6
- package/src/families/bitcoin/serialization.ts +0 -6
- package/src/families/bitcoin/specs.ts +14 -5
- package/src/families/bitcoin/speculos-deviceActions.ts +1 -1
- package/src/families/bitcoin/test-dataset.ts +1 -1
- package/src/families/bitcoin/transaction.ts +93 -98
- package/src/families/bitcoin/types.ts +37 -95
- package/src/families/crypto_org/datasets/croeseid.ts +7 -9
- package/src/families/elrond/api/apiCalls.ts +3 -31
- package/src/families/elrond/api/sdk.ts +2 -2
- package/src/families/elrond/types.ts +2 -0
- package/src/families/tezos/bridge/js.ts +1 -1
- package/src/generated/bridge/js.ts +0 -3
- package/src/migrations/accounts-dataset.ts +0 -3
- package/src/migrations/accounts.ts +0 -18
- package/src/mock/account.ts +2 -4
- package/lib/__tests__/families/bitcoin/logic.d.ts +0 -2
- package/lib/__tests__/families/bitcoin/logic.d.ts.map +0 -1
- package/lib/__tests__/families/bitcoin/logic.js +0 -39
- package/lib/__tests__/families/bitcoin/logic.js.map +0 -1
- package/lib/families/bitcoin/bridge/js.d.ts +0 -8
- package/lib/families/bitcoin/bridge/js.d.ts.map +0 -1
- package/lib/families/bitcoin/bridge/js.js +0 -149
- package/lib/families/bitcoin/bridge/js.js.map +0 -1
- package/lib/families/bitcoin/cache.d.ts +0 -28
- package/lib/families/bitcoin/cache.d.ts.map +0 -1
- package/lib/families/bitcoin/cache.js +0 -109
- package/lib/families/bitcoin/cache.js.map +0 -1
- package/lib/families/bitcoin/getAccountNetworkInfo.d.ts +0 -6
- package/lib/families/bitcoin/getAccountNetworkInfo.d.ts.map +0 -1
- package/lib/families/bitcoin/getAccountNetworkInfo.js.map +0 -1
- package/lib/families/bitcoin/getAccountNetworkInfo.test.d.ts +0 -2
- package/lib/families/bitcoin/getAccountNetworkInfo.test.d.ts.map +0 -1
- package/lib/families/bitcoin/getAccountNetworkInfo.test.js +0 -71
- package/lib/families/bitcoin/getAccountNetworkInfo.test.js.map +0 -1
- package/lib/families/bitcoin/js-broadcast.d.ts +0 -11
- package/lib/families/bitcoin/js-broadcast.d.ts.map +0 -1
- package/lib/families/bitcoin/js-broadcast.js +0 -80
- package/lib/families/bitcoin/js-broadcast.js.map +0 -1
- package/lib/families/bitcoin/js-buildTransaction.d.ts +0 -5
- package/lib/families/bitcoin/js-buildTransaction.d.ts.map +0 -1
- package/lib/families/bitcoin/js-buildTransaction.js +0 -106
- package/lib/families/bitcoin/js-buildTransaction.js.map +0 -1
- package/lib/families/bitcoin/js-createTransaction.d.ts +0 -9
- package/lib/families/bitcoin/js-createTransaction.d.ts.map +0 -1
- package/lib/families/bitcoin/js-createTransaction.js +0 -27
- package/lib/families/bitcoin/js-createTransaction.js.map +0 -1
- package/lib/families/bitcoin/js-estimateMaxSpendable.d.ts +0 -15
- package/lib/families/bitcoin/js-estimateMaxSpendable.d.ts.map +0 -1
- package/lib/families/bitcoin/js-estimateMaxSpendable.js +0 -95
- package/lib/families/bitcoin/js-estimateMaxSpendable.js.map +0 -1
- package/lib/families/bitcoin/js-getFeesForTransaction.d.ts +0 -26
- package/lib/families/bitcoin/js-getFeesForTransaction.d.ts.map +0 -1
- package/lib/families/bitcoin/js-getFeesForTransaction.js +0 -100
- package/lib/families/bitcoin/js-getFeesForTransaction.js.map +0 -1
- package/lib/families/bitcoin/js-getTransactionStatus.d.ts +0 -5
- package/lib/families/bitcoin/js-getTransactionStatus.d.ts.map +0 -1
- package/lib/families/bitcoin/js-getTransactionStatus.js +0 -131
- package/lib/families/bitcoin/js-getTransactionStatus.js.map +0 -1
- package/lib/families/bitcoin/js-prepareTransaction.d.ts +0 -5
- package/lib/families/bitcoin/js-prepareTransaction.d.ts.map +0 -1
- package/lib/families/bitcoin/js-prepareTransaction.js +0 -80
- package/lib/families/bitcoin/js-prepareTransaction.js.map +0 -1
- package/lib/families/bitcoin/js-signOperation.d.ts +0 -10
- package/lib/families/bitcoin/js-signOperation.d.ts.map +0 -1
- package/lib/families/bitcoin/js-signOperation.js +0 -212
- package/lib/families/bitcoin/js-signOperation.js.map +0 -1
- package/lib/families/bitcoin/js-synchronisation.d.ts +0 -10
- package/lib/families/bitcoin/js-synchronisation.d.ts.map +0 -1
- package/lib/families/bitcoin/js-synchronisation.js +0 -411
- package/lib/families/bitcoin/js-synchronisation.js.map +0 -1
- package/lib/families/bitcoin/logic.d.ts +0 -34
- package/lib/families/bitcoin/logic.d.ts.map +0 -1
- package/lib/families/bitcoin/logic.js +0 -292
- package/lib/families/bitcoin/logic.js.map +0 -1
- package/lib/families/bitcoin/networks.d.ts +0 -3
- package/lib/families/bitcoin/networks.d.ts.map +0 -1
- package/lib/families/bitcoin/networks.js +0 -379
- package/lib/families/bitcoin/networks.js.map +0 -1
- package/lib/families/bitcoin/wallet.d.ts +0 -6
- package/lib/families/bitcoin/wallet.d.ts.map +0 -1
- package/lib/families/bitcoin/wallet.js +0 -66
- package/lib/families/bitcoin/wallet.js.map +0 -1
- package/src/__tests__/families/bitcoin/logic.ts +0 -40
- package/src/families/bitcoin/bridge/js.ts +0 -76
- package/src/families/bitcoin/cache.ts +0 -76
- package/src/families/bitcoin/getAccountNetworkInfo.test.ts +0 -27
- package/src/families/bitcoin/getAccountNetworkInfo.ts +0 -61
- package/src/families/bitcoin/js-broadcast.ts +0 -30
- package/src/families/bitcoin/js-buildTransaction.ts +0 -57
- package/src/families/bitcoin/js-createTransaction.ts +0 -27
- package/src/families/bitcoin/js-estimateMaxSpendable.ts +0 -36
- package/src/families/bitcoin/js-getFeesForTransaction.ts +0 -59
- package/src/families/bitcoin/js-getTransactionStatus.ts +0 -118
- package/src/families/bitcoin/js-prepareTransaction.ts +0 -35
- package/src/families/bitcoin/js-signOperation.ts +0 -164
- package/src/families/bitcoin/js-synchronisation.ts +0 -362
- package/src/families/bitcoin/logic.ts +0 -253
- package/src/families/bitcoin/networks.ts +0 -360
- package/src/families/bitcoin/wallet.ts +0 -21
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import { renderHook, act } from "@testing-library/react-hooks";
|
|
2
|
+
import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets";
|
|
3
|
+
import BigNumber from "bignumber.js";
|
|
4
|
+
import { mocked } from "ts-jest/utils";
|
|
5
|
+
import { useProviderRates } from ".";
|
|
6
|
+
import { getExchangeRates } from "..";
|
|
7
|
+
import { genAccount } from "../../../mock/account";
|
|
8
|
+
import { mockGetExchangeRates } from "../mock";
|
|
9
|
+
|
|
10
|
+
jest.mock("..");
|
|
11
|
+
|
|
12
|
+
const BTC = getCryptoCurrencyById("bitcoin");
|
|
13
|
+
const ETH = getCryptoCurrencyById("ethereum");
|
|
14
|
+
const fromAccount = genAccount("from-account", {
|
|
15
|
+
currency: ETH,
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const mockedGetExchangeRates = mocked(getExchangeRates, true);
|
|
19
|
+
const mockedRatesPromise = mockGetExchangeRates(
|
|
20
|
+
{ fromAccount } as any,
|
|
21
|
+
{ amount: new BigNumber(1) } as any,
|
|
22
|
+
BTC
|
|
23
|
+
);
|
|
24
|
+
|
|
25
|
+
describe("useProviderRates", () => {
|
|
26
|
+
let mockedRates;
|
|
27
|
+
const baseInitalProps = {
|
|
28
|
+
fromState: { account: fromAccount } as any,
|
|
29
|
+
toState: { currency: ETH } as any,
|
|
30
|
+
transaction: { amount: new BigNumber(1) } as any,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
beforeAll(async () => {
|
|
34
|
+
mockedRates = await mockedRatesPromise;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
beforeEach(() => {
|
|
38
|
+
mockedGetExchangeRates.mockReset();
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should fetch and store the rates", async () => {
|
|
42
|
+
mockedGetExchangeRates.mockResolvedValue(mockedRatesPromise);
|
|
43
|
+
const setExchangeRate = jest.fn();
|
|
44
|
+
|
|
45
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
46
|
+
initialProps: {
|
|
47
|
+
...baseInitalProps,
|
|
48
|
+
setExchangeRate,
|
|
49
|
+
},
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
expect(result.current.rates.status).toBe("loading");
|
|
53
|
+
|
|
54
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
55
|
+
|
|
56
|
+
// Check mock
|
|
57
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
58
|
+
// Check callback
|
|
59
|
+
expect(setExchangeRate).toHaveBeenCalledTimes(1);
|
|
60
|
+
expect(setExchangeRate.mock.calls[0][0]).toBe(mockedRates[0]);
|
|
61
|
+
// Check result
|
|
62
|
+
const { rates } = result.current;
|
|
63
|
+
expect(rates.error).toBeUndefined();
|
|
64
|
+
expect(rates.status).toBeNull();
|
|
65
|
+
expect(rates.value).toMatchObject(mockedRates);
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
it("should not fetch the rates if the conditions are not met", async () => {
|
|
69
|
+
const testProps = [
|
|
70
|
+
// No account
|
|
71
|
+
{
|
|
72
|
+
...baseInitalProps,
|
|
73
|
+
fromState: { account: null } as any,
|
|
74
|
+
},
|
|
75
|
+
// No currency
|
|
76
|
+
{
|
|
77
|
+
...baseInitalProps,
|
|
78
|
+
toState: { currency: null } as any,
|
|
79
|
+
},
|
|
80
|
+
// No transaction
|
|
81
|
+
{
|
|
82
|
+
...baseInitalProps,
|
|
83
|
+
transaction: null,
|
|
84
|
+
},
|
|
85
|
+
// Amount <= 0
|
|
86
|
+
{
|
|
87
|
+
...baseInitalProps,
|
|
88
|
+
transaction: { amount: new BigNumber(0) } as any,
|
|
89
|
+
},
|
|
90
|
+
// Nothing
|
|
91
|
+
{} as any,
|
|
92
|
+
];
|
|
93
|
+
|
|
94
|
+
testProps.forEach((initialProps) => {
|
|
95
|
+
renderHook(useProviderRates, {
|
|
96
|
+
initialProps,
|
|
97
|
+
});
|
|
98
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(0);
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
it("should refetch the rates if a dependency has been updated", async () => {
|
|
103
|
+
mockedGetExchangeRates.mockResolvedValue(mockedRatesPromise);
|
|
104
|
+
|
|
105
|
+
const { waitForNextUpdate, rerender } = renderHook(useProviderRates, {
|
|
106
|
+
initialProps: baseInitalProps,
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
110
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
111
|
+
mockedGetExchangeRates.mockClear();
|
|
112
|
+
|
|
113
|
+
rerender();
|
|
114
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(0);
|
|
115
|
+
|
|
116
|
+
rerender({
|
|
117
|
+
...baseInitalProps,
|
|
118
|
+
fromState: { account: { ...fromAccount } } as any,
|
|
119
|
+
});
|
|
120
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
121
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
122
|
+
mockedGetExchangeRates.mockClear();
|
|
123
|
+
|
|
124
|
+
rerender({ ...baseInitalProps, toState: { currency: { ...ETH } } as any });
|
|
125
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
126
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
127
|
+
mockedGetExchangeRates.mockClear();
|
|
128
|
+
|
|
129
|
+
rerender({
|
|
130
|
+
...baseInitalProps,
|
|
131
|
+
transaction: { amount: new BigNumber(2) } as any,
|
|
132
|
+
});
|
|
133
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
134
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it("should refetch the rates if refetchRates is called", async () => {
|
|
138
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
139
|
+
initialProps: baseInitalProps,
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
143
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
144
|
+
mockedGetExchangeRates.mockClear();
|
|
145
|
+
|
|
146
|
+
act(() => {
|
|
147
|
+
result.current.refetchRates();
|
|
148
|
+
});
|
|
149
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
150
|
+
expect(mockedGetExchangeRates).toHaveBeenCalledTimes(1);
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("should filter out errored rates", async () => {
|
|
154
|
+
const mockedRatesWithSingleError = mockedRates.map((rate, index) =>
|
|
155
|
+
index === 0
|
|
156
|
+
? {
|
|
157
|
+
...rate,
|
|
158
|
+
error: new Error("kaboom"),
|
|
159
|
+
}
|
|
160
|
+
: rate
|
|
161
|
+
);
|
|
162
|
+
mockedGetExchangeRates.mockResolvedValue(mockedRatesWithSingleError);
|
|
163
|
+
|
|
164
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
165
|
+
initialProps: baseInitalProps,
|
|
166
|
+
});
|
|
167
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
168
|
+
expect(result.current.rates.value).toMatchObject([mockedRates[1]]);
|
|
169
|
+
expect(result.current.rates.status).toBeNull();
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
it("should dispatch an error if all the rates contain an error", async () => {
|
|
173
|
+
const error = new Error("kaboom");
|
|
174
|
+
const mockedRatesWithError = mockedRates.map((rate) => ({
|
|
175
|
+
...rate,
|
|
176
|
+
error,
|
|
177
|
+
}));
|
|
178
|
+
mockedGetExchangeRates.mockResolvedValue(mockedRatesWithError);
|
|
179
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
180
|
+
initialProps: baseInitalProps,
|
|
181
|
+
});
|
|
182
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
183
|
+
expect(result.current.rates.status).toBe("error");
|
|
184
|
+
expect(result.current.rates.error).toBe(error);
|
|
185
|
+
expect(result.current.rates.value).toBeUndefined();
|
|
186
|
+
});
|
|
187
|
+
|
|
188
|
+
it("should call 'onNoRates' if there are no rates returned by the server", async () => {
|
|
189
|
+
const onNoRates = jest.fn(() => {});
|
|
190
|
+
mockedGetExchangeRates.mockResolvedValue([]);
|
|
191
|
+
|
|
192
|
+
const { result, waitForNextUpdate } = renderHook(useProviderRates, {
|
|
193
|
+
initialProps: { ...baseInitalProps, onNoRates },
|
|
194
|
+
});
|
|
195
|
+
await waitForNextUpdate({ timeout: 1000 });
|
|
196
|
+
expect(result.current.rates.value).toMatchObject([]);
|
|
197
|
+
expect(result.current.rates.status).toBeNull();
|
|
198
|
+
expect(onNoRates).toHaveBeenCalledTimes(1);
|
|
199
|
+
});
|
|
200
|
+
});
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
import { useCallback, useEffect, useReducer, useState } from "react";
|
|
2
|
+
import {
|
|
3
|
+
OnNoRatesCallback,
|
|
4
|
+
SetExchangeRateCallback,
|
|
5
|
+
SwapSelectorStateType,
|
|
6
|
+
} from "./useSwapTransaction";
|
|
7
|
+
import { getExchangeRates } from "..";
|
|
8
|
+
import { Exchange, ExchangeRate } from "../types";
|
|
9
|
+
import { pickExchangeRate } from "../utils";
|
|
10
|
+
import { Transaction } from "../../../generated/types";
|
|
11
|
+
|
|
12
|
+
export type RatesReducerState = {
|
|
13
|
+
status?: string | null;
|
|
14
|
+
value?: ExchangeRate[];
|
|
15
|
+
error?: Error;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
const ratesReducerInitialState: RatesReducerState = {};
|
|
19
|
+
const ratesReducer = (state: RatesReducerState, action): RatesReducerState => {
|
|
20
|
+
switch (action.type) {
|
|
21
|
+
case "set":
|
|
22
|
+
return { value: action.payload, status: null };
|
|
23
|
+
case "idle":
|
|
24
|
+
return { ...state, status: null };
|
|
25
|
+
case "loading":
|
|
26
|
+
return { ...state, status: "loading" };
|
|
27
|
+
case "error":
|
|
28
|
+
return { status: "error", error: action.payload };
|
|
29
|
+
}
|
|
30
|
+
return state;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/* Fetch and update provider rates. */
|
|
34
|
+
export const useProviderRates = ({
|
|
35
|
+
fromState,
|
|
36
|
+
toState,
|
|
37
|
+
exchangeRate,
|
|
38
|
+
transaction,
|
|
39
|
+
onNoRates,
|
|
40
|
+
setExchangeRate,
|
|
41
|
+
}: {
|
|
42
|
+
fromState: SwapSelectorStateType;
|
|
43
|
+
toState: SwapSelectorStateType;
|
|
44
|
+
exchangeRate?: ExchangeRate | null | undefined;
|
|
45
|
+
transaction?: Transaction | null | undefined;
|
|
46
|
+
onNoRates?: OnNoRatesCallback | null | undefined;
|
|
47
|
+
setExchangeRate?: SetExchangeRateCallback | null | undefined;
|
|
48
|
+
}): {
|
|
49
|
+
rates: RatesReducerState;
|
|
50
|
+
refetchRates: () => void;
|
|
51
|
+
} => {
|
|
52
|
+
const { account: fromAccount } = fromState;
|
|
53
|
+
const { currency: toCurrency } = toState;
|
|
54
|
+
const [rates, dispatchRates] = useReducer(
|
|
55
|
+
ratesReducer,
|
|
56
|
+
ratesReducerInitialState
|
|
57
|
+
);
|
|
58
|
+
const [getRatesDependency, setGetRatesDependency] = useState<unknown | null>(
|
|
59
|
+
null
|
|
60
|
+
);
|
|
61
|
+
const refetchRates = useCallback(() => setGetRatesDependency({}), []);
|
|
62
|
+
|
|
63
|
+
useEffect(
|
|
64
|
+
() => {
|
|
65
|
+
let abort = false;
|
|
66
|
+
async function getRates() {
|
|
67
|
+
if (
|
|
68
|
+
!transaction ||
|
|
69
|
+
!transaction?.amount ||
|
|
70
|
+
!transaction?.amount.gt(0) ||
|
|
71
|
+
!toCurrency ||
|
|
72
|
+
!fromAccount
|
|
73
|
+
) {
|
|
74
|
+
setExchangeRate && setExchangeRate(null);
|
|
75
|
+
return dispatchRates({ type: "set", payload: [] });
|
|
76
|
+
}
|
|
77
|
+
dispatchRates({ type: "loading" });
|
|
78
|
+
try {
|
|
79
|
+
let rates: ExchangeRate[] = await getExchangeRates(
|
|
80
|
+
{ fromAccount } as Exchange,
|
|
81
|
+
transaction,
|
|
82
|
+
undefined,
|
|
83
|
+
toCurrency
|
|
84
|
+
);
|
|
85
|
+
if (abort) return;
|
|
86
|
+
if (rates.length === 0) {
|
|
87
|
+
onNoRates && onNoRates({ fromState, toState });
|
|
88
|
+
}
|
|
89
|
+
// Discard bad provider rates
|
|
90
|
+
let rateError: Error | null | undefined = null;
|
|
91
|
+
rates = rates.reduce<ExchangeRate[]>((acc, rate) => {
|
|
92
|
+
rateError = rateError ?? rate.error;
|
|
93
|
+
return rate.error ? acc : [...acc, rate];
|
|
94
|
+
}, []);
|
|
95
|
+
if (rates.length === 0 && rateError) {
|
|
96
|
+
// If all the rates are in error
|
|
97
|
+
dispatchRates({ type: "error", payload: rateError });
|
|
98
|
+
} else {
|
|
99
|
+
dispatchRates({ type: "set", payload: rates });
|
|
100
|
+
setExchangeRate &&
|
|
101
|
+
pickExchangeRate(rates, exchangeRate, setExchangeRate);
|
|
102
|
+
}
|
|
103
|
+
} catch (error) {
|
|
104
|
+
!abort && dispatchRates({ type: "error", payload: error });
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
getRates();
|
|
109
|
+
|
|
110
|
+
return () => {
|
|
111
|
+
abort = true;
|
|
112
|
+
dispatchRates({ type: "idle" });
|
|
113
|
+
};
|
|
114
|
+
},
|
|
115
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
116
|
+
[
|
|
117
|
+
fromAccount,
|
|
118
|
+
toCurrency,
|
|
119
|
+
transaction,
|
|
120
|
+
getRatesDependency,
|
|
121
|
+
onNoRates,
|
|
122
|
+
setExchangeRate,
|
|
123
|
+
]
|
|
124
|
+
);
|
|
125
|
+
|
|
126
|
+
return {
|
|
127
|
+
rates,
|
|
128
|
+
refetchRates,
|
|
129
|
+
};
|
|
130
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import { getCryptoCurrencyById, getTokenById } from "@ledgerhq/cryptoassets";
|
|
2
|
+
import { renderHook, act } from "@testing-library/react-hooks";
|
|
3
|
+
import { genAccount, genTokenAccount } from "../../../mock/account";
|
|
4
|
+
import { Account } from "../../../types";
|
|
5
|
+
import { useReverseAccounts } from "./useReverseAccounts";
|
|
6
|
+
|
|
7
|
+
const BTC = getCryptoCurrencyById("bitcoin");
|
|
8
|
+
const ETH = getCryptoCurrencyById("ethereum");
|
|
9
|
+
const USDT = getTokenById("ethereum/erc20/usd_tether__erc20_");
|
|
10
|
+
|
|
11
|
+
const fromParentAccount = genAccount("mocked-account-2", {
|
|
12
|
+
currency: ETH,
|
|
13
|
+
});
|
|
14
|
+
const fromAccount = genTokenAccount(1, fromParentAccount, USDT);
|
|
15
|
+
const toAccount = genAccount("mocked-account-1", { currency: BTC });
|
|
16
|
+
const allAccounts = [fromAccount, fromParentAccount, toAccount] as Account[];
|
|
17
|
+
|
|
18
|
+
describe("useReverseAccounts", () => {
|
|
19
|
+
const setFromAccount = jest.fn();
|
|
20
|
+
const setToAccount = jest.fn();
|
|
21
|
+
|
|
22
|
+
const defaultProps: Parameters<typeof useReverseAccounts>[0] = {
|
|
23
|
+
accounts: allAccounts,
|
|
24
|
+
fromAccount,
|
|
25
|
+
fromParentAccount,
|
|
26
|
+
toAccount,
|
|
27
|
+
fromCurrency: USDT,
|
|
28
|
+
setFromAccount,
|
|
29
|
+
setToAccount,
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
it("should tell if the accounts are swappable", () => {
|
|
33
|
+
const { result, rerender } = renderHook(
|
|
34
|
+
(props) => useReverseAccounts(props),
|
|
35
|
+
{
|
|
36
|
+
initialProps: defaultProps,
|
|
37
|
+
}
|
|
38
|
+
);
|
|
39
|
+
|
|
40
|
+
expect(result.current.isSwapReversable).toBe(true);
|
|
41
|
+
|
|
42
|
+
rerender({ ...defaultProps, toAccount: null });
|
|
43
|
+
expect(result.current.isSwapReversable).toBe(false);
|
|
44
|
+
|
|
45
|
+
rerender({ ...defaultProps, fromCurrency: null });
|
|
46
|
+
expect(result.current.isSwapReversable).toBe(false);
|
|
47
|
+
|
|
48
|
+
rerender({
|
|
49
|
+
...defaultProps,
|
|
50
|
+
// detached account - not in the main accounts list
|
|
51
|
+
toAccount: genAccount("mocked-account-3", { currency: BTC }),
|
|
52
|
+
});
|
|
53
|
+
expect(result.current.isSwapReversable).toBe(false);
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("should reverse accounts when 'reverseSwap' is called", () => {
|
|
57
|
+
const { result, rerender } = renderHook(useReverseAccounts, {
|
|
58
|
+
initialProps: {
|
|
59
|
+
setToAccount,
|
|
60
|
+
setFromAccount,
|
|
61
|
+
} as any,
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
// if isSwapReversable is false, then reverseSwap should be a no-op
|
|
65
|
+
expect(result.current.isSwapReversable).toBe(false);
|
|
66
|
+
act(() => result.current.reverseSwap());
|
|
67
|
+
expect(setToAccount).toHaveBeenCalledTimes(0);
|
|
68
|
+
expect(setFromAccount).toHaveBeenCalledTimes(0);
|
|
69
|
+
|
|
70
|
+
// Reverse from/to accounts
|
|
71
|
+
rerender(defaultProps);
|
|
72
|
+
expect(result.current.isSwapReversable).toBe(true);
|
|
73
|
+
act(() => result.current.reverseSwap());
|
|
74
|
+
expect(setToAccount).toHaveBeenCalledTimes(1);
|
|
75
|
+
expect(setToAccount.mock.calls[0]).toMatchObject([
|
|
76
|
+
USDT,
|
|
77
|
+
fromAccount,
|
|
78
|
+
fromParentAccount,
|
|
79
|
+
]);
|
|
80
|
+
expect(setFromAccount).toHaveBeenCalledTimes(1);
|
|
81
|
+
expect(setFromAccount.mock.calls[0]).toMatchObject([toAccount]);
|
|
82
|
+
});
|
|
83
|
+
});
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { useCallback, useMemo } from "react";
|
|
2
|
+
import { SwapDataType, SwapSelectorStateType, SwapTransactionType } from ".";
|
|
3
|
+
import { flattenAccounts } from "../../../account";
|
|
4
|
+
import { Account } from "../../../types";
|
|
5
|
+
|
|
6
|
+
export const useReverseAccounts = ({
|
|
7
|
+
accounts,
|
|
8
|
+
toAccount,
|
|
9
|
+
fromAccount,
|
|
10
|
+
fromParentAccount,
|
|
11
|
+
fromCurrency,
|
|
12
|
+
setFromAccount,
|
|
13
|
+
setToAccount,
|
|
14
|
+
}: {
|
|
15
|
+
accounts: Account[] | undefined;
|
|
16
|
+
toAccount: SwapSelectorStateType["account"];
|
|
17
|
+
fromAccount: SwapSelectorStateType["account"];
|
|
18
|
+
fromParentAccount: SwapSelectorStateType["parentAccount"];
|
|
19
|
+
fromCurrency: SwapSelectorStateType["currency"];
|
|
20
|
+
setFromAccount: SwapTransactionType["setFromAccount"];
|
|
21
|
+
setToAccount: SwapTransactionType["setToAccount"];
|
|
22
|
+
}): {
|
|
23
|
+
isSwapReversable: SwapDataType["isSwapReversable"];
|
|
24
|
+
reverseSwap: SwapTransactionType["reverseSwap"];
|
|
25
|
+
} => {
|
|
26
|
+
const isSwapReversable = useMemo(() => {
|
|
27
|
+
if (!toAccount || !fromCurrency) return false;
|
|
28
|
+
|
|
29
|
+
const allAccounstWithSub = accounts ? flattenAccounts(accounts) : [];
|
|
30
|
+
const isToSwappable = !!allAccounstWithSub.find(
|
|
31
|
+
(account) => account.id === toAccount?.id
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
return isToSwappable;
|
|
35
|
+
}, [toAccount, fromCurrency, accounts]);
|
|
36
|
+
|
|
37
|
+
const reverseSwap: SwapTransactionType["reverseSwap"] = useCallback(() => {
|
|
38
|
+
if (isSwapReversable === false) return;
|
|
39
|
+
|
|
40
|
+
setFromAccount(toAccount);
|
|
41
|
+
setToAccount(fromCurrency, fromAccount, fromParentAccount);
|
|
42
|
+
}, [
|
|
43
|
+
toAccount,
|
|
44
|
+
fromCurrency,
|
|
45
|
+
fromAccount,
|
|
46
|
+
fromParentAccount,
|
|
47
|
+
setFromAccount,
|
|
48
|
+
setToAccount,
|
|
49
|
+
isSwapReversable,
|
|
50
|
+
]);
|
|
51
|
+
|
|
52
|
+
return {
|
|
53
|
+
isSwapReversable,
|
|
54
|
+
reverseSwap,
|
|
55
|
+
};
|
|
56
|
+
};
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react-hooks";
|
|
2
|
+
|
|
3
|
+
import { useSelectableCurrencies } from "./useSelectableCurrencies";
|
|
4
|
+
import { getCryptoCurrencyById } from "../../../currencies";
|
|
5
|
+
|
|
6
|
+
describe("useSelectableCurrencies", () => {
|
|
7
|
+
test("returns an empty array when empty list are passed", () => {
|
|
8
|
+
const allCurrencies = [];
|
|
9
|
+
|
|
10
|
+
const { result } = renderHook(() =>
|
|
11
|
+
useSelectableCurrencies({ allCurrencies })
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
expect(result.current).toEqual([]);
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
test("returns an empty array when incorrect ids are passed", () => {
|
|
18
|
+
const allCurrencies = ["ethercoin", "bitether", "polkamos"];
|
|
19
|
+
|
|
20
|
+
const { result } = renderHook(() =>
|
|
21
|
+
useSelectableCurrencies({ allCurrencies })
|
|
22
|
+
);
|
|
23
|
+
|
|
24
|
+
expect(result.current).toEqual([]);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
test("returns correct currencies for all correct ids, in the same order", () => {
|
|
28
|
+
const allCurrencies = ["ethereum", "bitcoin", "polkamos"];
|
|
29
|
+
const ethereumCurrency = getCryptoCurrencyById("ethereum");
|
|
30
|
+
const bitcoinCurrency = getCryptoCurrencyById("bitcoin");
|
|
31
|
+
|
|
32
|
+
const { result } = renderHook(() =>
|
|
33
|
+
useSelectableCurrencies({ allCurrencies })
|
|
34
|
+
);
|
|
35
|
+
|
|
36
|
+
expect(result.current).toHaveLength(2);
|
|
37
|
+
expect(result.current).toEqual([ethereumCurrency, bitcoinCurrency]);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useMemo } from "react";
|
|
2
|
+
import {
|
|
3
|
+
CryptoCurrency,
|
|
4
|
+
findCryptoCurrencyById,
|
|
5
|
+
findTokenById,
|
|
6
|
+
TokenCurrency,
|
|
7
|
+
} from "@ledgerhq/cryptoassets";
|
|
8
|
+
|
|
9
|
+
export const useSelectableCurrencies = ({
|
|
10
|
+
allCurrencies,
|
|
11
|
+
}: {
|
|
12
|
+
allCurrencies: string[];
|
|
13
|
+
}): (TokenCurrency | CryptoCurrency)[] => {
|
|
14
|
+
const currencies = useMemo(() => {
|
|
15
|
+
const tokens = allCurrencies.map(findTokenById).filter(Boolean);
|
|
16
|
+
const cryptoCurrencies = allCurrencies
|
|
17
|
+
.map(findCryptoCurrencyById)
|
|
18
|
+
.filter(Boolean);
|
|
19
|
+
return [...tokens, ...cryptoCurrencies] as (
|
|
20
|
+
| TokenCurrency
|
|
21
|
+
| CryptoCurrency
|
|
22
|
+
)[];
|
|
23
|
+
}, [allCurrencies]);
|
|
24
|
+
|
|
25
|
+
return currencies;
|
|
26
|
+
};
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import { renderHook } from "@testing-library/react-hooks";
|
|
2
|
+
import { mocked } from "ts-jest/utils";
|
|
3
|
+
|
|
4
|
+
import { useSwapProviders } from "./useSwapProviders";
|
|
5
|
+
import { getProviders } from "..";
|
|
6
|
+
|
|
7
|
+
jest.mock("..");
|
|
8
|
+
|
|
9
|
+
const mockedGetProviders = mocked(getProviders);
|
|
10
|
+
|
|
11
|
+
describe("useSwapProviders", () => {
|
|
12
|
+
const OLD_ENV = process.env;
|
|
13
|
+
const mockedProviders = [
|
|
14
|
+
{
|
|
15
|
+
provider: "changelly",
|
|
16
|
+
pairs: [
|
|
17
|
+
{ from: "ETH", to: "BTC", tradeMethod: "fixed" },
|
|
18
|
+
{ from: "DOT", to: "BTC", tradeMethod: "fixed" },
|
|
19
|
+
{ from: "EGLD", to: "BTC", tradeMethod: "float" },
|
|
20
|
+
{ from: "LINK", to: "ETH", tradeMethod: "fixed" },
|
|
21
|
+
{ from: "LINK", to: "GRT", tradeMethod: "float" },
|
|
22
|
+
],
|
|
23
|
+
},
|
|
24
|
+
{
|
|
25
|
+
provider: "wyre",
|
|
26
|
+
pairs: [
|
|
27
|
+
{ from: "ETH", to: "BTC", tradeMethod: "fixed" },
|
|
28
|
+
{ from: "BTC", to: "ETH", tradeMethod: "float" },
|
|
29
|
+
],
|
|
30
|
+
},
|
|
31
|
+
];
|
|
32
|
+
|
|
33
|
+
beforeEach(() => {
|
|
34
|
+
mockedGetProviders.mockResolvedValue(mockedProviders);
|
|
35
|
+
// Clear the cache
|
|
36
|
+
jest.resetModules();
|
|
37
|
+
// Copy envs already present before running this test to process.env
|
|
38
|
+
process.env = { ...OLD_ENV };
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
afterEach(() => {
|
|
42
|
+
mockedGetProviders.mockClear();
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
afterAll(() => {
|
|
46
|
+
// Restore the old envs
|
|
47
|
+
process.env = OLD_ENV;
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
test("returns the initial state on mount", () => {
|
|
51
|
+
const { result, unmount } = renderHook(() => useSwapProviders());
|
|
52
|
+
|
|
53
|
+
// interrupt the useEffect function by unmounting the component
|
|
54
|
+
unmount();
|
|
55
|
+
|
|
56
|
+
expect(result.current.isLoading).toBeTruthy();
|
|
57
|
+
expect(result.current.error).toBeNull();
|
|
58
|
+
expect(result.current.providers).toBeNull();
|
|
59
|
+
// automatically fired on mount
|
|
60
|
+
expect(mockedGetProviders).toHaveBeenCalledTimes(1);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
test("returns the fetched providers", async () => {
|
|
64
|
+
const { result, waitForNextUpdate } = renderHook(() => useSwapProviders());
|
|
65
|
+
|
|
66
|
+
await waitForNextUpdate();
|
|
67
|
+
|
|
68
|
+
expect(mockedGetProviders).toHaveBeenCalledTimes(1);
|
|
69
|
+
expect(result.current.isLoading).toBeFalsy();
|
|
70
|
+
expect(result.current.error).toBeNull();
|
|
71
|
+
expect(result.current.providers).toEqual(mockedProviders);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
test("only returns no-filtered fetched providers", async () => {
|
|
75
|
+
// set the SWAP_DISABLED_PROVIDERS env variable for this test
|
|
76
|
+
process.env.SWAP_DISABLED_PROVIDERS = "wyre";
|
|
77
|
+
const { result, waitForNextUpdate } = renderHook(() => useSwapProviders());
|
|
78
|
+
|
|
79
|
+
await waitForNextUpdate();
|
|
80
|
+
|
|
81
|
+
expect(mockedGetProviders).toHaveBeenCalledTimes(1);
|
|
82
|
+
expect(result.current.isLoading).toBeFalsy();
|
|
83
|
+
expect(result.current.error).toBeNull();
|
|
84
|
+
expect(result.current.providers).toEqual(
|
|
85
|
+
mockedProviders.filter((provider) => provider.provider !== "wyre")
|
|
86
|
+
);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
test("returns the fetched error", async () => {
|
|
90
|
+
const error = new Error("test error");
|
|
91
|
+
mockedGetProviders.mockRejectedValueOnce(error);
|
|
92
|
+
|
|
93
|
+
const { result, waitForNextUpdate } = renderHook(() => useSwapProviders());
|
|
94
|
+
|
|
95
|
+
await waitForNextUpdate();
|
|
96
|
+
|
|
97
|
+
expect(mockedGetProviders).toHaveBeenCalledTimes(1);
|
|
98
|
+
expect(result.current.isLoading).toBeFalsy();
|
|
99
|
+
expect(result.current.error).toEqual(error);
|
|
100
|
+
expect(result.current.providers).toBeNull();
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
test("only fetch providers on mount", async () => {
|
|
104
|
+
const { rerender, waitForNextUpdate } = renderHook(() =>
|
|
105
|
+
useSwapProviders()
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
await waitForNextUpdate();
|
|
109
|
+
rerender();
|
|
110
|
+
|
|
111
|
+
expect(mockedGetProviders).toHaveBeenCalledTimes(1);
|
|
112
|
+
});
|
|
113
|
+
});
|