@rango-dev/widget-embedded 0.29.1-next.11 → 0.29.1-next.13
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.map +1 -1
- package/dist/hooks/useBootstrap/useBootstrap.d.ts.map +1 -1
- package/dist/hooks/useSubscribeToWidgetEvents/useSubscribeToWidgetEvents.d.ts.map +1 -1
- package/dist/hooks/useSyncStoresWithConfig.d.ts.map +1 -1
- package/dist/hooks/useWalletList.d.ts +2 -1
- package/dist/hooks/useWalletList.d.ts.map +1 -1
- package/dist/hooks/useWidgetEvents/index.d.ts +2 -0
- package/dist/hooks/useWidgetEvents/index.d.ts.map +1 -0
- package/dist/hooks/useWidgetEvents/useWidgetEvents.d.ts +3 -0
- package/dist/hooks/useWidgetEvents/useWidgetEvents.d.ts.map +1 -0
- package/dist/index.d.ts +16 -4
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -4
- package/dist/services/eventEmitter.d.ts +4 -0
- package/dist/services/eventEmitter.d.ts.map +1 -0
- package/dist/store/quote.d.ts +2 -1
- package/dist/store/quote.d.ts.map +1 -1
- package/dist/store/wallets.d.ts.map +1 -1
- package/dist/types/event.d.ts +68 -0
- package/dist/types/event.d.ts.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/widget-embedded.build.json +1 -1
- package/package.json +3 -3
- package/src/QueueManager.tsx +4 -0
- package/src/hooks/useBootstrap/useBootstrap.ts +4 -0
- package/src/hooks/useSubscribeToWidgetEvents/useSubscribeToWidgetEvents.ts +8 -9
- package/src/hooks/useSyncStoresWithConfig.ts +3 -1
- package/src/hooks/useWalletList.ts +6 -3
- package/src/hooks/useWidgetEvents/index.ts +1 -0
- package/src/hooks/useWidgetEvents/useWidgetEvents.ts +9 -0
- package/src/index.ts +26 -2
- package/src/services/eventEmitter.ts +11 -0
- package/src/store/quote.ts +63 -1
- package/src/store/wallets.ts +194 -173
- package/src/types/event.ts +89 -0
- package/src/types/index.ts +1 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rango-dev/widget-embedded",
|
|
3
|
-
"version": "0.29.1-next.
|
|
3
|
+
"version": "0.29.1-next.13",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"@rango-dev/logging-core": "^0.4.0",
|
|
28
28
|
"@rango-dev/provider-all": "^0.34.1-next.6",
|
|
29
29
|
"@rango-dev/queue-manager-core": "^0.26.0",
|
|
30
|
-
"@rango-dev/queue-manager-rango-preset": "^0.34.1-next.
|
|
30
|
+
"@rango-dev/queue-manager-rango-preset": "^0.34.1-next.6",
|
|
31
31
|
"@rango-dev/queue-manager-react": "^0.26.0",
|
|
32
32
|
"@rango-dev/signer-solana": "^0.29.1-next.1",
|
|
33
33
|
"@rango-dev/ui": "^0.35.1-next.6",
|
|
@@ -53,4 +53,4 @@
|
|
|
53
53
|
"publishConfig": {
|
|
54
54
|
"access": "public"
|
|
55
55
|
}
|
|
56
|
-
}
|
|
56
|
+
}
|
package/src/QueueManager.tsx
CHANGED
|
@@ -12,6 +12,7 @@ import { convertEvmBlockchainMetaToEvmChainInfo } from '@rango-dev/wallets-share
|
|
|
12
12
|
import { isEvmBlockchain } from 'rango-types';
|
|
13
13
|
import React, { useMemo } from 'react';
|
|
14
14
|
|
|
15
|
+
import { eventEmitter } from './services/eventEmitter';
|
|
15
16
|
import { useAppStore } from './store/AppStore';
|
|
16
17
|
import { useUiStore } from './store/ui';
|
|
17
18
|
import { useWalletsStore } from './store/wallets';
|
|
@@ -32,6 +33,9 @@ function QueueManager(props: PropsWithChildren<{ apiKey?: string }>) {
|
|
|
32
33
|
return makeQueueDefinition({
|
|
33
34
|
API_KEY: props.apiKey || getConfig('API_KEY'),
|
|
34
35
|
BASE_URL: getConfig('BASE_URL'),
|
|
36
|
+
emitter: {
|
|
37
|
+
emit: eventEmitter.emit,
|
|
38
|
+
},
|
|
35
39
|
});
|
|
36
40
|
}, [props.apiKey]);
|
|
37
41
|
|
|
@@ -10,6 +10,7 @@ import { WidgetContext } from '../../containers/Wallets';
|
|
|
10
10
|
import { globalFont } from '../../globalStyles';
|
|
11
11
|
import { useAppStore } from '../../store/AppStore';
|
|
12
12
|
import { useNotificationStore } from '../../store/notification';
|
|
13
|
+
import { unsubscribeQuoteStore } from '../../store/quote';
|
|
13
14
|
import { tabManager } from '../../store/ui';
|
|
14
15
|
import { useFetchApiConfig } from '../useFetchApiConfig';
|
|
15
16
|
import { useForceAutoConnect } from '../useForceAutoConnect';
|
|
@@ -32,6 +33,9 @@ export function useBootstrap() {
|
|
|
32
33
|
|
|
33
34
|
const { fetchApiConfig } = useFetchApiConfig();
|
|
34
35
|
|
|
36
|
+
// Unsubscribe QuoteStore listeners
|
|
37
|
+
useEffect(() => () => unsubscribeQuoteStore(), []);
|
|
38
|
+
|
|
35
39
|
// At the moment, we only detect the disconnection of EVM wallets.
|
|
36
40
|
useQueueManager({
|
|
37
41
|
lastConnectedWallet: lastConnectedWalletWithNetwork,
|
|
@@ -2,14 +2,14 @@ import type { RouteEventData, StepEventData } from '../..';
|
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
isApprovalTX,
|
|
5
|
-
MainEvents,
|
|
6
5
|
RouteEventType,
|
|
7
6
|
StepEventType,
|
|
8
7
|
StepExecutionEventStatus,
|
|
9
|
-
|
|
8
|
+
WidgetEvents,
|
|
10
9
|
} from '@rango-dev/queue-manager-rango-preset';
|
|
11
10
|
import { useEffect } from 'react';
|
|
12
11
|
|
|
12
|
+
import { eventEmitter } from '../../services/eventEmitter';
|
|
13
13
|
import { useAppStore } from '../../store/AppStore';
|
|
14
14
|
import { useNotificationStore } from '../../store/notification';
|
|
15
15
|
import { useWalletsStore } from '../../store/wallets';
|
|
@@ -19,7 +19,6 @@ export function useSubscribeToWidgetEvents() {
|
|
|
19
19
|
const getWalletsDetails = useWalletsStore.use.getWalletsDetails();
|
|
20
20
|
const setNotification = useNotificationStore.use.setNotification();
|
|
21
21
|
const { findToken } = useAppStore();
|
|
22
|
-
const widgetEvents = useEvents();
|
|
23
22
|
|
|
24
23
|
useEffect(() => {
|
|
25
24
|
const handleStepEvent = (widgetEvent: StepEventData) => {
|
|
@@ -46,10 +45,10 @@ export function useSubscribeToWidgetEvents() {
|
|
|
46
45
|
|
|
47
46
|
setNotification(event, route);
|
|
48
47
|
};
|
|
49
|
-
|
|
48
|
+
eventEmitter.on(WidgetEvents.StepEvent, handleStepEvent);
|
|
50
49
|
|
|
51
|
-
return () =>
|
|
52
|
-
}, [
|
|
50
|
+
return () => eventEmitter.off(WidgetEvents.StepEvent, handleStepEvent);
|
|
51
|
+
}, [eventEmitter, connectedWallets.length]);
|
|
53
52
|
|
|
54
53
|
useEffect(() => {
|
|
55
54
|
const handleRouteEvent = (widgetEvent: RouteEventData) => {
|
|
@@ -62,8 +61,8 @@ export function useSubscribeToWidgetEvents() {
|
|
|
62
61
|
setNotification(event, route);
|
|
63
62
|
}
|
|
64
63
|
};
|
|
65
|
-
|
|
64
|
+
eventEmitter.on(WidgetEvents.RouteEvent, handleRouteEvent);
|
|
66
65
|
|
|
67
|
-
return () =>
|
|
68
|
-
}, [
|
|
66
|
+
return () => eventEmitter.off(WidgetEvents.RouteEvent, handleRouteEvent);
|
|
67
|
+
}, [eventEmitter, connectedWallets.length]);
|
|
69
68
|
}
|
|
@@ -42,7 +42,9 @@ export function useSyncStoresWithConfig() {
|
|
|
42
42
|
const prevConfigToBlockchain = useRef<string | undefined>(undefined);
|
|
43
43
|
|
|
44
44
|
useEffect(() => {
|
|
45
|
-
|
|
45
|
+
if (typeof config.amount !== 'undefined') {
|
|
46
|
+
setInputAmount(config.amount.toString());
|
|
47
|
+
}
|
|
46
48
|
}, [config?.amount]);
|
|
47
49
|
|
|
48
50
|
useEffect(() => {
|
|
@@ -28,6 +28,7 @@ interface Params {
|
|
|
28
28
|
chain?: string;
|
|
29
29
|
onBeforeConnect?: (walletType: string) => void;
|
|
30
30
|
onConnect?: (walletType: string) => void;
|
|
31
|
+
onError?: (error?: string) => void;
|
|
31
32
|
}
|
|
32
33
|
|
|
33
34
|
/**
|
|
@@ -36,7 +37,7 @@ interface Params {
|
|
|
36
37
|
* you can use this list whenever you need to show the list of wallets and needed callbacks
|
|
37
38
|
*/
|
|
38
39
|
export function useWalletList(params: Params) {
|
|
39
|
-
const { chain, onBeforeConnect, onConnect } = params;
|
|
40
|
+
const { chain, onBeforeConnect, onConnect, onError } = params;
|
|
40
41
|
const { config } = useAppStore();
|
|
41
42
|
const { state, disconnect, getWalletInfo, connect } = useWallets();
|
|
42
43
|
const { connectedWallets } = useWalletsStore();
|
|
@@ -90,8 +91,10 @@ export function useWalletList(params: Params) {
|
|
|
90
91
|
await connect(type, undefined, namespaces);
|
|
91
92
|
onConnect?.(type);
|
|
92
93
|
}
|
|
93
|
-
|
|
94
|
-
|
|
94
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
95
|
+
} catch (e: any) {
|
|
96
|
+
onError?.(e?.message);
|
|
97
|
+
setError('Error: ' + e?.message);
|
|
95
98
|
}
|
|
96
99
|
};
|
|
97
100
|
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useWidgetEvents } from './useWidgetEvents';
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import type { WidgetEventEmitter } from '../../types';
|
|
2
|
+
|
|
3
|
+
import { eventEmitter } from '../../services/eventEmitter';
|
|
4
|
+
|
|
5
|
+
export function useWidgetEvents(): WidgetEventEmitter {
|
|
6
|
+
// To be exported for Dapps
|
|
7
|
+
const { on, off } = eventEmitter;
|
|
8
|
+
return { on, off };
|
|
9
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -2,7 +2,10 @@ import type { WidgetProps } from './containers/Widget';
|
|
|
2
2
|
import type { ConnectedWallet } from './store/wallets';
|
|
3
3
|
import type {
|
|
4
4
|
BlockchainAndTokenConfig,
|
|
5
|
+
QuoteEventData,
|
|
5
6
|
Tokens,
|
|
7
|
+
WalletEventData,
|
|
8
|
+
WalletInfoWithNamespaces,
|
|
6
9
|
WidgetColors,
|
|
7
10
|
WidgetColorsKeys,
|
|
8
11
|
WidgetConfig,
|
|
@@ -43,12 +46,10 @@ import type { PendingSwap, PendingSwapStep } from 'rango-types';
|
|
|
43
46
|
|
|
44
47
|
import {
|
|
45
48
|
EventSeverity,
|
|
46
|
-
MainEvents,
|
|
47
49
|
RouteEventType,
|
|
48
50
|
StepEventType,
|
|
49
51
|
StepExecutionBlockedEventStatus,
|
|
50
52
|
StepExecutionEventStatus,
|
|
51
|
-
useEvents as useWidgetEvents,
|
|
52
53
|
} from '@rango-dev/queue-manager-rango-preset';
|
|
53
54
|
import {
|
|
54
55
|
readAccountAddress,
|
|
@@ -64,6 +65,15 @@ import { WidgetWallets } from './containers/Wallets';
|
|
|
64
65
|
import { Widget } from './containers/Widget';
|
|
65
66
|
import { useWidget } from './containers/WidgetInfo';
|
|
66
67
|
import { WidgetProvider } from './containers/WidgetProvider';
|
|
68
|
+
import { useWalletList } from './hooks/useWalletList';
|
|
69
|
+
import { useWidgetEvents } from './hooks/useWidgetEvents';
|
|
70
|
+
import { widgetEventEmitter } from './services/eventEmitter';
|
|
71
|
+
import {
|
|
72
|
+
WidgetEvents as MainEvents,
|
|
73
|
+
QuoteEventTypes,
|
|
74
|
+
WalletEventTypes,
|
|
75
|
+
WidgetEvents,
|
|
76
|
+
} from './types';
|
|
67
77
|
import { customizedThemeTokens } from './utils/ui';
|
|
68
78
|
|
|
69
79
|
export type {
|
|
@@ -96,6 +106,9 @@ export type {
|
|
|
96
106
|
ConnectedWallet,
|
|
97
107
|
Tokens,
|
|
98
108
|
WidgetVariant,
|
|
109
|
+
WalletEventData,
|
|
110
|
+
QuoteEventData,
|
|
111
|
+
WalletInfoWithNamespaces,
|
|
99
112
|
};
|
|
100
113
|
export {
|
|
101
114
|
Widget,
|
|
@@ -106,9 +119,20 @@ export {
|
|
|
106
119
|
WidgetProvider,
|
|
107
120
|
useWidget,
|
|
108
121
|
useWallets,
|
|
122
|
+
/**
|
|
123
|
+
* @deprecated Use `widgetEventEmitter` instead. This hook will be removed in future versions.
|
|
124
|
+
*/
|
|
109
125
|
useWidgetEvents,
|
|
126
|
+
useWalletList,
|
|
110
127
|
customizedThemeTokens,
|
|
128
|
+
widgetEventEmitter,
|
|
129
|
+
WidgetEvents,
|
|
130
|
+
/**
|
|
131
|
+
* @deprecated Use `WidgetEvents` instead. This enum will be removed in future versions.
|
|
132
|
+
*/
|
|
111
133
|
MainEvents,
|
|
134
|
+
QuoteEventTypes,
|
|
135
|
+
WalletEventTypes,
|
|
112
136
|
RouteEventType,
|
|
113
137
|
StepEventType,
|
|
114
138
|
StepExecutionEventStatus,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Events, WidgetEventEmitter } from '../types';
|
|
2
|
+
|
|
3
|
+
import mitt from 'mitt';
|
|
4
|
+
|
|
5
|
+
export const eventEmitter = mitt<Events>();
|
|
6
|
+
|
|
7
|
+
// To be exported for Dapps
|
|
8
|
+
export const widgetEventEmitter: WidgetEventEmitter = {
|
|
9
|
+
on: eventEmitter.on,
|
|
10
|
+
off: eventEmitter.off,
|
|
11
|
+
};
|
package/src/store/quote.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import type { QuoteError, QuoteWarning, SelectedQuote, Wallet } from '../types';
|
|
2
1
|
import type {
|
|
3
2
|
BlockchainMeta,
|
|
4
3
|
MetaResponse,
|
|
@@ -12,6 +11,15 @@ import { create } from 'zustand';
|
|
|
12
11
|
import { subscribeWithSelector } from 'zustand/middleware';
|
|
13
12
|
|
|
14
13
|
import { ZERO } from '../constants/numbers';
|
|
14
|
+
import { eventEmitter } from '../services/eventEmitter';
|
|
15
|
+
import {
|
|
16
|
+
type QuoteError,
|
|
17
|
+
QuoteEventTypes,
|
|
18
|
+
type QuoteWarning,
|
|
19
|
+
type SelectedQuote,
|
|
20
|
+
type Wallet,
|
|
21
|
+
WidgetEvents,
|
|
22
|
+
} from '../types';
|
|
15
23
|
import { isPositiveNumber } from '../utils/numbers';
|
|
16
24
|
import { getQuoteToTokenUsdPrice } from '../utils/quote';
|
|
17
25
|
import { calcOutputUsdValue } from '../utils/swap';
|
|
@@ -289,3 +297,57 @@ export const useQuoteStore = createSelectors(
|
|
|
289
297
|
}))
|
|
290
298
|
)
|
|
291
299
|
);
|
|
300
|
+
|
|
301
|
+
export const unsubscribeQuoteStore = useQuoteStore.subscribe(
|
|
302
|
+
(selectedState, previousSelectedState) => {
|
|
303
|
+
if (
|
|
304
|
+
selectedState.fromBlockchain !== previousSelectedState.fromBlockchain ||
|
|
305
|
+
selectedState.fromToken !== previousSelectedState.fromToken ||
|
|
306
|
+
selectedState.toBlockchain !== previousSelectedState.toBlockchain ||
|
|
307
|
+
selectedState.toToken !== previousSelectedState.toToken ||
|
|
308
|
+
selectedState.inputAmount !== previousSelectedState.inputAmount
|
|
309
|
+
) {
|
|
310
|
+
// useEffect hook can not be used in Zustand subscribe
|
|
311
|
+
eventEmitter.emit(WidgetEvents.QuoteEvent, {
|
|
312
|
+
type: QuoteEventTypes.QUOTE_INPUT_UPDATE,
|
|
313
|
+
payload: {
|
|
314
|
+
fromBlockchain: selectedState.fromBlockchain?.name,
|
|
315
|
+
toBlockchain: selectedState.toBlockchain?.name,
|
|
316
|
+
fromToken: selectedState.fromToken
|
|
317
|
+
? {
|
|
318
|
+
symbol: selectedState.fromToken.symbol,
|
|
319
|
+
name: selectedState.fromToken.name,
|
|
320
|
+
address: selectedState.fromToken.address,
|
|
321
|
+
}
|
|
322
|
+
: undefined,
|
|
323
|
+
toToken: selectedState.toToken
|
|
324
|
+
? {
|
|
325
|
+
symbol: selectedState.toToken.symbol,
|
|
326
|
+
name: selectedState.toToken.name,
|
|
327
|
+
address: selectedState.toToken.address,
|
|
328
|
+
}
|
|
329
|
+
: undefined,
|
|
330
|
+
requestAmount: selectedState.inputAmount,
|
|
331
|
+
},
|
|
332
|
+
});
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
if (
|
|
336
|
+
selectedState.selectedQuote?.requestId !==
|
|
337
|
+
previousSelectedState.selectedQuote?.requestId
|
|
338
|
+
) {
|
|
339
|
+
eventEmitter.emit(WidgetEvents.QuoteEvent, {
|
|
340
|
+
type: QuoteEventTypes.QUOTE_OUTPUT_UPDATE,
|
|
341
|
+
payload: selectedState.selectedQuote
|
|
342
|
+
? {
|
|
343
|
+
requestAmount: selectedState.selectedQuote.requestAmount,
|
|
344
|
+
swaps: selectedState.selectedQuote.swaps,
|
|
345
|
+
outputAmount: selectedState.selectedQuote.outputAmount,
|
|
346
|
+
resultType: selectedState.selectedQuote.resultType,
|
|
347
|
+
tags: selectedState.selectedQuote.tags,
|
|
348
|
+
}
|
|
349
|
+
: null,
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
);
|