@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
package/src/shared.ts ADDED
@@ -0,0 +1,342 @@
1
+ import { Network, WalletType } from '@rango-dev/wallets-shared';
2
+ import {
3
+ CosmosTransaction,
4
+ EvmBlockchainMeta,
5
+ EvmTransaction,
6
+ SimulationResult,
7
+ SolanaTransaction,
8
+ StarknetTransaction,
9
+ TronTransaction,
10
+ Transfer as TransferTransaction,
11
+ AmountRestrictionType,
12
+ } from 'rango-sdk';
13
+
14
+ import { PrettyError } from './shared-errors';
15
+
16
+ export interface PendingSwapWithQueueID {
17
+ id: string;
18
+ swap: PendingSwap;
19
+ }
20
+
21
+ export type SwapProgressNotification = {
22
+ eventType: EventType;
23
+ swap: PendingSwap | null;
24
+ step: PendingSwapStep | null;
25
+ };
26
+
27
+ export type WalletBalance = {
28
+ chain: Network;
29
+ symbol: string;
30
+ ticker: string;
31
+ address: string | null;
32
+ rawAmount: string;
33
+ decimal: number | null;
34
+ amount: string;
35
+ logo: string | null;
36
+ usdPrice: number | null;
37
+ };
38
+
39
+ export type Account = {
40
+ balances: WalletBalance[] | null;
41
+ address: string;
42
+ loading: boolean;
43
+ walletType: WalletType;
44
+ error: boolean;
45
+ explorerUrl: string | null;
46
+ isConnected?: boolean;
47
+ };
48
+ export type Blockchain = { name: string; accounts: Account[] };
49
+ export type Wallet = { blockchains: Blockchain[] };
50
+
51
+ export type EventType =
52
+ | 'swap_started'
53
+ | 'confirm_contract'
54
+ | 'confirm_transfer'
55
+ | 'task_failed'
56
+ | 'task_completed'
57
+ | 'task_canceled'
58
+ | 'task_paused'
59
+ | 'contract_confirmed'
60
+ | 'confirm_approve_contract'
61
+ | 'contract_rejected'
62
+ | 'check_tx_status'
63
+ | 'check_approve_tx_status'
64
+ | 'transfer_rejected'
65
+ | 'calling_smart_contract'
66
+ | 'smart_contract_called'
67
+ | 'smart_contract_call_failed'
68
+ | 'step_completed_with_output'
69
+ | 'waiting_for_network_change'
70
+ | 'waiting_for_connecting_wallet'
71
+ | 'waiting_for_change_wallet_account'
72
+ | 'network_changed'
73
+ | 'not_enough_balance'
74
+ | 'waiting_for_queue'
75
+ | 'check_fee_failed'
76
+ | 'route_failed_to_find'
77
+ | 'transaction_expired';
78
+
79
+ export type SwapSavedSettings = {
80
+ slippage: string;
81
+ disabledSwappersIds: string[];
82
+ disabledSwappersGroups: string[];
83
+ infiniteApprove: boolean;
84
+ };
85
+
86
+ type InternalStepState =
87
+ | 'PENDING'
88
+ | 'CREATED'
89
+ | 'WAITING'
90
+ | 'SIGNED'
91
+ | 'SUCCESSED'
92
+ | 'FAILED';
93
+
94
+ export type SwapperStatusStep = {
95
+ name: string;
96
+ state: InternalStepState;
97
+ current: boolean;
98
+ };
99
+
100
+ export enum PendingSwapNetworkStatus {
101
+ WaitingForConnectingWallet = 'waitingForConnectingWallet',
102
+ WaitingForQueue = 'waitingForQueue',
103
+ WaitingForNetworkChange = 'waitingForNetworkChange',
104
+ NetworkChanged = 'networkChanged',
105
+ }
106
+
107
+ export type SwapExplorerUrl = {
108
+ url: string;
109
+ description: string | null;
110
+ };
111
+
112
+ export type StepStatus =
113
+ | 'created'
114
+ | 'running'
115
+ | 'failed'
116
+ | 'success'
117
+ | 'waitingForApproval'
118
+ | 'approved';
119
+
120
+ export type PendingSwapStep = {
121
+ // routing data
122
+ id: number;
123
+ fromBlockchain: Network;
124
+ fromSymbol: string;
125
+ fromSymbolAddress: string | null;
126
+ fromDecimals: number;
127
+ fromAmountPrecision: string | null;
128
+ fromAmountMinValue: string | null;
129
+ fromAmountMaxValue: string | null;
130
+ fromAmountRestrictionType: AmountRestrictionType | null;
131
+ fromLogo: string;
132
+ toBlockchain: string;
133
+ toSymbol: string;
134
+ toSymbolAddress: string | null;
135
+ toDecimals: number;
136
+ toLogo: string;
137
+ swapperId: string;
138
+ expectedOutputAmountHumanReadable: string | null;
139
+ startTransactionTime: number;
140
+ internalSteps: SwapperStatusStep[] | null;
141
+ estimatedTimeInSeconds: number | null;
142
+
143
+ // status data
144
+ status: StepStatus;
145
+ networkStatus: PendingSwapNetworkStatus | null;
146
+ executedTransactionId: string | null;
147
+ executedTransactionTime: string | null;
148
+ explorerUrl: SwapExplorerUrl[] | null;
149
+ diagnosisUrl: string | null;
150
+ outputAmount: string | null;
151
+
152
+ // txs data
153
+ cosmosTransaction: CosmosTransaction | null;
154
+ transferTransaction: TransferTransaction | null;
155
+ solanaTransaction: SolanaTransaction | null;
156
+ evmApprovalTransaction: EvmTransaction | null;
157
+ evmTransaction: EvmTransaction | null;
158
+ tronApprovalTransaction: TronTransaction | null;
159
+ tronTransaction: TronTransaction | null;
160
+ starknetApprovalTransaction: StarknetTransaction | null;
161
+ starknetTransaction: StarknetTransaction | null;
162
+ };
163
+
164
+ export type WalletTypeAndAddress = {
165
+ walletType: WalletType;
166
+ address: string;
167
+ };
168
+
169
+ export enum MessageSeverity {
170
+ error = 'error',
171
+ warning = 'warning',
172
+ info = 'info',
173
+ success = 'success',
174
+ }
175
+
176
+ export type SwapStatus = 'running' | 'failed' | 'success';
177
+
178
+ export type PendingSwap = {
179
+ creationTime: string;
180
+ finishTime: string | null;
181
+ requestId: string;
182
+ inputAmount: string;
183
+ status: SwapStatus;
184
+ isPaused: boolean;
185
+ extraMessage: string | null;
186
+ extraMessageSeverity: MessageSeverity | null;
187
+ extraMessageErrorCode: string | null;
188
+ extraMessageDetail: string | null | undefined;
189
+ networkStatusExtraMessage: string | null;
190
+ networkStatusExtraMessageDetail: string | null;
191
+ lastNotificationTime: string | null;
192
+ wallets: { [p: string]: WalletTypeAndAddress };
193
+ settings: SwapSavedSettings;
194
+ steps: PendingSwapStep[];
195
+ simulationResult: SimulationResult;
196
+ validateBalanceOrFee: boolean;
197
+ hasAlreadyProceededToSign?: boolean | null;
198
+ };
199
+
200
+ export const getCurrentBlockchainOfOrNull = (
201
+ swap: PendingSwap,
202
+ step: PendingSwapStep
203
+ ): Network | null => {
204
+ try {
205
+ return getCurrentBlockchainOf(swap, step);
206
+ } catch (e) {
207
+ return null;
208
+ }
209
+ };
210
+
211
+ export const getCurrentBlockchainOf = (
212
+ swap: PendingSwap,
213
+ step: PendingSwapStep
214
+ ): Network => {
215
+ const b1 =
216
+ step.evmTransaction?.blockChain ||
217
+ step.evmApprovalTransaction?.blockChain ||
218
+ step.starknetTransaction?.blockChain ||
219
+ step.starknetApprovalTransaction?.blockChain ||
220
+ step.tronTransaction?.blockChain ||
221
+ step.tronApprovalTransaction?.blockChain ||
222
+ step.cosmosTransaction?.blockChain ||
223
+ step.solanaTransaction?.blockChain;
224
+ if (!!b1) return b1 as Network;
225
+
226
+ const transferAddress = step.transferTransaction?.fromWalletAddress;
227
+ if (!transferAddress) throw PrettyError.BlockchainMissing();
228
+
229
+ const blockchain =
230
+ Object.keys(swap.wallets).find(
231
+ (b) => swap.wallets[b]?.address === transferAddress
232
+ ) || null;
233
+ if (blockchain == null) throw PrettyError.BlockchainMissing();
234
+
235
+ // TODO: check why it returns string
236
+ return blockchain as Network;
237
+ };
238
+
239
+ export const getEvmApproveUrl = (
240
+ tx: string,
241
+ network: Network,
242
+ evmBasedBlockchains: EvmBlockchainMeta[]
243
+ ): string => {
244
+ const evmBlochain = evmBasedBlockchains.find(
245
+ (blockchain) => blockchain.name === network
246
+ );
247
+
248
+ if (!evmBlochain) {
249
+ throw Error(`unsupported network: ${network} for getting approve url.`);
250
+ }
251
+
252
+ if (evmBlochain.info.transactionUrl)
253
+ return evmBlochain.info.transactionUrl.replace(
254
+ '{txHash}',
255
+ tx.toLowerCase()
256
+ );
257
+
258
+ throw Error(`Explorer url for ${network} is not implemented`);
259
+ };
260
+
261
+ export const getStarknetApproveUrl = (tx: string): string => {
262
+ return 'https://starkscan.co/tx/{txHash}'.replace(
263
+ '{txHash}',
264
+ tx.toLowerCase()
265
+ );
266
+ };
267
+
268
+ export const getTronApproveUrl = (tx: string): string => {
269
+ return 'https://tronscan.org/#/transaction/{txHash}'.replace(
270
+ '{txHash}',
271
+ tx.toLowerCase()
272
+ );
273
+ };
274
+
275
+ export function getNextStep(
276
+ swap: PendingSwap,
277
+ currentStep: PendingSwapStep
278
+ ): PendingSwapStep | null {
279
+ return (
280
+ swap.steps.find(
281
+ (step) =>
282
+ step.status !== 'failed' &&
283
+ step.status !== 'success' &&
284
+ step.id !== currentStep.id
285
+ ) || null
286
+ );
287
+ }
288
+
289
+ /**
290
+ * Returns the wallet address, based on the current step of `PendingSwap`.
291
+ */
292
+ export const getCurrentAddressOf = (
293
+ swap: PendingSwap,
294
+ step: PendingSwapStep
295
+ ): string => {
296
+ const result =
297
+ swap.wallets[step.evmTransaction?.blockChain || ''] ||
298
+ swap.wallets[step.evmApprovalTransaction?.blockChain || ''] ||
299
+ swap.wallets[step.tronTransaction?.blockChain || ''] ||
300
+ swap.wallets[step.tronApprovalTransaction?.blockChain || ''] ||
301
+ swap.wallets[step.starknetTransaction?.blockChain || ''] ||
302
+ swap.wallets[step.starknetApprovalTransaction?.blockChain || ''] ||
303
+ swap.wallets[step.cosmosTransaction?.blockChain || ''] ||
304
+ swap.wallets[step.solanaTransaction?.blockChain || ''] ||
305
+ (step.transferTransaction?.fromWalletAddress
306
+ ? { address: step.transferTransaction?.fromWalletAddress }
307
+ : null) ||
308
+ null;
309
+ if (result == null) throw PrettyError.WalletMissing();
310
+ return result.address;
311
+ };
312
+
313
+ export function getRelatedWallet(
314
+ swap: PendingSwap,
315
+ currentStep: PendingSwapStep
316
+ ): WalletTypeAndAddress {
317
+ const walletAddress = getCurrentAddressOf(swap, currentStep);
318
+ const walletKV =
319
+ Object.keys(swap.wallets)
320
+ .map((k) => ({ k, v: swap.wallets[k] }))
321
+ .find(({ v }) => v.address === walletAddress) || null;
322
+ const blockchain = walletKV?.k || null;
323
+ const wallet = walletKV?.v || null;
324
+
325
+ const walletType = wallet?.walletType;
326
+ if (walletType === WalletType.UNKNOWN || wallet === null)
327
+ throw PrettyError.AssertionFailed(
328
+ `Wallet for source ${blockchain} not passed: walletType: ${walletType}`
329
+ );
330
+ return wallet;
331
+ }
332
+
333
+ export function getRelatedWalletOrNull(
334
+ swap: PendingSwap,
335
+ currentStep: PendingSwapStep
336
+ ): WalletTypeAndAddress | null {
337
+ try {
338
+ return getRelatedWallet(swap, currentStep);
339
+ } catch (e) {
340
+ return null;
341
+ }
342
+ }
package/src/types.ts ADDED
@@ -0,0 +1,76 @@
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-core';
4
+ import {
5
+ Meta,
6
+ Network,
7
+ WalletState,
8
+ WalletType,
9
+ } from '@rango-dev/wallets-shared';
10
+ import { PendingSwap, SwapProgressNotification, Wallet } from './shared';
11
+ import { EvmBlockchainMeta, SignerFactory } from 'rango-types';
12
+
13
+ export type SwapQueueDef = QueueDef<
14
+ SwapStorage,
15
+ SwapActionTypes,
16
+ SwapQueueContext
17
+ >;
18
+
19
+ export interface SwapStorage extends QueueStorage {
20
+ swapDetails: PendingSwap;
21
+ }
22
+
23
+ export enum SwapActionTypes {
24
+ START = 'START',
25
+ SCHEDULE_NEXT_STEP = 'SCHEDULE_NEXT_STEP',
26
+ CREATE_TRANSACTION = 'CREATE_TRANSACTION',
27
+ EXECUTE_TRANSACTION = 'EXECUTE_TRANSACTION',
28
+ CHECK_TRANSACTION_STATUS = 'CHECK_TRANSACTION_STATUS',
29
+ }
30
+
31
+ export type GetCurrentAddress = (
32
+ type: WalletType,
33
+ network: Network
34
+ ) => string | undefined;
35
+
36
+ export enum BlockReason {
37
+ WAIT_FOR_CONNECT_WALLET = 'waiting_for_connecting_wallet',
38
+ WAIT_FOR_NETWORK_CHANGE = 'waiting_for_network_change',
39
+ DEPENDS_ON_OTHER_QUEUES = 'depends_on_other_queues',
40
+ }
41
+
42
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
43
+ export interface Block<T = any> {
44
+ reason: BlockReason;
45
+ description: string;
46
+ details?: T;
47
+ }
48
+
49
+ export interface SwapQueueContext extends QueueContext {
50
+ meta: Meta;
51
+ wallets: Wallet | null;
52
+ providers: Providers;
53
+ getSigners: (type: WalletType) => SignerFactory;
54
+ switchNetwork: (
55
+ wallet: WalletType,
56
+ network: Network
57
+ ) => Promise<ConnectResult> | undefined;
58
+ connect: (
59
+ wallet: WalletType,
60
+ network: Network
61
+ ) => Promise<ConnectResult> | undefined;
62
+ state: (type: WalletType) => WalletState;
63
+ notifier: (data: SwapProgressNotification) => void;
64
+
65
+ // Dynamically will be added to context.
66
+ claimedBy?: string;
67
+ resetClaimedBy?: () => void;
68
+ }
69
+
70
+ export interface UseQueueManagerParams {
71
+ lastConnectedWallet: string;
72
+ disconnectedWallet: WalletType | undefined;
73
+ clearDisconnectedWallet: () => void;
74
+ evmChains: EvmBlockchainMeta[];
75
+ notifier: SwapQueueContext['notifier'];
76
+ }