@rango-dev/queue-manager-rango-preset 0.5.1-next.2 → 0.5.1-next.20

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
@@ -5,11 +5,16 @@ import {
5
5
  QueueType,
6
6
  } from '@rango-dev/queue-manager-core';
7
7
  import {
8
+ ArrayElement,
8
9
  BlockReason,
10
+ StepEventType,
9
11
  SwapActionTypes,
10
12
  SwapQueueContext,
11
13
  SwapQueueDef,
12
14
  SwapStorage,
15
+ StepExecutionEventStatus,
16
+ StepExecutionBlockedEventStatus,
17
+ Step,
13
18
  } from './types';
14
19
  import {
15
20
  getBlockChainNameFromId,
@@ -26,15 +31,10 @@ import {
26
31
  TransactionType,
27
32
  EvmBlockchainMeta,
28
33
  CreateTransactionResponse,
29
- isEvmTransaction,
30
- isCosmosTransaction,
31
- isSolanaTransaction,
32
- isTronTransaction,
33
- isStarknetTransaction,
34
- isTransferTransaction,
35
34
  } from 'rango-sdk';
36
35
 
37
36
  import {
37
+ DEFAULT_ERROR_CODE,
38
38
  ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK,
39
39
  ERROR_MESSAGE_WAIT_FOR_WALLET,
40
40
  ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION,
@@ -42,7 +42,6 @@ import {
42
42
  import { Manager } from '@rango-dev/queue-manager-core';
43
43
  import { Status } from '@rango-dev/queue-manager-core';
44
44
  import {
45
- EventType,
46
45
  getCurrentBlockchainOf,
47
46
  getCurrentBlockchainOfOrNull,
48
47
  getScannerUrl,
@@ -65,6 +64,7 @@ import {
65
64
  } from './shared-errors';
66
65
  import { httpService } from './services';
67
66
  import { APIErrorCode, SignerErrorCode } from 'rango-types/lib';
67
+ import { notifier } from './services/eventEmitter';
68
68
 
69
69
  type WhenTaskBlocked = Parameters<NonNullable<SwapQueueDef['whenTaskBlocked']>>;
70
70
  type WhenTaskBlockedEvent = WhenTaskBlocked[0];
@@ -102,7 +102,7 @@ type TransactionData = {
102
102
  receiptReceived?: boolean; // e.g. is TransactionReceipt ready in case of EVM transactions
103
103
  };
104
104
  const swapTransactionToDataMap: { [id: string]: TransactionData } = {};
105
- export function useTransactionsData() {
105
+ export function inMemoryTransactionsData() {
106
106
  return {
107
107
  getTransactionDataByHash: (hash: string) =>
108
108
  swapTransactionToDataMap[hash] || {},
@@ -207,24 +207,39 @@ export const setCurrentStepTx = (
207
207
  currentStep.tronApprovalTransaction = null;
208
208
  currentStep.tronTransaction = null;
209
209
 
210
- if (isEvmTransaction(transaction)) {
211
- if (transaction.isApprovalTx)
212
- currentStep.evmApprovalTransaction = transaction;
213
- else currentStep.evmTransaction = transaction;
214
- } else if (isCosmosTransaction(transaction)) {
215
- currentStep.cosmosTransaction = transaction;
216
- } else if (isSolanaTransaction(transaction)) {
217
- currentStep.solanaTransaction = transaction;
218
- } else if (isTransferTransaction(transaction)) {
219
- currentStep.transferTransaction = transaction;
220
- } else if (isStarknetTransaction(transaction)) {
221
- if (transaction.isApprovalTx)
222
- currentStep.starknetApprovalTransaction = transaction;
223
- else currentStep.starknetTransaction = transaction;
224
- } else if (isTronTransaction(transaction)) {
225
- if (transaction.isApprovalTx)
226
- currentStep.tronApprovalTransaction = transaction;
227
- else currentStep.tronTransaction = transaction;
210
+ const txType = transaction.type;
211
+ switch (txType) {
212
+ case TransactionType.EVM:
213
+ if (transaction.isApprovalTx)
214
+ currentStep.evmApprovalTransaction = transaction;
215
+ else currentStep.evmTransaction = transaction;
216
+ break;
217
+ case TransactionType.TRON:
218
+ if (transaction.isApprovalTx)
219
+ currentStep.tronApprovalTransaction = transaction;
220
+ else currentStep.tronTransaction = transaction;
221
+ break;
222
+ case TransactionType.STARKNET:
223
+ if (transaction.isApprovalTx)
224
+ currentStep.starknetApprovalTransaction = transaction;
225
+ else currentStep.starknetTransaction = transaction;
226
+ break;
227
+ case TransactionType.COSMOS:
228
+ currentStep.cosmosTransaction = transaction;
229
+ break;
230
+ case TransactionType.SOLANA:
231
+ currentStep.solanaTransaction = transaction;
232
+ break;
233
+ case TransactionType.TRANSFER:
234
+ currentStep.transferTransaction = transaction;
235
+ break;
236
+ case TransactionType.TON:
237
+ currentStep.tonTransaction = transaction;
238
+ break;
239
+ default:
240
+ ((x: never) => {
241
+ throw new Error(`${x} was unhandled!`);
242
+ })(txType);
228
243
  }
229
244
  return currentStep;
230
245
  };
@@ -293,9 +308,18 @@ export function updateSwapStatus({
293
308
  }): {
294
309
  swap: PendingSwap;
295
310
  step: PendingSwapStep | null;
311
+ failureType?: APIErrorCode;
296
312
  } {
297
313
  const swap = getStorage().swapDetails;
298
314
  const currentStep = getCurrentStep(swap);
315
+ const updatedResult: {
316
+ swap: PendingSwap;
317
+ step: PendingSwapStep | null;
318
+ failureType?: APIErrorCode;
319
+ } = {
320
+ swap,
321
+ step: currentStep,
322
+ };
299
323
  if (!!nextStepStatus && !!currentStep) currentStep.status = nextStepStatus;
300
324
 
301
325
  if (nextStatus) swap.status = nextStatus;
@@ -317,13 +341,20 @@ export function updateSwapStatus({
317
341
  const walletType = getRelatedWalletOrNull(swap, currentStep!)?.walletType;
318
342
  swap.extraMessageSeverity = MessageSeverity.error;
319
343
 
344
+ const failureType = mapAppErrorCodesToAPIErrorCode(errorCode);
345
+ updatedResult.failureType = failureType;
346
+
320
347
  httpService()
321
348
  .reportFailure({
322
349
  requestId: swap.requestId,
323
350
  step: currentStep?.id || 1,
324
- eventType: mapAppErrorCodesToAPIErrorCode(errorCode),
351
+ eventType: failureType,
325
352
  reason: errorReason || '',
326
- data: walletType ? { wallet: walletType } : undefined,
353
+ tags: walletType
354
+ ? {
355
+ wallet: walletType,
356
+ }
357
+ : undefined,
327
358
  })
328
359
  .then()
329
360
  .catch();
@@ -342,10 +373,7 @@ export function updateSwapStatus({
342
373
  swapDetails: swap,
343
374
  });
344
375
 
345
- return {
346
- swap,
347
- step: currentStep,
348
- };
376
+ return updatedResult;
349
377
  }
350
378
 
351
379
  /**
@@ -356,8 +384,6 @@ export function updateSwapStatus({
356
384
  export function setStepTransactionIds(
357
385
  { getStorage, setStorage }: ExecuterActions<SwapStorage, SwapActionTypes>,
358
386
  txId: string | null,
359
- notifier: SwapQueueContext['notifier'],
360
- eventType?: EventType,
361
387
  explorerUrl?: { url?: string; description?: string }
362
388
  ): void {
363
389
  const swap = getStorage().swapDetails;
@@ -374,22 +400,34 @@ export function setStepTransactionIds(
374
400
  description: explorerUrl.description || null,
375
401
  },
376
402
  ];
377
- if (eventType === 'check_tx_status') {
378
- swap.extraMessage = 'Checking transaction status ...';
379
- swap.extraMessageDetail = '';
380
- swap.extraMessageSeverity = MessageSeverity.info;
381
- } else if (eventType === 'check_approve_tx_status') {
382
- swap.extraMessage = 'Checking approve transaction status ...';
383
- swap.extraMessageDetail = '';
384
- swap.extraMessageSeverity = MessageSeverity.info;
385
- }
403
+
404
+ const isApproval = isApprovalCurrentStepTx(currentStep);
405
+
406
+ if (isApproval) swap.extraMessage = 'Checking approve transaction status ...';
407
+ else swap.extraMessage = 'Checking transaction status ...';
408
+
409
+ swap.extraMessageDetail = '';
410
+ swap.extraMessageSeverity = MessageSeverity.info;
386
411
 
387
412
  setStorage({
388
413
  ...getStorage(),
389
414
  swapDetails: swap,
390
415
  });
391
- if (eventType)
392
- notifier({ eventType: eventType, swap: swap, step: currentStep });
416
+
417
+ notifier({
418
+ event: {
419
+ type: StepEventType.TX_EXECUTION,
420
+ status: StepExecutionEventStatus.TX_SENT,
421
+ },
422
+ swap: swap,
423
+ step: currentStep,
424
+ });
425
+
426
+ notifier({
427
+ event: { type: StepEventType.CHECK_STATUS },
428
+ swap: swap,
429
+ step: currentStep,
430
+ });
393
431
  }
394
432
 
395
433
  /**
@@ -480,10 +518,7 @@ export function markRunningSwapAsSwitchingNetwork({
480
518
  export function markRunningSwapAsDependsOnOtherQueues({
481
519
  getStorage,
482
520
  setStorage,
483
- notifier,
484
- }: Pick<ExecuterActions, 'getStorage' | 'setStorage'> & {
485
- notifier: SwapQueueContext['notifier'];
486
- }):
521
+ }: Pick<ExecuterActions, 'getStorage' | 'setStorage'>):
487
522
  | {
488
523
  swap: PendingSwap;
489
524
  step: PendingSwapStep;
@@ -498,7 +533,10 @@ export function markRunningSwapAsDependsOnOtherQueues({
498
533
  currentStep.networkStatus = PendingSwapNetworkStatus.WaitingForQueue;
499
534
 
500
535
  notifier({
501
- eventType: 'waiting_for_queue',
536
+ event: {
537
+ type: StepEventType.TX_EXECUTION_BLOCKED,
538
+ status: StepExecutionBlockedEventStatus.WAITING_FOR_QUEUE,
539
+ },
502
540
  swap,
503
541
  step: currentStep,
504
542
  });
@@ -737,11 +775,23 @@ export function onBlockForConnectWallet(
737
775
  if (!ok) {
738
776
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
739
777
  const currentStep = getCurrentStep(swap)!;
740
- context.notifier({
741
- eventType:
742
- reason === 'account_miss_match'
743
- ? 'waiting_for_change_wallet_account'
744
- : 'waiting_for_connecting_wallet',
778
+ const { type: walletType, address } = getRequiredWallet(swap);
779
+ notifier({
780
+ event: {
781
+ type: StepEventType.TX_EXECUTION_BLOCKED,
782
+ ...(reason === 'account_miss_match'
783
+ ? {
784
+ status:
785
+ StepExecutionBlockedEventStatus.WAITING_FOR_CHANGE_WALLET_ACCOUNT,
786
+ requiredAccount: address ?? undefined,
787
+ }
788
+ : {
789
+ status:
790
+ StepExecutionBlockedEventStatus.WAITING_FOR_WALLET_CONNECT,
791
+ requiredWallet: walletType ?? undefined,
792
+ requiredAccount: address ?? undefined,
793
+ }),
794
+ },
745
795
  swap: swap,
746
796
  step: currentStep,
747
797
  });
@@ -784,9 +834,22 @@ export function onBlockForChangeNetwork(
784
834
  setStorage: queue.setStorage.bind(queue),
785
835
  });
786
836
 
837
+ const requiredNetwork = getCurrentBlockchainOfOrNull(swap, currentStep);
838
+
839
+ const requiredWallet = getRequiredWallet(swap).type;
840
+
841
+ const currentNetwork = requiredWallet
842
+ ? context.state(requiredWallet).network
843
+ : undefined;
844
+
787
845
  if (result) {
788
- context.notifier({
789
- eventType: 'waiting_for_network_change',
846
+ notifier({
847
+ event: {
848
+ type: StepEventType.TX_EXECUTION_BLOCKED,
849
+ status: StepExecutionBlockedEventStatus.WAITING_FOR_NETWORK_CHANGE,
850
+ requiredNetwork: requiredNetwork ?? undefined,
851
+ currentNetwork: currentNetwork ?? undefined,
852
+ },
790
853
  swap: result.swap,
791
854
  step: result.step,
792
855
  });
@@ -845,7 +908,6 @@ export function onDependsOnOtherQueues(
845
908
  markRunningSwapAsDependsOnOtherQueues({
846
909
  getStorage: queue.getStorage.bind(queue),
847
910
  setStorage: queue.setStorage.bind(queue),
848
- notifier: context.notifier,
849
911
  });
850
912
  return;
851
913
  }
@@ -876,7 +938,6 @@ export function onDependsOnOtherQueues(
876
938
  // TODO: Use key generator
877
939
  retryOn(
878
940
  `${type}-${network}:${address}`,
879
- context.notifier,
880
941
  manager,
881
942
  context.canSwitchNetworkTo
882
943
  );
@@ -907,9 +968,9 @@ export function isRequiredWalletConnected(
907
968
  export function singTransaction(
908
969
  actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
909
970
  ): void {
910
- const { setTransactionDataByHash } = useTransactionsData();
971
+ const { setTransactionDataByHash } = inMemoryTransactionsData();
911
972
  const { getStorage, setStorage, failed, next, schedule, context } = actions;
912
- const { meta, getSigners, notifier, isMobileWallet } = context;
973
+ const { meta, getSigners, isMobileWallet } = context;
913
974
  const swap = getStorage().swapDetails;
914
975
  // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
915
976
  const currentStep = getCurrentStep(swap)!;
@@ -930,20 +991,40 @@ export function singTransaction(
930
991
  const tx = getCurrentStepTx(currentStep);
931
992
  const txType = tx?.type;
932
993
  const isApproval = isApprovalCurrentStepTx(currentStep);
933
- const isSmartContractCall = [
934
- TransactionType.EVM,
935
- TransactionType.STARKNET,
936
- TransactionType.TRON,
937
- ].includes(txType!);
994
+
995
+ if (!tx || !txType) {
996
+ const extraMessage = 'Unexpected Error: tx is null!';
997
+ const updateResult = updateSwapStatus({
998
+ getStorage,
999
+ setStorage,
1000
+ nextStatus: 'failed',
1001
+ nextStepStatus: 'failed',
1002
+ message: extraMessage,
1003
+ details: undefined,
1004
+ errorCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
1005
+ });
1006
+ notifier({
1007
+ event: {
1008
+ type: StepEventType.FAILED,
1009
+ reason: extraMessage,
1010
+ reasonCode: 'CLIENT_UNEXPECTED_BEHAVIOUR',
1011
+ },
1012
+ ...updateResult,
1013
+ });
1014
+ failed();
1015
+ return onFinish();
1016
+ }
1017
+
1018
+ const chainId = meta.blockchains?.[tx.blockChain]?.chainId;
938
1019
 
939
1020
  const hasAlreadyProceededToSign =
940
1021
  typeof swap.hasAlreadyProceededToSign === 'boolean';
941
1022
 
942
1023
  let nextStatus: SwapStatus | undefined,
943
1024
  nextStepStatus: StepStatus,
944
- eventType: EventType,
945
1025
  message: string,
946
- details: string;
1026
+ details: string,
1027
+ eventType: StepEventType;
947
1028
 
948
1029
  if (isApproval) {
949
1030
  message = `Waiting for approval of ${currentStep?.fromSymbol} coin ${
@@ -953,21 +1034,19 @@ export function singTransaction(
953
1034
  'Waiting for approve transaction to be mined and confirmed successfully';
954
1035
  nextStepStatus = 'waitingForApproval';
955
1036
  nextStatus = undefined;
956
- eventType = 'confirm_approve_contract';
1037
+ eventType = StepEventType.TX_EXECUTION;
957
1038
  } else if (hasAlreadyProceededToSign) {
958
1039
  message = 'Transaction is expired. Please try again.';
959
1040
  nextStepStatus = 'failed';
960
1041
  nextStatus = 'failed';
961
1042
  details = '';
962
- eventType = 'transaction_expired';
1043
+ eventType = StepEventType.FAILED;
963
1044
  } else {
964
1045
  message = 'Executing transaction ...';
965
1046
  nextStepStatus = 'running';
966
1047
  nextStatus = 'running';
967
1048
  details = `${mobileWallet ? 'Check your mobile phone!' : ''}`;
968
- eventType = isSmartContractCall
969
- ? 'calling_smart_contract'
970
- : 'confirm_transfer';
1049
+ eventType = StepEventType.TX_EXECUTION;
971
1050
  }
972
1051
 
973
1052
  const updateResult = updateSwapStatus({
@@ -982,18 +1061,29 @@ export function singTransaction(
982
1061
  : hasAlreadyProceededToSign,
983
1062
  errorCode: hasAlreadyProceededToSign ? 'TX_EXPIRED' : undefined,
984
1063
  });
985
- notifier({
986
- eventType,
987
- ...updateResult,
988
- });
1064
+
1065
+ if (eventType === StepEventType.FAILED) {
1066
+ notifier({
1067
+ event: {
1068
+ type: eventType,
1069
+ reason: message,
1070
+ reasonCode: updateResult.failureType ?? DEFAULT_ERROR_CODE,
1071
+ },
1072
+ ...updateResult,
1073
+ });
1074
+ } else
1075
+ notifier({
1076
+ event: { type: eventType, status: StepExecutionEventStatus.SEND_TX },
1077
+ ...updateResult,
1078
+ });
989
1079
 
990
1080
  if (hasAlreadyProceededToSign) {
991
1081
  failed();
992
1082
  onFinish();
993
1083
  return;
994
1084
  }
995
- const signer = walletSigners.getSigner(txType!);
996
- signer.signAndSendTx(tx!, walletAddress, null).then(
1085
+ const signer = walletSigners.getSigner(txType);
1086
+ signer.signAndSendTx(tx, walletAddress, chainId).then(
997
1087
  ({ hash, response }) => {
998
1088
  const explorerUrl = getScannerUrl(
999
1089
  hash,
@@ -1003,8 +1093,6 @@ export function singTransaction(
1003
1093
  setStepTransactionIds(
1004
1094
  actions,
1005
1095
  hash,
1006
- notifier,
1007
- isApproval ? 'check_approve_tx_status' : 'check_tx_status',
1008
1096
  explorerUrl
1009
1097
  ? { url: explorerUrl, description: isApproval ? 'Approve' : 'Swap' }
1010
1098
  : undefined
@@ -1021,15 +1109,12 @@ export function singTransaction(
1021
1109
  const { extraMessage, extraMessageDetail, extraMessageErrorCode } =
1022
1110
  prettifyErrorMessage(error);
1023
1111
 
1024
- // if it is an rpc error with details, send the log to sentry
1025
- if (
1026
- error &&
1027
- error?.root &&
1028
- error?.root?.message &&
1029
- error?.root?.code &&
1030
- error?.root?.reason
1031
- )
1032
- logRPCError(error.root, swap, currentStep, sourceWallet?.walletType);
1112
+ logRPCError(
1113
+ error?.trace?.stack || error?.trace || error?.root || error,
1114
+ swap,
1115
+ currentStep,
1116
+ sourceWallet?.walletType
1117
+ );
1033
1118
 
1034
1119
  const updateResult = updateSwapStatus({
1035
1120
  getStorage,
@@ -1040,14 +1125,13 @@ export function singTransaction(
1040
1125
  details: extraMessageDetail,
1041
1126
  errorCode: extraMessageErrorCode,
1042
1127
  });
1043
- const eventType =
1044
- extraMessageErrorCode === 'REJECTED_BY_USER'
1045
- ? 'contract_rejected'
1046
- : isSmartContractCall
1047
- ? 'smart_contract_call_failed'
1048
- : 'transfer_failed';
1128
+
1049
1129
  notifier({
1050
- eventType,
1130
+ event: {
1131
+ type: StepEventType.FAILED,
1132
+ reason: extraMessage,
1133
+ reasonCode: updateResult.failureType ?? DEFAULT_ERROR_CODE,
1134
+ },
1051
1135
  ...updateResult,
1052
1136
  });
1053
1137
  failed();
@@ -1060,7 +1144,6 @@ export function checkWaitingForConnectWalletChange(params: {
1060
1144
  wallet_network: string;
1061
1145
  manager?: Manager;
1062
1146
  evmChains: EvmBlockchainMeta[];
1063
- notifier: SwapQueueContext['notifier'];
1064
1147
  }): void {
1065
1148
  const { wallet_network, evmChains, manager } = params;
1066
1149
  const [wallet, network] = splitWalletNetwork(wallet_network);
@@ -1089,10 +1172,12 @@ export function checkWaitingForConnectWalletChange(params: {
1089
1172
  }
1090
1173
  );
1091
1174
 
1175
+ const requiredNetwork = getCurrentBlockchainOfOrNull(swap, currentStep);
1176
+
1092
1177
  if (
1093
1178
  currentStepRequiredWallet === wallet &&
1094
1179
  hasWaitingForConnect &&
1095
- getCurrentBlockchainOfOrNull(swap, currentStep) != network
1180
+ requiredNetwork != network
1096
1181
  ) {
1097
1182
  const queueInstance = q.list;
1098
1183
  const { type } = getRequiredWallet(swap);
@@ -1112,8 +1197,14 @@ export function checkWaitingForConnectWalletChange(params: {
1112
1197
  });
1113
1198
 
1114
1199
  if (result) {
1115
- params?.notifier({
1116
- eventType: 'waiting_for_network_change',
1200
+ notifier({
1201
+ event: {
1202
+ type: StepEventType.TX_EXECUTION_BLOCKED,
1203
+ status:
1204
+ StepExecutionBlockedEventStatus.WAITING_FOR_NETWORK_CHANGE,
1205
+ currentNetwork: network,
1206
+ requiredNetwork: requiredNetwork ?? undefined,
1207
+ },
1117
1208
  swap: result.swap,
1118
1209
  step: result.step,
1119
1210
  });
@@ -1186,21 +1277,26 @@ export function getRunningSwaps(manager: Manager): PendingSwap[] {
1186
1277
  * @param notifier
1187
1278
  * @returns
1188
1279
  */
1189
- export function resetRunningSwapNotifsOnPageLoad(
1190
- runningSwaps: PendingSwap[],
1191
- notifier: SwapQueueContext['notifier']
1192
- ) {
1280
+ export function resetRunningSwapNotifsOnPageLoad(runningSwaps: PendingSwap[]) {
1193
1281
  runningSwaps.forEach((swap) => {
1194
1282
  const currentStep = getCurrentStep(swap);
1195
- let eventType: EventType | undefined;
1283
+ const eventType = StepEventType.TX_EXECUTION_BLOCKED;
1284
+ let eventSubtype:
1285
+ | StepExecutionBlockedEventStatus.WAITING_FOR_QUEUE
1286
+ | StepExecutionBlockedEventStatus.WAITING_FOR_WALLET_CONNECT
1287
+ | undefined;
1196
1288
  if (currentStep?.networkStatus === PendingSwapNetworkStatus.WaitingForQueue)
1197
- eventType = 'waiting_for_queue';
1289
+ eventSubtype = StepExecutionBlockedEventStatus.WAITING_FOR_QUEUE;
1198
1290
  else if (swap?.status === 'running') {
1199
- eventType = 'waiting_for_connecting_wallet';
1291
+ eventSubtype = StepExecutionBlockedEventStatus.WAITING_FOR_WALLET_CONNECT;
1200
1292
  }
1201
1293
  if (!!eventType && !!notifier) {
1202
1294
  notifier({
1203
- eventType,
1295
+ event: {
1296
+ type: eventType,
1297
+ status:
1298
+ eventSubtype ?? StepExecutionBlockedEventStatus.WAITING_FOR_QUEUE,
1299
+ },
1204
1300
  swap: swap,
1205
1301
  step: currentStep,
1206
1302
  });
@@ -1221,7 +1317,6 @@ export function resetRunningSwapNotifsOnPageLoad(
1221
1317
  */
1222
1318
  export function retryOn(
1223
1319
  wallet_network: string,
1224
- notifier: SwapQueueContext['notifier'],
1225
1320
  manager?: Manager,
1226
1321
  canSwitchNetworkTo?: (type: WalletType, network: Network) => boolean,
1227
1322
  options = { fallbackToOnlyWallet: true }
@@ -1270,7 +1365,6 @@ export function retryOn(
1270
1365
  markRunningSwapAsDependsOnOtherQueues({
1271
1366
  getStorage: currentQueue.getStorage.bind(currentQueue),
1272
1367
  setStorage: currentQueue.setStorage.bind(currentQueue),
1273
- notifier: notifier,
1274
1368
  });
1275
1369
  }
1276
1370
  }
@@ -1326,8 +1420,49 @@ export function cancelSwap(
1326
1420
  nextStepStatus: 'failed',
1327
1421
  errorCode: 'USER_CANCEL',
1328
1422
  });
1423
+
1424
+ notifier({
1425
+ event: {
1426
+ type: StepEventType.FAILED,
1427
+ reasonCode: 'USER_CANCEL',
1428
+ reason: updateResult.swap.extraMessage ?? undefined,
1429
+ },
1430
+
1431
+ swap: updateResult.swap,
1432
+ step: updateResult.step,
1433
+ });
1434
+
1329
1435
  reset();
1330
1436
  if (manager) manager?.retry();
1331
1437
 
1332
1438
  return updateResult;
1333
1439
  }
1440
+
1441
+ export function getLastSuccessfulStep<T extends { status: StepStatus }[]>(
1442
+ steps: T
1443
+ ): ArrayElement<T> | undefined {
1444
+ return steps
1445
+ .slice()
1446
+ .reverse()
1447
+ .find((step) => step.status === 'success') as ArrayElement<T> | undefined;
1448
+ }
1449
+
1450
+ export function getFailedStep<T extends { status: StepStatus }[]>(
1451
+ steps: T
1452
+ ): ArrayElement<T> | undefined {
1453
+ return steps
1454
+ .slice()
1455
+ .reverse()
1456
+ .find((step) => step.status === 'failed') as ArrayElement<T> | undefined;
1457
+ }
1458
+
1459
+ export function isApprovalTX(step: Step): boolean {
1460
+ const { transaction } = step;
1461
+ const approvalTx =
1462
+ (transaction?.type === TransactionType.EVM && transaction.isApprovalTx) ||
1463
+ (transaction?.type === TransactionType.STARKNET &&
1464
+ transaction.isApprovalTx) ||
1465
+ (transaction?.type === TransactionType.TRON && transaction.isApprovalTx);
1466
+
1467
+ return approvalTx;
1468
+ }
package/src/hooks.ts CHANGED
@@ -7,6 +7,7 @@ import {
7
7
  } from './helpers';
8
8
  import { migrated, migration } from './migration';
9
9
  import { UseQueueManagerParams } from './types';
10
+ import { eventEmitter } from './services/eventEmitter';
10
11
 
11
12
  let isCalled = 0;
12
13
 
@@ -54,14 +55,8 @@ function useQueueManager(params: UseQueueManagerParams): void {
54
55
  evmChains: params.evmChains,
55
56
  wallet_network: params.lastConnectedWallet,
56
57
  manager,
57
- notifier: params.notifier,
58
58
  });
59
- retryOn(
60
- params.lastConnectedWallet,
61
- params.notifier,
62
- manager,
63
- params.canSwitchNetworkTo
64
- );
59
+ retryOn(params.lastConnectedWallet, manager, params.canSwitchNetworkTo);
65
60
  }
66
61
  }, [params.lastConnectedWallet]);
67
62
 
@@ -78,4 +73,8 @@ function useQueueManager(params: UseQueueManagerParams): void {
78
73
  }, [params.disconnectedWallet]);
79
74
  }
80
75
 
81
- export { useQueueManager, useMigration };
76
+ function useEvents() {
77
+ return eventEmitter;
78
+ }
79
+
80
+ export { useQueueManager, useMigration, useEvents };