@rango-dev/queue-manager-rango-preset 0.24.0 → 0.24.1-next.1

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.
@@ -1,26 +1,30 @@
1
- import mitt from 'mitt';
2
- import {
3
- MainEvents,
1
+ import type {
4
2
  RemoveNameField,
5
3
  Route,
6
4
  RouteEvent,
7
- RouteEventType,
8
5
  RouteExecutionEvents,
9
- EventSeverity,
10
6
  Step,
11
7
  StepEvent,
12
- StepEventType,
13
- StepExecutionEventStatus,
14
- StepExecutionBlockedEventStatus,
15
8
  } from '../types';
16
- import { PendingSwap, PendingSwapStep } from 'rango-types/lib';
17
- import { getCurrentBlockchainOfOrNull } from '../shared';
9
+ import type { PendingSwap, PendingSwapStep } from 'rango-types';
10
+
11
+ import mitt from 'mitt';
12
+
18
13
  import {
19
14
  getCurrentStepTx,
20
15
  getFailedStep,
21
16
  getLastSuccessfulStep,
22
17
  isApprovalCurrentStepTx,
23
18
  } from '../helpers';
19
+ import { getCurrentBlockchainOfOrNull } from '../shared';
20
+ import {
21
+ EventSeverity,
22
+ MainEvents,
23
+ RouteEventType,
24
+ StepEventType,
25
+ StepExecutionBlockedEventStatus,
26
+ StepExecutionEventStatus,
27
+ } from '../types';
24
28
 
25
29
  type NotifierParams = {
26
30
  swap: PendingSwap;
@@ -119,14 +123,19 @@ function getEventPayload(
119
123
  route,
120
124
  step: routeSteps[routeSteps.length - 1],
121
125
  };
122
- if (swapStep) result.step = createSteps([swapStep])[0];
123
- else {
126
+ if (swapStep) {
127
+ result.step = createSteps([swapStep])[0];
128
+ } else {
124
129
  if (type === 'failed') {
125
130
  const failedStep = getFailedStep(routeSteps);
126
- if (failedStep) result.step = failedStep;
131
+ if (failedStep) {
132
+ result.step = failedStep;
133
+ }
127
134
  } else {
128
135
  const lastSuccessfulStep = getLastSuccessfulStep(routeSteps);
129
- if (lastSuccessfulStep) result.step = lastSuccessfulStep;
136
+ if (lastSuccessfulStep) {
137
+ result.step = lastSuccessfulStep;
138
+ }
130
139
  }
131
140
  }
132
141
 
@@ -151,8 +160,9 @@ function emitRouteEvent(stepEvent: StepEvent, route: Route) {
151
160
  default:
152
161
  break;
153
162
  }
154
- if (routeEvent)
163
+ if (routeEvent) {
155
164
  eventEmitter.emit(MainEvents.RouteEvent, { event: routeEvent, route });
165
+ }
156
166
  }
157
167
 
158
168
  function emitStepEvent(stepEvent: StepEvent, route: Route, step: Step) {
@@ -196,9 +206,11 @@ export function notifier(params: NotifierParams) {
196
206
  message = 'Please wait while the transaction is created ...';
197
207
  messageSeverity = EventSeverity.INFO;
198
208
  } else if (event.status === StepExecutionEventStatus.SEND_TX) {
199
- if (params.step && isApprovalCurrentStepTx(params.step))
209
+ if (params.step && isApprovalCurrentStepTx(params.step)) {
200
210
  message = `Please confirm '${step.swapperName}' smart contract access to ${fromAsset}`;
201
- else message = 'Please confirm transaction request in your wallet';
211
+ } else {
212
+ message = 'Please confirm transaction request in your wallet';
213
+ }
202
214
  messageSeverity = EventSeverity.WARNING;
203
215
  } else if (event.status === StepExecutionEventStatus.TX_SENT) {
204
216
  message = 'Transaction sent successfully';
@@ -206,9 +218,11 @@ export function notifier(params: NotifierParams) {
206
218
  }
207
219
  break;
208
220
  case StepEventType.CHECK_STATUS:
209
- if (params.step && isApprovalCurrentStepTx(params.step))
210
- message = 'Checking approve transacation status ...';
211
- else message = 'Checking transacation status ...';
221
+ if (params.step && isApprovalCurrentStepTx(params.step)) {
222
+ message = 'Checking approve transaction status ...';
223
+ } else {
224
+ message = 'Checking transaction status ...';
225
+ }
212
226
  messageSeverity = EventSeverity.INFO;
213
227
  break;
214
228
  case StepEventType.APPROVAL_TX_SUCCEEDED:
@@ -251,7 +265,10 @@ export function notifier(params: NotifierParams) {
251
265
  break;
252
266
  }
253
267
 
254
- if (params.step)
268
+ if (params.step) {
255
269
  emitStepEvent({ ...event, message, messageSeverity }, route, step);
256
- else emitRouteEvent({ ...event, message, messageSeverity }, route);
270
+ }
271
+ if (params.event.type === StepEventType.FAILED || !params.step) {
272
+ emitRouteEvent({ ...event, message, messageSeverity }, route);
273
+ }
257
274
  }
@@ -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,31 +1,30 @@
1
- import { Network, WalletType } from '@rango-dev/wallets-shared';
2
- import {
3
- CosmosTransaction,
4
- EvmTransaction,
5
- SimulationResult,
6
- SolanaTransaction,
7
- StarknetTransaction,
8
- TronTransaction,
9
- Transfer as TransferTransaction,
10
- AmountRestrictionType,
1
+ import type { Network, WalletType } from '@rango-dev/wallets-shared';
2
+ import type {
11
3
  BestRouteResponse,
4
+ BlockchainMeta,
12
5
  MetaResponse,
13
- Token,
14
6
  SwapResult,
15
- BlockchainMeta,
7
+ Token,
16
8
  } from 'rango-sdk';
9
+ import type {
10
+ PendingSwap,
11
+ PendingSwapStep,
12
+ SwapSavedSettings,
13
+ SwapStepRoute,
14
+ WalletTypeAndAddress,
15
+ } from 'rango-types';
17
16
 
18
- import { PrettyError } from './shared-errors';
19
17
  import BigNumber from 'bignumber.js';
20
- import { numberToString } from './numbers';
21
18
  import {
22
- TonTransaction,
23
19
  isCosmosBlockchain,
24
20
  isEvmBlockchain,
25
21
  isStarknetBlockchain,
26
22
  isTronBlockchain,
27
23
  } from 'rango-types';
28
24
 
25
+ import { numberToString } from './numbers';
26
+ import { PrettyError } from './shared-errors';
27
+
29
28
  export interface PendingSwapWithQueueID {
30
29
  id: string;
31
30
  swap: PendingSwap;
@@ -85,105 +84,6 @@ export type EventType =
85
84
  | 'route_failed_to_find'
86
85
  | 'transaction_expired';
87
86
 
88
- export type SwapSavedSettings = {
89
- slippage: string;
90
- disabledSwappersIds?: string[];
91
- disabledSwappersGroups?: string[];
92
- infiniteApprove?: boolean;
93
- };
94
-
95
- type InternalStepState =
96
- | 'PENDING'
97
- | 'CREATED'
98
- | 'WAITING'
99
- | 'SIGNED'
100
- | 'SUCCESSED'
101
- | 'FAILED';
102
-
103
- export type SwapperStatusStep = {
104
- name: string;
105
- state: InternalStepState;
106
- current: boolean;
107
- };
108
-
109
- export enum PendingSwapNetworkStatus {
110
- WaitingForConnectingWallet = 'waitingForConnectingWallet',
111
- WaitingForQueue = 'waitingForQueue',
112
- WaitingForNetworkChange = 'waitingForNetworkChange',
113
- NetworkChanged = 'networkChanged',
114
- }
115
-
116
- export type SwapExplorerUrl = {
117
- url: string;
118
- description: string | null;
119
- };
120
-
121
- export type StepStatus =
122
- | 'created'
123
- | 'running'
124
- | 'failed'
125
- | 'success'
126
- | 'waitingForApproval'
127
- | 'approved';
128
-
129
- export type PendingSwapStep = {
130
- // routing data
131
- id: number;
132
- fromBlockchain: string;
133
- fromSymbol: string;
134
- fromSymbolAddress: string | null;
135
- fromDecimals: number;
136
- fromAmountPrecision: string | null;
137
- fromAmountMinValue: string | null;
138
- fromAmountMaxValue: string | null;
139
- fromAmountRestrictionType: AmountRestrictionType | null;
140
- fromLogo: string;
141
- toBlockchain: string;
142
- toSymbol: string;
143
- toSymbolAddress: string | null;
144
- toDecimals: number;
145
- toLogo: string;
146
- swapperId: string;
147
- expectedOutputAmountHumanReadable: string | null;
148
- startTransactionTime: number;
149
- internalSteps: SwapperStatusStep[] | null;
150
- estimatedTimeInSeconds: number | null;
151
-
152
- // status data
153
- status: StepStatus;
154
- networkStatus: PendingSwapNetworkStatus | null;
155
- executedTransactionId: string | null;
156
- executedTransactionTime: string | null;
157
- explorerUrl: SwapExplorerUrl[] | null;
158
- diagnosisUrl: string | null;
159
- outputAmount: string | null;
160
-
161
- // txs data
162
- cosmosTransaction: CosmosTransaction | null;
163
- transferTransaction: TransferTransaction | null;
164
- solanaTransaction: SolanaTransaction | null;
165
- evmApprovalTransaction: EvmTransaction | null;
166
- evmTransaction: EvmTransaction | null;
167
- tronApprovalTransaction: TronTransaction | null;
168
- tronTransaction: TronTransaction | null;
169
- starknetApprovalTransaction: StarknetTransaction | null;
170
- starknetTransaction: StarknetTransaction | null;
171
- tonTransaction: TonTransaction | null;
172
-
173
- // missing fields in older versions
174
- // keeping null for backward compatability
175
- swapperLogo: string | null;
176
- swapperType: string | null;
177
- fromBlockchainLogo: string | null;
178
- toBlockchainLogo: string | null;
179
- feeInUsd: string | null;
180
- };
181
-
182
- export type WalletTypeAndAddress = {
183
- walletType: WalletType;
184
- address: string;
185
- };
186
-
187
87
  export enum MessageSeverity {
188
88
  error = 'error',
189
89
  warning = 'warning',
@@ -193,28 +93,6 @@ export enum MessageSeverity {
193
93
 
194
94
  export type SwapStatus = 'running' | 'failed' | 'success';
195
95
 
196
- export type PendingSwap = {
197
- creationTime: string;
198
- finishTime: string | null;
199
- requestId: string;
200
- inputAmount: string;
201
- status: SwapStatus;
202
- isPaused: boolean;
203
- extraMessage: string | null;
204
- extraMessageSeverity: MessageSeverity | null;
205
- extraMessageErrorCode: string | null;
206
- extraMessageDetail: string | null | undefined;
207
- networkStatusExtraMessage: string | null;
208
- networkStatusExtraMessageDetail: string | null;
209
- lastNotificationTime: string | null;
210
- wallets: { [p: string]: WalletTypeAndAddress };
211
- settings: SwapSavedSettings;
212
- steps: PendingSwapStep[];
213
- simulationResult: SimulationResult;
214
- validateBalanceOrFee: boolean;
215
- hasAlreadyProceededToSign?: boolean | null;
216
- };
217
-
218
96
  export const getCurrentBlockchainOfOrNull = (
219
97
  swap: PendingSwap,
220
98
  step: PendingSwapStep
@@ -240,16 +118,22 @@ export const getCurrentBlockchainOf = (
240
118
  step.cosmosTransaction?.blockChain ||
241
119
  step.solanaTransaction?.blockChain ||
242
120
  step.tonTransaction?.blockChain;
243
- if (b1) return b1;
121
+ if (b1) {
122
+ return b1;
123
+ }
244
124
 
245
125
  const transferAddress = step.transferTransaction?.fromWalletAddress;
246
- if (!transferAddress) throw PrettyError.BlockchainMissing();
126
+ if (!transferAddress) {
127
+ throw PrettyError.BlockchainMissing();
128
+ }
247
129
 
248
130
  const blockchain =
249
131
  Object.keys(swap.wallets).find(
250
132
  (b) => swap.wallets[b]?.address === transferAddress
251
133
  ) || null;
252
- if (blockchain == null) throw PrettyError.BlockchainMissing();
134
+ if (blockchain == null) {
135
+ throw PrettyError.BlockchainMissing();
136
+ }
253
137
 
254
138
  return blockchain;
255
139
  };
@@ -257,14 +141,15 @@ export const getCurrentBlockchainOf = (
257
141
  const getBlockchainMetaExplorerBaseUrl = (
258
142
  blockchainMeta: BlockchainMeta
259
143
  ): string | undefined => {
260
- if (isCosmosBlockchain(blockchainMeta))
144
+ if (isCosmosBlockchain(blockchainMeta)) {
261
145
  return blockchainMeta.info?.explorerUrlToTx;
262
- else if (
146
+ } else if (
263
147
  isEvmBlockchain(blockchainMeta) ||
264
148
  isStarknetBlockchain(blockchainMeta) ||
265
149
  isTronBlockchain(blockchainMeta)
266
- )
150
+ ) {
267
151
  return blockchainMeta.info.transactionUrl;
152
+ }
268
153
  return;
269
154
  };
270
155
 
@@ -275,9 +160,12 @@ export const getScannerUrl = (
275
160
  ): string | undefined => {
276
161
  const blockchainMeta = blockchainMetaMap[network];
277
162
  const baseUrl = getBlockchainMetaExplorerBaseUrl(blockchainMeta);
278
- if (!baseUrl) return;
279
- if (baseUrl.indexOf('/{txHash}') !== -1)
163
+ if (!baseUrl) {
164
+ return;
165
+ }
166
+ if (baseUrl.indexOf('/{txHash}') !== -1) {
280
167
  return baseUrl.replace('{txHash}', txHash?.toLowerCase());
168
+ }
281
169
  return `${baseUrl}/${txHash?.toLowerCase()}`;
282
170
  };
283
171
 
@@ -316,7 +204,9 @@ export const getCurrentAddressOf = (
316
204
  ? { address: step.transferTransaction?.fromWalletAddress }
317
205
  : null) ||
318
206
  null;
319
- if (result == null) throw PrettyError.WalletMissing();
207
+ if (result == null) {
208
+ throw PrettyError.WalletMissing();
209
+ }
320
210
  return result.address;
321
211
  };
322
212
 
@@ -333,17 +223,21 @@ export function getRelatedWallet(
333
223
  const wallet = walletKV?.v || null;
334
224
 
335
225
  const walletType = wallet?.walletType;
336
- if (wallet === null)
226
+ if (wallet === null) {
337
227
  throw PrettyError.AssertionFailed(
338
228
  `Wallet for source ${blockchain} not passed: walletType: ${walletType}`
339
229
  );
230
+ }
340
231
  return wallet;
341
232
  }
342
233
 
343
234
  export function getRelatedWalletOrNull(
344
235
  swap: PendingSwap,
345
- currentStep: PendingSwapStep
236
+ currentStep: PendingSwapStep | null
346
237
  ): WalletTypeAndAddress | null {
238
+ if (!currentStep) {
239
+ return null;
240
+ }
347
241
  try {
348
242
  return getRelatedWallet(swap, currentStep);
349
243
  } catch (e) {
@@ -373,7 +267,9 @@ export function getUsdFeeOfStep(
373
267
  let totalFeeInUsd = new BigNumber(0);
374
268
  for (let i = 0; i < step.fee.length; i++) {
375
269
  const fee = step.fee[i];
376
- if (fee.expenseType === 'DECREASE_FROM_OUTPUT') continue;
270
+ if (fee.expenseType === 'DECREASE_FROM_OUTPUT') {
271
+ continue;
272
+ }
377
273
 
378
274
  const unitPrice = getUsdPrice(
379
275
  fee.asset.blockchain,
@@ -389,16 +285,61 @@ export function getUsdFeeOfStep(
389
285
  return totalFeeInUsd;
390
286
  }
391
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
+
392
331
  export function calculatePendingSwap(
393
332
  inputAmount: string,
394
333
  bestRoute: BestRouteResponse,
395
334
  wallets: { [p: string]: WalletTypeAndAddress },
396
335
  settings: SwapSavedSettings,
397
336
  validateBalanceOrFee: boolean,
398
- meta: MetaResponse | null
337
+ meta: Pick<MetaResponse, 'blockchains' | 'tokens'> | null
399
338
  ): PendingSwap {
400
339
  const simulationResult = bestRoute.result;
401
- if (!simulationResult) throw Error('Simulation result should not be null');
340
+ if (!simulationResult) {
341
+ throw Error('Simulation result should not be null');
342
+ }
402
343
 
403
344
  return {
404
345
  creationTime: new Date().getTime().toString(),
@@ -420,43 +361,23 @@ export function calculatePendingSwap(
420
361
  validateBalanceOrFee,
421
362
  steps:
422
363
  bestRoute.result?.swaps?.map((swap, index) => {
364
+ const stepRoute = mapSwapStepToPendingSwapStep(swap, meta);
423
365
  return {
424
366
  id: index + 1,
425
367
 
426
- // from
427
- fromBlockchain: swap.from.blockchain,
428
- fromBlockchainLogo: swap.from.blockchainLogo,
429
- fromLogo: swap.from.logo,
430
- fromSymbol: swap.from.symbol,
431
- fromSymbolAddress: swap.from.address,
432
- fromDecimals: swap.from.decimals,
433
- fromAmountPrecision: swap.fromAmountPrecision,
434
- fromAmountMinValue: swap.fromAmountMinValue,
435
- fromAmountMaxValue: swap.fromAmountMaxValue,
436
- fromAmountRestrictionType: swap.fromAmountRestrictionType,
437
-
438
- // to
439
- toBlockchain: swap.to.blockchain,
440
- toBlockchainLogo: swap.to.blockchainLogo,
441
- toSymbol: swap.to.symbol,
442
- toSymbolAddress: swap.to.address,
443
- toDecimals: swap.to.decimals,
444
- toLogo: swap.to.logo,
445
-
446
- // swapper
447
- swapperId: swap.swapperId,
448
- swapperLogo: swap.swapperLogo,
449
- swapperType: swap.swapperType,
450
-
451
- // output, fee, timing
452
- expectedOutputAmountHumanReadable: swap.toAmount,
453
- outputAmount: '',
454
- feeInUsd: meta
455
- ? numberToString(getUsdFeeOfStep(swap, meta?.tokens), null, 8)
456
- : null,
457
- 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,
458
378
 
459
379
  // status, tracking
380
+ outputAmount: '',
460
381
  status: 'created',
461
382
  networkStatus: null,
462
383
  startTransactionTime: new Date().getTime(),
package/src/types.ts CHANGED
@@ -1,15 +1,25 @@
1
- import { QueueStorage, QueueDef } from '@rango-dev/queue-manager-core';
2
- import { QueueContext } from '@rango-dev/queue-manager-core/dist/queue';
3
- import { ConnectResult, Providers } from '@rango-dev/wallets-react';
4
- import {
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';
8
+ import type {
5
9
  Meta,
6
10
  Network,
11
+ Providers,
7
12
  WalletState,
8
13
  WalletType,
9
14
  } from '@rango-dev/wallets-shared';
10
- import { APIErrorCode, EvmBlockchainMeta, SignerFactory } from 'rango-types';
11
- import { Transaction } from 'rango-sdk';
12
- import { PendingSwap, PendingSwapStep, Wallet } from './shared';
15
+ import type { Transaction } from 'rango-sdk';
16
+ import type {
17
+ APIErrorCode,
18
+ EvmBlockchainMeta,
19
+ PendingSwap,
20
+ PendingSwapStep,
21
+ SignerFactory,
22
+ } from 'rango-types';
13
23
 
14
24
  export type RemoveNameField<T, U extends string> = {
15
25
  [Property in keyof T as Exclude<Property, U>]: T[Property];
@@ -61,7 +71,7 @@ export interface SwapQueueContext extends QueueContext {
61
71
  switchNetwork: (
62
72
  wallet: WalletType,
63
73
  network: Network
64
- ) => Promise<ConnectResult> | undefined;
74
+ ) => Promise<ConnectResult | undefined> | undefined;
65
75
  canSwitchNetworkTo: (type: WalletType, network: Network) => boolean;
66
76
  connect: (
67
77
  wallet: WalletType,