@rango-dev/queue-manager-rango-preset 0.1.10-next.98 → 0.1.11-next.2

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 (41) hide show
  1. package/dist/actions/checkStatus.d.ts.map +1 -1
  2. package/dist/actions/createTransaction.d.ts.map +1 -1
  3. package/dist/actions/scheduleNextStep.d.ts.map +1 -1
  4. package/dist/constants.d.ts +1 -1
  5. package/dist/constants.d.ts.map +1 -1
  6. package/dist/helpers.d.ts +41 -13
  7. package/dist/helpers.d.ts.map +1 -1
  8. package/dist/hooks.d.ts.map +1 -1
  9. package/dist/index.d.ts +4 -11
  10. package/dist/index.d.ts.map +1 -1
  11. package/dist/numbers.d.ts +3 -0
  12. package/dist/numbers.d.ts.map +1 -0
  13. package/dist/queue-manager-rango-preset.cjs.development.js +511 -300
  14. package/dist/queue-manager-rango-preset.cjs.development.js.map +1 -1
  15. package/dist/queue-manager-rango-preset.cjs.production.min.js +1 -1
  16. package/dist/queue-manager-rango-preset.cjs.production.min.js.map +1 -1
  17. package/dist/queue-manager-rango-preset.esm.js +488 -286
  18. package/dist/queue-manager-rango-preset.esm.js.map +1 -1
  19. package/dist/shared-errors.d.ts +5 -37
  20. package/dist/shared-errors.d.ts.map +1 -1
  21. package/dist/shared-sentry.d.ts +1 -1
  22. package/dist/shared-sentry.d.ts.map +1 -1
  23. package/dist/shared.d.ts +35 -21
  24. package/dist/shared.d.ts.map +1 -1
  25. package/dist/types.d.ts +1 -0
  26. package/dist/types.d.ts.map +1 -1
  27. package/package.json +2 -2
  28. package/readme.md +1 -9
  29. package/src/actions/checkStatus.ts +54 -3
  30. package/src/actions/createTransaction.ts +2 -3
  31. package/src/actions/executeTransaction.ts +3 -3
  32. package/src/actions/scheduleNextStep.ts +1 -0
  33. package/src/constants.ts +1 -1
  34. package/src/helpers.ts +202 -172
  35. package/src/hooks.ts +2 -1
  36. package/src/index.ts +19 -44
  37. package/src/numbers.ts +68 -0
  38. package/src/shared-errors.ts +53 -76
  39. package/src/shared-sentry.ts +1 -1
  40. package/src/shared.ts +187 -58
  41. package/src/types.ts +1 -0
package/src/shared.ts CHANGED
@@ -8,10 +8,16 @@ import {
8
8
  StarknetTransaction,
9
9
  TronTransaction,
10
10
  Transfer as TransferTransaction,
11
+ AmountRestrictionType,
12
+ BestRouteResponse,
13
+ MetaResponse,
14
+ Token,
15
+ SwapResult,
11
16
  } from 'rango-sdk';
12
17
 
13
- import { ErrorDetail, PrettyError } from './shared-errors';
14
- import { SignerError } from 'rango-types';
18
+ import { PrettyError } from './shared-errors';
19
+ import BigNumber from 'bignumber.js';
20
+ import { numberToString } from './numbers';
15
21
 
