@rango-dev/queue-manager-rango-preset 0.1.10-next.78 → 0.1.10-next.80

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.
@@ -0,0 +1 @@
1
+ export { httpService } from './httpService';
@@ -1,4 +1,5 @@
1
1
  import { WalletType } from '@rango-dev/wallets-shared';
2
+ import { SignerErrorCode, isSignerErrorCode } from 'rango-types';
2
3
 
3
4
  export type ErrorDetail = {
4
5
  extraMessage: string;
@@ -153,4 +154,30 @@ export const ERROR_CONFIRM_SWAP = (status?: number | string): string =>
153
154
  export const WARNING_STARKNET_FOUND =
154
155
  'StarknNet blockchain is still an ALPHA version. As such, delays may occur, and catastrophic bugs may lurk.';
155
156
 
156
-
157
+ function isAPIErrorCode(value: string): value is APIErrorCode {
158
+ return (Object.values(APIErrorCode) as string[]).includes(value);
159
+ }
160
+
161
+ export function mapAppErrorCodesToAPIErrorCode(
162
+ errorCode: string | null
163
+ ): APIErrorCode {
164
+ const defaultErrorCode = APIErrorCode.CLIENT_UNEXPECTED_BEHAVIOUR;
165
+ try {
166
+ if (!errorCode) return defaultErrorCode;
167
+ if (isAPIErrorCode(errorCode)) return errorCode;
168
+ if (isSignerErrorCode(errorCode)) {
169
+ const t: { [key in SignerErrorCode]: APIErrorCode } = {
170
+ [SignerErrorCode.REJECTED_BY_USER]: APIErrorCode.USER_REJECT,
171
+ [SignerErrorCode.SIGN_TX_ERROR]: APIErrorCode.CALL_WALLET_FAILED,
172
+ [SignerErrorCode.SEND_TX_ERROR]: APIErrorCode.SEND_TX_FAILED,
173
+ [SignerErrorCode.NOT_IMPLEMENTED]: defaultErrorCode,
174
+ [SignerErrorCode.OPERATION_UNSUPPORTED]: defaultErrorCode,
175
+ [SignerErrorCode.UNEXPECTED_BEHAVIOUR]: defaultErrorCode,
176
+ };
177
+ return t[errorCode];
178
+ }
179
+ return defaultErrorCode;
180
+ } catch (err) {
181
+ return defaultErrorCode;
182
+ }
183
+ }
package/src/shared.ts CHANGED
@@ -6,27 +6,17 @@ import {
6
6
  SimulationResult,
7
7
  SolanaTransaction,
8
8
  StarknetTransaction,
9
- Transaction,
10
9
  TronTransaction,
11
10
  Transfer as TransferTransaction,
12
11
  } from 'rango-sdk';
13
- import { BigNumber } from 'bignumber.js';
14
12
 
15
13
  import { ErrorDetail, PrettyError } from './shared-errors';
16
- import { getRelatedWallet } from './helpers';
17
14
  import { SignerError } from 'rango-types';
18
15
 
19
- export type SwapperStatusResponse = {
20
- status: 'running' | 'failed' | 'success' | null;
21
- extraMessage: string | null;
22
- timestamp: number;
23
- outputAmount: BigNumber | null;
24
- explorerUrl: SwapExplorerUrl[] | null;
25
- trackingCode: string;
26
- newTx: Transaction | null;
27
- diagnosisUrl: string | null;
28
- steps: SwapperStatusStep[] | null;
29
- };
16
+ export interface PendingSwapWithQueueID {
17
+ id: string;
18
+ swap: PendingSwap;
19
+ }
30
20
 
31
21
  export type SwapProgressNotification = {
32
22
  eventType: EventType;
@@ -53,7 +43,7 @@ export type Account = {
53
43
  walletType: WalletType;
54
44
  error: boolean;
55
45
  explorerUrl: string | null;
56
- isConnected: boolean;
46
+ isConnected?: boolean;
57
47
  };
58
48
  export type Blockchain = { name: string; accounts: Account[] };
59
49
  export type Wallet = { blockchains: Blockchain[] };
@@ -267,6 +257,20 @@ export const getEvmApproveUrl = (
267
257
  throw Error(`Explorer url for ${network} is not implemented`);
268
258
  };
269
259
 
260
+ export const getStarknetApproveUrl = (tx: string): string => {
261
+ return 'https://starkscan.co/tx/{txHash}'.replace(
262
+ '{txHash}',
263
+ tx.toLowerCase()
264
+ );
265
+ };
266
+
267
+ export const getTronApproveUrl = (tx: string): string => {
268
+ return 'https://tronscan.org/#/transaction/{txHash}'.replace(
269
+ '{txHash}',
270
+ tx.toLowerCase()
271
+ );
272
+ };
273
+
270
274
  export const prettifyErrorMessage = (obj: unknown): ErrorDetail => {
271
275
  if (!obj) return { extraMessage: '', extraMessageErrorCode: null };
272
276
  if (obj instanceof PrettyError) return obj.getErrorDetail();
@@ -291,19 +295,6 @@ export const prettifyErrorMessage = (obj: unknown): ErrorDetail => {
291
295
  return { extraMessage: obj, extraMessageErrorCode: null };
292
296
  };
293
297
 
294
- export function getCookieId(): string {
295
- const key = 'X-Rango-Id';
296
- const cookieId = window.localStorage.getItem(key);
297
- if (cookieId) {
298
- return cookieId;
299
- }
300
- const value =
301
- Math.random().toString(36).substring(2, 15) +
302
- Math.random().toString(36).substring(2, 15);
303
- window.localStorage.setItem(key, value);
304
- return value;
305
- }
306
-
307
298
  export function getNextStep(
308
299
  swap: PendingSwap,
309
300
  currentStep: PendingSwapStep
@@ -318,6 +309,45 @@ export function getNextStep(
318
309
  );
319
310
  }
320
311
 
312
+ // TODO: we have samething in `helpers`, for fixing circular dependency, we copied and should be removed eventually.
313
+ export const getCurrentAddressOf = (
314
+ swap: PendingSwap,
315
+ step: PendingSwapStep
316
+ ): string => {
317
+ const result =
318
+ swap.wallets[step.evmTransaction?.blockChain || ''] ||
319
+ swap.wallets[step.evmApprovalTransaction?.blockChain || ''] ||
320
+ swap.wallets[step.cosmosTransaction?.blockChain || ''] ||
321
+ swap.wallets[step.solanaTransaction?.blockChain || ''] ||
322
+ (step.transferTransaction?.fromWalletAddress
323
+ ? { address: step.transferTransaction?.fromWalletAddress }
324
+ : null) ||
325
+ null;
326
+ if (result == null) throw PrettyError.WalletMissing();
327
+ return result.address;
328
+ };
329
+
330
+ // TODO: we have samething in `helpers`, for fixing circular dependency, we copied and should be removed eventually.
331
+ export function getRelatedWallet(
332
+ swap: PendingSwap,
333
+ currentStep: PendingSwapStep
334
+ ): WalletTypeAndAddress {
335
+ const walletAddress = getCurrentAddressOf(swap, currentStep);
336
+ const walletKV =
337
+ Object.keys(swap.wallets)
338
+ .map((k) => ({ k, v: swap.wallets[k] }))
339
+ .find(({ v }) => v.address === walletAddress) || null;
340
+ const blockchain = walletKV?.k || null;
341
+ const wallet = walletKV?.v || null;
342
+
343
+ const walletType = wallet?.walletType;
344
+ if (walletType === WalletType.UNKNOWN || wallet === null)
345
+ throw PrettyError.AssertionFailed(
346
+ `Wallet for source ${blockchain} not passed: walletType: ${walletType}`
347
+ );
348
+ return wallet;
349
+ }
350
+
321
351
  export function getRelatedWalletOrNull(
322
352
  swap: PendingSwap,
323
353
  currentStep: PendingSwapStep
@@ -1,10 +0,0 @@
1
- import { WalletType } from '@rango-dev/wallets-shared';
2
- import { SwapperStatusResponse } from './shared';
3
- import { CheckApprovalResponse, CreateTransactionRequest, CreateTransactionResponse } from 'rango-sdk';
4
- import { APIErrorCode } from './shared-errors';
5
- export declare function checkSwapStatus(requestId: string, txId: string, step: number): Promise<SwapperStatusResponse | null>;
6
- export declare function createTransaction(request: CreateTransactionRequest): Promise<CreateTransactionResponse>;
7
- export declare function checkApproved(requestId: string): Promise<CheckApprovalResponse>;
8
- export declare function mapAppErrorCodesToAPIErrorCode(errorCode: string | null): APIErrorCode;
9
- export declare function reportFailed(requestId: string, step: number, eventType: APIErrorCode, reason: string, walletType: WalletType | null): Promise<void>;
10
- //# sourceMappingURL=shared-api.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"shared-api.d.ts","sourceRoot":"","sources":["src/shared-api.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,2BAA2B,CAAC;AACvD,OAAO,EAAe,qBAAqB,EAAE,MAAM,UAAU,CAAC;AAC9D,OAAO,EACL,qBAAqB,EAErB,wBAAwB,EACxB,yBAAyB,EAC1B,MAAM,WAAW,CAAC;AACnB,OAAO,EACL,YAAY,EAIb,MAAM,iBAAiB,CAAC;AASzB,wBAAsB,eAAe,CACnC,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,MAAM,EACZ,IAAI,EAAE,MAAM,GACX,OAAO,CAAC,qBAAqB,GAAG,IAAI,CAAC,CA4BvC;AAED,wBAAsB,iBAAiB,CACrC,OAAO,EAAE,wBAAwB,GAChC,OAAO,CAAC,yBAAyB,CAAC,CAoCpC;AAED,wBAAsB,aAAa,CACjC,SAAS,EAAE,MAAM,GAChB,OAAO,CAAC,qBAAqB,CAAC,CAmBhC;AAMD,wBAAgB,8BAA8B,CAC5C,SAAS,EAAE,MAAM,GAAG,IAAI,GACvB,YAAY,CAoBd;AAED,wBAAsB,YAAY,CAChC,SAAS,EAAE,MAAM,EACjB,IAAI,EAAE,MAAM,EACZ,SAAS,EAAE,YAAY,EACvB,MAAM,EAAE,MAAM,EACd,UAAU,EAAE,UAAU,GAAG,IAAI,GAC5B,OAAO,CAAC,IAAI,CAAC,CAkBf"}
package/src/shared-api.ts DELETED
@@ -1,172 +0,0 @@
1
- import { WalletType } from '@rango-dev/wallets-shared';
2
- import { getCookieId, SwapperStatusResponse } from './shared';
3
- import {
4
- CheckApprovalResponse,
5
- CheckTxStatusRequest,
6
- CreateTransactionRequest,
7
- CreateTransactionResponse,
8
- } from 'rango-sdk';
9
- import {
10
- APIErrorCode,
11
- ApiMethodName,
12
- ERROR_COMMUNICATING_WITH_API,
13
- PrettyError,
14
- } from './shared-errors';
15
- import { BigNumber } from 'bignumber.js';
16
- import {
17
- BASE_URL,
18
- RANGO_COOKIE_HEADER,
19
- RANGO_DAPP_ID_QUERY,
20
- } from './constants';
21
- import { isSignerErrorCode, SignerErrorCode } from 'rango-types';
22
-
23
- export async function checkSwapStatus(
24
- requestId: string,
25
- txId: string,
26
- step: number
27
- ): Promise<SwapperStatusResponse | null> {
28
- const url = `${BASE_URL}/tx/check-status?${RANGO_DAPP_ID_QUERY}`;
29
- const body: CheckTxStatusRequest = { step, txId, requestId };
30
-
31
- const response = await fetch(url, {
32
- method: 'POST',
33
- headers: {
34
- [RANGO_COOKIE_HEADER]: getCookieId(),
35
- 'content-type': 'application/json;charset=UTF-8',
36
- },
37
- body: JSON.stringify(body),
38
- });
39
-
40
- if (
41
- (!!response.status && (response.status < 200 || response.status >= 400)) ||
42
- !response.ok
43
- ) {
44
- const apiError = ERROR_COMMUNICATING_WITH_API(
45
- ApiMethodName.CheckingTransactionStatus
46
- );
47
- throw PrettyError.BadStatusCode(apiError, response.status);
48
- }
49
-
50
- const res = await response.json();
51
- return {
52
- ...res,
53
- outputAmount: res.outputAmount ? new BigNumber(res.outputAmount) : null,
54
- };
55
- }
56
-
57
- export async function createTransaction(
58
- request: CreateTransactionRequest
59
- ): Promise<CreateTransactionResponse> {
60
- const url = `${BASE_URL}/tx/create?${RANGO_DAPP_ID_QUERY}`;
61
- try {
62
- const response = await fetch(url, {
63
- method: 'POST',
64
- headers: {
65
- 'content-type': 'application/json;charset=UTF-8',
66
- [RANGO_COOKIE_HEADER]: getCookieId(),
67
- },
68
- body: JSON.stringify(request),
69
- });
70
-
71
- if (
72
- (!!response.status &&
73
- (response.status < 200 || response.status >= 400)) ||
74
- !response.ok
75
- ) {
76
- throw PrettyError.CreateTransaction(
77
- `Error creating the transaction, status code: ${response.status}`
78
- );
79
- }
80
-
81
- const result: CreateTransactionResponse = await response.json();
82
- if (!result.ok || !result.transaction)
83
- throw PrettyError.CreateTransaction(
84
- result.error || 'bad response from create tx endpoint'
85
- );
86
-
87
- return result;
88
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
89
- } catch (error) {
90
- if (error instanceof Error) {
91
- throw PrettyError.CreateTransaction(error.message);
92
- }
93
- throw error;
94
- }
95
- }
96
-
97
- export async function checkApproved(
98
- requestId: string
99
- ): Promise<CheckApprovalResponse> {
100
- const url = `${BASE_URL}/tx/${requestId}/check-approval?${RANGO_DAPP_ID_QUERY}`;
101
- const response = await fetch(url, {
102
- method: 'GET',
103
- headers: {
104
- 'content-type': 'application/json;charset=UTF-8',
105
- [RANGO_COOKIE_HEADER]: getCookieId(),
106
- },
107
- });
108
-
109
- if (
110
- (!!response.status && (response.status < 200 || response.status >= 400)) ||
111
- !response.ok
112
- ) {
113
- const apiError = ERROR_COMMUNICATING_WITH_API(ApiMethodName.CheckApproval);
114
- throw PrettyError.BadStatusCode(apiError, response.status);
115
- }
116
-
117
- return await response.json();
118
- }
119
-
120
- function isAPIErrorCode(value: string): value is APIErrorCode {
121
- return (Object.values(APIErrorCode) as string[]).includes(value);
122
- }
123
-
124
- export function mapAppErrorCodesToAPIErrorCode(
125
- errorCode: string | null
126
- ): APIErrorCode {
127
- const defaultErrorCode = APIErrorCode.CLIENT_UNEXPECTED_BEHAVIOUR;
128
- try {
129
- if (!errorCode) return defaultErrorCode;
130
- if (isAPIErrorCode(errorCode)) return errorCode;
131
- if (isSignerErrorCode(errorCode)) {
132
- const t: { [key in SignerErrorCode]: APIErrorCode } = {
133
- [SignerErrorCode.REJECTED_BY_USER]: APIErrorCode.USER_REJECT,
134
- [SignerErrorCode.SIGN_TX_ERROR]: APIErrorCode.CALL_WALLET_FAILED,
135
- [SignerErrorCode.SEND_TX_ERROR]: APIErrorCode.SEND_TX_FAILED,
136
- [SignerErrorCode.NOT_IMPLEMENTED]: defaultErrorCode,
137
- [SignerErrorCode.OPERATION_UNSUPPORTED]: defaultErrorCode,
138
- [SignerErrorCode.UNEXPECTED_BEHAVIOUR]: defaultErrorCode,
139
- };
140
- return t[errorCode];
141
- }
142
- return defaultErrorCode;
143
- } catch (err) {
144
- return defaultErrorCode;
145
- }
146
- }
147
-
148
- export async function reportFailed(
149
- requestId: string,
150
- step: number,
151
- eventType: APIErrorCode,
152
- reason: string,
153
- walletType: WalletType | null
154
- ): Promise<void> {
155
- const url = `${BASE_URL}/tx/report-tx?${RANGO_DAPP_ID_QUERY}`;
156
- await fetch(url, {
157
- method: 'POST',
158
- headers: {
159
- 'content-type': 'application/json;charset=UTF-8',
160
- [RANGO_COOKIE_HEADER]: getCookieId(),
161
- },
162
- body: JSON.stringify({
163
- requestId,
164
- step,
165
- eventType,
166
- reason,
167
- tags: {
168
- wallet: walletType,
169
- },
170
- }),
171
- });
172
- }