@0xsquid/react-hooks 6.4.1-beta.1 → 6.4.1-beta.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.
- package/dist/core/constants.d.ts +15 -0
- package/dist/core/constants.js +15 -0
- package/dist/core/constants.js.map +1 -1
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/index.js.map +1 -1
- package/dist/core/types/error.d.ts +1 -0
- package/dist/core/types/error.js.map +1 -1
- package/dist/core/types/event.d.ts +24 -1
- package/dist/hooks/client/useClient.d.ts +1 -0
- package/dist/hooks/client/useClient.js +13 -1
- package/dist/hooks/client/useClient.js.map +1 -1
- package/dist/hooks/transaction/useExecuteTransaction.js +48 -21
- package/dist/hooks/transaction/useExecuteTransaction.js.map +1 -1
- package/dist/hooks/transaction/useGetRoute.js +35 -7
- package/dist/hooks/transaction/useGetRoute.js.map +1 -1
- package/dist/services/external/revolutClientAdapter.js +3 -3
- package/dist/services/external/revolutClientAdapter.js.map +1 -1
- package/dist/services/internal/assetsService.js +1 -1
- package/dist/services/internal/errorService.d.ts +19 -21
- package/dist/services/internal/errorService.js +105 -77
- package/dist/services/internal/errorService.js.map +1 -1
- package/dist/services/internal/eventService.d.ts +39 -1
- package/dist/services/internal/eventService.js +76 -0
- package/dist/services/internal/eventService.js.map +1 -1
- package/dist/services/internal/fiatToCryptoService.js +2 -1
- package/dist/services/internal/fiatToCryptoService.js.map +1 -1
- package/dist/services/internal/numberService.d.ts +22 -3
- package/dist/services/internal/numberService.js +22 -11
- package/dist/services/internal/numberService.js.map +1 -1
- package/dist/services/internal/transactionService.js +6 -3
- package/dist/services/internal/transactionService.js.map +1 -1
- package/dist/services/internal/walletService.js +28 -3
- package/dist/services/internal/walletService.js.map +1 -1
- package/dist/tests/numberService.test.js +27 -5
- package/dist/tests/numberService.test.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,30 +1,28 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { RouteResponse } from "@0xsquid/squid-types";
|
|
2
|
+
import type { SquidRouteError, SquidStatusError, TransactionErrorWithMessage } from "../../core/types/error";
|
|
3
|
+
import type { TransactionParams } from "../../core/types/transaction";
|
|
2
4
|
export declare const transactionErrorCode: Record<string, TransactionErrorWithMessage>;
|
|
3
5
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
6
|
-
*
|
|
7
|
-
* @param error
|
|
8
|
-
* @returns boolean
|
|
6
|
+
* Converts any transaction error into a standardized TransactionErrorWithMessage format
|
|
7
|
+
* @param error The error to convert
|
|
8
|
+
* @returns A standardized error object
|
|
9
9
|
*/
|
|
10
|
-
export declare const
|
|
10
|
+
export declare const getTransactionError: (error: any) => TransactionErrorWithMessage;
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
13
|
-
* @param error
|
|
14
|
-
* @returns boolean
|
|
12
|
+
* Handles dispatching appropriate events based on the error type
|
|
15
13
|
*/
|
|
16
|
-
export declare const
|
|
14
|
+
export declare const handleTransactionErrorEvents: ({ error, transactionParams, walletProviderName, squidRoute, }: {
|
|
15
|
+
error: TransactionErrorWithMessage;
|
|
16
|
+
transactionParams?: TransactionParams | undefined;
|
|
17
|
+
walletProviderName?: string | undefined;
|
|
18
|
+
squidRoute?: {
|
|
19
|
+
estimate: import("@0xsquid/squid-types").Estimate;
|
|
20
|
+
transactionRequest?: import("@0xsquid/squid-types").SquidData | undefined;
|
|
21
|
+
params: import("@0xsquid/squid-types").RouteRequest;
|
|
22
|
+
} | undefined;
|
|
23
|
+
}) => TransactionErrorWithMessage;
|
|
17
24
|
/**
|
|
18
|
-
* Check if the error is
|
|
19
|
-
* Because Typescript supports casting but that's only for compilation time,
|
|
20
|
-
* We need it at runtime since we're getting the error object from the wallet
|
|
21
|
-
* @param error
|
|
22
|
-
* @returns boolean
|
|
25
|
+
* Check if the error is a Squid route error
|
|
23
26
|
*/
|
|
24
27
|
export declare const isSwapRouteError: (error: any) => error is SquidRouteError;
|
|
25
28
|
export declare const isStatusError: (error: any) => error is SquidStatusError;
|
|
26
|
-
/**
|
|
27
|
-
* Tries to parse as SquidRouteError Type & Return the error from Record entries
|
|
28
|
-
* @param error
|
|
29
|
-
* @returns string error message
|
|
30
|
-
*/
|
|
@@ -1,103 +1,152 @@
|
|
|
1
1
|
import { TransactionErrorType } from "../../core/types/error";
|
|
2
|
+
import { WidgetEvents } from "./eventService";
|
|
3
|
+
/**
|
|
4
|
+
* Normalizes different error structures into a consistent format
|
|
5
|
+
* This helps handle different wallet error formats uniformly
|
|
6
|
+
*/
|
|
7
|
+
const normalizeError = (error) => {
|
|
8
|
+
// Handle nested error objects (like Rainbow's error.error structure)
|
|
9
|
+
if (error?.error && typeof error.error === "object") {
|
|
10
|
+
return {
|
|
11
|
+
code: error.error.code,
|
|
12
|
+
message: error.error.message,
|
|
13
|
+
reason: error.error.reason,
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
// Handle direct error properties
|
|
17
|
+
return {
|
|
18
|
+
code: error?.code,
|
|
19
|
+
message: error?.message,
|
|
20
|
+
reason: error?.reason,
|
|
21
|
+
};
|
|
22
|
+
};
|
|
2
23
|
export const transactionErrorCode = {
|
|
3
24
|
// Metamask
|
|
4
25
|
ACTION_REJECTED: {
|
|
5
26
|
type: TransactionErrorType.REJECTED_BY_USER,
|
|
27
|
+
internalLabel: "EVM: ACTION_REJECTED",
|
|
6
28
|
message: "Return to the swap page and try again.",
|
|
7
29
|
},
|
|
8
30
|
// Keplr
|
|
9
31
|
"Request rejected": {
|
|
10
32
|
type: TransactionErrorType.REJECTED_BY_USER,
|
|
33
|
+
internalLabel: "Request rejected",
|
|
11
34
|
message: "Return to the swap page and try again.",
|
|
12
35
|
},
|
|
13
36
|
// Phantom Solana
|
|
14
37
|
"User rejected the request.": {
|
|
15
38
|
type: TransactionErrorType.REJECTED_BY_USER,
|
|
39
|
+
internalLabel: "SOL: User rejected the request",
|
|
40
|
+
message: "Return to the swap page and try again.",
|
|
41
|
+
},
|
|
42
|
+
// Gnosis Safe
|
|
43
|
+
"Transaction was rejected": {
|
|
44
|
+
type: TransactionErrorType.REJECTED_BY_USER,
|
|
45
|
+
internalLabel: "GNOSIS: Transaction was rejected",
|
|
16
46
|
message: "Return to the swap page and try again.",
|
|
17
47
|
},
|
|
18
48
|
// Metamask
|
|
19
49
|
4001: {
|
|
20
50
|
type: TransactionErrorType.REJECTED_BY_USER,
|
|
51
|
+
internalLabel: "EVM: REJECTED: CODE 4001",
|
|
52
|
+
message: "Return to the swap page and try again.",
|
|
53
|
+
},
|
|
54
|
+
// Rainbow
|
|
55
|
+
"-32603": {
|
|
56
|
+
type: TransactionErrorType.REJECTED_BY_USER,
|
|
57
|
+
internalLabel: "EVM: REJECTED: CODE -32603",
|
|
21
58
|
message: "Return to the swap page and try again.",
|
|
22
59
|
},
|
|
23
60
|
CALL_EXCEPTION: {
|
|
24
61
|
type: TransactionErrorType.CALL_EXCEPTION,
|
|
62
|
+
internalLabel: "EVM: CALL_EXCEPTION",
|
|
25
63
|
message: "Swap failed on the source chain. Please let us know by submitting a ticket here.",
|
|
26
64
|
},
|
|
27
65
|
UNKNOWN: {
|
|
28
66
|
type: TransactionErrorType.UNKNOWN,
|
|
67
|
+
internalLabel: "UNKNOWN ERROR: Transaction failed",
|
|
29
68
|
message: "The transaction could not be initiated.",
|
|
30
69
|
},
|
|
31
70
|
};
|
|
32
71
|
/**
|
|
33
|
-
*
|
|
34
|
-
* Because Typescript supports casting but that's only for compilation time,
|
|
35
|
-
* We need it at runtime since we're getting the error object from the wallet
|
|
36
|
-
* @param error
|
|
37
|
-
* @returns boolean
|
|
72
|
+
* Detects if an error is a user rejection based on normalized error structure
|
|
38
73
|
*/
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
catch {
|
|
74
|
+
const isUserRejectionError = (normalizedError) => {
|
|
75
|
+
const { code, message } = normalizedError;
|
|
76
|
+
// Check if the error matches any known rejection patterns from transactionErrorCode
|
|
77
|
+
return Object.entries(transactionErrorCode).some(([key, value]) => {
|
|
78
|
+
if (value.type !== TransactionErrorType.REJECTED_BY_USER)
|
|
79
|
+
return false;
|
|
80
|
+
// Check if code matches (converting to string for comparison)
|
|
81
|
+
if (code != undefined && key === code.toString())
|
|
82
|
+
return true;
|
|
83
|
+
// Check if message matches
|
|
84
|
+
if (message && key === message)
|
|
85
|
+
return true;
|
|
52
86
|
return false;
|
|
53
|
-
}
|
|
54
|
-
};
|
|
55
|
-
const isRainbowWalletTxRejectedError = (error) => {
|
|
56
|
-
try {
|
|
57
|
-
return error.error.code === -32603;
|
|
58
|
-
}
|
|
59
|
-
catch {
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
87
|
+
});
|
|
62
88
|
};
|
|
63
89
|
/**
|
|
64
|
-
*
|
|
65
|
-
* @param error
|
|
66
|
-
* @returns
|
|
90
|
+
* Converts any transaction error into a standardized TransactionErrorWithMessage format
|
|
91
|
+
* @param error The error to convert
|
|
92
|
+
* @returns A standardized error object
|
|
67
93
|
*/
|
|
68
94
|
export const getTransactionError = (error) => {
|
|
69
|
-
|
|
70
|
-
|
|
95
|
+
const normalizedError = normalizeError(error);
|
|
96
|
+
if (isUserRejectionError(normalizedError)) {
|
|
97
|
+
// Return the specific rejection error if we can find it
|
|
98
|
+
if (normalizedError.code) {
|
|
99
|
+
const codeError = transactionErrorCode[normalizedError.code.toString()];
|
|
100
|
+
if (codeError?.type === TransactionErrorType.REJECTED_BY_USER)
|
|
101
|
+
return codeError;
|
|
102
|
+
}
|
|
103
|
+
if (normalizedError.message) {
|
|
104
|
+
const messageError = transactionErrorCode[normalizedError.message];
|
|
105
|
+
if (messageError?.type === TransactionErrorType.REJECTED_BY_USER)
|
|
106
|
+
return messageError;
|
|
107
|
+
}
|
|
71
108
|
return transactionErrorCode.ACTION_REJECTED;
|
|
72
109
|
}
|
|
73
|
-
if (
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
110
|
+
if (normalizedError.code) {
|
|
111
|
+
return (transactionErrorCode[normalizedError.code.toString()] ||
|
|
112
|
+
transactionErrorCode.UNKNOWN);
|
|
113
|
+
}
|
|
114
|
+
if (normalizedError.message) {
|
|
115
|
+
return (transactionErrorCode[normalizedError.message] ||
|
|
116
|
+
transactionErrorCode.UNKNOWN);
|
|
80
117
|
}
|
|
81
118
|
return transactionErrorCode.UNKNOWN;
|
|
82
119
|
};
|
|
83
|
-
// --------------------
|
|
84
|
-
// SQUID ROUTE ERRORS
|
|
85
|
-
// --------------------
|
|
86
|
-
// TODO: Components will be handled outside hooks lib
|
|
87
|
-
// const squidRouteErrorMapping: Record<
|
|
88
|
-
// SquidRouteErrorType,
|
|
89
|
-
// string | JSX.Element
|
|
90
|
-
// > = {
|
|
91
|
-
// Unknown: RouteDefaultErrorMsg(),
|
|
92
|
-
// UnknownError: RouteDefaultErrorMsg(),
|
|
93
|
-
// SquidServiceError: RouteDefaultErrorMsg(),
|
|
94
|
-
// };
|
|
95
120
|
/**
|
|
96
|
-
*
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
121
|
+
* Handles dispatching appropriate events based on the error type
|
|
122
|
+
*/
|
|
123
|
+
export const handleTransactionErrorEvents = ({ error, transactionParams, walletProviderName, squidRoute, }) => {
|
|
124
|
+
// Handle wallet rejection events
|
|
125
|
+
if (error.type === TransactionErrorType.REJECTED_BY_USER) {
|
|
126
|
+
// At this point, we don't have transactionParams so we should use the squidRoute
|
|
127
|
+
WidgetEvents.getInstance().dispatchWalletReject({
|
|
128
|
+
chainId: squidRoute?.params.fromChain || "",
|
|
129
|
+
provider: walletProviderName || "",
|
|
130
|
+
reason: error.internalLabel,
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
else if (error.type === TransactionErrorType.CALL_EXCEPTION) {
|
|
134
|
+
// Handle on-chain transaction errors
|
|
135
|
+
WidgetEvents.getInstance().dispatchOnchainReject({
|
|
136
|
+
walletAddress: transactionParams?.fromAddress || "",
|
|
137
|
+
chainId: transactionParams?.fromChain?.chainId || "",
|
|
138
|
+
reason: error.internalLabel,
|
|
139
|
+
txHash: transactionParams?.transactionId,
|
|
140
|
+
});
|
|
141
|
+
}
|
|
142
|
+
// Always dispatch the swap route execution error
|
|
143
|
+
WidgetEvents.getInstance().dispatchSwapRouteExecutionError({
|
|
144
|
+
error,
|
|
145
|
+
});
|
|
146
|
+
return error;
|
|
147
|
+
};
|
|
148
|
+
/**
|
|
149
|
+
* Check if the error is a Squid route error
|
|
101
150
|
*/
|
|
102
151
|
export const isSwapRouteError = (error) => {
|
|
103
152
|
return (error &&
|
|
@@ -106,25 +155,4 @@ export const isSwapRouteError = (error) => {
|
|
|
106
155
|
export const isStatusError = (error) => {
|
|
107
156
|
return error && typeof error.errorType === "string";
|
|
108
157
|
};
|
|
109
|
-
/**
|
|
110
|
-
* Tries to parse as SquidRouteError Type & Return the error from Record entries
|
|
111
|
-
* @param error
|
|
112
|
-
* @returns string error message
|
|
113
|
-
*/
|
|
114
|
-
// TODO: move this outside hooks
|
|
115
|
-
// export const getSquidRouteErrorMessage = (error: any): string | JSX.Element => {
|
|
116
|
-
// if (isSwapRouteError(error)) {
|
|
117
|
-
// // Try to get the error message from the error code
|
|
118
|
-
// const codeMsg =
|
|
119
|
-
// error.code || error.errorType
|
|
120
|
-
// ? squidRouteErrorMapping[
|
|
121
|
-
// (error.code ?? error.errorType) as SquidRouteErrorType
|
|
122
|
-
// ]
|
|
123
|
-
// : undefined;
|
|
124
|
-
// if (codeMsg) return codeMsg;
|
|
125
|
-
// // If there is no error code, try to get the error message from backend
|
|
126
|
-
// if (error.message) return error.message;
|
|
127
|
-
// }
|
|
128
|
-
// return squidRouteErrorMapping.Unknown;
|
|
129
|
-
// };
|
|
130
158
|
//# sourceMappingURL=errorService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorService.js","sourceRoot":"","sources":["../../../src/services/internal/errorService.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D,MAAM,CAAC,MAAM,oBAAoB,GAC/B;IACE,WAAW;IACX,eAAe,EAAE;QACf,IAAI,EAAE,oBAAoB,CAAC,gBAAgB;QAC3C,OAAO,EAAE,wCAAwC;KAClD;IACD,QAAQ;IACR,kBAAkB,EAAE;QAClB,IAAI,EAAE,oBAAoB,CAAC,gBAAgB;QAC3C,OAAO,EAAE,wCAAwC;KAClD;IACD,iBAAiB;IACjB,4BAA4B,EAAE;QAC5B,IAAI,EAAE,oBAAoB,CAAC,gBAAgB;QAC3C,OAAO,EAAE,wCAAwC;KAClD;IACD,WAAW;IACX,IAAI,EAAE;QACJ,IAAI,EAAE,oBAAoB,CAAC,gBAAgB;QAC3C,OAAO,EAAE,wCAAwC;KAClD;IACD,cAAc,EAAE;QACd,IAAI,EAAE,oBAAoB,CAAC,cAAc;QACzC,OAAO,EACL,kFAAkF;KACrF;IACD,OAAO,EAAE;QACP,IAAI,EAAE,oBAAoB,CAAC,OAAO;QAClC,OAAO,EAAE,yCAAyC;KACnD;CACF,CAAC;AAEJ
|
|
1
|
+
{"version":3,"file":"errorService.js","sourceRoot":"","sources":["../../../src/services/internal/errorService.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAE9D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAS9C;;;GAGG;AACH,MAAM,cAAc,GAAG,CAAC,KAAU,EAAmB,EAAE;IACrD,qEAAqE;IACrE,IAAI,KAAK,EAAE,KAAK,IAAI,OAAO,KAAK,CAAC,KAAK,KAAK,QAAQ,EAAE;QACnD,OAAO;YACL,IAAI,EAAE,KAAK,CAAC,KAAK,CAAC,IAAI;YACtB,OAAO,EAAE,K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J;IAED,iDAAiD;IACjD,YAAY,CAAC,WAAW,EAAE,CAAC,+BAA+B,CAAC;QACzD,KAAK;KACN,CAAC,CAAC;IAEH,OAAO,KAAK,CAAC;AACf,CAAC,CAAC;AAEF;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG,CAAC,KAAU,EAA4B,EAAE;IACvE,OAAO,CACL,KAAK;QACL,CAAC,OAAO,KAAK,CAAC,IAAI,KAAK,QAAQ,IAAI,OAAO,KAAK,CAAC,SAAS,KAAK,QAAQ,CAAC,CACxE,CAAC;AACJ,CAAC,CAAC;AAEF,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,KAAU,EAA6B,EAAE;IACrE,OAAO,KAAK,IAAI,OAAO,KAAK,CAAC,SAAS,KAAK,QAAQ,CAAC;AACtD,CAAC,CAAC"}
|
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import type { RouteResponse } from "@0xsquid/squid-types";
|
|
1
|
+
import type { RouteRequest, RouteResponse, Token } from "@0xsquid/squid-types";
|
|
2
2
|
import type { EventListenerFunction, WidgetEventMap } from "../../core/types/event";
|
|
3
|
+
type FindTokenMethod = (address?: string, chainId?: string) => Token | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* Creates a hash string from quote request parameters to track changes
|
|
6
|
+
*/
|
|
7
|
+
export declare const createQuoteRequestParamsHash: (params: RouteRequest) => string;
|
|
3
8
|
export declare class WidgetEvents extends EventTarget {
|
|
4
9
|
private static instance;
|
|
5
10
|
private constructor();
|
|
@@ -59,4 +64,37 @@ export declare class WidgetEvents extends EventTarget {
|
|
|
59
64
|
* @param data
|
|
60
65
|
*/
|
|
61
66
|
dispatchSwapRouteExecutionError(data: WidgetEventMap["swapRouteExecutionError"]): void;
|
|
67
|
+
/**
|
|
68
|
+
* Dispatch event when requesting a quote for swap
|
|
69
|
+
* @param data Quote request parameters
|
|
70
|
+
*/
|
|
71
|
+
dispatchRequestQuote(data: WidgetEventMap["requestQuote"]): void;
|
|
72
|
+
/**
|
|
73
|
+
* Dispatch event before swap execution
|
|
74
|
+
* @param data Pre-swap parameters
|
|
75
|
+
* @param findToken Function to find token details
|
|
76
|
+
*/
|
|
77
|
+
dispatchPreSwap(data: WidgetEventMap["preSwap"], findToken: FindTokenMethod): void;
|
|
78
|
+
/**
|
|
79
|
+
* Dispatch event after swap execution
|
|
80
|
+
* @param data Post-swap parameters including transaction hash
|
|
81
|
+
* @param findToken Function to find token details
|
|
82
|
+
*/
|
|
83
|
+
dispatchPostSwap(data: WidgetEventMap["postSwap"], findToken: FindTokenMethod): void;
|
|
84
|
+
/**
|
|
85
|
+
* Dispatch event when wallet is successfully connected
|
|
86
|
+
* @param data Wallet connection details
|
|
87
|
+
*/
|
|
88
|
+
dispatchWalletConnect(data: WidgetEventMap["walletConnect"]): void;
|
|
89
|
+
/**
|
|
90
|
+
* Dispatch event when wallet connection is rejected
|
|
91
|
+
* @param data Wallet rejection details
|
|
92
|
+
*/
|
|
93
|
+
dispatchWalletReject(data: WidgetEventMap["walletReject"]): void;
|
|
94
|
+
/**
|
|
95
|
+
* Dispatch event when onchain transaction is rejected
|
|
96
|
+
* @param data Onchain rejection details
|
|
97
|
+
*/
|
|
98
|
+
dispatchOnchainReject(data: WidgetEventMap["onchainReject"]): void;
|
|
62
99
|
}
|
|
100
|
+
export {};
|
|
@@ -1,3 +1,35 @@
|
|
|
1
|
+
import { formatAmount } from "./estimateService";
|
|
2
|
+
/**
|
|
3
|
+
* Creates a hash string from quote request parameters to track changes
|
|
4
|
+
*/
|
|
5
|
+
export const createQuoteRequestParamsHash = (params) => {
|
|
6
|
+
return [
|
|
7
|
+
params.fromAddress || "",
|
|
8
|
+
params.toAddress || "",
|
|
9
|
+
params.fromChain,
|
|
10
|
+
params.toChain,
|
|
11
|
+
`${params.fromToken}_${params.fromChain}`,
|
|
12
|
+
`${params.toToken}_${params.toChain}`,
|
|
13
|
+
params.fromAmount,
|
|
14
|
+
].join("|");
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Formats the amount based on token details
|
|
18
|
+
* @param data Event parameters containing fromToken, fromChain, and fromAmount
|
|
19
|
+
* @param findToken Function to find token details
|
|
20
|
+
* @returns Original data with formatted amount
|
|
21
|
+
*/
|
|
22
|
+
const formatTokenAmount = (data, findToken) => {
|
|
23
|
+
const fromToken = findToken(data.fromToken, data.fromChain);
|
|
24
|
+
const formattedFromAmount = formatAmount(data.fromAmount, fromToken?.decimals);
|
|
25
|
+
// Rewrite the fromAmount with the formatted amount
|
|
26
|
+
// Example: 1000000000000000000 -> 1 with decimals 18
|
|
27
|
+
// For readability of the event data
|
|
28
|
+
return {
|
|
29
|
+
...data,
|
|
30
|
+
fromAmount: formattedFromAmount,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
1
33
|
export class WidgetEvents extends EventTarget {
|
|
2
34
|
static instance;
|
|
3
35
|
constructor() {
|
|
@@ -76,5 +108,49 @@ export class WidgetEvents extends EventTarget {
|
|
|
76
108
|
dispatchSwapRouteExecutionError(data) {
|
|
77
109
|
this.dispatch("swapRouteExecutionError", data);
|
|
78
110
|
}
|
|
111
|
+
/**
|
|
112
|
+
* Dispatch event when requesting a quote for swap
|
|
113
|
+
* @param data Quote request parameters
|
|
114
|
+
*/
|
|
115
|
+
dispatchRequestQuote(data) {
|
|
116
|
+
this.dispatch("requestQuote", data);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Dispatch event before swap execution
|
|
120
|
+
* @param data Pre-swap parameters
|
|
121
|
+
* @param findToken Function to find token details
|
|
122
|
+
*/
|
|
123
|
+
dispatchPreSwap(data, findToken) {
|
|
124
|
+
this.dispatch("preSwap", formatTokenAmount(data, findToken));
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Dispatch event after swap execution
|
|
128
|
+
* @param data Post-swap parameters including transaction hash
|
|
129
|
+
* @param findToken Function to find token details
|
|
130
|
+
*/
|
|
131
|
+
dispatchPostSwap(data, findToken) {
|
|
132
|
+
this.dispatch("postSwap", formatTokenAmount(data, findToken));
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Dispatch event when wallet is successfully connected
|
|
136
|
+
* @param data Wallet connection details
|
|
137
|
+
*/
|
|
138
|
+
dispatchWalletConnect(data) {
|
|
139
|
+
this.dispatch("walletConnect", data);
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Dispatch event when wallet connection is rejected
|
|
143
|
+
* @param data Wallet rejection details
|
|
144
|
+
*/
|
|
145
|
+
dispatchWalletReject(data) {
|
|
146
|
+
this.dispatch("walletReject", data);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Dispatch event when onchain transaction is rejected
|
|
150
|
+
* @param data Onchain rejection details
|
|
151
|
+
*/
|
|
152
|
+
dispatchOnchainReject(data) {
|
|
153
|
+
this.dispatch("onchainReject", data);
|
|
154
|
+
}
|
|
79
155
|
}
|
|
80
156
|
//# sourceMappingURL=eventService.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"eventService.js","sourceRoot":"","sources":["../../../src/services/internal/eventService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"eventService.js","sourceRoot":"","sources":["../../../src/services/internal/eventService.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAOjD;;GAEG;AACH,MAAM,CAAC,MAAM,4BAA4B,GAAG,CAAC,MAAoB,EAAU,EAAE;IAC3E,OAAO;QACL,MAAM,CAAC,WAAW,IAAI,EAAE;QACxB,MAAM,CAAC,SAAS,IAAI,EAAE;QACtB,MAAM,CAAC,SAAS;QAChB,MAAM,CAAC,OAAO;QACd,GAAG,MAAM,CAAC,SAAS,IAAI,MAAM,CAAC,SAAS,EAAE;QACzC,GAAG,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,OAAO,EAAE;QACrC,MAAM,CAAC,UAAU;KAClB,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACd,CAAC,C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CAAC,CAAC;IACtC,CAAC;IAED;;;OAGG;IACH,qBAAqB,CAAC,IAAqC;QACzD,IAAI,CAAC,QAAQ,CAAC,eAAe,EAAE,IAAI,CAAC,CAAC;IACvC,CAAC;CACF"}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { countries } from "countries-list";
|
|
2
2
|
import getSymbolFromCurrency from "currency-symbol-map";
|
|
3
|
+
import { DEFAULT_COUNTRY_CODE } from "../../core/constants";
|
|
3
4
|
import { countryCurrencyMap } from "./countryCurrencyMap";
|
|
4
5
|
export class FiatToCryptoService {
|
|
5
6
|
provider;
|
|
@@ -9,7 +10,7 @@ export class FiatToCryptoService {
|
|
|
9
10
|
static getProviderInfo(provider) {
|
|
10
11
|
return provider.getInfo();
|
|
11
12
|
}
|
|
12
|
-
async getQuote(fiatCurrency, cryptoCurrencyID, amount, paymentMethod = "card", region =
|
|
13
|
+
async getQuote(fiatCurrency, cryptoCurrencyID, amount, paymentMethod = "card", region = DEFAULT_COUNTRY_CODE) {
|
|
13
14
|
if (!this.provider)
|
|
14
15
|
throw new Error("Provider is not initialized");
|
|
15
16
|
return this.provider.getQuote(fiatCurrency, cryptoCurrencyID, amount, paymentMethod, region);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"fiatToCryptoService.js","sourceRoot":"","sources":["../../../src/services/internal/fiatToCryptoService.ts"],"names":[],"mappings":"AAYA,OAAO,EAAE,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,qBAAqB,MAAM,qBAAqB,CAAC;AACxD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAC;AAE1D,MAAM,OAAO,mBAAmB;IACtB,QAAQ,CAAwB;IAExC,YAAY,QAA+B;QACzC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED,MAAM,CAAC,eAAe,CAAC,QAA8B;QACnD,OAAO,QAAQ,CAAC,OAAO,EAAE,CAAC;IAC5B,CAAC;IA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string, decimals?: number) => bigint
|
|
|
16
16
|
export declare const formatBNToReadable: (value: bigint | string, decimals?: number) => string;
|
|
17
17
|
export declare const roundNumericValue: (value?: string, precision?: number, useComaEvery3Digits?: boolean, significantFigures?: number) => string;
|
|
18
18
|
export declare function formatUnitsRounded(value: string, decimals?: string | number, maxDecimalDigits?: number): string;
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
19
|
+
interface FormatTokenAmountOptions {
|
|
20
|
+
/**
|
|
21
|
+
* When set to true, formats `amount` to its exact value, without any rounding.
|
|
22
|
+
*/
|
|
23
|
+
exact?: boolean;
|
|
24
|
+
/**
|
|
25
|
+
* When set to true, formats `amount` to a compact value.
|
|
26
|
+
* Useful for cases where precision isn’t needed, just a rough estimate.
|
|
27
|
+
*/
|
|
28
|
+
simplified?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export declare function formatTokenAmount(amount: string, { exact, simplified }?: FormatTokenAmountOptions): string;
|
|
22
31
|
interface FormatUsdAmountOptions {
|
|
32
|
+
/**
|
|
33
|
+
* When set to true, `amount` will be prefixed with a dollar sign ($)
|
|
34
|
+
*/
|
|
23
35
|
includeSign?: boolean;
|
|
36
|
+
/**
|
|
37
|
+
* If amount is above the threshold, uses 2 decimal places.
|
|
38
|
+
*
|
|
39
|
+
* If amount is below the threshold, uses a minimum of 2 decimal places
|
|
40
|
+
* and a maximum of 5 decimal places.
|
|
41
|
+
* If there are more decimal places than the maximum, will round the extra decimals.
|
|
42
|
+
*/
|
|
24
43
|
decimalPrecision?: boolean;
|
|
25
44
|
}
|
|
26
45
|
export declare function formatUsdAmount(amount?: number | bigint | string, { includeSign, decimalPrecision }?: FormatUsdAmountOptions): string;
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
import take from "lodash/take";
|
|
3
3
|
import takeWhile from "lodash/takeWhile";
|
|
4
4
|
import { formatUnits, parseUnits } from "viem";
|
|
5
|
+
import { DEFAULT_LOCALE } from "../../core/constants";
|
|
5
6
|
// =====================================================
|
|
6
7
|
// NOTE: We wrap viem utility method here
|
|
7
8
|
// So that in the future we can change the implementation
|
|
@@ -47,7 +48,7 @@ export const roundNumericValue = (value = "0", precision = 2, useComaEvery3Digit
|
|
|
47
48
|
// We might monitor and have a polyfill if some people are not
|
|
48
49
|
// Seeing the comas.
|
|
49
50
|
if (useComaEvery3Digits && integers !== "0") {
|
|
50
|
-
integers = parseInt(integers, 10).toLocaleString(
|
|
51
|
+
integers = parseInt(integers, 10).toLocaleString(DEFAULT_LOCALE).toString();
|
|
51
52
|
}
|
|
52
53
|
if (significantFigures && decimals) {
|
|
53
54
|
const zeros = takeWhile(decimals?.toString(), (c) => c === "0").length;
|
|
@@ -73,17 +74,20 @@ export function formatUnitsRounded(value, decimals, maxDecimalDigits) {
|
|
|
73
74
|
}
|
|
74
75
|
}
|
|
75
76
|
const MAX_TOKEN_DECIMALS = 8;
|
|
76
|
-
const
|
|
77
|
+
const TOKEN_EXACT_AMOUNT_FORMATTER = new Intl.NumberFormat(DEFAULT_LOCALE, {
|
|
77
78
|
minimumFractionDigits: 0,
|
|
78
79
|
maximumFractionDigits: MAX_TOKEN_DECIMALS,
|
|
79
80
|
});
|
|
80
|
-
const
|
|
81
|
+
const TOKEN_ROUNDED_AMOUNT_FORMATTER = new Intl.NumberFormat(DEFAULT_LOCALE, {
|
|
81
82
|
minimumFractionDigits: 0,
|
|
82
83
|
maximumFractionDigits: MAX_TOKEN_DECIMALS,
|
|
83
84
|
minimumSignificantDigits: 2,
|
|
84
85
|
maximumSignificantDigits: 4,
|
|
85
86
|
});
|
|
86
|
-
|
|
87
|
+
const TOKEN_SIMPLIFIED_AMOUNT_FORMATTER = new Intl.NumberFormat(DEFAULT_LOCALE, {
|
|
88
|
+
maximumSignificantDigits: 2,
|
|
89
|
+
});
|
|
90
|
+
export function formatTokenAmount(amount, { exact = true, simplified = false } = {}) {
|
|
87
91
|
const numericAmount = parseFloat(amount || "0");
|
|
88
92
|
if (numericAmount <= 0) {
|
|
89
93
|
return "0";
|
|
@@ -92,13 +96,17 @@ export function formatTokenAmount(amount, { exact = true } = {}) {
|
|
|
92
96
|
if (numericAmount < Math.pow(10, -MAX_TOKEN_DECIMALS)) {
|
|
93
97
|
return `<${Math.pow(10, -MAX_TOKEN_DECIMALS).toFixed(MAX_TOKEN_DECIMALS)}`;
|
|
94
98
|
}
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
99
|
+
if (simplified) {
|
|
100
|
+
return TOKEN_SIMPLIFIED_AMOUNT_FORMATTER.format(numericAmount);
|
|
101
|
+
}
|
|
102
|
+
if (exact) {
|
|
103
|
+
return TOKEN_EXACT_AMOUNT_FORMATTER.format(numericAmount);
|
|
104
|
+
}
|
|
105
|
+
return TOKEN_ROUNDED_AMOUNT_FORMATTER.format(numericAmount);
|
|
98
106
|
}
|
|
99
107
|
// For values below 100,000, use exact integer precision
|
|
100
108
|
// e.g 1,234.56, 98,765.43
|
|
101
|
-
const STANDARD_USD_INTL_FORMATTER = new Intl.NumberFormat(
|
|
109
|
+
const STANDARD_USD_INTL_FORMATTER = new Intl.NumberFormat(DEFAULT_LOCALE, {
|
|
102
110
|
style: "currency",
|
|
103
111
|
currency: "USD",
|
|
104
112
|
minimumFractionDigits: 2,
|
|
@@ -106,7 +114,7 @@ const STANDARD_USD_INTL_FORMATTER = new Intl.NumberFormat("en-US", {
|
|
|
106
114
|
});
|
|
107
115
|
// For values above 100,000, use compact notation
|
|
108
116
|
// e.g $12,34K, $98,765.43M
|
|
109
|
-
const COMPACT_USD_INTL_FORMATTER = new Intl.NumberFormat(
|
|
117
|
+
const COMPACT_USD_INTL_FORMATTER = new Intl.NumberFormat(DEFAULT_LOCALE, {
|
|
110
118
|
style: "currency",
|
|
111
119
|
currency: "USD",
|
|
112
120
|
notation: "compact",
|
|
@@ -116,12 +124,13 @@ const COMPACT_USD_INTL_FORMATTER = new Intl.NumberFormat("en-US", {
|
|
|
116
124
|
});
|
|
117
125
|
// For small amounts where decimal precision is important
|
|
118
126
|
// e.g $0.00045, $1,231.3209, $57.00002
|
|
119
|
-
const PRECISE_USD_INTL_FORMATTER = new Intl.NumberFormat(
|
|
127
|
+
const PRECISE_USD_INTL_FORMATTER = new Intl.NumberFormat(DEFAULT_LOCALE, {
|
|
120
128
|
style: "currency",
|
|
121
129
|
currency: "USD",
|
|
122
130
|
minimumFractionDigits: 2,
|
|
123
131
|
maximumFractionDigits: 5,
|
|
124
132
|
});
|
|
133
|
+
const DECIMAL_PRECISION_THRESHOLD = 1;
|
|
125
134
|
export function formatUsdAmount(amount = "0", { includeSign = true, decimalPrecision = false } = {}) {
|
|
126
135
|
const parsedAmount = Number(amount);
|
|
127
136
|
if (parsedAmount < 0.01 && parsedAmount > 0 && !decimalPrecision) {
|
|
@@ -131,7 +140,9 @@ export function formatUsdAmount(amount = "0", { includeSign = true, decimalPreci
|
|
|
131
140
|
if (parsedAmount >= 100_000) {
|
|
132
141
|
return COMPACT_USD_INTL_FORMATTER.format(parsedAmount).replace(/^\$/, includeSign ? "$" : "");
|
|
133
142
|
}
|
|
134
|
-
|
|
143
|
+
// Handle small amounts with decimal precision
|
|
144
|
+
// We don't care about decimal precision for big amounts
|
|
145
|
+
if (decimalPrecision && parsedAmount < DECIMAL_PRECISION_THRESHOLD) {
|
|
135
146
|
return PRECISE_USD_INTL_FORMATTER.format(parsedAmount).replace(/^\$/, includeSign ? "$" : "");
|
|
136
147
|
}
|
|
137
148
|
// Handle amounts less than 100,000 with exact integer precision
|