@rango-dev/queue-manager-rango-preset 0.23.0 → 0.24.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.
@@ -1,30 +1,26 @@
1
- import type {
1
+ import mitt from 'mitt';
2
+ import {
3
+ MainEvents,
2
4
  RemoveNameField,
3
5
  Route,
4
6
  RouteEvent,
7
+ RouteEventType,
5
8
  RouteExecutionEvents,
9
+ EventSeverity,
6
10
  Step,
7
11
  StepEvent,
12
+ StepEventType,
13
+ StepExecutionEventStatus,
14
+ StepExecutionBlockedEventStatus,
8
15
  } from '../types';
9
- import type { PendingSwap, PendingSwapStep } from 'rango-types';
10
-
11
- import mitt from 'mitt';
12
-
16
+ import { PendingSwap, PendingSwapStep } from 'rango-types/lib';
17
+ import { getCurrentBlockchainOfOrNull } from '../shared';
13
18
  import {
14
19
  getCurrentStepTx,
15
20
  getFailedStep,
16
21
  getLastSuccessfulStep,
17
22
  isApprovalCurrentStepTx,
18
23
  } from '../helpers';
19
- import { getCurrentBlockchainOfOrNull } from '../shared';
20
- import {
21
- EventSeverity,
22
- MainEvents,
23
- RouteEventType,
24
- StepEventType,
25
- StepExecutionBlockedEventStatus,
26
- StepExecutionEventStatus,
27
- } from '../types';
28
24
 
29
25
  type NotifierParams = {
30
26
  swap: PendingSwap;
@@ -123,19 +119,14 @@ function getEventPayload(
123
119
  route,
124
120
  step: routeSteps[routeSteps.length - 1],
125
121
  };
126
- if (swapStep) {
127
- result.step = createSteps([swapStep])[0];
128
- } else {
122
+ if (swapStep) result.step = createSteps([swapStep])[0];
123
+ else {
129
124
  if (type === 'failed') {
130
125
  const failedStep = getFailedStep(routeSteps);
131
- if (failedStep) {
132
- result.step = failedStep;
133
- }
126
+ if (failedStep) result.step = failedStep;
134
127
  } else {
135
128
  const lastSuccessfulStep = getLastSuccessfulStep(routeSteps);
136
- if (lastSuccessfulStep) {
137
- result.step = lastSuccessfulStep;
138
- }
129
+ if (lastSuccessfulStep) result.step = lastSuccessfulStep;
139
130
  }
140
131
  }
141
132
 
@@ -160,9 +151,8 @@ function emitRouteEvent(stepEvent: StepEvent, route: Route) {
160
151
  default:
161
152
  break;
162
153
  }
163
- if (routeEvent) {
154
+ if (routeEvent)
164
155
  eventEmitter.emit(MainEvents.RouteEvent, { event: routeEvent, route });
165
- }
166
156
  }
167
157
 
168
158
  function emitStepEvent(stepEvent: StepEvent, route: Route, step: Step) {
@@ -206,11 +196,9 @@ export function notifier(params: NotifierParams) {
206
196
  message = 'Please wait while the transaction is created ...';
207
197
  messageSeverity = EventSeverity.INFO;
208
198
  } else if (event.status === StepExecutionEventStatus.SEND_TX) {
209
- if (params.step && isApprovalCurrentStepTx(params.step)) {
199
+ if (params.step && isApprovalCurrentStepTx(params.step))
210
200
  message = `Please confirm '${step.swapperName}' smart contract access to ${fromAsset}`;
211
- } else {
212
- message = 'Please confirm transaction request in your wallet';
213
- }
201
+ else message = 'Please confirm transaction request in your wallet';
214
202
  messageSeverity = EventSeverity.WARNING;
215
203
  } else if (event.status === StepExecutionEventStatus.TX_SENT) {
216
204
  message = 'Transaction sent successfully';
@@ -218,11 +206,9 @@ export function notifier(params: NotifierParams) {
218
206
  }
219
207
  break;
220
208
  case StepEventType.CHECK_STATUS:
221
- if (params.step && isApprovalCurrentStepTx(params.step)) {
222
- message = 'Checking approve transaction status ...';
223
- } else {
224
- message = 'Checking transaction status ...';
225
- }
209
+ if (params.step && isApprovalCurrentStepTx(params.step))
210
+ message = 'Checking approve transacation status ...';
211
+ else message = 'Checking transacation status ...';
226
212
  messageSeverity = EventSeverity.INFO;
227
213
  break;
228
214
  case StepEventType.APPROVAL_TX_SUCCEEDED:
@@ -265,10 +251,7 @@ export function notifier(params: NotifierParams) {
265
251
  break;
266
252
  }
267
253
 
268
- if (params.step) {
254
+ if (params.step)
269
255
  emitStepEvent({ ...event, message, messageSeverity }, route, step);
270
- }
271
- if (params.event.type === StepEventType.FAILED || !params.step) {
272
- emitRouteEvent({ ...event, message, messageSeverity }, route);
273
- }
256
+ else emitRouteEvent({ ...event, message, messageSeverity }, route);
274
257
  }
@@ -1,7 +1,6 @@
1
- import type { WalletType } from '@rango-dev/wallets-shared';
2
- import type { PendingSwap, PendingSwapStep } from 'rango-types';
3
-
4
1
  import * as Sentry from '@sentry/browser';
2
+ import { PendingSwap, PendingSwapStep } from './shared';
3
+ import { WalletType } from '@rango-dev/wallets-shared';
5
4
 
6
5
  export function logRPCError(
7
6
  error: unknown,
package/src/shared.ts CHANGED
@@ -1,30 +1,31 @@
1
- import type { Network, WalletType } from '@rango-dev/wallets-shared';
2
- import type {
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,
3
11
  BestRouteResponse,
4
- BlockchainMeta,
5
12
  MetaResponse,
6
- SwapResult,
7
13
  Token,
14
+ SwapResult,
15
+ BlockchainMeta,
8
16
  } from 'rango-sdk';
9
- import type {
10
- PendingSwap,
11
- PendingSwapStep,
12
- SwapSavedSettings,
13
- SwapStepRoute,
14
- WalletTypeAndAddress,
15
- } from 'rango-types';
16
17
 
18
+ import { PrettyError } from './shared-errors';
17
19
  import BigNumber from 'bignumber.js';
20
+ import { numberToString } from './numbers';
18
21
  import {
22
+ TonTransaction,
19
23
  isCosmosBlockchain,
20
24
  isEvmBlockchain,
21
25
  isStarknetBlockchain,
22
26
  isTronBlockchain,
23
27
  } from 'rango-types';
24
28
 
25
- import { numberToString } from './numbers';
26
- import { PrettyError } from './shared-errors';
27
-
28
29
  export interface PendingSwapWithQueueID {
29
30
  id: string;
30
31
  swap: PendingSwap;
@@ -84,6 +85,105 @@ export type EventType =
84
85
  | 'route_failed_to_find'
85
86
  | 'transaction_expired';
86
87
 
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
+
87
187
  export enum MessageSeverity {
88
188
  error = 'error',
89
189
  warning = 'warning',
@@ -93,6 +193,28 @@ export enum MessageSeverity {
93
193
 
94
194
  export type SwapStatus = 'running' | 'failed' | 'success';
95
195
 
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
+
96
218
  export const getCurrentBlockchainOfOrNull = (
97
219
  swap: PendingSwap,
98
220
  step: PendingSwapStep
@@ -118,22 +240,16 @@ export const getCurrentBlockchainOf = (
118
240
  step.cosmosTransaction?.blockChain ||
119
241
  step.solanaTransaction?.blockChain ||
120
242
  step.tonTransaction?.blockChain;
121
- if (b1) {
122
- return b1;
123
- }
243
+ if (b1) return b1;
124
244
 
125
245
  const transferAddress = step.transferTransaction?.fromWalletAddress;
126
- if (!transferAddress) {
127
- throw PrettyError.BlockchainMissing();
128
- }
246
+ if (!transferAddress) throw PrettyError.BlockchainMissing();
129
247
 
130
248
  const blockchain =
131
249
  Object.keys(swap.wallets).find(
132
250
  (b) => swap.wallets[b]?.address === transferAddress
133
251
  ) || null;
134
- if (blockchain == null) {
135
- throw PrettyError.BlockchainMissing();
136
- }
252
+ if (blockchain == null) throw PrettyError.BlockchainMissing();
137
253
 
138
254
  return blockchain;
139
255
  };
@@ -141,15 +257,14 @@ export const getCurrentBlockchainOf = (
141
257
  const getBlockchainMetaExplorerBaseUrl = (
142
258
  blockchainMeta: BlockchainMeta
143
259
  ): string | undefined => {
144
- if (isCosmosBlockchain(blockchainMeta)) {
260
+ if (isCosmosBlockchain(blockchainMeta))
145
261
  return blockchainMeta.info?.explorerUrlToTx;
146
- } else if (
262
+ else if (
147
263
  isEvmBlockchain(blockchainMeta) ||
148
264
  isStarknetBlockchain(blockchainMeta) ||
149
265
  isTronBlockchain(blockchainMeta)
150
- ) {
266
+ )
151
267
  return blockchainMeta.info.transactionUrl;
152
- }
153
268
  return;
154
269
  };
155
270
 
@@ -160,12 +275,9 @@ export const getScannerUrl = (
160
275
  ): string | undefined => {
161
276
  const blockchainMeta = blockchainMetaMap[network];
162
277
  const baseUrl = getBlockchainMetaExplorerBaseUrl(blockchainMeta);
163
- if (!baseUrl) {
164
- return;
165
- }
166
- if (baseUrl.indexOf('/{txHash}') !== -1) {
278
+ if (!baseUrl) return;
279
+ if (baseUrl.indexOf('/{txHash}') !== -1)
167
280
  return baseUrl.replace('{txHash}', txHash?.toLowerCase());
168
- }
169
281
  return `${baseUrl}/${txHash?.toLowerCase()}`;
170
282
  };
171
283
 
@@ -204,9 +316,7 @@ export const getCurrentAddressOf = (
204
316
  ? { address: step.transferTransaction?.fromWalletAddress }
205
317
  : null) ||
206
318
  null;
207
- if (result == null) {
208
- throw PrettyError.WalletMissing();
209
- }
319
+ if (result == null) throw PrettyError.WalletMissing();
210
320
  return result.address;
211
321
  };
212
322
 
@@ -223,21 +333,17 @@ export function getRelatedWallet(
223
333
  const wallet = walletKV?.v || null;
224
334
 
225
335
  const walletType = wallet?.walletType;
226
- if (wallet === null) {
336
+ if (wallet === null)
227
337
  throw PrettyError.AssertionFailed(
228
338
  `Wallet for source ${blockchain} not passed: walletType: ${walletType}`
229
339
  );
230
- }
231
340
  return wallet;
232
341
  }
233
342
 
234
343
  export function getRelatedWalletOrNull(
235
344
  swap: PendingSwap,
236
- currentStep: PendingSwapStep | null
345
+ currentStep: PendingSwapStep
237
346
  ): WalletTypeAndAddress | null {
238
- if (!currentStep) {
239
- return null;
240
- }
241
347
  try {
242
348
  return getRelatedWallet(swap, currentStep);
243
349
  } catch (e) {
@@ -267,9 +373,7 @@ export function getUsdFeeOfStep(
267
373
  let totalFeeInUsd = new BigNumber(0);
268
374
  for (let i = 0; i < step.fee.length; i++) {
269
375
  const fee = step.fee[i];
270
- if (fee.expenseType === 'DECREASE_FROM_OUTPUT') {
271
- continue;
272
- }
376
+ if (fee.expenseType === 'DECREASE_FROM_OUTPUT') continue;
273
377
 
274
378
  const unitPrice = getUsdPrice(
275
379
  fee.asset.blockchain,
@@ -285,61 +389,16 @@ export function getUsdFeeOfStep(
285
389
  return totalFeeInUsd;
286
390
  }
287
391
 
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
-
331
392
  export function calculatePendingSwap(
332
393
  inputAmount: string,
333
394
  bestRoute: BestRouteResponse,
334
395
  wallets: { [p: string]: WalletTypeAndAddress },
335
396
  settings: SwapSavedSettings,
336
397
  validateBalanceOrFee: boolean,
337
- meta: Pick<MetaResponse, 'blockchains' | 'tokens'> | null
398
+ meta: MetaResponse | null
338
399
  ): PendingSwap {
339
400
  const simulationResult = bestRoute.result;
340
- if (!simulationResult) {
341
- throw Error('Simulation result should not be null');
342
- }
401
+ if (!simulationResult) throw Error('Simulation result should not be null');
343
402
 
344
403
  return {
345
404
  creationTime: new Date().getTime().toString(),
@@ -361,23 +420,43 @@ export function calculatePendingSwap(
361
420
  validateBalanceOrFee,
362
421
  steps:
363
422
  bestRoute.result?.swaps?.map((swap, index) => {
364
- const stepRoute = mapSwapStepToPendingSwapStep(swap, meta);
365
423
  return {
366
424
  id: index + 1,
367
425
 
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,
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,
378
458
 
379
459
  // status, tracking
380
- outputAmount: '',
381
460
  status: 'created',
382
461
  networkStatus: null,
383
462
  startTransactionTime: new Date().getTime(),
package/src/types.ts CHANGED
@@ -1,25 +1,15 @@
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 {
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 {
9
5
  Meta,
10
6
  Network,
11
- Providers,
12
7
  WalletState,
13
8
  WalletType,
14
9
  } from '@rango-dev/wallets-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';
10
+ import { APIErrorCode, EvmBlockchainMeta, SignerFactory } from 'rango-types';
11
+ import { Transaction } from 'rango-sdk';
12
+ import { PendingSwap, PendingSwapStep, Wallet } from './shared';
23
13
 
24
14
  export type RemoveNameField<T, U extends string> = {
25
15
  [Property in keyof T as Exclude<Property, U>]: T[Property];
@@ -71,7 +61,7 @@ export interface SwapQueueContext extends QueueContext {
71
61
  switchNetwork: (
72
62
  wallet: WalletType,
73
63
  network: Network
74
- ) => Promise<ConnectResult | undefined> | undefined;
64
+ ) => Promise<ConnectResult> | undefined;
75
65
  canSwitchNetworkTo: (type: WalletType, network: Network) => boolean;
76
66
  connect: (
77
67
  wallet: WalletType,