@rango-dev/queue-manager-rango-preset 0.22.1-next.8 → 0.23.0

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/src/helpers.ts CHANGED
@@ -1,13 +1,7 @@
1
1
  /* eslint-disable destructuring/in-params */
2
2
  /* eslint-disable @typescript-eslint/no-magic-numbers */
3
3
  /* eslint-disable @typescript-eslint/no-floating-promises */
4
- import type {
5
- PendingSwap,
6
- PendingSwapStep,
7
- StepStatus,
8
- SwapStatus,
9
- Wallet,
10
- } from './shared';
4
+ import type { SwapStatus, Wallet } from './shared';
11
5
  import type {
12
6
  ArrayElement,
13
7
  Step,
@@ -22,10 +16,10 @@ import type {
22
16
  QueueName,
23
17
  QueueType,
24
18
  } from '@rango-dev/queue-manager-core';
25
- import type { Providers } from '@rango-dev/wallets-react';
26
19
  import type {
27
20
  Meta,
28
21
  Network,
22
+ Providers,
29
23
  WalletState,
30
24
  WalletType,
31
25
  } from '@rango-dev/wallets-shared';
@@ -34,12 +28,23 @@ import type {
34
28
  EvmBlockchainMeta,
35
29
  Transaction,
36
30
  } from 'rango-sdk';
37
- import type { APIErrorCode, SignerErrorCode } from 'rango-types/lib';
31
+ import type {
32
+ APIErrorCode,
33
+ PendingSwap,
34
+ PendingSwapStep,
35
+ SignerErrorCode,
36
+ StepStatus,
37
+ } from 'rango-types';
38
38
 
39
39
  import { Status } from '@rango-dev/queue-manager-core';
40
- import { readAccountAddress } from '@rango-dev/wallets-react';
41
- import { getBlockChainNameFromId, Networks } from '@rango-dev/wallets-shared';
40
+ import { readAccountAddress } from '@rango-dev/wallets-core';
41
+ import {
42
+ getBlockChainNameFromId,
43
+ getEvmProvider,
44
+ splitWalletNetwork,
45
+ } from '@rango-dev/wallets-shared';
42
46
  import { TransactionType } from 'rango-sdk';
47
+ import { PendingSwapNetworkStatus } from 'rango-types';
43
48
 
44
49
  import {
45
50
  DEFAULT_ERROR_CODE,
@@ -57,7 +62,6 @@ import {
57
62
  getRelatedWalletOrNull,
58
63
  getScannerUrl,
59
64
  MessageSeverity,
60
- PendingSwapNetworkStatus,
61
65
  } from './shared';
62
66
  import {
63
67
  mapAppErrorCodesToAPIErrorCode,
@@ -128,29 +132,6 @@ export function inMemoryTransactionsData() {
128
132
  };
129
133
  }
130
134
 
131
- /**
132
- * Sample inputs are:
133
- * - "metamask-ETH"
134
- * - "metamask-BSC-BSC:0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
135
- * - "token-pocket-BSC-BSC:0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
136
- * Returns "wallet and network" separately, even if the wallet is dashed inside.
137
- *
138
- */
139
-
140
- export function splitWalletNetwork(input: string): string[] {
141
- const removedAddressInput = input?.split(':')[0] || '';
142
- const splittedInput = removedAddressInput.split('-');
143
- const network = splittedInput[splittedInput.length - 1];
144
- const walletNetwork = splittedInput.slice(0, -1);
145
-
146
- if (walletNetwork[walletNetwork.length - 1] === network) {
147
- walletNetwork.pop();
148
- }
149
- const wallet = walletNetwork.join('-');
150
-
151
- return [wallet, network];
152
- }
153
-
154
135
  /**
155
136
  *
156
137
  * Returns `steps`, if it's a `running` swap.
@@ -361,7 +342,7 @@ export function updateSwapStatus({
361
342
  details && details.includes('Warning')
362
343
  ? 'Swap canceled by user.'
363
344
  : details;
364
- const walletType = getRelatedWalletOrNull(swap, currentStep!)?.walletType;
345
+ const walletType = getRelatedWalletOrNull(swap, currentStep)?.walletType;
365
346
  swap.extraMessageSeverity = MessageSeverity.error;
366
347
 
367
348
  const failureType = mapAppErrorCodesToAPIErrorCode(errorCode);
@@ -624,25 +605,6 @@ export function isWalletNull(wallet: Wallet | null): boolean {
624
605
  );
625
606
  }
626
607
 
627
- /**
628
- * On our implementation for `wallets` package, We keep the instance in 2 ways
629
- * If it's a single chain wallet, it returns the instance directly,
630
- * If it's a multichain wallet, it returns a `Map` of instances.
631
- * This function will get the `ETHEREUM` instance in both types.
632
- */
633
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
634
- export function getEvmProvider(providers: Providers, type: WalletType): any {
635
- if (type && providers[type]) {
636
- // we need this because provider can return an instance or a map of instances, so what you are doing here is try to detect that.
637
- if (providers[type].size) {
638
- return providers[type].get(Networks.ETHEREUM);
639
- }
640
-
641
- return providers[type];
642
- }
643
- return null;
644
- }
645
-
646
608
  /**
647
609
  * In a `PendingSwap`, each step needs a wallet to proceed,
648
610
  * By using this function we can access what wallet exactly we need to run current step.
package/src/index.ts CHANGED
@@ -33,29 +33,21 @@ export {
33
33
  StepExecutionEventStatus,
34
34
  StepExecutionBlockedEventStatus,
35
35
  } from './types';
36
- export type {
37
- PendingSwapWithQueueID,
38
- PendingSwapStep,
39
- PendingSwap,
40
- EventType,
41
- } from './shared';
36
+ export type { PendingSwapWithQueueID, EventType } from './shared';
42
37
  export {
43
38
  getCurrentBlockchainOfOrNull,
44
39
  getRelatedWalletOrNull,
45
40
  getRelatedWallet,
46
41
  MessageSeverity,
47
- PendingSwapNetworkStatus,
48
42
  calculatePendingSwap,
49
43
  } from './shared';
50
44
  export {
51
45
  updateSwapStatus,
52
46
  checkWaitingForNetworkChange,
53
47
  getCurrentStep,
54
- getEvmProvider,
55
48
  cancelSwap,
56
49
  getRequiredWallet,
57
50
  getRunningSwaps,
58
- splitWalletNetwork,
59
51
  resetRunningSwapNotifsOnPageLoad,
60
52
  isApprovalTX,
61
53
  getLastSuccessfulStep,
package/src/migration.ts CHANGED
@@ -1,11 +1,9 @@
1
- import {
2
- PersistedQueue,
3
- Persistor,
4
- Status,
5
- DB_NAME,
6
- } from '@rango-dev/queue-manager-core';
1
+ import type { PersistedQueue } from '@rango-dev/queue-manager-core';
2
+ import type { PendingSwap } from 'rango-types';
3
+
4
+ import { DB_NAME, Persistor, Status } from '@rango-dev/queue-manager-core';
7
5
  import { v4 as uuid } from 'uuid';
8
- import { PendingSwap } from './shared';
6
+
9
7
  import { SwapActionTypes } from './types';
10
8
 
11
9
  const MIGRATED_KEY = 'migratedToQueueManager';
@@ -49,9 +47,9 @@ async function migration(): Promise<boolean> {
49
47
  const convertedSwaps: PersistedQueue[] = [];
50
48
 
51
49
  swaps.forEach((swap) => {
52
- /*
53
- For running task we need to add some more work
54
- We need to create a queue task to be run and resume the running task from queue manager.
50
+ /*
51
+ *For running task we need to add some more work
52
+ *We need to create a queue task to be run and resume the running task from queue manager.
55
53
  */
56
54
  if (swap.status === 'running') {
57
55
  const taskId = uuid();
@@ -112,7 +110,9 @@ async function migration(): Promise<boolean> {
112
110
  // Getting an instance from persistor, so we can directly put our data inside it.
113
111
  const persistor = new Persistor();
114
112
 
115
- const promises = convertedSwaps.map((queue) => persistor.insertQueue(queue));
113
+ const promises = convertedSwaps.map(async (queue) =>
114
+ persistor.insertQueue(queue)
115
+ );
116
116
  await Promise.all(promises);
117
117
 
118
118
  // Mark as the data has been successfully migrated.
@@ -6,7 +6,7 @@ import type {
6
6
  Step,
7
7
  StepEvent,
8
8
  } from '../types';
9
- import type { PendingSwap, PendingSwapStep } from 'rango-types/lib';
9
+ import type { PendingSwap, PendingSwapStep } from 'rango-types';
10
10
 
11
11
  import mitt from 'mitt';
12
12
 
@@ -1,6 +1,7 @@
1
+ import type { WalletType } from '@rango-dev/wallets-shared';
2
+ import type { PendingSwap, PendingSwapStep } from 'rango-types';
3
+
1
4
  import * as Sentry from '@sentry/browser';
2
- import { PendingSwap, PendingSwapStep } from './shared';
3
- import { WalletType } from '@rango-dev/wallets-shared';
4
5
 
5
6
  export function logRPCError(
6
7
  error: unknown,
package/src/shared.ts CHANGED
@@ -1,20 +1,18 @@
1
1
  import type { Network, WalletType } from '@rango-dev/wallets-shared';
2
2
  import type {
3
- AmountRestrictionType,
4
3
  BestRouteResponse,
5
4
  BlockchainMeta,
6
- CosmosTransaction,
7
- EvmTransaction,
8
5
  MetaResponse,
9
- SimulationResult,
10
- SolanaTransaction,
11
- StarknetTransaction,
12
6
  SwapResult,
13
7
  Token,
14
- Transfer as TransferTransaction,
15
- TronTransaction,
16
8
  } from 'rango-sdk';
17
- import type { TonTransaction } from 'rango-types';
9
+ import type {
10
+ PendingSwap,
11
+ PendingSwapStep,
12
+ SwapSavedSettings,
13
+ SwapStepRoute,
14
+ WalletTypeAndAddress,
15
+ } from 'rango-types';
18
16
 
19
17
  import BigNumber from 'bignumber.js';
20
18
  import {
@@ -86,107 +84,6 @@ export type EventType =
86
84
  | 'route_failed_to_find'
87
85
  | 'transaction_expired';
88
86
 
89
- export type SwapSavedSettings = {
90
- slippage: string;
91
- disabledSwappersIds?: string[];
92
- disabledSwappersGroups?: string[];
93
- infiniteApprove?: boolean;
94
- };
95
-
96
- type InternalStepState =
97
- | 'PENDING'
98
- | 'CREATED'
99
- | 'WAITING'
100
- | 'SIGNED'
101
- | 'SUCCESSED'
102
- | 'FAILED';
103
-
104
- export type SwapperStatusStep = {
105
- name: string;
106
- state: InternalStepState;
107
- current: boolean;
108
- };
109
-
110
- export enum PendingSwapNetworkStatus {
111
- WaitingForConnectingWallet = 'waitingForConnectingWallet',
112
- WaitingForQueue = 'waitingForQueue',
113
- WaitingForNetworkChange = 'waitingForNetworkChange',
114
- NetworkChanged = 'networkChanged',
115
- }
116
-
117
- export type SwapExplorerUrl = {
118
- url: string;
119
- description: string | null;
120
- };
121
-
122
- export type StepStatus =
123
- | 'created'
124
- | 'running'
125
- | 'failed'
126
- | 'success'
127
- | 'waitingForApproval'
128
- | 'approved';
129
-
130
- export type PendingSwapStep = {
131
- // routing data
132
- id: number;
133
- fromBlockchain: string;
134
- fromSymbol: string;
135
- fromSymbolAddress: string | null;
136
- fromDecimals: number;
137
- fromAmountPrecision: string | null;
138
- fromAmountMinValue: string | null;
139
- fromAmountMaxValue: string | null;
140
- fromAmountRestrictionType: AmountRestrictionType | null;
141
- fromLogo: string;
142
- toBlockchain: string;
143
- toSymbol: string;
144
- toSymbolAddress: string | null;
145
- toDecimals: number;
146
- toLogo: string;
147
- swapperId: string;
148
- expectedOutputAmountHumanReadable: string | null;
149
- startTransactionTime: number;
150
- internalSteps: SwapperStatusStep[] | null;
151
- estimatedTimeInSeconds: number | null;
152
-
153
- // status data
154
- status: StepStatus;
155
- networkStatus: PendingSwapNetworkStatus | null;
156
- executedTransactionId: string | null;
157
- executedTransactionTime: string | null;
158
- explorerUrl: SwapExplorerUrl[] | null;
159
- diagnosisUrl: string | null;
160
- outputAmount: string | null;
161
-
162
- // txs data
163
- cosmosTransaction: CosmosTransaction | null;
164
- transferTransaction: TransferTransaction | null;
165
- solanaTransaction: SolanaTransaction | null;
166
- evmApprovalTransaction: EvmTransaction | null;
167
- evmTransaction: EvmTransaction | null;
168
- tronApprovalTransaction: TronTransaction | null;
169
- tronTransaction: TronTransaction | null;
170
- starknetApprovalTransaction: StarknetTransaction | null;
171
- starknetTransaction: StarknetTransaction | null;
172
- tonTransaction: TonTransaction | null;
173
-
174
- /*
175
- * missing fields in older versions
176
- * keeping null for backward compatability
177
- */
178
- swapperLogo: string | null;
179
- swapperType: string | null;
180
- fromBlockchainLogo: string | null;
181
- toBlockchainLogo: string | null;
182
- feeInUsd: string | null;
183
- };
184
-
185
- export type WalletTypeAndAddress = {
186
- walletType: WalletType;
187
- address: string;
188
- };
189
-
190
87
  export enum MessageSeverity {
191
88
  error = 'error',
192
89
  warning = 'warning',
@@ -196,28 +93,6 @@ export enum MessageSeverity {
196
93
 
197
94
  export type SwapStatus = 'running' | 'failed' | 'success';
198
95
 
199
- export type PendingSwap = {
200
- creationTime: string;
201
- finishTime: string | null;
202
- requestId: string;
203
- inputAmount: string;
204
- status: SwapStatus;
205
- isPaused: boolean;
206
- extraMessage: string | null;
207
- extraMessageSeverity: MessageSeverity | null;
208
- extraMessageErrorCode: string | null;
209
- extraMessageDetail: string | null | undefined;
210
- networkStatusExtraMessage: string | null;
211
- networkStatusExtraMessageDetail: string | null;
212
- lastNotificationTime: string | null;
213
- wallets: { [p: string]: WalletTypeAndAddress };
214
- settings: SwapSavedSettings;
215
- steps: PendingSwapStep[];
216
- simulationResult: SimulationResult;
217
- validateBalanceOrFee: boolean;
218
- hasAlreadyProceededToSign?: boolean | null;
219
- };
220
-
221
96
  export const getCurrentBlockchainOfOrNull = (
222
97
  swap: PendingSwap,
223
98
  step: PendingSwapStep
@@ -358,8 +233,11 @@ export function getRelatedWallet(
358
233
 
359
234
  export function getRelatedWalletOrNull(
360
235
  swap: PendingSwap,
361
- currentStep: PendingSwapStep
236
+ currentStep: PendingSwapStep | null
362
237
  ): WalletTypeAndAddress | null {
238
+ if (!currentStep) {
239
+ return null;
240
+ }
363
241
  try {
364
242
  return getRelatedWallet(swap, currentStep);
365
243
  } catch (e) {
@@ -407,6 +285,49 @@ export function getUsdFeeOfStep(
407
285
  return totalFeeInUsd;
408
286
  }
409
287
 
288
+ function mapSwapStepToPendingSwapStep(
289
+ swap: SwapResult,
290
+ meta: Pick<MetaResponse, 'blockchains' | 'tokens'> | null
291
+ ): SwapStepRoute {
292
+ return {
293
+ // from
294
+ fromBlockchain: swap.from.blockchain,
295
+ fromBlockchainLogo: swap.from.blockchainLogo,
296
+ fromLogo: swap.from.logo,
297
+ fromSymbol: swap.from.symbol,
298
+ fromSymbolAddress: swap.from.address,
299
+ fromDecimals: swap.from.decimals,
300
+ fromAmountPrecision: swap.fromAmountPrecision,
301
+ fromAmountMinValue: swap.fromAmountMinValue,
302
+ fromAmountMaxValue: swap.fromAmountMaxValue,
303
+ fromAmountRestrictionType: swap.fromAmountRestrictionType,
304
+ fromUsdPrice: swap.from.usdPrice,
305
+
306
+ // to
307
+ toBlockchain: swap.to.blockchain,
308
+ toBlockchainLogo: swap.to.blockchainLogo,
309
+ toSymbol: swap.to.symbol,
310
+ toSymbolAddress: swap.to.address,
311
+ toDecimals: swap.to.decimals,
312
+ toLogo: swap.to.logo,
313
+ toUsdPrice: swap.to.usdPrice,
314
+
315
+ // swapper
316
+ swapperId: swap.swapperId,
317
+ swapperLogo: swap.swapperLogo,
318
+ swapperType: swap.swapperType,
319
+
320
+ // route
321
+ expectedOutputAmountHumanReadable: swap.toAmount,
322
+ feeInUsd: meta
323
+ ? // eslint-disable-next-line @typescript-eslint/no-magic-numbers
324
+ numberToString(getUsdFeeOfStep(swap, meta?.tokens), null, 8)
325
+ : null,
326
+ estimatedTimeInSeconds: swap.estimatedTimeInSeconds || null,
327
+ internalSteps: null,
328
+ };
329
+ }
330
+
410
331
  export function calculatePendingSwap(
411
332
  inputAmount: string,
412
333
  bestRoute: BestRouteResponse,
@@ -440,44 +361,23 @@ export function calculatePendingSwap(
440
361
  validateBalanceOrFee,
441
362
  steps:
442
363
  bestRoute.result?.swaps?.map((swap, index) => {
364
+ const stepRoute = mapSwapStepToPendingSwapStep(swap, meta);
443
365
  return {
444
366
  id: index + 1,
445
367
 
446
- // from
447
- fromBlockchain: swap.from.blockchain,
448
- fromBlockchainLogo: swap.from.blockchainLogo,
449
- fromLogo: swap.from.logo,
450
- fromSymbol: swap.from.symbol,
451
- fromSymbolAddress: swap.from.address,
452
- fromDecimals: swap.from.decimals,
453
- fromAmountPrecision: swap.fromAmountPrecision,
454
- fromAmountMinValue: swap.fromAmountMinValue,
455
- fromAmountMaxValue: swap.fromAmountMaxValue,
456
- fromAmountRestrictionType: swap.fromAmountRestrictionType,
457
-
458
- // to
459
- toBlockchain: swap.to.blockchain,
460
- toBlockchainLogo: swap.to.blockchainLogo,
461
- toSymbol: swap.to.symbol,
462
- toSymbolAddress: swap.to.address,
463
- toDecimals: swap.to.decimals,
464
- toLogo: swap.to.logo,
465
-
466
- // swapper
467
- swapperId: swap.swapperId,
468
- swapperLogo: swap.swapperLogo,
469
- swapperType: swap.swapperType,
470
-
471
- // output, fee, timing
472
- expectedOutputAmountHumanReadable: swap.toAmount,
473
- outputAmount: '',
474
- feeInUsd: meta
475
- ? // eslint-disable-next-line @typescript-eslint/no-magic-numbers
476
- numberToString(getUsdFeeOfStep(swap, meta?.tokens), null, 8)
477
- : null,
478
- estimatedTimeInSeconds: swap.estimatedTimeInSeconds || null,
368
+ // route
369
+ ...stepRoute,
370
+ internalSwaps:
371
+ swap?.internalSwaps?.map((internalSwap) => {
372
+ const stepRoute = mapSwapStepToPendingSwapStep(
373
+ internalSwap,
374
+ meta
375
+ );
376
+ return stepRoute;
377
+ }) || null,
479
378
 
480
379
  // status, tracking
380
+ outputAmount: '',
481
381
  status: 'created',
482
382
  networkStatus: null,
483
383
  startTransactionTime: new Date().getTime(),
package/src/types.ts CHANGED
@@ -1,10 +1,14 @@
1
- import type { PendingSwap, PendingSwapStep, Wallet } from './shared';
2
- import type { QueueDef, QueueStorage } from '@rango-dev/queue-manager-core';
3
- import type { QueueContext } from '@rango-dev/queue-manager-core/dist/queue';
4
- import type { ConnectResult, Providers } from '@rango-dev/wallets-react';
1
+ import type { Wallet } from './shared';
2
+ import type {
3
+ QueueContext,
4
+ QueueDef,
5
+ QueueStorage,
6
+ } from '@rango-dev/queue-manager-core';
7
+ import type { ConnectResult } from '@rango-dev/wallets-core';
5
8
  import type {
6
9
  Meta,
7
10
  Network,
11
+ Providers,
8
12
  WalletState,
9
13
  WalletType,
10
14
  } from '@rango-dev/wallets-shared';
@@ -12,6 +16,8 @@ import type { Transaction } from 'rango-sdk';
12
16
  import type {
13
17
  APIErrorCode,
14
18
  EvmBlockchainMeta,
19
+ PendingSwap,
20
+ PendingSwapStep,
15
21
  SignerFactory,
16
22
  } from 'rango-types';
17
23