@ledgerhq/live-common 34.53.0-nightly.20251118023800 → 34.53.0-nightly.20251119110540
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/__tests__/test-helpers/bridge.d.ts.map +1 -1
- package/lib/__tests__/test-helpers/bridge.js +0 -4
- package/lib/__tests__/test-helpers/bridge.js.map +1 -1
- package/lib/bridge/generic-alpaca/getAccountShape.d.ts.map +1 -1
- package/lib/bridge/generic-alpaca/getAccountShape.js +12 -10
- package/lib/bridge/generic-alpaca/getAccountShape.js.map +1 -1
- package/lib/deposit/type.d.ts +0 -17
- package/lib/deposit/type.d.ts.map +1 -1
- package/lib/deposit/type.js.map +1 -1
- package/lib/deviceSDK/tasks/core.d.ts +1 -1
- package/lib/deviceSDK/tasks/core.d.ts.map +1 -1
- package/lib/deviceSDK/tasks/core.js +1 -1
- package/lib/deviceSDK/tasks/core.js.map +1 -1
- package/lib/e2e/enum/Account.d.ts +0 -1
- package/lib/e2e/enum/Account.d.ts.map +1 -1
- package/lib/e2e/enum/Account.js +0 -1
- package/lib/e2e/enum/Account.js.map +1 -1
- package/lib/e2e/enum/Currency.d.ts +0 -1
- package/lib/e2e/enum/Currency.d.ts.map +1 -1
- package/lib/e2e/enum/Currency.js +1 -5
- package/lib/e2e/enum/Currency.js.map +1 -1
- package/lib/e2e/enum/ReceiveFundsOptions.d.ts +6 -0
- package/lib/e2e/enum/ReceiveFundsOptions.d.ts.map +1 -0
- package/lib/e2e/enum/ReceiveFundsOptions.js +8 -0
- package/lib/e2e/enum/ReceiveFundsOptions.js.map +1 -0
- package/lib/exchange/providers/swap.d.ts +1 -0
- package/lib/exchange/providers/swap.d.ts.map +1 -1
- package/lib/exchange/providers/swap.js +5 -1
- package/lib/exchange/providers/swap.js.map +1 -1
- package/lib/exchange/swap/hooks/v5/useFetchCurrencyAll.d.ts.map +1 -1
- package/lib/exchange/swap/hooks/v5/useFetchCurrencyAll.js +2 -3
- package/lib/exchange/swap/hooks/v5/useFetchCurrencyAll.js.map +1 -1
- package/lib/exchange/swap/hooks/v5/useFilteredProviders.d.ts.map +1 -1
- package/lib/exchange/swap/hooks/v5/useFilteredProviders.js +15 -10
- package/lib/exchange/swap/hooks/v5/useFilteredProviders.js.map +1 -1
- package/lib/market/hooks/useMarketDataProvider.js +1 -0
- package/lib/market/hooks/useMarketDataProvider.js.map +1 -1
- package/lib/market/utils/types.d.ts +1 -0
- package/lib/market/utils/types.d.ts.map +1 -1
- package/lib/market/utils/types.js.map +1 -1
- package/lib/modularDrawer/utils/{groupCurrenciesByProvider.d.ts → groupCurrenciesByAsset.d.ts} +2 -2
- package/lib/modularDrawer/utils/groupCurrenciesByAsset.d.ts.map +1 -0
- package/lib/modularDrawer/utils/{groupCurrenciesByProvider.js → groupCurrenciesByAsset.js} +7 -7
- package/lib/modularDrawer/utils/groupCurrenciesByAsset.js.map +1 -0
- package/lib/modularDrawer/utils/index.d.ts +1 -1
- package/lib/modularDrawer/utils/index.d.ts.map +1 -1
- package/lib/modularDrawer/utils/index.js +3 -3
- package/lib/modularDrawer/utils/index.js.map +1 -1
- package/lib/wallet-api/types.d.ts +3 -0
- package/lib/wallet-api/types.d.ts.map +1 -1
- package/lib/wallet-api/utils/deriveAccountIdForManifest.d.ts +6 -0
- package/lib/wallet-api/utils/deriveAccountIdForManifest.d.ts.map +1 -1
- package/lib/wallet-api/utils/deriveAccountIdForManifest.js +13 -3
- package/lib/wallet-api/utils/deriveAccountIdForManifest.js.map +1 -1
- package/lib-es/__tests__/test-helpers/bridge.d.ts.map +1 -1
- package/lib-es/__tests__/test-helpers/bridge.js +0 -4
- package/lib-es/__tests__/test-helpers/bridge.js.map +1 -1
- package/lib-es/bridge/generic-alpaca/getAccountShape.d.ts.map +1 -1
- package/lib-es/bridge/generic-alpaca/getAccountShape.js +13 -11
- package/lib-es/bridge/generic-alpaca/getAccountShape.js.map +1 -1
- package/lib-es/deposit/type.d.ts +0 -17
- package/lib-es/deposit/type.d.ts.map +1 -1
- package/lib-es/deposit/type.js.map +1 -1
- package/lib-es/deviceSDK/tasks/core.d.ts +1 -1
- package/lib-es/deviceSDK/tasks/core.d.ts.map +1 -1
- package/lib-es/deviceSDK/tasks/core.js +1 -1
- package/lib-es/deviceSDK/tasks/core.js.map +1 -1
- package/lib-es/e2e/enum/Account.d.ts +0 -1
- package/lib-es/e2e/enum/Account.d.ts.map +1 -1
- package/lib-es/e2e/enum/Account.js +0 -1
- package/lib-es/e2e/enum/Account.js.map +1 -1
- package/lib-es/e2e/enum/Currency.d.ts +0 -1
- package/lib-es/e2e/enum/Currency.d.ts.map +1 -1
- package/lib-es/e2e/enum/Currency.js +1 -5
- package/lib-es/e2e/enum/Currency.js.map +1 -1
- package/lib-es/e2e/enum/ReceiveFundsOptions.d.ts +6 -0
- package/lib-es/e2e/enum/ReceiveFundsOptions.d.ts.map +1 -0
- package/lib-es/e2e/enum/ReceiveFundsOptions.js +5 -0
- package/lib-es/e2e/enum/ReceiveFundsOptions.js.map +1 -0
- package/lib-es/exchange/providers/swap.d.ts +1 -0
- package/lib-es/exchange/providers/swap.d.ts.map +1 -1
- package/lib-es/exchange/providers/swap.js +3 -0
- package/lib-es/exchange/providers/swap.js.map +1 -1
- package/lib-es/exchange/swap/hooks/v5/useFetchCurrencyAll.d.ts.map +1 -1
- package/lib-es/exchange/swap/hooks/v5/useFetchCurrencyAll.js +2 -3
- package/lib-es/exchange/swap/hooks/v5/useFetchCurrencyAll.js.map +1 -1
- package/lib-es/exchange/swap/hooks/v5/useFilteredProviders.d.ts.map +1 -1
- package/lib-es/exchange/swap/hooks/v5/useFilteredProviders.js +17 -12
- package/lib-es/exchange/swap/hooks/v5/useFilteredProviders.js.map +1 -1
- package/lib-es/market/hooks/useMarketDataProvider.js +1 -0
- package/lib-es/market/hooks/useMarketDataProvider.js.map +1 -1
- package/lib-es/market/utils/types.d.ts +1 -0
- package/lib-es/market/utils/types.d.ts.map +1 -1
- package/lib-es/market/utils/types.js.map +1 -1
- package/lib-es/modularDrawer/utils/{groupCurrenciesByProvider.d.ts → groupCurrenciesByAsset.d.ts} +2 -2
- package/lib-es/modularDrawer/utils/groupCurrenciesByAsset.d.ts.map +1 -0
- package/lib-es/modularDrawer/utils/{groupCurrenciesByProvider.js → groupCurrenciesByAsset.js} +5 -5
- package/lib-es/modularDrawer/utils/groupCurrenciesByAsset.js.map +1 -0
- package/lib-es/modularDrawer/utils/index.d.ts +1 -1
- package/lib-es/modularDrawer/utils/index.d.ts.map +1 -1
- package/lib-es/modularDrawer/utils/index.js +1 -1
- package/lib-es/modularDrawer/utils/index.js.map +1 -1
- package/lib-es/wallet-api/types.d.ts +3 -0
- package/lib-es/wallet-api/types.d.ts.map +1 -1
- package/lib-es/wallet-api/utils/deriveAccountIdForManifest.d.ts +6 -0
- package/lib-es/wallet-api/utils/deriveAccountIdForManifest.d.ts.map +1 -1
- package/lib-es/wallet-api/utils/deriveAccountIdForManifest.js +11 -2
- package/lib-es/wallet-api/utils/deriveAccountIdForManifest.js.map +1 -1
- package/package.json +54 -54
- package/src/__tests__/test-helpers/bridge.ts +0 -2
- package/src/bridge/generic-alpaca/getAccountShape.ts +15 -12
- package/src/bridge/generic-alpaca/tests/getAccountShape.test.ts +142 -101
- package/src/deposit/type.ts +0 -21
- package/src/deviceSDK/tasks/core.test.ts +20 -0
- package/src/deviceSDK/tasks/core.ts +2 -1
- package/src/e2e/enum/Account.ts +0 -6
- package/src/e2e/enum/Currency.ts +1 -5
- package/src/e2e/enum/ReceiveFundsOptions.ts +7 -0
- package/src/exchange/providers/swap.ts +4 -0
- package/src/exchange/swap/hooks/v5/useFetchCurrencyAll.ts +2 -3
- package/src/exchange/swap/hooks/v5/useFilteredProviders.ts +37 -12
- package/src/families/stellar/__snapshots__/bridge.integration.test.ts.snap +84 -12
- package/src/market/hooks/useMarketDataProvider.ts +1 -0
- package/src/market/utils/types.ts +1 -0
- package/src/modularDrawer/modules/__test__/createAssetConfiguration.test.tsx +2 -2
- package/src/modularDrawer/utils/__tests__/{groupCurrenciesByProvider.test.ts → groupCurrenciesByAsset.test.ts} +6 -6
- package/src/modularDrawer/utils/{groupCurrenciesByProvider.ts → groupCurrenciesByAsset.ts} +4 -4
- package/src/modularDrawer/utils/index.ts +1 -1
- package/src/wallet-api/types.ts +5 -0
- package/src/wallet-api/utils/deriveAccountIdForManifest.ts +14 -2
- package/lib/deposit/helper.d.ts +0 -8
- package/lib/deposit/helper.d.ts.map +0 -1
- package/lib/deposit/helper.js +0 -125
- package/lib/deposit/helper.js.map +0 -1
- package/lib/deposit/index.d.ts +0 -3
- package/lib/deposit/index.d.ts.map +0 -1
- package/lib/deposit/index.js +0 -6
- package/lib/deposit/index.js.map +0 -1
- package/lib/deposit/mock.d.ts +0 -75
- package/lib/deposit/mock.d.ts.map +0 -1
- package/lib/deposit/mock.js +0 -15111
- package/lib/deposit/mock.js.map +0 -1
- package/lib/deposit/useGroupedCurrenciesByProvider.hook.d.ts +0 -3
- package/lib/deposit/useGroupedCurrenciesByProvider.hook.d.ts.map +0 -1
- package/lib/deposit/useGroupedCurrenciesByProvider.hook.js +0 -40
- package/lib/deposit/useGroupedCurrenciesByProvider.hook.js.map +0 -1
- package/lib/modularDrawer/__mocks__/useGroupedCurrenciesByProvider.mock.d.ts +0 -22
- package/lib/modularDrawer/__mocks__/useGroupedCurrenciesByProvider.mock.d.ts.map +0 -1
- package/lib/modularDrawer/__mocks__/useGroupedCurrenciesByProvider.mock.js +0 -41
- package/lib/modularDrawer/__mocks__/useGroupedCurrenciesByProvider.mock.js.map +0 -1
- package/lib/modularDrawer/utils/currencyUtils.d.ts +0 -10
- package/lib/modularDrawer/utils/currencyUtils.d.ts.map +0 -1
- package/lib/modularDrawer/utils/currencyUtils.js +0 -74
- package/lib/modularDrawer/utils/currencyUtils.js.map +0 -1
- package/lib/modularDrawer/utils/groupCurrenciesByProvider.d.ts.map +0 -1
- package/lib/modularDrawer/utils/groupCurrenciesByProvider.js.map +0 -1
- package/lib-es/deposit/helper.d.ts +0 -8
- package/lib-es/deposit/helper.d.ts.map +0 -1
- package/lib-es/deposit/helper.js +0 -117
- package/lib-es/deposit/helper.js.map +0 -1
- package/lib-es/deposit/index.d.ts +0 -3
- package/lib-es/deposit/index.d.ts.map +0 -1
- package/lib-es/deposit/index.js +0 -3
- package/lib-es/deposit/index.js.map +0 -1
- package/lib-es/deposit/mock.d.ts +0 -75
- package/lib-es/deposit/mock.d.ts.map +0 -1
- package/lib-es/deposit/mock.js +0 -15108
- package/lib-es/deposit/mock.js.map +0 -1
- package/lib-es/deposit/useGroupedCurrenciesByProvider.hook.d.ts +0 -3
- package/lib-es/deposit/useGroupedCurrenciesByProvider.hook.d.ts.map +0 -1
- package/lib-es/deposit/useGroupedCurrenciesByProvider.hook.js +0 -36
- package/lib-es/deposit/useGroupedCurrenciesByProvider.hook.js.map +0 -1
- package/lib-es/modularDrawer/__mocks__/useGroupedCurrenciesByProvider.mock.d.ts +0 -22
- package/lib-es/modularDrawer/__mocks__/useGroupedCurrenciesByProvider.mock.d.ts.map +0 -1
- package/lib-es/modularDrawer/__mocks__/useGroupedCurrenciesByProvider.mock.js +0 -38
- package/lib-es/modularDrawer/__mocks__/useGroupedCurrenciesByProvider.mock.js.map +0 -1
- package/lib-es/modularDrawer/utils/currencyUtils.d.ts +0 -10
- package/lib-es/modularDrawer/utils/currencyUtils.d.ts.map +0 -1
- package/lib-es/modularDrawer/utils/currencyUtils.js +0 -65
- package/lib-es/modularDrawer/utils/currencyUtils.js.map +0 -1
- package/lib-es/modularDrawer/utils/groupCurrenciesByProvider.d.ts.map +0 -1
- package/lib-es/modularDrawer/utils/groupCurrenciesByProvider.js.map +0 -1
- package/src/deposit/deposit.integration.test.ts +0 -88
- package/src/deposit/deposit.test.ts +0 -684
- package/src/deposit/helper.ts +0 -143
- package/src/deposit/index.ts +0 -3
- package/src/deposit/mock.ts +0 -15112
- package/src/deposit/useGroupedCurrenciesByProvider.hook.ts +0 -46
- package/src/modularDrawer/__mocks__/useGroupedCurrenciesByProvider.mock.ts +0 -49
- package/src/modularDrawer/modules/__test__/createNetworkConfiguration.test.ts +0 -342
- package/src/modularDrawer/utils/__tests__/currencyUtils.test.ts +0 -126
- package/src/modularDrawer/utils/currencyUtils.ts +0 -95
|
@@ -1,46 +0,0 @@
|
|
|
1
|
-
import { GroupedCurrencies, LoadingBasedGroupedCurrencies, LoadingStatus } from "./type";
|
|
2
|
-
import { CryptoOrTokenCurrency } from "@ledgerhq/types-cryptoassets";
|
|
3
|
-
import { useEffect, useMemo, useState } from "react";
|
|
4
|
-
import { isCurrencySupported, listSupportedCurrencies, listTokens } from "../currencies";
|
|
5
|
-
import { loadCurrenciesByProvider } from "./helper";
|
|
6
|
-
import { useAcceptedCurrency } from "../modularDrawer/hooks/useAcceptedCurrency";
|
|
7
|
-
|
|
8
|
-
// FIXME(LIVE-10505): bad performane & shared utility to move to coin-framework
|
|
9
|
-
const listSupportedTokens = () => listTokens().filter(t => isCurrencySupported(t.parentCurrency));
|
|
10
|
-
|
|
11
|
-
const initialResult: GroupedCurrencies = {
|
|
12
|
-
sortedCryptoCurrencies: [],
|
|
13
|
-
currenciesByProvider: [],
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const useGroupedCurrenciesByProvider = (
|
|
17
|
-
withLoading?: boolean,
|
|
18
|
-
): GroupedCurrencies | LoadingBasedGroupedCurrencies => {
|
|
19
|
-
const [result, setResult] = useState(initialResult);
|
|
20
|
-
const isAcceptedCurrency = useAcceptedCurrency();
|
|
21
|
-
|
|
22
|
-
const [loadingStatus, setLoadingStatus] = useState<LoadingStatus>(LoadingStatus.Idle);
|
|
23
|
-
const coinsAndTokensSupported = useMemo(
|
|
24
|
-
() =>
|
|
25
|
-
(listSupportedCurrencies() as CryptoOrTokenCurrency[])
|
|
26
|
-
.concat(listSupportedTokens())
|
|
27
|
-
.filter(isAcceptedCurrency),
|
|
28
|
-
[isAcceptedCurrency],
|
|
29
|
-
);
|
|
30
|
-
|
|
31
|
-
// Get mapped assets filtered by supported & sorted currencies, grouped by provider id
|
|
32
|
-
useEffect(() => {
|
|
33
|
-
if (withLoading) {
|
|
34
|
-
setLoadingStatus(LoadingStatus.Idle);
|
|
35
|
-
loadCurrenciesByProvider(coinsAndTokensSupported)
|
|
36
|
-
.then(data => {
|
|
37
|
-
setResult(data);
|
|
38
|
-
setLoadingStatus(LoadingStatus.Success);
|
|
39
|
-
})
|
|
40
|
-
.catch(() => setLoadingStatus(LoadingStatus.Error));
|
|
41
|
-
} else {
|
|
42
|
-
loadCurrenciesByProvider(coinsAndTokensSupported).then(setResult);
|
|
43
|
-
}
|
|
44
|
-
}, [coinsAndTokensSupported, withLoading]);
|
|
45
|
-
return withLoading ? { result, loadingStatus } : result;
|
|
46
|
-
};
|
|
@@ -1,49 +0,0 @@
|
|
|
1
|
-
import { LoadingStatus } from "../../deposit/type";
|
|
2
|
-
import {
|
|
3
|
-
arbitrumToken,
|
|
4
|
-
mockArbitrumCryptoCurrency,
|
|
5
|
-
mockBaseCryptoCurrency,
|
|
6
|
-
mockBtcCryptoCurrency,
|
|
7
|
-
mockEthCryptoCurrency,
|
|
8
|
-
mockInjectiveCryptoCurrency,
|
|
9
|
-
mockScrollCryptoCurrency,
|
|
10
|
-
usdcToken,
|
|
11
|
-
} from "./currencies.mock";
|
|
12
|
-
|
|
13
|
-
export const providers = [
|
|
14
|
-
{
|
|
15
|
-
providerId: "bitcoin",
|
|
16
|
-
currenciesByNetwork: [mockBtcCryptoCurrency],
|
|
17
|
-
},
|
|
18
|
-
{
|
|
19
|
-
providerId: "ethereum",
|
|
20
|
-
currenciesByNetwork: [
|
|
21
|
-
mockEthCryptoCurrency,
|
|
22
|
-
mockArbitrumCryptoCurrency,
|
|
23
|
-
mockBaseCryptoCurrency,
|
|
24
|
-
mockScrollCryptoCurrency,
|
|
25
|
-
],
|
|
26
|
-
},
|
|
27
|
-
{ providerId: "arbitrum", currenciesByNetwork: [arbitrumToken] },
|
|
28
|
-
{ providerId: "usd-coin", currenciesByNetwork: [usdcToken] },
|
|
29
|
-
{ providerId: "injective-protocol", currenciesByNetwork: [mockInjectiveCryptoCurrency] },
|
|
30
|
-
];
|
|
31
|
-
|
|
32
|
-
export const res = {
|
|
33
|
-
result: {
|
|
34
|
-
currenciesByProvider: providers,
|
|
35
|
-
sortedCryptoCurrencies: [
|
|
36
|
-
mockBtcCryptoCurrency,
|
|
37
|
-
mockEthCryptoCurrency,
|
|
38
|
-
mockArbitrumCryptoCurrency,
|
|
39
|
-
mockBaseCryptoCurrency,
|
|
40
|
-
mockScrollCryptoCurrency,
|
|
41
|
-
mockInjectiveCryptoCurrency,
|
|
42
|
-
],
|
|
43
|
-
},
|
|
44
|
-
};
|
|
45
|
-
|
|
46
|
-
export const useGroupedCurrenciesByProvider: jest.Mock = jest.fn(() => ({
|
|
47
|
-
...res,
|
|
48
|
-
loadingStatus: LoadingStatus.Success,
|
|
49
|
-
}));
|
|
@@ -1,342 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @jest-environment jsdom
|
|
3
|
-
*/
|
|
4
|
-
|
|
5
|
-
import { BigNumber } from "bignumber.js";
|
|
6
|
-
import { genAccount, genTokenAccount } from "@ledgerhq/coin-framework/lib/mocks/account";
|
|
7
|
-
import { createFixtureCryptoCurrency } from "../../../mock/fixtures/cryptoCurrencies";
|
|
8
|
-
import { createNetworkConfigurationHook } from "../createNetworkConfiguration";
|
|
9
|
-
import { renderHook } from "@testing-library/react";
|
|
10
|
-
import { CounterValuesState } from "@ledgerhq/live-countervalues/lib/types";
|
|
11
|
-
import { FiatCurrency, TokenCurrency, CryptoCurrency } from "@ledgerhq/types-cryptoassets";
|
|
12
|
-
import { makeUsdcToken } from "./createAssetConfiguration.test";
|
|
13
|
-
|
|
14
|
-
const bscCurrency = createFixtureCryptoCurrency("bsc");
|
|
15
|
-
const bscAccount = genAccount("bsc-account", { currency: bscCurrency });
|
|
16
|
-
const bscUsdcToken: TokenCurrency = makeUsdcToken(
|
|
17
|
-
bscCurrency,
|
|
18
|
-
"0x8AC76a51cc950d9822D68b83fE1Ad97B32Cd580d",
|
|
19
|
-
"bep20",
|
|
20
|
-
"USD Coin",
|
|
21
|
-
"bsc/erc20/usdc",
|
|
22
|
-
);
|
|
23
|
-
const bscUsdcTokenAccount = genTokenAccount(0, bscAccount, bscUsdcToken);
|
|
24
|
-
bscAccount.subAccounts = [bscUsdcTokenAccount];
|
|
25
|
-
|
|
26
|
-
const baseCurrency = createFixtureCryptoCurrency("base");
|
|
27
|
-
const baseAccount = genAccount("base-account", { currency: baseCurrency });
|
|
28
|
-
const baseUsdcToken: TokenCurrency = makeUsdcToken(
|
|
29
|
-
baseCurrency,
|
|
30
|
-
"0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
|
|
31
|
-
"erc20",
|
|
32
|
-
"USD Coin",
|
|
33
|
-
"base/erc20/usd_coin",
|
|
34
|
-
);
|
|
35
|
-
const baseUsdcTokenAccount = genTokenAccount(0, baseAccount, baseUsdcToken);
|
|
36
|
-
baseAccount.subAccounts = [baseUsdcTokenAccount];
|
|
37
|
-
|
|
38
|
-
const ethereumCurrency = createFixtureCryptoCurrency("ethereum");
|
|
39
|
-
const ethereumAccountHigh = genAccount("ethereum-account-high", {
|
|
40
|
-
currency: ethereumCurrency,
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
ethereumAccountHigh.balance = new BigNumber("1000000000000000000000");
|
|
44
|
-
ethereumAccountHigh.spendableBalance = ethereumAccountHigh.balance;
|
|
45
|
-
|
|
46
|
-
const ethereumUsdcToken: TokenCurrency = makeUsdcToken(
|
|
47
|
-
ethereumCurrency,
|
|
48
|
-
"0xA0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
|
|
49
|
-
"erc20",
|
|
50
|
-
|
|
51
|
-
"USD Coin",
|
|
52
|
-
);
|
|
53
|
-
const ethereumUsdcTokenAccount = genTokenAccount(0, ethereumAccountHigh, ethereumUsdcToken);
|
|
54
|
-
ethereumAccountHigh.subAccounts = [ethereumUsdcTokenAccount];
|
|
55
|
-
|
|
56
|
-
const ethereumAccountLow = genAccount("ethereum-account-low", {
|
|
57
|
-
currency: ethereumCurrency,
|
|
58
|
-
});
|
|
59
|
-
ethereumAccountLow.balance = new BigNumber("100000000000000000");
|
|
60
|
-
ethereumAccountLow.spendableBalance = ethereumAccountLow.balance;
|
|
61
|
-
|
|
62
|
-
const ethereumAccountZero = genAccount("ethereum-account-zero", {
|
|
63
|
-
currency: ethereumCurrency,
|
|
64
|
-
});
|
|
65
|
-
ethereumAccountZero.balance = new BigNumber("0");
|
|
66
|
-
ethereumAccountZero.spendableBalance = ethereumAccountZero.balance;
|
|
67
|
-
|
|
68
|
-
const polygonCurrency = createFixtureCryptoCurrency("polygon");
|
|
69
|
-
const polygonAccountMedium = genAccount("polygon-account-medium", {
|
|
70
|
-
currency: polygonCurrency,
|
|
71
|
-
});
|
|
72
|
-
polygonAccountMedium.balance = new BigNumber("500000000000000000000");
|
|
73
|
-
polygonAccountMedium.spendableBalance = polygonAccountMedium.balance;
|
|
74
|
-
|
|
75
|
-
const polygonUsdcToken: TokenCurrency = makeUsdcToken(
|
|
76
|
-
polygonCurrency,
|
|
77
|
-
"0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
|
|
78
|
-
"erc20",
|
|
79
|
-
"USD Coin",
|
|
80
|
-
);
|
|
81
|
-
const polygonUsdcTokenAccount = genTokenAccount(0, polygonAccountMedium, polygonUsdcToken);
|
|
82
|
-
polygonAccountMedium.subAccounts = [polygonUsdcTokenAccount];
|
|
83
|
-
|
|
84
|
-
const makeAccountData = (asset, count) => ({
|
|
85
|
-
asset,
|
|
86
|
-
label: `${count} account`,
|
|
87
|
-
count,
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
const useAccountData = () => {
|
|
91
|
-
return [
|
|
92
|
-
makeAccountData(createFixtureCryptoCurrency("ethereum"), 3),
|
|
93
|
-
makeAccountData(createFixtureCryptoCurrency("polygon"), 1),
|
|
94
|
-
makeAccountData(createFixtureCryptoCurrency("bsc"), 2),
|
|
95
|
-
makeAccountData(createFixtureCryptoCurrency("base"), 1),
|
|
96
|
-
makeAccountData(createFixtureCryptoCurrency("avalanche_c_chain"), 0),
|
|
97
|
-
];
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
const mockFiatCurrency: FiatCurrency = {
|
|
101
|
-
type: "FiatCurrency",
|
|
102
|
-
ticker: "USD",
|
|
103
|
-
name: "US Dollar",
|
|
104
|
-
symbol: "$",
|
|
105
|
-
units: [
|
|
106
|
-
{
|
|
107
|
-
code: "$",
|
|
108
|
-
name: "US Dollar",
|
|
109
|
-
magnitude: 2,
|
|
110
|
-
showAllDigits: true,
|
|
111
|
-
prefixCode: true,
|
|
112
|
-
},
|
|
113
|
-
],
|
|
114
|
-
};
|
|
115
|
-
|
|
116
|
-
const accountsCount = jest.fn(() => null);
|
|
117
|
-
const accountsCountAndApy = () => null;
|
|
118
|
-
const useBalanceDeps = () => {
|
|
119
|
-
const mockCounterValuesState: CounterValuesState = {
|
|
120
|
-
cache: {
|
|
121
|
-
"USD testCoinId": {
|
|
122
|
-
fallback: 2354.213,
|
|
123
|
-
map: new Map(),
|
|
124
|
-
stats: {
|
|
125
|
-
oldest: "2024-09-12",
|
|
126
|
-
earliest: "2025-10-02T13",
|
|
127
|
-
oldestDate: new Date(),
|
|
128
|
-
earliestDate: new Date(),
|
|
129
|
-
earliestStableDate: new Date(),
|
|
130
|
-
},
|
|
131
|
-
},
|
|
132
|
-
},
|
|
133
|
-
data: {},
|
|
134
|
-
status: {},
|
|
135
|
-
};
|
|
136
|
-
|
|
137
|
-
return {
|
|
138
|
-
counterValueCurrency: mockFiatCurrency,
|
|
139
|
-
flattenedAccounts: [
|
|
140
|
-
bscAccount,
|
|
141
|
-
bscUsdcTokenAccount,
|
|
142
|
-
baseAccount,
|
|
143
|
-
baseUsdcTokenAccount,
|
|
144
|
-
ethereumAccountHigh,
|
|
145
|
-
ethereumUsdcTokenAccount,
|
|
146
|
-
ethereumAccountLow,
|
|
147
|
-
ethereumAccountZero,
|
|
148
|
-
polygonAccountMedium,
|
|
149
|
-
polygonUsdcTokenAccount,
|
|
150
|
-
],
|
|
151
|
-
state: mockCounterValuesState,
|
|
152
|
-
locale: "en-US",
|
|
153
|
-
};
|
|
154
|
-
};
|
|
155
|
-
const balanceItem = jest.fn(() => null);
|
|
156
|
-
|
|
157
|
-
const networkConfigurationDeps = {
|
|
158
|
-
useAccountData,
|
|
159
|
-
accountsCount,
|
|
160
|
-
accountsCountAndApy,
|
|
161
|
-
useBalanceDeps,
|
|
162
|
-
balanceItem,
|
|
163
|
-
};
|
|
164
|
-
|
|
165
|
-
describe("createNetworkConfiguration", () => {
|
|
166
|
-
beforeEach(() => {
|
|
167
|
-
jest.clearAllMocks();
|
|
168
|
-
});
|
|
169
|
-
|
|
170
|
-
it("should call balanceItem with correct BalanceUI objects for each network", () => {
|
|
171
|
-
const { result } = renderHook(() => createNetworkConfigurationHook(networkConfigurationDeps));
|
|
172
|
-
const res = result.current({ networksConfig: { rightElement: "balance" } });
|
|
173
|
-
res([baseUsdcToken, bscUsdcToken]);
|
|
174
|
-
|
|
175
|
-
expect(balanceItem).toHaveBeenCalledTimes(2);
|
|
176
|
-
expect(balanceItem).toHaveBeenNthCalledWith(
|
|
177
|
-
1,
|
|
178
|
-
expect.objectContaining({ balance: baseUsdcTokenAccount.balance }),
|
|
179
|
-
);
|
|
180
|
-
expect(balanceItem).toHaveBeenNthCalledWith(
|
|
181
|
-
2,
|
|
182
|
-
expect.objectContaining({ balance: bscUsdcTokenAccount.balance }),
|
|
183
|
-
);
|
|
184
|
-
});
|
|
185
|
-
|
|
186
|
-
it("account count", () => {
|
|
187
|
-
const { result } = renderHook(() => createNetworkConfigurationHook(networkConfigurationDeps));
|
|
188
|
-
const res = result.current({ networksConfig: { leftElement: "numberOfAccounts" } });
|
|
189
|
-
res([baseUsdcToken, bscUsdcToken]);
|
|
190
|
-
|
|
191
|
-
expect(accountsCount).toHaveBeenCalledTimes(4);
|
|
192
|
-
expect(accountsCount).toHaveBeenNthCalledWith(1, { label: "3 account" });
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
it("should order by balance (highest first) when only balance element is present", () => {
|
|
196
|
-
const { result } = renderHook(() => createNetworkConfigurationHook(networkConfigurationDeps));
|
|
197
|
-
const res = result.current({
|
|
198
|
-
networksConfig: { rightElement: "balance" },
|
|
199
|
-
});
|
|
200
|
-
|
|
201
|
-
const orderedResult = res([ethereumUsdcToken, polygonUsdcToken, bscUsdcToken]);
|
|
202
|
-
|
|
203
|
-
expect(orderedResult).toHaveLength(3);
|
|
204
|
-
|
|
205
|
-
// Should be ordered by balance amount (highest first)
|
|
206
|
-
expect(orderedResult[0].id).toBe(ethereumCurrency.id); // 1000 ETH
|
|
207
|
-
expect(orderedResult[1].id).toBe(polygonCurrency.id); // 500 MATIC
|
|
208
|
-
expect(orderedResult[2].id).toBe(bscCurrency.id); // Lower balance
|
|
209
|
-
});
|
|
210
|
-
|
|
211
|
-
it("should order by account count (highest first) when only account count element is present", () => {
|
|
212
|
-
const { result } = renderHook(() => createNetworkConfigurationHook(networkConfigurationDeps));
|
|
213
|
-
const res = result.current({
|
|
214
|
-
networksConfig: { leftElement: "numberOfAccounts" },
|
|
215
|
-
});
|
|
216
|
-
|
|
217
|
-
const orderedResult = res([ethereumUsdcToken, polygonUsdcToken, bscUsdcToken]);
|
|
218
|
-
|
|
219
|
-
expect(orderedResult).toHaveLength(3);
|
|
220
|
-
|
|
221
|
-
// Should be ordered by account count (highest first)
|
|
222
|
-
expect(orderedResult[0].id).toBe(ethereumCurrency.id); // 3 accounts
|
|
223
|
-
expect(orderedResult[1].id).toBe(bscCurrency.id); // 2 accounts
|
|
224
|
-
expect(orderedResult[2].id).toBe(polygonCurrency.id); // 1 account
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
it("should handle networks with zero balances by ordering them by account count", () => {
|
|
228
|
-
const zeroBalanceEth = { ...ethereumAccountZero };
|
|
229
|
-
const zeroBalancePoly = {
|
|
230
|
-
...polygonAccountMedium,
|
|
231
|
-
balance: new BigNumber("0"),
|
|
232
|
-
spendableBalance: new BigNumber("0"),
|
|
233
|
-
};
|
|
234
|
-
|
|
235
|
-
const mockUseBalanceDepsZero = () => ({
|
|
236
|
-
counterValueCurrency: {
|
|
237
|
-
type: "FiatCurrency" as const,
|
|
238
|
-
ticker: "USD",
|
|
239
|
-
name: "US Dollar",
|
|
240
|
-
symbol: "$",
|
|
241
|
-
units: [
|
|
242
|
-
{ code: "$", name: "US Dollar", magnitude: 2, showAllDigits: true, prefixCode: true },
|
|
243
|
-
],
|
|
244
|
-
},
|
|
245
|
-
flattenedAccounts: [zeroBalanceEth, zeroBalancePoly],
|
|
246
|
-
state: { cache: {}, data: {}, status: {} },
|
|
247
|
-
locale: "en-US",
|
|
248
|
-
});
|
|
249
|
-
|
|
250
|
-
const { result } = renderHook(() =>
|
|
251
|
-
createNetworkConfigurationHook({
|
|
252
|
-
...networkConfigurationDeps,
|
|
253
|
-
useBalanceDeps: mockUseBalanceDepsZero,
|
|
254
|
-
}),
|
|
255
|
-
);
|
|
256
|
-
const res = result.current({
|
|
257
|
-
networksConfig: { leftElement: "numberOfAccounts", rightElement: "balance" },
|
|
258
|
-
});
|
|
259
|
-
|
|
260
|
-
const orderedResult = res([ethereumUsdcToken, polygonUsdcToken]);
|
|
261
|
-
|
|
262
|
-
expect(orderedResult).toHaveLength(2);
|
|
263
|
-
|
|
264
|
-
// When both have zero balance, should order by account count
|
|
265
|
-
expect(orderedResult[0].id).toBe(ethereumCurrency.id); // 3 accounts
|
|
266
|
-
expect(orderedResult[1].id).toBe(polygonCurrency.id); // 1 account
|
|
267
|
-
});
|
|
268
|
-
|
|
269
|
-
it("should prioritise non-zero balances over account count", () => {
|
|
270
|
-
const testEthereumCurrency: CryptoCurrency = createFixtureCryptoCurrency("ethereum");
|
|
271
|
-
const testPolygonCurrency: CryptoCurrency = createFixtureCryptoCurrency("polygon");
|
|
272
|
-
const testAvalancheCurrency: CryptoCurrency = createFixtureCryptoCurrency("avalanche_c_chain");
|
|
273
|
-
const testArbitrumCurrency: CryptoCurrency = createFixtureCryptoCurrency("arbitrum");
|
|
274
|
-
|
|
275
|
-
const testEthereumAccount = genAccount("test-ethereum-account", {
|
|
276
|
-
currency: testEthereumCurrency,
|
|
277
|
-
});
|
|
278
|
-
testEthereumAccount.balance = new BigNumber("1000000000000000000000");
|
|
279
|
-
testEthereumAccount.spendableBalance = testEthereumAccount.balance;
|
|
280
|
-
|
|
281
|
-
const testPolygonAccount = genAccount("test-polygon-account", {
|
|
282
|
-
currency: testPolygonCurrency,
|
|
283
|
-
});
|
|
284
|
-
testPolygonAccount.balance = new BigNumber("500000000000000000000");
|
|
285
|
-
testPolygonAccount.spendableBalance = testPolygonAccount.balance;
|
|
286
|
-
|
|
287
|
-
const testEthereumUsdcToken = makeUsdcToken(
|
|
288
|
-
testEthereumCurrency,
|
|
289
|
-
"0xA0b86991c6218b36c1d19d4a2e9eb0ce3606eb48",
|
|
290
|
-
);
|
|
291
|
-
const testPolygonUsdcToken = makeUsdcToken(
|
|
292
|
-
testPolygonCurrency,
|
|
293
|
-
"0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
|
|
294
|
-
);
|
|
295
|
-
const testAvalancheUsdcToken = makeUsdcToken(
|
|
296
|
-
testAvalancheCurrency,
|
|
297
|
-
"0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
|
|
298
|
-
);
|
|
299
|
-
const testArbitrumUsdcToken = makeUsdcToken(
|
|
300
|
-
testArbitrumCurrency,
|
|
301
|
-
"0xFF970A61A04b1cA14834A43f5de4533eBDDB5CC8",
|
|
302
|
-
);
|
|
303
|
-
|
|
304
|
-
const testUseBalanceDeps = () => ({
|
|
305
|
-
counterValueCurrency: mockFiatCurrency,
|
|
306
|
-
flattenedAccounts: [testEthereumAccount, testPolygonAccount],
|
|
307
|
-
state: { cache: {}, data: {}, status: {} },
|
|
308
|
-
locale: "en-US",
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
const testUseAccountData = () => [
|
|
312
|
-
makeAccountData(testEthereumCurrency, 3),
|
|
313
|
-
makeAccountData(testPolygonCurrency, 1),
|
|
314
|
-
makeAccountData(testAvalancheCurrency, 1),
|
|
315
|
-
makeAccountData(testArbitrumCurrency, 2),
|
|
316
|
-
];
|
|
317
|
-
|
|
318
|
-
const { result } = renderHook(() =>
|
|
319
|
-
createNetworkConfigurationHook({
|
|
320
|
-
...networkConfigurationDeps,
|
|
321
|
-
useAccountData: testUseAccountData,
|
|
322
|
-
useBalanceDeps: testUseBalanceDeps,
|
|
323
|
-
}),
|
|
324
|
-
);
|
|
325
|
-
|
|
326
|
-
const res = result.current({
|
|
327
|
-
networksConfig: { leftElement: "numberOfAccounts", rightElement: "balance" },
|
|
328
|
-
});
|
|
329
|
-
|
|
330
|
-
const orderedResult = res([
|
|
331
|
-
testEthereumUsdcToken,
|
|
332
|
-
testPolygonUsdcToken,
|
|
333
|
-
testAvalancheUsdcToken,
|
|
334
|
-
testArbitrumUsdcToken,
|
|
335
|
-
]);
|
|
336
|
-
|
|
337
|
-
expect(orderedResult[0].id).toBe(testEthereumCurrency.id); // 1000 ETH balance, 3 accounts
|
|
338
|
-
expect(orderedResult[1].id).toBe(testPolygonCurrency.id); // 500 MATIC balance, 1 account
|
|
339
|
-
expect(orderedResult[2].id).toBe(testArbitrumCurrency.id); // (0 balance, 2 accounts)
|
|
340
|
-
expect(orderedResult[3].id).toBe(testAvalancheCurrency.id); // (0 balance, 1 account)
|
|
341
|
-
});
|
|
342
|
-
});
|
|
@@ -1,126 +0,0 @@
|
|
|
1
|
-
import { useGroupedCurrenciesByProvider } from "../../__mocks__/useGroupedCurrenciesByProvider.mock";
|
|
2
|
-
import { TokenCurrency } from "@ledgerhq/types-cryptoassets";
|
|
3
|
-
|
|
4
|
-
import {
|
|
5
|
-
buildProviderCoverageMap,
|
|
6
|
-
extractProviderCurrencies,
|
|
7
|
-
filterProvidersByIds,
|
|
8
|
-
getProviderCurrency,
|
|
9
|
-
isProviderToken,
|
|
10
|
-
safeCurrencyLookup,
|
|
11
|
-
} from "../currencyUtils";
|
|
12
|
-
import {
|
|
13
|
-
mockBaseCryptoCurrency,
|
|
14
|
-
mockBtcCryptoCurrency,
|
|
15
|
-
mockEthCryptoCurrency,
|
|
16
|
-
usdcToken,
|
|
17
|
-
} from "../../__mocks__/currencies.mock";
|
|
18
|
-
|
|
19
|
-
describe("safeCurrencyLookup", () => {
|
|
20
|
-
it("should return the currency if it is found", async () => {
|
|
21
|
-
const currency = await safeCurrencyLookup("ethereum");
|
|
22
|
-
expect(currency).toBeDefined();
|
|
23
|
-
});
|
|
24
|
-
it("should return null if the currency is not found", async () => {
|
|
25
|
-
const currency = await safeCurrencyLookup("not-a-currency");
|
|
26
|
-
expect(currency).toBeNull();
|
|
27
|
-
});
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
describe("isProviderToken", () => {
|
|
31
|
-
it("should return true if the currency is a provider token", () => {
|
|
32
|
-
const baseToken: TokenCurrency = {
|
|
33
|
-
type: "TokenCurrency",
|
|
34
|
-
id: "base/erc20/base",
|
|
35
|
-
contractAddress: "0x0000000000000000000000000000000000000000",
|
|
36
|
-
parentCurrency: mockBaseCryptoCurrency,
|
|
37
|
-
tokenType: "erc20",
|
|
38
|
-
name: "Base",
|
|
39
|
-
ticker: "BASE",
|
|
40
|
-
units: [
|
|
41
|
-
{
|
|
42
|
-
name: "Base",
|
|
43
|
-
code: "BASE",
|
|
44
|
-
magnitude: 18,
|
|
45
|
-
},
|
|
46
|
-
],
|
|
47
|
-
};
|
|
48
|
-
const currency = isProviderToken(baseToken, "base");
|
|
49
|
-
expect(currency).toBe(true);
|
|
50
|
-
});
|
|
51
|
-
it("should return false if the currency is not a provider token", () => {
|
|
52
|
-
const currency = isProviderToken(mockEthCryptoCurrency, "ethereum");
|
|
53
|
-
expect(currency).toBe(false);
|
|
54
|
-
});
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
describe("getProviderCurrency", () => {
|
|
58
|
-
it("should return the currency if it is a provider currency", async () => {
|
|
59
|
-
const { result } = useGroupedCurrenciesByProvider();
|
|
60
|
-
const currency = await getProviderCurrency(result.currenciesByProvider[0]);
|
|
61
|
-
expect(currency).toEqual(mockBtcCryptoCurrency);
|
|
62
|
-
});
|
|
63
|
-
it("should return the currency if it is a provider token", async () => {
|
|
64
|
-
const { result } = useGroupedCurrenciesByProvider();
|
|
65
|
-
const currency = await getProviderCurrency(result.currenciesByProvider[3]);
|
|
66
|
-
expect(currency).toEqual(usdcToken);
|
|
67
|
-
});
|
|
68
|
-
});
|
|
69
|
-
|
|
70
|
-
describe("buildProviderCoverageMap", () => {
|
|
71
|
-
it("should build a map of provider coverage correctly", () => {
|
|
72
|
-
const { result } = useGroupedCurrenciesByProvider();
|
|
73
|
-
const coverageMap = buildProviderCoverageMap(result.currenciesByProvider);
|
|
74
|
-
|
|
75
|
-
expect(coverageMap).toBeInstanceOf(Map);
|
|
76
|
-
expect(coverageMap.get("bitcoin")).toEqual(new Set(["bitcoin"]));
|
|
77
|
-
expect(coverageMap.get("ethereum")).toEqual(new Set(["ethereum"]));
|
|
78
|
-
expect(coverageMap.get("arbitrum")).toEqual(new Set(["ethereum"]));
|
|
79
|
-
expect(coverageMap.get("base")).toEqual(new Set(["ethereum"]));
|
|
80
|
-
expect(coverageMap.get("scroll")).toEqual(new Set(["ethereum"]));
|
|
81
|
-
expect(coverageMap.get("arbitrum/erc20/arbitrum")).toEqual(new Set(["arbitrum"]));
|
|
82
|
-
expect(coverageMap.get("ethereum/erc20/usd__coin")).toEqual(new Set(["usd-coin"]));
|
|
83
|
-
expect(coverageMap.get("injective")).toEqual(new Set(["injective-protocol"]));
|
|
84
|
-
});
|
|
85
|
-
it("should handle empty input", () => {
|
|
86
|
-
const coverageMap = buildProviderCoverageMap([]);
|
|
87
|
-
expect(coverageMap).toBeInstanceOf(Map);
|
|
88
|
-
expect(coverageMap.size).toBe(0);
|
|
89
|
-
});
|
|
90
|
-
});
|
|
91
|
-
|
|
92
|
-
describe("filterProvidersByIds", () => {
|
|
93
|
-
it("should filter providers by ids correctly #evm", () => {
|
|
94
|
-
const { result } = useGroupedCurrenciesByProvider();
|
|
95
|
-
const coverageMap = buildProviderCoverageMap(result.currenciesByProvider);
|
|
96
|
-
const filteredProviders = filterProvidersByIds(
|
|
97
|
-
result.currenciesByProvider,
|
|
98
|
-
new Set(["base"]),
|
|
99
|
-
coverageMap,
|
|
100
|
-
);
|
|
101
|
-
expect(filteredProviders).toHaveLength(1);
|
|
102
|
-
expect(filteredProviders[0].providerId).toBe("ethereum");
|
|
103
|
-
});
|
|
104
|
-
it("should filter providers by ids correctly #bitcoin", () => {
|
|
105
|
-
const { result } = useGroupedCurrenciesByProvider();
|
|
106
|
-
const coverageMap = buildProviderCoverageMap(result.currenciesByProvider);
|
|
107
|
-
const filteredProviders = filterProvidersByIds(
|
|
108
|
-
result.currenciesByProvider,
|
|
109
|
-
new Set(["bitcoin"]),
|
|
110
|
-
coverageMap,
|
|
111
|
-
);
|
|
112
|
-
expect(filteredProviders).toHaveLength(1);
|
|
113
|
-
expect(filteredProviders[0].providerId).toBe("bitcoin");
|
|
114
|
-
});
|
|
115
|
-
});
|
|
116
|
-
|
|
117
|
-
describe("extractProviderCurrencies", () => {
|
|
118
|
-
it("should extract provider currencies correctly", async () => {
|
|
119
|
-
const { result } = useGroupedCurrenciesByProvider();
|
|
120
|
-
const providerCurrencies = await extractProviderCurrencies(result.currenciesByProvider);
|
|
121
|
-
expect(providerCurrencies).toHaveLength(5);
|
|
122
|
-
expect(providerCurrencies[0]).toEqual(mockBtcCryptoCurrency);
|
|
123
|
-
expect(providerCurrencies[1].id).toBe("ethereum");
|
|
124
|
-
expect(providerCurrencies[2].id).toBe("arbitrum/erc20/arbitrum");
|
|
125
|
-
});
|
|
126
|
-
});
|
|
@@ -1,95 +0,0 @@
|
|
|
1
|
-
import { CryptoOrTokenCurrency } from "@ledgerhq/types-cryptoassets";
|
|
2
|
-
import { getTokenOrCryptoCurrencyById } from "../../deposit/helper";
|
|
3
|
-
import { isTokenCurrency } from "../../currencies";
|
|
4
|
-
import { CurrenciesByProviderId } from "../../deposit/type";
|
|
5
|
-
|
|
6
|
-
export type ProviderCoverageMap = Map<string, Set<string>>;
|
|
7
|
-
|
|
8
|
-
export const safeCurrencyLookup = async (id: string): Promise<CryptoOrTokenCurrency | null> => {
|
|
9
|
-
try {
|
|
10
|
-
return await getTokenOrCryptoCurrencyById(id);
|
|
11
|
-
} catch {
|
|
12
|
-
return null;
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
|
|
16
|
-
export const isProviderToken = (currency: CryptoOrTokenCurrency, providerId: string): boolean => {
|
|
17
|
-
return isTokenCurrency(currency) && currency.id.toLowerCase().includes(providerId.toLowerCase());
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
export const getProviderCurrency = async (
|
|
21
|
-
provider: CurrenciesByProviderId,
|
|
22
|
-
): Promise<CryptoOrTokenCurrency | null> => {
|
|
23
|
-
const providerToken = provider.currenciesByNetwork.find(currency => {
|
|
24
|
-
return isProviderToken(currency, provider.providerId);
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
if (providerToken) {
|
|
28
|
-
return providerToken;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const lookupResult = await safeCurrencyLookup(provider.providerId);
|
|
32
|
-
return lookupResult ?? provider.currenciesByNetwork[0];
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
export const buildProviderCoverageMap = (
|
|
36
|
-
providers: CurrenciesByProviderId[],
|
|
37
|
-
): ProviderCoverageMap => {
|
|
38
|
-
const providerIdToCoveringProviders = new Map<string, Set<string>>();
|
|
39
|
-
|
|
40
|
-
for (const { providerId, currenciesByNetwork } of providers) {
|
|
41
|
-
for (const { id } of currenciesByNetwork) {
|
|
42
|
-
if (!providerIdToCoveringProviders.has(id)) {
|
|
43
|
-
providerIdToCoveringProviders.set(id, new Set());
|
|
44
|
-
}
|
|
45
|
-
providerIdToCoveringProviders.get(id)!.add(providerId);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
return providerIdToCoveringProviders;
|
|
50
|
-
};
|
|
51
|
-
|
|
52
|
-
export const filterProvidersByIds = (
|
|
53
|
-
providers: CurrenciesByProviderId[],
|
|
54
|
-
currencyIdsSet: Set<string>,
|
|
55
|
-
providerCoverageMap: ProviderCoverageMap,
|
|
56
|
-
): CurrenciesByProviderId[] => {
|
|
57
|
-
const filtered: CurrenciesByProviderId[] = [];
|
|
58
|
-
|
|
59
|
-
for (const provider of providers) {
|
|
60
|
-
const filteredCurrencies = provider.currenciesByNetwork.filter(currency =>
|
|
61
|
-
currencyIdsSet.has(currency.id),
|
|
62
|
-
);
|
|
63
|
-
|
|
64
|
-
if (filteredCurrencies.length === 0) continue;
|
|
65
|
-
|
|
66
|
-
const providerHasOwnCurrency = provider.currenciesByNetwork.some(
|
|
67
|
-
currency => currency.id === provider.providerId,
|
|
68
|
-
);
|
|
69
|
-
|
|
70
|
-
if (!providerHasOwnCurrency) {
|
|
71
|
-
const coveringProviders = providerCoverageMap.get(provider.providerId);
|
|
72
|
-
const isProviderIdCoveredElsewhere = coveringProviders && coveringProviders.size > 1;
|
|
73
|
-
|
|
74
|
-
if (isProviderIdCoveredElsewhere) continue;
|
|
75
|
-
}
|
|
76
|
-
|
|
77
|
-
if (filteredCurrencies.length === provider.currenciesByNetwork.length) {
|
|
78
|
-
filtered.push(provider);
|
|
79
|
-
} else {
|
|
80
|
-
filtered.push({
|
|
81
|
-
...provider,
|
|
82
|
-
currenciesByNetwork: filteredCurrencies,
|
|
83
|
-
});
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
|
|
87
|
-
return filtered;
|
|
88
|
-
};
|
|
89
|
-
|
|
90
|
-
export const extractProviderCurrencies = async (
|
|
91
|
-
providers: CurrenciesByProviderId[],
|
|
92
|
-
): Promise<CryptoOrTokenCurrency[]> => {
|
|
93
|
-
const currencies = await Promise.all(providers.map(provider => getProviderCurrency(provider)));
|
|
94
|
-
return currencies.filter((currency): currency is CryptoOrTokenCurrency => currency !== null);
|
|
95
|
-
};
|