@rango-dev/widget-embedded 0.29.1-next.11 → 0.29.1-next.12

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.
Files changed (35) hide show
  1. package/dist/QueueManager.d.ts.map +1 -1
  2. package/dist/hooks/useBootstrap/useBootstrap.d.ts.map +1 -1
  3. package/dist/hooks/useSubscribeToWidgetEvents/useSubscribeToWidgetEvents.d.ts.map +1 -1
  4. package/dist/hooks/useSyncStoresWithConfig.d.ts.map +1 -1
  5. package/dist/hooks/useWidgetEvents/index.d.ts +2 -0
  6. package/dist/hooks/useWidgetEvents/index.d.ts.map +1 -0
  7. package/dist/hooks/useWidgetEvents/useWidgetEvents.d.ts +3 -0
  8. package/dist/hooks/useWidgetEvents/useWidgetEvents.d.ts.map +1 -0
  9. package/dist/index.d.ts +15 -4
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/index.js +2 -2
  12. package/dist/index.js.map +4 -4
  13. package/dist/services/eventEmitter.d.ts +4 -0
  14. package/dist/services/eventEmitter.d.ts.map +1 -0
  15. package/dist/store/quote.d.ts +2 -1
  16. package/dist/store/quote.d.ts.map +1 -1
  17. package/dist/store/wallets.d.ts.map +1 -1
  18. package/dist/types/event.d.ts +68 -0
  19. package/dist/types/event.d.ts.map +1 -0
  20. package/dist/types/index.d.ts +1 -0
  21. package/dist/types/index.d.ts.map +1 -1
  22. package/dist/widget-embedded.build.json +1 -1
  23. package/package.json +2 -2
  24. package/src/QueueManager.tsx +4 -0
  25. package/src/hooks/useBootstrap/useBootstrap.ts +4 -0
  26. package/src/hooks/useSubscribeToWidgetEvents/useSubscribeToWidgetEvents.ts +8 -9
  27. package/src/hooks/useSyncStoresWithConfig.ts +3 -1
  28. package/src/hooks/useWidgetEvents/index.ts +1 -0
  29. package/src/hooks/useWidgetEvents/useWidgetEvents.ts +9 -0
  30. package/src/index.ts +22 -2
  31. package/src/services/eventEmitter.ts +11 -0
  32. package/src/store/quote.ts +63 -1
  33. package/src/store/wallets.ts +194 -173
  34. package/src/types/event.ts +89 -0
  35. 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.11",
3
+ "version": "0.29.1-next.12",
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.5",
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",
@@ -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
- useEvents,
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
- widgetEvents.on(MainEvents.StepEvent, handleStepEvent);
48
+ eventEmitter.on(WidgetEvents.StepEvent, handleStepEvent);
50
49
 
51
- return () => widgetEvents.off(MainEvents.StepEvent, handleStepEvent);
52
- }, [widgetEvents, connectedWallets.length]);
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
- widgetEvents.on(MainEvents.RouteEvent, handleRouteEvent);
64
+ eventEmitter.on(WidgetEvents.RouteEvent, handleRouteEvent);
66
65
 
67
- return () => widgetEvents.off(MainEvents.RouteEvent, handleRouteEvent);
68
- }, [widgetEvents, connectedWallets.length]);
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
- setInputAmount(config?.amount?.toString() || '');
45
+ if (typeof config.amount !== 'undefined') {
46
+ setInputAmount(config.amount.toString());
47
+ }
46
48
  }, [config?.amount]);
47
49
 
48
50
  useEffect(() => {
@@ -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,9 @@ 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,
6
8
  WidgetColors,
7
9
  WidgetColorsKeys,
8
10
  WidgetConfig,
@@ -43,12 +45,10 @@ import type { PendingSwap, PendingSwapStep } from 'rango-types';
43
45
 
44
46
  import {
45
47
  EventSeverity,
46
- MainEvents,
47
48
  RouteEventType,
48
49
  StepEventType,
49
50
  StepExecutionBlockedEventStatus,
50
51
  StepExecutionEventStatus,
51
- useEvents as useWidgetEvents,
52
52
  } from '@rango-dev/queue-manager-rango-preset';
53
53
  import {
54
54
  readAccountAddress,
@@ -64,6 +64,14 @@ import { WidgetWallets } from './containers/Wallets';
64
64
  import { Widget } from './containers/Widget';
65
65
  import { useWidget } from './containers/WidgetInfo';
66
66
  import { WidgetProvider } from './containers/WidgetProvider';
67
+ import { useWidgetEvents } from './hooks/useWidgetEvents';
68
+ import { widgetEventEmitter } from './services/eventEmitter';
69
+ import {
70
+ WidgetEvents as MainEvents,
71
+ QuoteEventTypes,
72
+ WalletEventTypes,
73
+ WidgetEvents,
74
+ } from './types';
67
75
  import { customizedThemeTokens } from './utils/ui';
68
76
 
69
77
  export type {
@@ -96,6 +104,8 @@ export type {
96
104
  ConnectedWallet,
97
105
  Tokens,
98
106
  WidgetVariant,
107
+ WalletEventData,
108
+ QuoteEventData,
99
109
  };
100
110
  export {
101
111
  Widget,
@@ -106,9 +116,19 @@ export {
106
116
  WidgetProvider,
107
117
  useWidget,
108
118
  useWallets,
119
+ /**
120
+ * @deprecated Use `widgetEventEmitter` instead. This hook will be removed in future versions.
121
+ */
109
122
  useWidgetEvents,
110
123
  customizedThemeTokens,
124
+ widgetEventEmitter,
125
+ WidgetEvents,
126
+ /**
127
+ * @deprecated Use `WidgetEvents` instead. This enum will be removed in future versions.
128
+ */
111
129
  MainEvents,
130
+ QuoteEventTypes,
131
+ WalletEventTypes,
112
132
  RouteEventType,
113
133
  StepEventType,
114
134
  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
+ };
@@ -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
+ );