16
22
  export interface PendingSwapWithQueueID {
17
23
  id: string;
@@ -57,8 +63,10 @@ export type EventType =
57
63
  | 'task_canceled'
58
64
  | 'task_paused'
59
65
  | 'contract_confirmed'
66
+ | 'confirm_approve_contract'
60
67
  | 'contract_rejected'
61
- | 'transfer_confirmed'
68
+ | 'check_tx_status'
69
+ | 'check_approve_tx_status'
62
70
  | 'transfer_rejected'
63
71
  | 'calling_smart_contract'
64
72
  | 'smart_contract_called'
@@ -66,17 +74,19 @@ export type EventType =
66
74
  | 'step_completed_with_output'
67
75
  | 'waiting_for_network_change'
68
76
  | 'waiting_for_connecting_wallet'
77
+ | 'waiting_for_change_wallet_account'
69
78
  | 'network_changed'
70
79
  | 'not_enough_balance'
80
+ | 'waiting_for_queue'
71
81
  | 'check_fee_failed'
72
82
  | 'route_failed_to_find'
73
83
  | 'transaction_expired';
74
84
 
75
85
  export type SwapSavedSettings = {
76
86
  slippage: string;
77
- disabledSwappersIds: string[];
78
- disabledSwappersGroups: string[];
79
- infiniteApprove: boolean;
87
+ disabledSwappersIds?: string[];
88
+ disabledSwappersGroups?: string[];
89
+ infiniteApprove?: boolean;
80
90
  };
81
91
 
82
92
  type InternalStepState =
@@ -105,19 +115,6 @@ export type SwapExplorerUrl = {
105
115
  description: string | null;
106
116
  };
107
117
 
108
- export type SwapperId =
109
- | 'ThorChain'
110
- | 'OneInchEth'
111
- | 'Binance Bridge'
112
- | 'OneInchBsc'
113
- | 'OneInchPolygon'
114
- | 'Terra Bridge'
115
- | 'TerraSwap'
116
- | 'Osmosis'
117
- | 'Lido'
118
- | 'PoS Bridge'
119
- | 'Wormhole';
120
-
121
118
  export type StepStatus =
122
119
  | 'created'
123
120
  | 'running'
@@ -127,39 +124,55 @@ export type StepStatus =
127
124
  | 'approved';
128
125
 
129
126
  export type PendingSwapStep = {
127
+ // routing data
130
128
  id: number;
131
- fromBlockchain: Network;
129
+ fromBlockchain: string;
132
130
  fromSymbol: string;
133
131
  fromSymbolAddress: string | null;
134
132
  fromDecimals: number;
135
- fromAmountPrecision: number | null;
136
- fromAmountMinValue: number | null;
137
- fromAmountMaxValue: number | null;
133
+ fromAmountPrecision: string | null;
134
+ fromAmountMinValue: string | null;
135
+ fromAmountMaxValue: string | null;
136
+ fromAmountRestrictionType: AmountRestrictionType | null;
138
137
  fromLogo: string;
139
138
  toBlockchain: string;
140
139
  toSymbol: string;
141
140
  toSymbolAddress: string | null;
142
141
  toDecimals: number;
143
142
  toLogo: string;
144
- swapperId: SwapperId;
143
+ swapperId: string;
145
144
  expectedOutputAmountHumanReadable: string | null;
146
145
  startTransactionTime: number;
147
- outputAmount: string | null;
146
+ internalSteps: SwapperStatusStep[] | null;
147
+ estimatedTimeInSeconds: number | null;
148
+
149
+ // status data
148
150
  status: StepStatus;
149
151
  networkStatus: PendingSwapNetworkStatus | null;
150
152
  executedTransactionId: string | null;
153
+ executedTransactionTime: string | null;
151
154
  explorerUrl: SwapExplorerUrl[] | null;
152
- evmApprovalTransaction: EvmTransaction | null;
153
- evmTransaction: EvmTransaction | null;
155
+ diagnosisUrl: string | null;
156
+ outputAmount: string | null;
157
+
158
+ // txs data
154
159
  cosmosTransaction: CosmosTransaction | null;
155
160
  transferTransaction: TransferTransaction | null;
156
161
  solanaTransaction: SolanaTransaction | null;
157
- starknetTransaction: StarknetTransaction | null;
158
- starknetApprovalTransaction: StarknetTransaction | null;
159
- tronTransaction: TronTransaction | null;
162
+ evmApprovalTransaction: EvmTransaction | null;
163
+ evmTransaction: EvmTransaction | null;
160
164
  tronApprovalTransaction: TronTransaction | null;
161
- diagnosisUrl: string | null;
162
- internalSteps: SwapperStatusStep[] | null;
165
+ tronTransaction: TronTransaction | null;
166
+ starknetApprovalTransaction: StarknetTransaction | null;
167
+ starknetTransaction: StarknetTransaction | null;
168
+
169
+ // missing fields in older versions
170
+ // keeping null for backward compatability
171
+ swapperLogo: string | null;
172
+ swapperType: string | null;
173
+ fromBlockchainLogo: string | null;
174
+ toBlockchainLogo: string | null;
175
+ feeInUsd: string | null;
163
176
  };
164
177
 
165
178
  export type WalletTypeAndAddress = {
@@ -273,30 +286,6 @@ export const getTronApproveUrl = (tx: string): string => {
273
286
  );
274
287
  };
275
288
 
276
- export const prettifyErrorMessage = (obj: unknown): ErrorDetail => {
277
- if (!obj) return { extraMessage: '', extraMessageErrorCode: null };
278
- if (obj instanceof PrettyError) return obj.getErrorDetail();
279
- if (obj instanceof SignerError) {
280
- const t = obj.getErrorDetail();
281
- return {
282
- extraMessage: t.message,
283
- extraMessageDetail: t.detail,
284
- extraMessageErrorCode: t.code,
285
- };
286
- }
287
- if (obj instanceof Error)
288
- return {
289
- extraMessage: obj.toString(),
290
- extraMessageErrorCode: null,
291
- };
292
- if (typeof obj !== 'string')
293
- return {
294
- extraMessage: JSON.stringify(obj),
295
- extraMessageErrorCode: null,
296
- };
297
- return { extraMessage: obj, extraMessageErrorCode: null };
298
- };
299
-
300
289
  export function getNextStep(
301
290
  swap: PendingSwap,
302
291
  currentStep: PendingSwapStep
@@ -311,7 +300,9 @@ export function getNextStep(
311
300
  );
312
301
  }
313
302
 
314
- // TODO: we have samething in `helpers`, for fixing circular dependency, we copied and should be removed eventually.
303
+ /**
304
+ * Returns the wallet address, based on the current step of `PendingSwap`.
305
+ */
315
306
  export const getCurrentAddressOf = (
316
307
  swap: PendingSwap,
317
308
  step: PendingSwapStep
@@ -319,6 +310,10 @@ export const getCurrentAddressOf = (
319
310
  const result =
320
311
  swap.wallets[step.evmTransaction?.blockChain || ''] ||
321
312
  swap.wallets[step.evmApprovalTransaction?.blockChain || ''] ||
313
+ swap.wallets[step.tronTransaction?.blockChain || ''] ||
314
+ swap.wallets[step.tronApprovalTransaction?.blockChain || ''] ||
315
+ swap.wallets[step.starknetTransaction?.blockChain || ''] ||
316
+ swap.wallets[step.starknetApprovalTransaction?.blockChain || ''] ||
322
317
  swap.wallets[step.cosmosTransaction?.blockChain || ''] ||
323
318
  swap.wallets[step.solanaTransaction?.blockChain || ''] ||
324
319
  (step.transferTransaction?.fromWalletAddress
@@ -329,7 +324,6 @@ export const getCurrentAddressOf = (
329
324
  return result.address;
330
325
  };
331
326
 
332
- // TODO: we have samething in `helpers`, for fixing circular dependency, we copied and should be removed eventually.
333
327
  export function getRelatedWallet(
334
328
  swap: PendingSwap,
335
329
  currentStep: PendingSwapStep
@@ -360,3 +354,138 @@ export function getRelatedWalletOrNull(
360
354
  return null;
361
355
  }
362
356
  }
357
+
358
+ export const getUsdPrice = (
359
+ blockchain: string,
360
+ symbol: string,
361
+ address: string | null,
362
+ allTokens: Token[]
363
+ ): number | null => {
364
+ const token = allTokens?.find(
365
+ (t) =>
366
+ t.blockchain === blockchain &&
367
+ t.symbol?.toUpperCase() === symbol?.toUpperCase() &&
368
+ t.address === address
369
+ );
370
+ return token?.usdPrice || null;
371
+ };
372
+
373
+ export function getUsdFeeOfStep(
374
+ step: SwapResult,
375
+ allTokens: Token[]
376
+ ): BigNumber {
377
+ let totalFeeInUsd = new BigNumber(0);
378
+ for (let i = 0; i < step.fee.length; i++) {
379
+ const fee = step.fee[i];
380
+ if (fee.expenseType === 'DECREASE_FROM_OUTPUT') continue;
381
+
382
+ const unitPrice = getUsdPrice(
383
+ fee.asset.blockchain,
384
+ fee.asset.symbol,
385
+ fee.asset.address,
386
+ allTokens
387
+ );
388
+ totalFeeInUsd = totalFeeInUsd.plus(
389
+ new BigNumber(fee.amount).multipliedBy(unitPrice || 0)
390
+ );
391
+ }
392
+
393
+ return totalFeeInUsd;
394
+ }
395
+
396
+ export function calculatePendingSwap(
397
+ inputAmount: string,
398
+ bestRoute: BestRouteResponse,
399
+ wallets: { [p: string]: WalletTypeAndAddress },
400
+ settings: SwapSavedSettings,
401
+ validateBalanceOrFee: boolean,
402
+ meta: MetaResponse | null
403
+ ): PendingSwap {
404
+ const simulationResult = bestRoute.result;
405
+ if (!simulationResult) throw Error('Simulation result should not be null');
406
+
407
+ return {
408
+ creationTime: new Date().getTime().toString(),
409
+ finishTime: null,
410
+ requestId: bestRoute.requestId || '',
411
+ inputAmount: inputAmount,
412
+ wallets,
413
+ status: 'running',
414
+ isPaused: false,
415
+ extraMessage: null,
416
+ extraMessageSeverity: null,
417
+ extraMessageDetail: null,
418
+ extraMessageErrorCode: null,
419
+ networkStatusExtraMessage: null,
420
+ networkStatusExtraMessageDetail: null,
421
+ lastNotificationTime: null,
422
+ settings: settings,
423
+ simulationResult: simulationResult,
424
+ validateBalanceOrFee,
425
+ steps:
426
+ bestRoute.result?.swaps?.map((swap, index) => {
427
+ return {
428
+ id: index + 1,
429
+
430
+ // from
431
+ fromBlockchain: swap.from.blockchain,
432
+ fromBlockchainLogo: swap.from.blockchainLogo,
433
+ fromLogo: swap.from.logo,
434
+ fromSymbol: swap.from.symbol,
435
+ fromSymbolAddress: swap.from.address,
436
+ fromDecimals: swap.from.decimals,
437
+ fromAmountPrecision: swap.fromAmountPrecision,
438
+ fromAmountMinValue: swap.fromAmountMinValue,
439
+ fromAmountMaxValue: swap.fromAmountMaxValue,
440
+ fromAmountRestrictionType: swap.fromAmountRestrictionType,
441
+
442
+ // to
443
+ toBlockchain: swap.to.blockchain,
444
+ toBlockchainLogo: swap.to.blockchainLogo,
445
+ toSymbol: swap.to.symbol,
446
+ toSymbolAddress: swap.to.address,
447
+ toDecimals: swap.to.decimals,
448
+ toLogo: swap.to.logo,
449
+
450
+ // swapper
451
+ swapperId: swap.swapperId,
452
+ swapperLogo: swap.swapperLogo,
453
+ swapperType: swap.swapperType,
454
+
455
+ // output, fee, timing
456
+ expectedOutputAmountHumanReadable: swap.toAmount,
457
+ outputAmount: '',
458
+ feeInUsd: meta
459
+ ? numberToString(getUsdFeeOfStep(swap, meta?.tokens), null, 8)
460
+ : null,
461
+ estimatedTimeInSeconds: swap.estimatedTimeInSeconds || null,
462
+
463
+ // status, tracking
464
+ status: 'created',
465
+ networkStatus: null,
466
+ startTransactionTime: new Date().getTime(),
467
+ externalTransactionId: null,
468
+ executedTransactionId: null,
469
+ executedTransactionTime: null,
470
+ explorerUrl: null,
471
+ diagnosisUrl: null,
472
+ trackingCode: null,
473
+ internalSteps: null,
474
+
475
+ // transactions
476
+ evmTransaction: null,
477
+ evmApprovalTransaction: null,
478
+ starknetTransaction: null,
479
+ starknetApprovalTransaction: null,
480
+ tronTransaction: null,
481
+ tronApprovalTransaction: null,
482
+ cosmosTransaction: null,
483
+ solanaTransaction: null,
484
+ transferTransaction: null,
485
+
486
+ // front fields
487
+ hasAlreadyProceededToSign: false,
488
+ };
489
+ }) || [],
490
+ };
491
+ }
package/src/types.ts CHANGED
@@ -72,4 +72,5 @@ export interface UseQueueManagerParams {
72
72
  disconnectedWallet: WalletType | undefined;
73
73
  clearDisconnectedWallet: () => void;
74
74
  evmChains: EvmBlockchainMeta[];
75
+ notifier: SwapQueueContext['notifier'];
75
76
  }