@rango-dev/widget-embedded 0.0.0-experimental-55d181be-20260613 → 0.0.0-experimental-b1308db7-20260613

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 (51) hide show
  1. package/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.d.ts.map +1 -1
  2. package/dist/components/CustomDestination/CustomDestination.d.ts.map +1 -1
  3. package/dist/components/Quote/QuoteCostDetails.d.ts.map +1 -1
  4. package/dist/components/Slippage/Slippage.d.ts.map +1 -1
  5. package/dist/components/SwapDetails/SwapDetails.d.ts.map +1 -1
  6. package/dist/containers/Settings/Lists.d.ts.map +1 -1
  7. package/dist/containers/Wallets/useUpdates.d.ts.map +1 -1
  8. package/dist/hooks/useSwapInput.d.ts.map +1 -1
  9. package/dist/hooks/useSyncNotifications/useSyncNotifications.d.ts.map +1 -1
  10. package/dist/hooks/useWalletList.d.ts.map +1 -1
  11. package/dist/index.js +2 -2
  12. package/dist/index.js.map +4 -4
  13. package/dist/pages/ConfirmSwapPage.d.ts.map +1 -1
  14. package/dist/pages/Home.d.ts.map +1 -1
  15. package/dist/pages/LiquiditySourcePage.d.ts.map +1 -1
  16. package/dist/pages/SelectBlockchainPage.d.ts.map +1 -1
  17. package/dist/pages/SelectSwapItemPage/SelectSwapItemsPage.d.ts.map +1 -1
  18. package/dist/pages/SwapDetailsPage.d.ts.map +1 -1
  19. package/dist/services/walletConnectionSource.d.ts +4 -0
  20. package/dist/services/walletConnectionSource.d.ts.map +1 -0
  21. package/dist/store/slices/wallets.d.ts +3 -1
  22. package/dist/store/slices/wallets.d.ts.map +1 -1
  23. package/dist/types/event.d.ts +135 -6
  24. package/dist/types/event.d.ts.map +1 -1
  25. package/dist/utils/eventPayloads.d.ts +8 -0
  26. package/dist/utils/eventPayloads.d.ts.map +1 -0
  27. package/dist/utils/events.d.ts +16 -5
  28. package/dist/utils/events.d.ts.map +1 -1
  29. package/dist/widget-embedded.build.json +1 -1
  30. package/package.json +1 -1
  31. package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +53 -2
  32. package/src/components/CustomDestination/CustomDestination.tsx +18 -0
  33. package/src/components/Quote/QuoteCostDetails.tsx +8 -0
  34. package/src/components/Slippage/Slippage.tsx +21 -0
  35. package/src/components/SwapDetails/SwapDetails.tsx +6 -0
  36. package/src/containers/Settings/Lists.tsx +13 -1
  37. package/src/containers/Wallets/useUpdates.ts +3 -1
  38. package/src/hooks/useSwapInput.ts +46 -1
  39. package/src/hooks/useSyncNotifications/useSyncNotifications.ts +26 -1
  40. package/src/hooks/useWalletList.ts +34 -0
  41. package/src/pages/ConfirmSwapPage.tsx +12 -1
  42. package/src/pages/Home.tsx +33 -4
  43. package/src/pages/LiquiditySourcePage.tsx +24 -0
  44. package/src/pages/SelectBlockchainPage.tsx +10 -0
  45. package/src/pages/SelectSwapItemPage/SelectSwapItemsPage.tsx +27 -0
  46. package/src/pages/SwapDetailsPage.tsx +6 -0
  47. package/src/services/walletConnectionSource.ts +19 -0
  48. package/src/store/slices/wallets.ts +11 -3
  49. package/src/types/event.ts +174 -6
  50. package/src/utils/eventPayloads.ts +37 -0
  51. package/src/utils/events.ts +51 -6
@@ -0,0 +1,19 @@
1
+ import type { WalletConnectionSource } from '../types';
2
+
3
+ /**
4
+ * Tracks where in the UI the in-flight wallet connection was triggered, so the
5
+ * `walletConnected` event can be enriched with a `source` once the wallet
6
+ * actually connects (the connection completes asynchronously, far from the
7
+ * click site). Best-effort: the most recently initiated source wins.
8
+ */
9
+ let currentSource: WalletConnectionSource | null = null;
10
+
11
+ export function setWalletConnectionSource(
12
+ source: WalletConnectionSource | null
13
+ ): void {
14
+ currentSource = source;
15
+ }
16
+
17
+ export function getWalletConnectionSource(): WalletConnectionSource | null {
18
+ return currentSource;
19
+ }
@@ -8,6 +8,7 @@ import { ZERO } from '../../constants/numbers';
8
8
  import { BALANCE_SEPARATOR } from '../../constants/wallets';
