@rango-dev/queue-manager-rango-preset 0.1.10-next.101

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 (60) hide show
  1. package/dist/actions/checkStatus.d.ts +12 -0
  2. package/dist/actions/checkStatus.d.ts.map +1 -0
  3. package/dist/actions/createTransaction.d.ts +11 -0
  4. package/dist/actions/createTransaction.d.ts.map +1 -0
  5. package/dist/actions/executeTransaction.d.ts +13 -0
  6. package/dist/actions/executeTransaction.d.ts.map +1 -0
  7. package/dist/actions/scheduleNextStep.d.ts +13 -0
  8. package/dist/actions/scheduleNextStep.d.ts.map +1 -0
  9. package/dist/actions/start.d.ts +4 -0
  10. package/dist/actions/start.d.ts.map +1 -0
  11. package/dist/constants.d.ts +8 -0
  12. package/dist/constants.d.ts.map +1 -0
  13. package/dist/helpers.d.ts +197 -0
  14. package/dist/helpers.d.ts.map +1 -0
  15. package/dist/hooks.d.ts +19 -0
  16. package/dist/hooks.d.ts.map +1 -0
  17. package/dist/index.d.ts +7 -0
  18. package/dist/index.d.ts.map +1 -0
  19. package/dist/index.js +8 -0
  20. package/dist/migration.d.ts +15 -0
  21. package/dist/migration.d.ts.map +1 -0
  22. package/dist/queue-manager-rango-preset.cjs.development.js +2799 -0
  23. package/dist/queue-manager-rango-preset.cjs.development.js.map +1 -0
  24. package/dist/queue-manager-rango-preset.cjs.production.min.js +2 -0
  25. package/dist/queue-manager-rango-preset.cjs.production.min.js.map +1 -0
  26. package/dist/queue-manager-rango-preset.esm.js +2781 -0
  27. package/dist/queue-manager-rango-preset.esm.js.map +1 -0
  28. package/dist/queueDef.d.ts +10 -0
  29. package/dist/queueDef.d.ts.map +1 -0
  30. package/dist/services/httpService.d.ts +3 -0
  31. package/dist/services/httpService.d.ts.map +1 -0
  32. package/dist/services/index.d.ts +2 -0
  33. package/dist/services/index.d.ts.map +1 -0
  34. package/dist/shared-errors.d.ts +25 -0
  35. package/dist/shared-errors.d.ts.map +1 -0
  36. package/dist/shared-sentry.d.ts +4 -0
  37. package/dist/shared-sentry.d.ts.map +1 -0
  38. package/dist/shared.d.ts +148 -0
  39. package/dist/shared.d.ts.map +1 -0
  40. package/dist/types.d.ts +48 -0
  41. package/dist/types.d.ts.map +1 -0
  42. package/package.json +62 -0
  43. package/readme.md +8 -0
  44. package/src/actions/checkStatus.ts +269 -0
  45. package/src/actions/createTransaction.ts +122 -0
  46. package/src/actions/executeTransaction.ts +121 -0
  47. package/src/actions/scheduleNextStep.ts +61 -0
  48. package/src/actions/start.ts +10 -0
  49. package/src/constants.ts +22 -0
  50. package/src/helpers.ts +1774 -0
  51. package/src/hooks.ts +76 -0
  52. package/src/index.ts +27 -0
  53. package/src/migration.ts +124 -0
  54. package/src/queueDef.ts +39 -0
  55. package/src/services/httpService.ts +7 -0
  56. package/src/services/index.ts +1 -0
  57. package/src/shared-errors.ts +160 -0
  58. package/src/shared-sentry.ts +24 -0
  59. package/src/shared.ts +342 -0
  60. package/src/types.ts +76 -0
