@rango-dev/widget-embedded 0.17.1-next.4 → 0.17.1-next.6
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/dist/QueueManager.d.ts +2 -1
- package/dist/QueueManager.d.ts.map +1 -1
- package/dist/Wallets.d.ts +1 -0
- package/dist/Wallets.d.ts.map +1 -1
- package/dist/Widget.d.ts +1 -1
- package/dist/Widget.d.ts.map +1 -1
- package/dist/components/AppRouter.d.ts +0 -2
- package/dist/components/AppRouter.d.ts.map +1 -1
- package/dist/components/AppRoutes.d.ts +1 -6
- package/dist/components/AppRoutes.d.ts.map +1 -1
- package/dist/components/ConfirmWalletsModal/ConfirmWallets.styles.d.ts +1 -1
- package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.d.ts.map +1 -1
- package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.types.d.ts +0 -2
- package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.types.d.ts.map +1 -1
- package/dist/components/ConfirmWalletsModal/WalletList.d.ts.map +1 -1
- package/dist/components/ConfirmWalletsModal/WalletList.type.d.ts +1 -4
- package/dist/components/ConfirmWalletsModal/WalletList.type.d.ts.map +1 -1
- package/dist/components/NoResult/NoResult.d.ts.map +1 -1
- package/dist/components/NotificationContent/NotificationContent.d.ts.map +1 -1
- package/dist/components/Quote/Quote.d.ts.map +1 -1
- package/dist/components/SwapDetails/SwapDetails.d.ts.map +1 -1
- package/dist/components/TokenList/TokenList.d.ts.map +1 -1
- package/dist/components/UpdateUrl.d.ts +1 -6
- package/dist/components/UpdateUrl.d.ts.map +1 -1
- package/dist/components/WidgetEvents.d.ts.map +1 -1
- package/dist/hooks/useSwapInput.d.ts.map +1 -1
- package/dist/hooks/useSyncStoresWithConfig.d.ts +1 -2
- package/dist/hooks/useSyncStoresWithConfig.d.ts.map +1 -1
- package/dist/hooks/useWalletList.d.ts.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +4 -4
- package/dist/pages/ConfirmSwapPage.d.ts +1 -6
- package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
- package/dist/pages/LiquiditySourcePage.d.ts +1 -2
- package/dist/pages/LiquiditySourcePage.d.ts.map +1 -1
- package/dist/pages/SelectBlockchainPage.d.ts.map +1 -1
- package/dist/pages/SettingsPage.d.ts +1 -6
- package/dist/pages/SettingsPage.d.ts.map +1 -1
- package/dist/pages/WalletsPage.d.ts +1 -8
- package/dist/pages/WalletsPage.d.ts.map +1 -1
- package/dist/store/AppStore.d.ts +59 -0
- package/dist/store/AppStore.d.ts.map +1 -0
- package/dist/store/app.d.ts +12 -6
- package/dist/store/app.d.ts.map +1 -1
- package/dist/store/quote.d.ts +14 -7
- package/dist/store/quote.d.ts.map +1 -1
- package/dist/store/settings.d.ts +3 -2
- package/dist/store/settings.d.ts.map +1 -1
- package/dist/store/slices/config.d.ts +3 -2
- package/dist/store/slices/config.d.ts.map +1 -1
- package/dist/store/slices/data.d.ts +3 -0
- package/dist/store/slices/data.d.ts.map +1 -1
- package/dist/store/wallets.d.ts +5 -4
- package/dist/store/wallets.d.ts.map +1 -1
- package/dist/types/config.d.ts +5 -5
- package/dist/types/config.d.ts.map +1 -1
- package/dist/utils/settings.d.ts +3 -2
- package/dist/utils/settings.d.ts.map +1 -1
- package/dist/utils/swap.d.ts +4 -3
- package/dist/utils/swap.d.ts.map +1 -1
- package/dist/utils/wallets.d.ts +1 -1
- package/dist/utils/wallets.d.ts.map +1 -1
- package/package.json +6 -6
- package/src/QueueManager.tsx +14 -14
- package/src/Wallets.tsx +35 -15
- package/src/Widget.tsx +11 -25
- package/src/components/AppRouter.tsx +3 -5
- package/src/components/AppRoutes.tsx +6 -39
- package/src/components/BlockchainList/BlockchainList.tsx +4 -4
- package/src/components/BlockchainsSection/BlockchainsSection.tsx +4 -4
- package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +5 -14
- package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.types.ts +0 -2
- package/src/components/ConfirmWalletsModal/WalletList.tsx +6 -5
- package/src/components/ConfirmWalletsModal/WalletList.type.ts +1 -4
- package/src/components/NoResult/NoResult.tsx +4 -1
- package/src/components/NotificationContent/NotificationContent.tsx +3 -2
- package/src/components/Quote/Quote.tsx +2 -4
- package/src/components/SwapDetails/SwapDetails.tsx +5 -6
- package/src/components/TokenList/TokenList.tsx +6 -7
- package/src/components/UpdateUrl.tsx +20 -14
- package/src/components/WidgetEvents.tsx +7 -4
- package/src/hooks/useConfirmSwap/useConfirmSwap.ts +5 -5
- package/src/hooks/useSwapInput.ts +6 -4
- package/src/hooks/useSyncStoresWithConfig.ts +20 -17
- package/src/hooks/useWalletList.ts +3 -5
- package/src/pages/ConfirmSwapPage.tsx +1 -9
- package/src/pages/Home.tsx +7 -7
- package/src/pages/LiquiditySourcePage.tsx +22 -31
- package/src/pages/SelectBlockchainPage.tsx +4 -5
- package/src/pages/SelectSwapItemsPage.tsx +5 -5
- package/src/pages/SettingsPage.tsx +8 -12
- package/src/pages/SwapDetailsPage.tsx +3 -3
- package/src/pages/WalletsPage.tsx +4 -11
- package/src/store/AppStore.tsx +35 -0
- package/src/store/app.ts +20 -5
- package/src/store/quote.ts +26 -22
- package/src/store/settings.ts +7 -4
- package/src/store/slices/config.ts +25 -15
- package/src/store/slices/data.ts +64 -29
- package/src/store/wallets.ts +11 -8
- package/src/types/config.ts +5 -5
- package/src/utils/settings.ts +8 -7
- package/src/utils/swap.ts +14 -16
- package/src/utils/wallets.ts +27 -15
- package/dist/store/meta.d.ts +0 -23
- package/dist/store/meta.d.ts.map +0 -1
- package/src/store/meta.ts +0 -39
|
@@ -6,7 +6,7 @@ import { SwapDetails } from '../components/SwapDetails';
|
|
|
6
6
|
import { SwapDetailsPlaceholder } from '../components/SwapDetails/SwapDetails.Placeholder';
|
|
7
7
|
import { navigationRoutes } from '../constants/navigationRoutes';
|
|
8
8
|
import { useNavigateBack } from '../hooks/useNavigateBack';
|
|
9
|
-
import {
|
|
9
|
+
import { useAppStore } from '../store/AppStore';
|
|
10
10
|
import { useUiStore } from '../store/ui';
|
|
11
11
|
import { getPendingSwaps } from '../utils/queue';
|
|
12
12
|
|
|
@@ -16,9 +16,9 @@ export function SwapDetailsPage() {
|
|
|
16
16
|
const pendingSwaps = getPendingSwaps(manager);
|
|
17
17
|
const requestId = useUiStore.use.selectedSwapRequestId();
|
|
18
18
|
const { navigateBackFrom } = useNavigateBack();
|
|
19
|
-
const {
|
|
19
|
+
const { fetchStatus: fetchMetaStatus } = useAppStore()();
|
|
20
20
|
|
|
21
|
-
const showSkeleton = loading ||
|
|
21
|
+
const showSkeleton = loading || fetchMetaStatus === 'loading';
|
|
22
22
|
|
|
23
23
|
const selectedSwap = pendingSwaps.find(
|
|
24
24
|
({ swap }) => swap.requestId === requestId
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { WidgetConfig } from '../types';
|
|
2
1
|
import type { WalletType } from '@rango-dev/wallets-shared';
|
|
3
2
|
|
|
4
3
|
import { i18n } from '@lingui/core';
|
|
@@ -10,15 +9,9 @@ import { WalletModal } from '../components/WalletModal';
|
|
|
10
9
|
import { navigationRoutes } from '../constants/navigationRoutes';
|
|
11
10
|
import { useNavigateBack } from '../hooks/useNavigateBack';
|
|
12
11
|
import { useWalletList } from '../hooks/useWalletList';
|
|
13
|
-
import {
|
|
12
|
+
import { useAppStore } from '../store/AppStore';
|
|
14
13
|
import { getContainer } from '../utils/common';
|
|
15
14
|
|
|
16
|
-
interface PropTypes {
|
|
17
|
-
supportedWallets: WidgetConfig['wallets'];
|
|
18
|
-
multiWallets: boolean;
|
|
19
|
-
config?: WidgetConfig;
|
|
20
|
-
}
|
|
21
|
-
|
|
22
15
|
const ListContainer = styled('div', {
|
|
23
16
|
display: 'grid',
|
|
24
17
|
gap: '$10',
|
|
@@ -35,7 +28,8 @@ const Container = styled('div', {
|
|
|
35
28
|
export const TIME_TO_CLOSE_MODAL = 3_000;
|
|
36
29
|
export const TIME_TO_IGNORE_MODAL = 300;
|
|
37
30
|
|
|
38
|
-
export function WalletsPage(
|
|
31
|
+
export function WalletsPage() {
|
|
32
|
+
const { config, fetchStatus: fetchMetaStatus } = useAppStore()();
|
|
39
33
|
const { navigateBackFrom } = useNavigateBack();
|
|
40
34
|
const [openModal, setOpenModal] = useState(false);
|
|
41
35
|
const [selectedWalletType, setSelectedWalletType] = useState<WalletType>('');
|
|
@@ -59,7 +53,6 @@ export function WalletsPage({ config }: PropTypes) {
|
|
|
59
53
|
},
|
|
60
54
|
});
|
|
61
55
|
|
|
62
|
-
const loadingMetaStatus = useMetaStore.use.loadingStatus();
|
|
63
56
|
const selectedWallet = list.find(
|
|
64
57
|
(wallet) => wallet.type === selectedWalletType
|
|
65
58
|
);
|
|
@@ -88,7 +81,7 @@ export function WalletsPage({ config }: PropTypes) {
|
|
|
88
81
|
onClick={(type) => {
|
|
89
82
|
void handleClick(type);
|
|
90
83
|
}}
|
|
91
|
-
isLoading={
|
|
84
|
+
isLoading={fetchMetaStatus === 'loading'}
|
|
92
85
|
/>
|
|
93
86
|
</Fragment>
|
|
94
87
|
);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { WidgetConfig } from '../types';
|
|
2
|
+
import type { PropsWithChildren } from 'react';
|
|
3
|
+
|
|
4
|
+
import React, { createContext, useContext, useRef } from 'react';
|
|
5
|
+
|
|
6
|
+
import { createAppStore } from './app';
|
|
7
|
+
|
|
8
|
+
type AppStore = ReturnType<typeof createAppStore>;
|
|
9
|
+
type AppStoreProviderProps = PropsWithChildren<{ config?: WidgetConfig }>;
|
|
10
|
+
|
|
11
|
+
export const AppStoreContext = createContext<AppStore | null>(null);
|
|
12
|
+
|
|
13
|
+
export function useAppStore() {
|
|
14
|
+
const store = useContext(AppStoreContext);
|
|
15
|
+
|
|
16
|
+
if (!store) {
|
|
17
|
+
throw new Error('Missing AppStoreContext.Provider in the tree');
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
return store;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function AppStoreProvider(props: AppStoreProviderProps) {
|
|
24
|
+
const appStoreRef = useRef<AppStore>();
|
|
25
|
+
|
|
26
|
+
if (!appStoreRef.current) {
|
|
27
|
+
appStoreRef.current = createAppStore(props.config);
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<AppStoreContext.Provider value={appStoreRef.current}>
|
|
32
|
+
{props.children}
|
|
33
|
+
</AppStoreContext.Provider>
|
|
34
|
+
);
|
|
35
|
+
}
|
package/src/store/app.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import type { ConfigSlice } from './slices/config';
|
|
2
2
|
import type { DataSlice } from './slices/data';
|
|
3
|
+
import type { WidgetConfig } from '../types';
|
|
4
|
+
import type { StateCreator } from 'zustand';
|
|
3
5
|
|
|
4
6
|
import { create } from 'zustand';
|
|
5
7
|
|
|
@@ -7,9 +9,22 @@ import createSelectors from './selectors';
|
|
|
7
9
|
import { createConfigSlice } from './slices/config';
|
|
8
10
|
import { createDataSlice } from './slices/data';
|
|
9
11
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
12
|
+
export type StateCreatorWithInitialData<
|
|
13
|
+
T extends Partial<WidgetConfig>,
|
|
14
|
+
R extends Record<string, unknown>,
|
|
15
|
+
V extends Record<string, unknown>
|
|
16
|
+
> = (
|
|
17
|
+
initialData: T | undefined,
|
|
18
|
+
...rest: Parameters<StateCreator<R, [], [], V>>
|
|
19
|
+
) => ReturnType<StateCreator<R, [], [], V>>;
|
|
14
20
|
|
|
15
|
-
export
|
|
21
|
+
export type AppStoreState = DataSlice & ConfigSlice;
|
|
22
|
+
|
|
23
|
+
export function createAppStore(initialData?: WidgetConfig) {
|
|
24
|
+
return createSelectors(
|
|
25
|
+
create<AppStoreState>()((...a) => ({
|
|
26
|
+
...createDataSlice(...a),
|
|
27
|
+
...createConfigSlice(initialData, ...a),
|
|
28
|
+
}))
|
|
29
|
+
);
|
|
30
|
+
}
|
package/src/store/quote.ts
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
import type { TokenWithBalance } from '../components/TokenList';
|
|
2
2
|
import type { Wallet } from '../types';
|
|
3
3
|
import type { PendingSwap } from '@rango-dev/queue-manager-rango-preset/dist/shared';
|
|
4
|
-
import type {
|
|
4
|
+
import type {
|
|
5
|
+
BestRouteResponse,
|
|
6
|
+
BlockchainMeta,
|
|
7
|
+
MetaResponse,
|
|
8
|
+
Token,
|
|
9
|
+
} from 'rango-sdk';
|
|
5
10
|
|
|
6
11
|
import BigNumber from 'bignumber.js';
|
|
7
12
|
import { create } from 'zustand';
|
|
@@ -12,7 +17,6 @@ import { isPositiveNumber } from '../utils/numbers';
|
|
|
12
17
|
import { createRetryQuote, getQuoteToTokenUsdPrice } from '../utils/quote';
|
|
13
18
|
import { calcOutputUsdValue } from '../utils/swap';
|
|
14
19
|
|
|
15
|
-
import { useMetaStore } from './meta';
|
|
16
20
|
import createSelectors from './selectors';
|
|
17
21
|
|
|
18
22
|
export const getUsdValue = (
|
|
@@ -23,6 +27,10 @@ export const getUsdValue = (
|
|
|
23
27
|
? new BigNumber(amount || ZERO).multipliedBy(token?.usdPrice || 0)
|
|
24
28
|
: null;
|
|
25
29
|
|
|
30
|
+
export type Meta = Pick<MetaResponse, 'blockchains' | 'tokens'>;
|
|
31
|
+
|
|
32
|
+
export type SetTokenParams = { token: Token; meta: Meta } | { token: null };
|
|
33
|
+
|
|
26
34
|
export interface QuoteState {
|
|
27
35
|
fromBlockchain: BlockchainMeta | null;
|
|
28
36
|
toBlockchain: BlockchainMeta | null;
|
|
@@ -41,12 +49,12 @@ export interface QuoteState {
|
|
|
41
49
|
setFromBlockchain: (chain: BlockchainMeta | null) => void;
|
|
42
50
|
setToBlockchain: (chian: BlockchainMeta | null) => void;
|
|
43
51
|
|
|
44
|
-
setFromToken: (
|
|
45
|
-
setToToken: (
|
|
52
|
+
setFromToken: (params: SetTokenParams) => void;
|
|
53
|
+
setToToken: (params: SetTokenParams) => void;
|
|
46
54
|
setInputAmount: (amount: string) => void;
|
|
47
55
|
quote: BestRouteResponse | null;
|
|
48
56
|
setQuote: (quote: BestRouteResponse | null) => void;
|
|
49
|
-
retry: (pendingSwap: PendingSwap) => void;
|
|
57
|
+
retry: (pendingSwap: PendingSwap, meta: Meta) => void;
|
|
50
58
|
switchFromAndTo: () => void;
|
|
51
59
|
setQuoteWalletConfirmed: (flag: boolean) => void;
|
|
52
60
|
setSelectedWallets: (wallets: Wallet[]) => void;
|
|
@@ -119,18 +127,17 @@ export const useQuoteStore = createSelectors(
|
|
|
119
127
|
};
|
|
120
128
|
});
|
|
121
129
|
},
|
|
122
|
-
setFromToken: (
|
|
123
|
-
const { blockchains } = useMetaStore.getState().meta;
|
|
130
|
+
setFromToken: (params) => {
|
|
124
131
|
return set((state) => ({
|
|
125
|
-
fromToken: token,
|
|
126
|
-
...(token && {
|
|
132
|
+
fromToken: params.token,
|
|
133
|
+
...(params.token && {
|
|
127
134
|
fromBlockchain:
|
|
128
|
-
blockchains.find(
|
|
129
|
-
(blockchain) => blockchain.name === token.blockchain
|
|
135
|
+
params.meta.blockchains.find(
|
|
136
|
+
(blockchain) => blockchain.name === params.token.blockchain
|
|
130
137
|
) ?? null,
|
|
131
138
|
}),
|
|
132
139
|
...(!!state.inputAmount && {
|
|
133
|
-
inputUsdValue: getUsdValue(token, state.inputAmount),
|
|
140
|
+
inputUsdValue: getUsdValue(params.token, state.inputAmount),
|
|
134
141
|
}),
|
|
135
142
|
}));
|
|
136
143
|
},
|
|
@@ -151,14 +158,13 @@ export const useQuoteStore = createSelectors(
|
|
|
151
158
|
};
|
|
152
159
|
});
|
|
153
160
|
},
|
|
154
|
-
setToToken: (
|
|
155
|
-
const { blockchains } = useMetaStore.getState().meta;
|
|
161
|
+
setToToken: (params) => {
|
|
156
162
|
return set(() => ({
|
|
157
|
-
toToken: token,
|
|
158
|
-
...(token && {
|
|
163
|
+
toToken: params.token,
|
|
164
|
+
...(params.token && {
|
|
159
165
|
toBlockchain:
|
|
160
|
-
blockchains.find(
|
|
161
|
-
(blockchain) => blockchain.name === token.blockchain
|
|
166
|
+
params.meta.blockchains.find(
|
|
167
|
+
(blockchain) => blockchain.name === params.token.blockchain
|
|
162
168
|
) ?? null,
|
|
163
169
|
}),
|
|
164
170
|
}));
|
|
@@ -176,9 +182,8 @@ export const useQuoteStore = createSelectors(
|
|
|
176
182
|
}),
|
|
177
183
|
}));
|
|
178
184
|
},
|
|
179
|
-
retry: (pendingSwap) => {
|
|
180
|
-
const { tokens, blockchains } =
|
|
181
|
-
|
|
185
|
+
retry: (pendingSwap, meta) => {
|
|
186
|
+
const { tokens, blockchains } = meta;
|
|
182
187
|
const {
|
|
183
188
|
fromBlockchain,
|
|
184
189
|
fromToken,
|
|
@@ -186,7 +191,6 @@ export const useQuoteStore = createSelectors(
|
|
|
186
191
|
toToken,
|
|
187
192
|
inputAmount,
|
|
188
193
|
} = createRetryQuote(pendingSwap, blockchains, tokens);
|
|
189
|
-
|
|
190
194
|
set({
|
|
191
195
|
fromBlockchain,
|
|
192
196
|
fromToken,
|
package/src/store/settings.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
import type { SwapperMeta } from 'rango-sdk';
|
|
2
|
+
|
|
1
3
|
import { type Language } from '@rango-dev/ui';
|
|
2
4
|
import { create } from 'zustand';
|
|
3
5
|
import { persist, subscribeWithSelector } from 'zustand/middleware';
|
|
@@ -7,7 +9,6 @@ import { DEFAULT_LANGUAGE } from '../constants/languages';
|
|
|
7
9
|
import { DEFAULT_SLIPPAGE } from '../constants/swapSettings';
|
|
8
10
|
import { removeDuplicateFrom } from '../utils/common';
|
|
9
11
|
|
|
10
|
-
import { useMetaStore } from './meta';
|
|
11
12
|
import createSelectors from './selectors';
|
|
12
13
|
|
|
13
14
|
export type ThemeMode = 'auto' | 'dark' | 'light';
|
|
@@ -31,7 +32,10 @@ export interface SettingsState {
|
|
|
31
32
|
toggleLiquiditySource: (name: string) => void;
|
|
32
33
|
setTheme: (theme: ThemeMode) => void;
|
|
33
34
|
setLanguage: (language: Language) => void;
|
|
34
|
-
toggleAllLiquiditySources: (
|
|
35
|
+
toggleAllLiquiditySources: (
|
|
36
|
+
swappers: SwapperMeta[],
|
|
37
|
+
shouldReset?: boolean
|
|
38
|
+
) => void;
|
|
35
39
|
setAffiliateRef: (affiliateRef: string | null) => void;
|
|
36
40
|
setAffiliatePercent: (affiliatePercent: number | null) => void;
|
|
37
41
|
setAffiliateWallets: (
|
|
@@ -102,12 +106,11 @@ export const useSettingsStore = createSelectors(
|
|
|
102
106
|
set(() => ({
|
|
103
107
|
affiliateWallets,
|
|
104
108
|
})),
|
|
105
|
-
toggleAllLiquiditySources: (shouldReset
|
|
109
|
+
toggleAllLiquiditySources: (swappers, shouldReset) =>
|
|
106
110
|
set((state) => {
|
|
107
111
|
if (shouldReset) {
|
|
108
112
|
return { disabledLiquiditySources: [] };
|
|
109
113
|
}
|
|
110
|
-
const { swappers } = useMetaStore.getState().meta;
|
|
111
114
|
const swappersGroup = removeDuplicateFrom(
|
|
112
115
|
swappers.map((swapper) => swapper.swapperGroup)
|
|
113
116
|
);
|
|
@@ -1,7 +1,11 @@
|
|
|
1
1
|
import type { WidgetConfig } from '../../types';
|
|
2
|
-
import type {
|
|
2
|
+
import type { StateCreatorWithInitialData } from '../app';
|
|
3
3
|
|
|
4
|
-
const
|
|
4
|
+
export const DEFAULT_CONFIG: WidgetConfig = {
|
|
5
|
+
apiKey: '',
|
|
6
|
+
multiWallets: true,
|
|
7
|
+
enableNewLiquiditySources: true,
|
|
8
|
+
};
|
|
5
9
|
|
|
6
10
|
export type ConfigSlice = {
|
|
7
11
|
config: WidgetConfig;
|
|
@@ -9,18 +13,24 @@ export type ConfigSlice = {
|
|
|
9
13
|
updateConfig: (config: WidgetConfig) => void;
|
|
10
14
|
};
|
|
11
15
|
|
|
12
|
-
export const createConfigSlice:
|
|
13
|
-
|
|
16
|
+
export const createConfigSlice: StateCreatorWithInitialData<
|
|
17
|
+
WidgetConfig,
|
|
18
|
+
ConfigSlice,
|
|
19
|
+
ConfigSlice
|
|
20
|
+
> = (initialData, set, get) => {
|
|
21
|
+
return {
|
|
22
|
+
config: { ...DEFAULT_CONFIG, ...initialData },
|
|
14
23
|
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
24
|
+
// Actions
|
|
25
|
+
updateConfig: (nextConfig: WidgetConfig) => {
|
|
26
|
+
const currentConfig = get().config;
|
|
18
27
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
}
|
|
28
|
+
set({
|
|
29
|
+
config: {
|
|
30
|
+
...currentConfig,
|
|
31
|
+
...nextConfig,
|
|
32
|
+
},
|
|
33
|
+
});
|
|
34
|
+
},
|
|
35
|
+
};
|
|
36
|
+
};
|
package/src/store/slices/data.ts
CHANGED
|
@@ -6,6 +6,7 @@ import type { StateCreator } from 'zustand';
|
|
|
6
6
|
|
|
7
7
|
import { httpService as sdk } from '../../services/httpService';
|
|
8
8
|
import { containsText } from '../../utils/common';
|
|
9
|
+
import { sortLiquiditySourcesByGroupTitle } from '../../utils/settings';
|
|
9
10
|
import { tokensAreEqual } from '../../utils/wallets';
|
|
10
11
|
|
|
11
12
|
type BlockchainOptions = {
|
|
@@ -18,15 +19,16 @@ type TokenOptions = {
|
|
|
18
19
|
blockchain?: string;
|
|
19
20
|
searchFor?: string;
|
|
20
21
|
};
|
|
21
|
-
|
|
22
|
+
export type FetchStatus = 'loading' | 'success' | 'failed';
|
|
22
23
|
export interface DataSlice {
|
|
23
24
|
_blockchains: BlockchainMeta[];
|
|
24
25
|
_tokens: Token[];
|
|
25
26
|
_popularTokens: Token[];
|
|
26
27
|
_swappers: SwapperMeta[];
|
|
27
|
-
|
|
28
|
+
fetchStatus: FetchStatus;
|
|
28
29
|
blockchains: (options?: BlockchainOptions) => BlockchainMeta[];
|
|
29
30
|
tokens: (options?: TokenOptions) => Token[];
|
|
31
|
+
swappers: () => SwapperMeta[];
|
|
30
32
|
isTokenPinned: (token: Token) => boolean;
|
|
31
33
|
|
|
32
34
|
fetch: () => Promise<void>;
|
|
@@ -43,7 +45,7 @@ export const createDataSlice: StateCreator<
|
|
|
43
45
|
_tokens: [],
|
|
44
46
|
_popularTokens: [],
|
|
45
47
|
_swappers: [],
|
|
46
|
-
|
|
48
|
+
fetchStatus: 'loading',
|
|
47
49
|
// Selectors
|
|
48
50
|
blockchains: (options) => {
|
|
49
51
|
const blockchainsFromState = get()._blockchains;
|
|
@@ -163,41 +165,74 @@ export const createDataSlice: StateCreator<
|
|
|
163
165
|
);
|
|
164
166
|
return pinned;
|
|
165
167
|
},
|
|
168
|
+
swappers: () => {
|
|
169
|
+
const {
|
|
170
|
+
config: { enableNewLiquiditySources, liquiditySources },
|
|
171
|
+
} = get();
|
|
172
|
+
|
|
173
|
+
/*
|
|
174
|
+
* If the enableNewLiquiditySources flag is set to true, we return all swappers that are not included in the config.
|
|
175
|
+
* Otherwise, we return all swappers that are included in the config.
|
|
176
|
+
*/
|
|
177
|
+
const swappers = get()._swappers.filter((swapper) => {
|
|
178
|
+
const swapperGroupIncludedInLiquiditySources = liquiditySources?.includes(
|
|
179
|
+
swapper.swapperGroup
|
|
180
|
+
);
|
|
181
|
+
|
|
182
|
+
const shouldExcludeLiquiditySources =
|
|
183
|
+
enableNewLiquiditySources ||
|
|
184
|
+
!liquiditySources ||
|
|
185
|
+
liquiditySources.length === 0;
|
|
186
|
+
|
|
187
|
+
return shouldExcludeLiquiditySources
|
|
188
|
+
? !swapperGroupIncludedInLiquiditySources
|
|
189
|
+
: swapperGroupIncludedInLiquiditySources;
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const sortedSwappers = swappers.sort(sortLiquiditySourcesByGroupTitle);
|
|
166
193
|
|
|
194
|
+
return sortedSwappers;
|
|
195
|
+
},
|
|
167
196
|
// Actions
|
|
168
197
|
fetch: async () => {
|
|
169
|
-
|
|
198
|
+
try {
|
|
199
|
+
const response = await sdk().getAllMetadata();
|
|
170
200
|
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
201
|
+
set({ fetchStatus: 'success' });
|
|
202
|
+
const blockchains: BlockchainMeta[] = [];
|
|
203
|
+
const tokens: Token[] = [];
|
|
204
|
+
const popularTokens: Token[] = response.popularTokens;
|
|
205
|
+
const swappers: SwapperMeta[] = response.swappers;
|
|
175
206
|
|
|
176
|
-
|
|
207
|
+
const blockchainsWithAtLeastOneToken = new Set();
|
|
177
208
|
|
|
178
|
-
|
|
179
|
-
|
|
209
|
+
response.tokens.forEach((token) => {
|
|
210
|
+
blockchainsWithAtLeastOneToken.add(token.blockchain);
|
|
180
211
|
|
|
181
|
-
|
|
182
|
-
|
|
212
|
+
tokens.push(token);
|
|
213
|
+
});
|
|
183
214
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
215
|
+
response.blockchains.forEach((blockchain) => {
|
|
216
|
+
if (
|
|
217
|
+
blockchain.enabled &&
|
|
218
|
+
blockchainsWithAtLeastOneToken.has(blockchain.name)
|
|
219
|
+
) {
|
|
220
|
+
blockchains.push(blockchain);
|
|
221
|
+
}
|
|
222
|
+
});
|
|
192
223
|
|
|
193
|
-
|
|
194
|
-
|
|
224
|
+
// Sort
|
|
225
|
+
blockchains.sort((a, b) => a.sort - b.sort);
|
|
195
226
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
227
|
+
set({
|
|
228
|
+
_blockchains: blockchains,
|
|
229
|
+
_tokens: tokens,
|
|
230
|
+
_popularTokens: popularTokens,
|
|
231
|
+
_swappers: swappers,
|
|
232
|
+
});
|
|
233
|
+
} catch (error) {
|
|
234
|
+
set({ fetchStatus: 'failed' });
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
202
237
|
},
|
|
203
238
|
});
|
package/src/store/wallets.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { Wallet } from '../types';
|
|
2
2
|
import type { WalletType } from '@rango-dev/wallets-shared';
|
|
3
|
+
import type { Token } from 'rango-sdk';
|
|
3
4
|
|
|
4
5
|
import { create } from 'zustand';
|
|
5
6
|
import { subscribeWithSelector } from 'zustand/middleware';
|
|
@@ -11,7 +12,6 @@ import {
|
|
|
11
12
|
resetConnectedWalletState,
|
|
12
13
|
} from '../utils/wallets';
|
|
13
14
|
|
|
14
|
-
import { useMetaStore } from './meta';
|
|
15
15
|
import createSelectors from './selectors';
|
|
16
16
|
|
|
17
17
|
export type TokenBalance = {
|
|
@@ -38,11 +38,15 @@ interface WalletsStore {
|
|
|
38
38
|
connectedWallets: ConnectedWallet[];
|
|
39
39
|
customDestination: string;
|
|
40
40
|
loading: boolean;
|
|
41
|
-
connectWallet: (accounts: Wallet[]) => void;
|
|
41
|
+
connectWallet: (accounts: Wallet[], tokens: Token[]) => void;
|
|
42
42
|
disconnectWallet: (walletType: WalletType) => void;
|
|
43
43
|
selectWallets: (wallets: { walletType: string; chain: string }[]) => void;
|
|
44
44
|
clearConnectedWallet: () => void;
|
|
45
|
-
getWalletsDetails: (
|
|
45
|
+
getWalletsDetails: (
|
|
46
|
+
accounts: Wallet[],
|
|
47
|
+
tokens: Token[],
|
|
48
|
+
shouldRetry?: boolean
|
|
49
|
+
) => void;
|
|
46
50
|
}
|
|
47
51
|
|
|
48
52
|
export const useWalletsStore = createSelectors(
|
|
@@ -51,7 +55,7 @@ export const useWalletsStore = createSelectors(
|
|
|
51
55
|
connectedWallets: [],
|
|
52
56
|
customDestination: '',
|
|
53
57
|
loading: false,
|
|
54
|
-
connectWallet: (accounts) => {
|
|
58
|
+
connectWallet: (accounts, tokens) => {
|
|
55
59
|
const getWalletsDetails = get().getWalletsDetails;
|
|
56
60
|
set((state) => ({
|
|
57
61
|
loading: true,
|
|
@@ -77,7 +81,7 @@ export const useWalletsStore = createSelectors(
|
|
|
77
81
|
})
|
|
78
82
|
),
|
|
79
83
|
}));
|
|
80
|
-
getWalletsDetails(accounts);
|
|
84
|
+
getWalletsDetails(accounts, tokens);
|
|
81
85
|
},
|
|
82
86
|
disconnectWallet: (walletType) => {
|
|
83
87
|
set((state) => {
|
|
@@ -135,9 +139,8 @@ export const useWalletsStore = createSelectors(
|
|
|
135
139
|
connectedWallets: [],
|
|
136
140
|
selectedWallets: [],
|
|
137
141
|
})),
|
|
138
|
-
getWalletsDetails: async (accounts, shouldRetry = true) => {
|
|
142
|
+
getWalletsDetails: async (accounts, tokens, shouldRetry = true) => {
|
|
139
143
|
const getWalletsDetails = get().getWalletsDetails;
|
|
140
|
-
const { tokens } = useMetaStore.getState().meta;
|
|
141
144
|
set((state) => ({
|
|
142
145
|
loading: true,
|
|
143
146
|
connectedWallets: state.connectedWallets.map((wallet) => {
|
|
@@ -173,7 +176,7 @@ export const useWalletsStore = createSelectors(
|
|
|
173
176
|
matchedAccount &&
|
|
174
177
|
shouldRetry
|
|
175
178
|
) {
|
|
176
|
-
getWalletsDetails([matchedAccount], false);
|
|
179
|
+
getWalletsDetails([matchedAccount], tokens, false);
|
|
177
180
|
}
|
|
178
181
|
return matchedAccount && retrievedBalanceAccount
|
|
179
182
|
? {
|
package/src/types/config.ts
CHANGED
|
@@ -61,7 +61,7 @@ export type WidgetTheme = {
|
|
|
61
61
|
* @property {{ [key: string]: string }} wallets - If you want to change the default affiliate wallet
|
|
62
62
|
* addresses, you can provide new values here. (Map of route blockchains to affiliate address)
|
|
63
63
|
*/
|
|
64
|
-
export type
|
|
64
|
+
export type WidgetAffiliate = {
|
|
65
65
|
ref?: string;
|
|
66
66
|
percent?: number;
|
|
67
67
|
wallets?: { [key: string]: string };
|
|
@@ -91,7 +91,7 @@ export type BlockchainAndTokenConfig = {
|
|
|
91
91
|
* and theme.
|
|
92
92
|
*
|
|
93
93
|
* @property {string} apiKey - The API key used to communicate with Rango API
|
|
94
|
-
* @property {
|
|
94
|
+
* @property {WidgetAffiliate} affiliate - If you want to charge users fee per transaction, you should
|
|
95
95
|
* pass `WidgetAffiliate` including affiliateRef, affiliatePercent, and affiliateWallets.
|
|
96
96
|
* @property {number} amount - The default input amount.
|
|
97
97
|
* @property {BlockchainAndTokenConfig} from - The `from` property is an optional property of type
|
|
@@ -121,13 +121,13 @@ export type BlockchainAndTokenConfig = {
|
|
|
121
121
|
* If `externalWallets` is `true`, you should add `WidgetWallets` to your app.
|
|
122
122
|
* @property {Asset} pinnedTokens - The `pinnedTokens` property is an optional array of `Asset` objects that
|
|
123
123
|
* you could use to pin tokens of your choice to the top of the token list.
|
|
124
|
-
* @property {boolean}
|
|
124
|
+
* @property {boolean} enableNewLiquiditySources - The `enableNewLiquiditySources` property is a boolean value that when you
|
|
125
125
|
* set it to true, whenever a new liquidity source is added, it will be added to your list as well.
|
|
126
126
|
*/
|
|
127
127
|
export type WidgetConfig = {
|
|
128
128
|
apiKey: string;
|
|
129
129
|
walletConnectProjectId?: string;
|
|
130
|
-
affiliate?:
|
|
130
|
+
affiliate?: WidgetAffiliate;
|
|
131
131
|
amount?: number;
|
|
132
132
|
from?: BlockchainAndTokenConfig;
|
|
133
133
|
to?: BlockchainAndTokenConfig;
|
|
@@ -139,5 +139,5 @@ export type WidgetConfig = {
|
|
|
139
139
|
theme?: WidgetTheme;
|
|
140
140
|
externalWallets?: boolean;
|
|
141
141
|
pinnedTokens?: Asset[];
|
|
142
|
-
|
|
142
|
+
enableNewLiquiditySources?: boolean;
|
|
143
143
|
};
|
package/src/utils/settings.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { SwapperMeta } from 'rango-sdk';
|
|
2
|
+
|
|
2
3
|
import { useSettingsStore } from '../store/settings';
|
|
3
4
|
|
|
4
5
|
import { removeDuplicateFrom } from './common';
|
|
@@ -14,11 +15,11 @@ export type UniqueSwappersGroupType = {
|
|
|
14
15
|
};
|
|
15
16
|
|
|
16
17
|
export function getUniqueSwappersGroups(
|
|
17
|
-
|
|
18
|
+
swappers: SwapperMeta[]
|
|
18
19
|
): UniqueSwappersGroupType[] {
|
|
19
|
-
const swappers = useMetaStore.use.meta().swappers;
|
|
20
20
|
const disabledLiquiditySources =
|
|
21
21
|
useSettingsStore.use.disabledLiquiditySources();
|
|
22
|
+
const supportedSwappers = swappers.map((swapper) => swapper.swapperGroup);
|
|
22
23
|
|
|
23
24
|
const uniqueSupportedSwappersGroups: Array<UniqueSwappersGroupType> = [];
|
|
24
25
|
|
|
@@ -57,14 +58,14 @@ export function getUniqueSwappersGroups(
|
|
|
57
58
|
}
|
|
58
59
|
|
|
59
60
|
export function sortLiquiditySourcesByGroupTitle(
|
|
60
|
-
a:
|
|
61
|
-
b:
|
|
61
|
+
a: SwapperMeta,
|
|
62
|
+
b: SwapperMeta
|
|
62
63
|
) {
|
|
63
|
-
if (a.
|
|
64
|
+
if (a.swapperGroup < b.swapperGroup) {
|
|
64
65
|
return -1;
|
|
65
66
|
}
|
|
66
67
|
|
|
67
|
-
if (a.
|
|
68
|
+
if (a.swapperGroup > b.swapperGroup) {
|
|
68
69
|
return 1;
|
|
69
70
|
}
|
|
70
71
|
|