9
9
  import { eventEmitter } from '../../services/eventEmitter';
10
10
  import { httpService } from '../../services/httpService';
11
+ import { getWalletConnectionSource } from '../../services/walletConnectionSource';
11
12
  import {
12
13
  type Balance,
13
14
  type Wallet,
@@ -114,7 +115,8 @@ export interface WalletsSlice {
114
115
  newWalletConnected: (
115
116
  accounts: Wallet[],
116
117
  namespace?: Namespace,
117
- derivationPath?: string
118
+ derivationPath?: string,
119
+ meta?: { walletName?: string }
118
120
  ) => Promise<void>;
119
121
  disconnectNamespaces: (walletType: string, namespaces: Namespace[]) => void;
120
122
  /**
@@ -443,14 +445,20 @@ export const createWalletsSlice = keepLastUpdated<AppStoreState, WalletsSlice>(
443
445
  connectedWallets: nextConnectedWalletsWithUpdatedSelectedStatus,
444
446
  });
445
447
  },
446
- newWalletConnected: async (accounts, namespace, derivationPath) => {
448
+ newWalletConnected: async (accounts, namespace, derivationPath, meta) => {
447
449
  const newAccount = accounts[0];
448
450
  if (!newAccount) {
449
451
  return;
450
452
  }
451
453
  eventEmitter.emit(WidgetEvents.WalletEvent, {
452
454
  type: WalletEventTypes.CONNECT,
453
- payload: { walletType: newAccount.walletType, accounts },
455
+ payload: {
456
+ walletType: newAccount.walletType,
457
+ accounts,
458
+ chain: newAccount.chain ?? null,
459
+ walletName: meta?.walletName ?? newAccount.walletType,
460
+ source: getWalletConnectionSource(),
461
+ },
454
462
  });
455
463
 
456
464
  get().addConnectedWallet(accounts, namespace, derivationPath);
@@ -23,19 +23,48 @@ type Account = Wallet;
23
23
  export enum QuoteEventTypes {
24
24
  QUOTE_INPUT_UPDATE = 'quoteInputUpdate',
25
25
  QUOTE_OUTPUT_UPDATE = 'quoteOutputUpdate',
26
+ /** A route/quote request was made with a valid source, destination and amount. */
27
+ ROUTE_REQUESTED = 'routeRequested',
28
+ /** The route API returned zero results for the requested pair. */
29
+ ROUTE_NOT_FOUND = 'routeNotFound',
30
+ /** The route API returned a 4xx/5xx error. */
31
+ ROUTE_FETCH_FAILED = 'routeFetchFailed',
32
+ /** The user overrode the auto-selected route. */
33
+ ROUTE_CHANGED = 'routeChanged',
26
34
  }
27
35
 
28
36
  export enum WalletEventTypes {
29
37
  CONNECT = 'connect',
30
38
  DISCONNECT = 'disconnect',
39
+ /** The user opened the wallet connection modal/flow. */
40
+ CONNECT_INITIATED = 'connectInitiated',
41
+ /** A wallet provider was detected on app load (one per detected wallet). */
42
+ DETECTED = 'detected',
31
43
  }
32
44
 
45
+ /** Where in the UI a wallet connection was triggered. */
46
+ export type WalletConnectionSource = 'nav' | 'swap_button' | 'confirm_modal';
47
+
33
48
  /**
34
49
  * We use a prefix for interaction type and a name for defining the event name: INTERACTION_EVENT_NAME
35
50
  * e.g. CLICK_X_BUTTON or NAVIGATE_WALLET_PAGE
36
51
  */
37
52
  export enum UiEventTypes {
38
53
  CLICK_CONNECT_WALLET = 'clickConnectWallet',
54
+ TOKEN_SELECTED = 'tokenSelected',
55
+ CHAIN_FILTER_APPLIED = 'chainFilterApplied',
56
+ SETTINGS_CHANGED = 'settingsChanged',
57
+ ROUTE_FEE_VIEWED = 'routeFeeViewed',
58
+ DESTINATION_ADDRESS_SET = 'destinationAddressSet',
59
+ GAS_WARNING_SHOWN = 'gasWarningShown',
60
+ GAS_WARNING_BYPASSED = 'gasWarningBypassed',
61
+ SWAP_INITIATED = 'swapInitiated',
62
+ SWAP_WALLETS_MODAL_SHOWN = 'swapWalletsModalShown',
63
+ SWAP_WALLETS_CONFIRMED = 'swapWalletsConfirmed',
64
+ SWAP_STARTED = 'swapStarted',
65
+ SWAP_RESUMED = 'swapResumed',
66
+ SWAP_RETRIED = 'swapRetried',
67
+ SWAP_CANCELLED = 'swapCancelled',
39
68
  }
40
69
 
41
70
  export type QuoteInputUpdateEventPayload = {
@@ -54,26 +83,165 @@ export type QuoteUpdateEventPayload = Pick<
54
83
  export type ConnectWalletEventPayload = {
55
84
  walletType: string;
56
85
  accounts: Account[];
86
+ /** Blockchain of the first connected account. */
87
+ chain: string | null;
88
+ /** Human-readable wallet name (e.g. "MetaMask"). */
89
+ walletName: string;
90
+ /** Where in the UI the connection was triggered, when known. */
91
+ source: WalletConnectionSource | null;
57
92
  };
58
93
 
59
94
  export type DisconnectWalletEventPayload = {
60
95
  walletType: string;
61
96
  };
62
97
 
98
+ export type ConnectInitiatedWalletEventPayload = {
99
+ /** Where in the UI the connection flow was opened, when known. */
100
+ source: WalletConnectionSource | null;
101
+ };
102
+
103
+ export type WalletDetectedEventPayload = {
104
+ walletType: string;
105
+ walletName: string;
106
+ };
107
+
63
108
  export type ClickConnectWalletPayload = PreventableEventPayload;
64
109
 
110
+ /** Shared shape describing a route/quote, used by several swap events. */
111
+ export type SwapEstimateEventPayload = {
112
+ sourceChain: string;
113
+ destinationChain: string;
114
+ sourceToken: string;
115
+ destinationToken: string;
116
+ inputAmountUsd: number | null;
117
+ outputAmountUsd: number | null;
118
+ sourceTokenAmount: number | null;
119
+ destinationTokenAmount: number | null;
120
+ routeId: string;
121
+ };
122
+
123
+ export type RouteRequestedEventPayload = SwapEstimateEventPayload & {
124
+ routeCount: number;
125
+ };
126
+
127
+ export type RouteNotFoundEventPayload = {
128
+ sourceChain: string;
129
+ destinationChain: string;
130
+ sourceToken: string;
131
+ destinationToken: string;
132
+ inputAmountUsd: number | null;
133
+ };
134
+
135
+ export type RouteFetchFailedEventPayload = {
136
+ sourceChain: string;
137
+ destinationChain: string;
138
+ /** HTTP status code returned by the route API, as a string. */
139
+ errorCode: string;
140
+ };
141
+
142
+ export type RouteChangedEventPayload = {
143
+ sourceChain: string;
144
+ destinationChain: string;
145
+ fromRouteId: string;
146
+ toRouteId: string;
147
+ };
148
+
149
+ export type TokenSelectedEventPayload = {
150
+ side: 'from' | 'to';
151
+ tokenName: string | null;
152
+ tokenSymbol: string;
153
+ tokenAddress: string | null;
154
+ chain: string;
155
+ selectionMethod: 'search' | 'popular' | 'recent';
156
+ /** `all` when no chain filter is active, otherwise the filtered chain name. */
157
+ activeChainFilter: string;
158
+ };
159
+
160
+ export type ChainFilterAppliedEventPayload = {
161
+ side: 'from' | 'to';
162
+ chain: string;
163
+ filterSource: 'featured' | 'expanded';
164
+ };
165
+
166
+ export type SettingsChangedEventPayload = {
167
+ settingName: 'slippage' | 'bridge_exclusion' | 'infinite_approval';
168
+ previousValue: string;
169
+ newValue: string;
170
+ };
171
+
172
+ export type RouteFeeViewedEventPayload = { routeId: string };
173
+
174
+ export type DestinationAddressSetEventPayload = { destinationChain: string };
175
+
176
+ export type GasWarningEventPayload = { routeId: string };
177
+
178
+ export type SwapInitiatedEventPayload = SwapEstimateEventPayload & {
179
+ walletConnected: boolean;
180
+ };
181
+
182
+ export type SwapStartedEventPayload = SwapEstimateEventPayload & {
183
+ stepCount: number;
184
+ };
185
+
186
+ export type SwapWalletsModalShownEventPayload = {
187
+ chainsRequired: number;
188
+ chainsPending: number;
189
+ };
190
+
191
+ export type SwapWalletsConfirmedEventPayload = { routeId: string };
192
+
193
+ export type SwapResumedEventPayload = {
194
+ routeId: string;
195
+ stepCount: number;
196
+ stepNumber: number;
197
+ };
198
+
199
+ export type SwapRetriedEventPayload = { routeId: string };
200
+
201
+ export type SwapCancelledEventPayload = { routeId: string };
202
+
65
203
  export type QuoteEventData =
66
204
  | EventData<QuoteEventTypes.QUOTE_INPUT_UPDATE, QuoteInputUpdateEventPayload>
67
- | EventData<QuoteEventTypes.QUOTE_OUTPUT_UPDATE, QuoteUpdateEventPayload>;
205
+ | EventData<QuoteEventTypes.QUOTE_OUTPUT_UPDATE, QuoteUpdateEventPayload>
206
+ | EventData<QuoteEventTypes.ROUTE_REQUESTED, RouteRequestedEventPayload>
207
+ | EventData<QuoteEventTypes.ROUTE_NOT_FOUND, RouteNotFoundEventPayload>
208
+ | EventData<QuoteEventTypes.ROUTE_FETCH_FAILED, RouteFetchFailedEventPayload>
209
+ | EventData<QuoteEventTypes.ROUTE_CHANGED, RouteChangedEventPayload>;
68
210
 
69
211
  export type WalletEventData =
70
212
  | EventData<WalletEventTypes.CONNECT, ConnectWalletEventPayload>
71
- | EventData<WalletEventTypes.DISCONNECT, DisconnectWalletEventPayload>;
213
+ | EventData<WalletEventTypes.DISCONNECT, DisconnectWalletEventPayload>
214
+ | EventData<
215
+ WalletEventTypes.CONNECT_INITIATED,
216
+ ConnectInitiatedWalletEventPayload
217
+ >
218
+ | EventData<WalletEventTypes.DETECTED, WalletDetectedEventPayload>;
72
219
 
73
- export type UiEventData = EventData<
74
- UiEventTypes.CLICK_CONNECT_WALLET,
75
- ClickConnectWalletPayload
76
- >;
220
+ export type UiEventData =
221
+ | EventData<UiEventTypes.CLICK_CONNECT_WALLET, ClickConnectWalletPayload>
222
+ | EventData<UiEventTypes.TOKEN_SELECTED, TokenSelectedEventPayload>
223
+ | EventData<UiEventTypes.CHAIN_FILTER_APPLIED, ChainFilterAppliedEventPayload>
224
+ | EventData<UiEventTypes.SETTINGS_CHANGED, SettingsChangedEventPayload>
225
+ | EventData<UiEventTypes.ROUTE_FEE_VIEWED, RouteFeeViewedEventPayload>
226
+ | EventData<
227
+ UiEventTypes.DESTINATION_ADDRESS_SET,
228
+ DestinationAddressSetEventPayload
229
+ >
230
+ | EventData<UiEventTypes.GAS_WARNING_SHOWN, GasWarningEventPayload>
231
+ | EventData<UiEventTypes.GAS_WARNING_BYPASSED, GasWarningEventPayload>
232
+ | EventData<UiEventTypes.SWAP_INITIATED, SwapInitiatedEventPayload>
233
+ | EventData<
234
+ UiEventTypes.SWAP_WALLETS_MODAL_SHOWN,
235
+ SwapWalletsModalShownEventPayload
236
+ >
237
+ | EventData<
238
+ UiEventTypes.SWAP_WALLETS_CONFIRMED,
239
+ SwapWalletsConfirmedEventPayload
240
+ >
241
+ | EventData<UiEventTypes.SWAP_STARTED, SwapStartedEventPayload>
242
+ | EventData<UiEventTypes.SWAP_RESUMED, SwapResumedEventPayload>
243
+ | EventData<UiEventTypes.SWAP_RETRIED, SwapRetriedEventPayload>
244
+ | EventData<UiEventTypes.SWAP_CANCELLED, SwapCancelledEventPayload>;
77
245
 
78
246
  export enum WidgetEvents {
79
247
  RouteEvent = QueueManagerEvents.RouteEvent,
@@ -0,0 +1,37 @@
1
+ import type { SelectedQuote, SwapEstimateEventPayload } from '../types';
2
+
3
+ import { getUsdInputFrom, getUsdOutputFrom } from './swap';
4
+
5
+ function toNumberOrNull(value: string | null | undefined): number | null {
6
+ if (value === null || value === undefined || value === '') {
7
+ return null;
8
+ }
9
+ const parsed = Number(value);
10
+ return Number.isFinite(parsed) ? parsed : null;
11
+ }
12
+
13
+ /**
14
+ * Derives the shared route/quote descriptor used by several swap
15
+ * events (swapInitiated, swapStarted, routeRequested, ...) from a quote.
16
+ * Amounts are estimates taken from the quote, not on-chain actuals.
17
+ */
18
+ export function buildSwapEstimatePayload(
19
+ quote: SelectedQuote
20
+ ): SwapEstimateEventPayload {
21
+ const firstSwap = quote.swaps[0];
22
+ const lastSwap = quote.swaps[quote.swaps.length - 1];
23
+ const inputUsd = getUsdInputFrom(quote);
24
+ const outputUsd = getUsdOutputFrom(quote);
25
+
26
+ return {
27
+ routeId: quote.requestId,
28
+ sourceChain: firstSwap?.from.blockchain ?? '',
29
+ destinationChain: lastSwap?.to.blockchain ?? '',
30
+ sourceToken: firstSwap?.from.symbol ?? '',
31
+ destinationToken: lastSwap?.to.symbol ?? '',
32
+ sourceTokenAmount: toNumberOrNull(quote.requestAmount),
33
+ destinationTokenAmount: toNumberOrNull(quote.outputAmount),
34
+ inputAmountUsd: inputUsd ? inputUsd.toNumber() : null,
35
+ outputAmountUsd: outputUsd ? outputUsd.toNumber() : null,
36
+ };
37
+ }
@@ -1,19 +1,32 @@
1
1
  import { eventEmitter } from '../services/eventEmitter';
2
- import { type UiEventData, WidgetEvents } from '../types';
2
+ import { setWalletConnectionSource } from '../services/walletConnectionSource';
3
+ import {
4
+ type ClickConnectWalletPayload,
5
+ type QuoteEventData,
6
+ type UiEventData,
7
+ type UiEventTypes,
8
+ type WalletConnectionSource,
9
+ type WalletDetectedEventPayload,
10
+ WalletEventTypes,
11
+ WidgetEvents,
12
+ } from '../types';
3
13
 
4
- type UiEvent = {
5
- type: UiEventData['type'];
6
- payload?: Omit<UiEventData['payload'], 'preventDefault'>;
14
+ type PreventableUiEvent = {
15
+ type: UiEventTypes.CLICK_CONNECT_WALLET;
16
+ payload?: Omit<ClickConnectWalletPayload, 'preventDefault'>;
7
17
  };
8
18
 
9
- export function emitPreventableEvent(event: UiEvent, action: () => void): void {
19
+ export function emitPreventableEvent(
20
+ event: PreventableUiEvent,
21
+ action: () => void
22
+ ): void {
10
23
  let defaultPrevented = false;
11
24
 
12
25
  const extendedPayload = {
26
+ ...event.payload,
13
27
  preventDefault() {
14
28
  defaultPrevented = true;
15
29
  },
16
- ...(event.payload === undefined && { payload: event.payload }),
17
30
  };
18
31
 
19
32
  eventEmitter.emit(WidgetEvents.UiEvent, {
@@ -25,3 +38,35 @@ export function emitPreventableEvent(event: UiEvent, action: () => void): void {
25
38
  action();
26
39
  }
27
40
  }
41
+
42
+ export function emitUiEvent(
43
+ event: Exclude<UiEventData, { type: UiEventTypes.CLICK_CONNECT_WALLET }>
44
+ ): void {
45
+ eventEmitter.emit(WidgetEvents.UiEvent, event);
46
+ }
47
+
48
+ /**
49
+ * Records the source of the wallet connection the user is about to start and
50
+ * emits a `connectInitiated` wallet event. The recorded source is later read
51
+ * when the wallet finishes connecting to enrich the `connect` event.
52
+ */
53
+ export function emitWalletConnectInitiated(
54
+ source: WalletConnectionSource | null
55
+ ): void {
56
+ setWalletConnectionSource(source);
57
+ eventEmitter.emit(WidgetEvents.WalletEvent, {
58
+ type: WalletEventTypes.CONNECT_INITIATED,
59
+ payload: { source },
60
+ });
61
+ }
62
+
63
+ export function emitQuoteEvent(event: QuoteEventData): void {
64
+ eventEmitter.emit(WidgetEvents.QuoteEvent, event);
65
+ }
66
+
67
+ export function emitWalletDetected(payload: WalletDetectedEventPayload): void {
68
+ eventEmitter.emit(WidgetEvents.WalletEvent, {
69
+ type: WalletEventTypes.DETECTED,
70
+ payload,
71
+ });
72
+ }