@@ -0,0 +1,121 @@
1
+ import { ExecuterActions } from '@rango-dev/queue-manager-core';
2
+ import {
3
+ ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK,
4
+ ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION,
5
+ ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION_WRONG_WALLET,
6
+ } from '../constants';
7
+
8
+ import {
9
+ getCurrentStep,
10
+ isNetworkMatchedForTransaction,
11
+ isRequiredWalletConnected,
12
+ updateNetworkStatus,
13
+ singTransaction,
14
+ resetNetworkStatus,
15
+ getRequiredWallet,
16
+ isNeedBlockQueueForParallel,
17
+ } from '../helpers';
18
+ import { getCurrentBlockchainOf, PendingSwapNetworkStatus } from '../shared';
19
+ import {
20
+ BlockReason,
21
+ SwapActionTypes,
22
+ SwapQueueContext,
23
+ SwapStorage,
24
+ } from '../types';
25
+
26
+ /**
27
+ * Excecute a created transaction.
28
+ *
29
+ * This function implemented the parallel mode by `claim` mechanism which means
30
+ * All the queues the meet certain situation (like multiple evm transaction) will go through
31
+ * a `claim` mechanims that decides which queue should be run and it blocks other ones.
32
+ *
33
+ * A queue will be go to sign process, if the wallet and network is matched.
34
+ */
35
+ export async function executeTransaction(
36
+ actions: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>
37
+ ): Promise<void> {
38
+ const { getStorage, context } = actions;
39
+ const { meta, wallets, providers } = context;
40
+
41
+ const isClaimed = context.claimedBy === context._queue?.id;
42
+ const requestBlock: typeof actions.block = (blockedFor) => {
43
+ actions.block(blockedFor);
44
+ if (isClaimed && actions.context.resetClaimedBy) {
45
+ actions.context.resetClaimedBy();
46
+ }
47
+ };
48
+
49
+ const swap = getStorage().swapDetails;
50
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
51
+ const currentStep = getCurrentStep(swap)!;
52
+
53
+ // Resetting network status, so we will set it again during the running of this task.
54
+ resetNetworkStatus(actions);
55
+
56
+ /* Make sure wallet is connected and also the connected wallet is matched with tx by checking address. */
57
+ const isWrongAddress = !isRequiredWalletConnected(swap, context.state).ok;
58
+ if (isWrongAddress) {
59
+ const { type, address } = getRequiredWallet(swap);
60
+ const isWalletInCompatible = wallets?.blockchains?.find(
61
+ (w) => !w.accounts?.find((account) => account.walletType === type)
62
+ );
63
+ const description =
64
+ !wallets || isWalletInCompatible
65
+ ? ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION(type)
66
+ : ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION_WRONG_WALLET(type, address);
67
+
68
+ const blockedFor = {
69
+ reason: BlockReason.WAIT_FOR_CONNECT_WALLET,
70
+ description,
71
+ };
72
+ requestBlock(blockedFor);
73
+ return;
74
+ }
75
+
76
+ /*
77
+ For avoiding conflict by making too many requests to wallet, we need to make sure
78
+ We only run one request at a time (In parallel mode).
79
+ */
80
+ const needsToBlockQueue = isNeedBlockQueueForParallel(currentStep);
81
+
82
+ if (needsToBlockQueue && !isClaimed && context.claimedBy) {
83
+ const blockedFor = {
84
+ reason: BlockReason.DEPENDS_ON_OTHER_QUEUES,
85
+ description: 'Waiting for other swaps to complete',
86
+ details: {},
87
+ };
88
+ requestBlock(blockedFor);
89
+ return;
90
+ }
91
+
92
+ /* Wallet should be on correct network */
93
+ const networkMatched = await isNetworkMatchedForTransaction(
94
+ swap,
95
+ currentStep,
96
+ wallets,
97
+ meta,
98
+ providers
99
+ );
100
+ if (!networkMatched) {
101
+ const fromBlockchain = getCurrentBlockchainOf(swap, currentStep);
102
+ const details = ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK(fromBlockchain);
103
+
104
+ const blockedFor = {
105
+ reason: BlockReason.WAIT_FOR_NETWORK_CHANGE,
106
+ details: details,
107
+ };
108
+ requestBlock(blockedFor);
109
+ return;
110
+ } else {
111
+ // Update network to mark it as network changed successfully.
112
+ updateNetworkStatus(actions, {
113
+ message: '',
114
+ details: 'Wallet network changed successfully',
115
+ status: PendingSwapNetworkStatus.NetworkChanged,
116
+ });
117
+ }
118
+
119
+ // All the conditions are met. We can safely send the tx to wallet for sign.
120
+ singTransaction(actions);
121
+ }
@@ -0,0 +1,61 @@
1
+ import { ExecuterActions } from '@rango-dev/queue-manager-core';
2
+ import { SwapActionTypes, SwapQueueContext, SwapStorage } from '../types';
3
+ import { getCurrentStep, isTxAlreadyCreated } from '../helpers';
4
+
5
+ /**
6
+ *
7
+ * This function is responsibe for scheduling the correct `action` based on `PendingSwap` status.
8
+ * It means `action`s schedule this step to decide what should be the next step/task.
9
+ *
10
+ * It's acts like a `while(true)` and will `break` the loop on certain `action`s like `CHECK_STATUS`.
11
+ *
12
+ *
13
+ */
14
+ export function scheduleNextStep({
15
+ schedule,
16
+ next,
17
+ failed,
18
+ setStorage,
19
+ getStorage,
20
+ context,
21
+ }: ExecuterActions<SwapStorage, SwapActionTypes, SwapQueueContext>): void {
22
+ const swap = getStorage().swapDetails;
23
+ const currentStep = getCurrentStep(swap);
24
+ if (!!currentStep) {
25
+ if (isTxAlreadyCreated(swap, currentStep)) {
26
+ schedule(SwapActionTypes.EXECUTE_TRANSACTION);
27
+ return next();
28
+ }
29
+
30
+ // TODO double check it after approval changes
31
+ if (currentStep?.executedTransactionId) {
32
+ schedule(SwapActionTypes.CHECK_TRANSACTION_STATUS);
33
+ return next();
34
+ }
35
+
36
+ swap.status = 'running';
37
+
38
+ setStorage({ ...getStorage(), swapDetails: swap });
39
+
40
+ schedule(SwapActionTypes.CREATE_TRANSACTION);
41
+ next();
42
+ } else {
43
+ const isFailed = swap.steps.find((step) => step.status === 'failed');
44
+ swap.status = isFailed ? 'failed' : 'success';
45
+ swap.finishTime = new Date().getTime().toString();
46
+
47
+ setStorage({
48
+ ...getStorage(),
49
+ swapDetails: swap,
50
+ });
51
+
52
+ context.notifier({
53
+ eventType: isFailed ? 'task_failed' : 'task_completed',
54
+ swap: swap,
55
+ step: null,
56
+ });
57
+
58
+ if (isFailed) failed();
59
+ else next();
60
+ }
61
+ }
@@ -0,0 +1,10 @@
1
+ import { ExecuterActions } from '@rango-dev/queue-manager-core';
2
+ import { SwapActionTypes, SwapStorage } from '../types';
3
+
4
+ export function start({
5
+ schedule,
6
+ next,
7
+ }: ExecuterActions<SwapStorage, SwapActionTypes>): void {
8
+ schedule(SwapActionTypes.SCHEDULE_NEXT_STEP);
9
+ next();
10
+ }
@@ -0,0 +1,22 @@
1
+ export const RANGO_DAPP_API_KEY = process.env.REACT_APP_API_KEY;
2
+ export const RANGO_DAPP_API_BASE_URL = process.env.REACT_APP_API_BASE_URL;
3
+
4
+ export const ERROR_MESSAGE_DEPENDS_ON_OTHER_QUEUES =
5
+ 'Waiting for other swaps to complete';
6
+ export const ERROR_MESSAGE_WAIT_FOR_WALLET = 'Waiting for connecting wallet';
7
+ export const ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION_WRONG_WALLET = (
8
+ type: string | null,
9
+ address: string | null
10
+ ): string =>
11
+ `Please change your ${type || 'wallet'} account to ${
12
+ address || 'proper address'
13
+ }`;
14
+ export const ERROR_MESSAGE_WAIT_FOR_WALLET_DESCRIPTION = (
15
+ type: string | null
16
+ ): string =>
17
+ `Please connect to ${
18
+ type || 'your wallet'
19
+ } by using bellow button or top right button on page.`;
20
+ export const ERROR_MESSAGE_WAIT_FOR_CHANGE_NETWORK = (
21
+ network: string | null
22
+ ): string => `Please change your network to ${network}.`;