@aurora-is-near/intents-swap-widget 1.3.1 → 1.3.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/hooks/useMakeQuote.js +90 -89
- package/dist/hooks/useMakeQuote.js.map +1 -1
- package/dist/machine/errors.d.ts +4 -0
- package/dist/machine/errors.js +23 -1
- package/dist/machine/errors.js.map +1 -1
- package/dist/machine/events/validateInputs.js +26 -25
- package/dist/machine/events/validateInputs.js.map +1 -1
- package/dist/utils/tokens/getMainTokenByChain.js +9 -7
- package/dist/utils/tokens/getMainTokenByChain.js.map +1 -1
- package/package.json +1 -1
- package/src/hooks/useMakeQuote.ts +30 -30
- package/src/machine/errors.ts +38 -0
- package/src/machine/events/validateInputs.ts +16 -3
- package/src/utils/tokens/getMainTokenByChain.ts +8 -0
|
@@ -1,170 +1,171 @@
|
|
|
1
|
-
import { useRef as
|
|
2
|
-
import { QuoteRequest as
|
|
1
|
+
import { useRef as _ } from "react";
|
|
2
|
+
import { QuoteRequest as r, OneClickService as I, CancelError as g, ApiError as A } from "@defuse-protocol/one-click-sdk-typescript";
|
|
3
3
|
import { logger as u } from "../logger.js";
|
|
4
|
-
import { useConfig as
|
|
5
|
-
import { QuoteError as
|
|
4
|
+
import { useConfig as E } from "../config.js";
|
|
5
|
+
import { QuoteError as a } from "../errors.js";
|
|
6
6
|
import { guardStates as O } from "../machine/guards/index.js";
|
|
7
7
|
import { useUnsafeSnapshot as k } from "../machine/snap.js";
|
|
8
|
-
import { getIntentsAccountId as
|
|
9
|
-
import { formatBigToHuman as
|
|
10
|
-
import { DRY_QUOTE_ADDRESSES as
|
|
8
|
+
import { getIntentsAccountId as h } from "../utils/intents/getIntentsAccountId.js";
|
|
9
|
+
import { formatBigToHuman as S } from "../utils/formatters/formatBigToHuman.js";
|
|
10
|
+
import { DRY_QUOTE_ADDRESSES as N } from "../constants/chains.js";
|
|
11
11
|
import { useTokens as D } from "./useTokens.js";
|
|
12
|
-
const
|
|
13
|
-
const { ctx: e } = k(), { intentsAccountType: T } =
|
|
12
|
+
const P = ({ variant: w }) => {
|
|
13
|
+
const { ctx: e } = k(), { intentsAccountType: T } = E(), s = !e.walletAddress, { tokens: y } = D(), d = h({
|
|
14
14
|
addressType: T,
|
|
15
|
-
walletAddress:
|
|
16
|
-
}),
|
|
15
|
+
walletAddress: s ? N[T] : e.walletAddress ?? ""
|
|
16
|
+
}), n = _(null), m = s ? N[T] : e.walletAddress ?? "";
|
|
17
17
|
return { make: async ({ message: l } = {}) => {
|
|
18
18
|
if (!O(e, [
|
|
19
19
|
"input_valid_dry",
|
|
20
20
|
"input_valid_external",
|
|
21
21
|
"input_valid_internal"
|
|
22
22
|
])) {
|
|
23
|
-
const
|
|
24
|
-
throw u.error(`[WIDGET] ${
|
|
23
|
+
const t = `Unable to run quote in current state ${e.state}`;
|
|
24
|
+
throw u.error(`[WIDGET] ${t}`), new a({
|
|
25
25
|
code: "QUOTE_INVALID_INITIAL",
|
|
26
|
-
meta: { isDry:
|
|
26
|
+
meta: { isDry: s, message: t }
|
|
27
27
|
});
|
|
28
28
|
}
|
|
29
|
-
if (!
|
|
30
|
-
const
|
|
31
|
-
throw u.error(`[WIDGET] ${
|
|
29
|
+
if (!d) {
|
|
30
|
+
const t = "No corresponding intents account to run a quote";
|
|
31
|
+
throw u.error(`[WIDGET] ${t}`), new a({
|
|
32
32
|
code: "QUOTE_INVALID_INITIAL",
|
|
33
|
-
meta: { isDry:
|
|
33
|
+
meta: { isDry: s, message: t }
|
|
34
34
|
});
|
|
35
35
|
}
|
|
36
|
-
|
|
37
|
-
let
|
|
36
|
+
n.current && !n.current.isCancelled && n.current.cancel();
|
|
37
|
+
let i;
|
|
38
38
|
try {
|
|
39
|
-
const
|
|
39
|
+
const t = {
|
|
40
40
|
// Settings
|
|
41
41
|
slippageTolerance: 100,
|
|
42
42
|
// 1%
|
|
43
|
-
swapType:
|
|
43
|
+
swapType: r.swapType.EXACT_INPUT,
|
|
44
44
|
deadline: new Date(Date.now() + 36e5).toISOString(),
|
|
45
45
|
// 1 hour
|
|
46
|
-
dry:
|
|
46
|
+
dry: s,
|
|
47
47
|
// Source
|
|
48
48
|
originAsset: e.sourceToken.assetId,
|
|
49
49
|
amount: e.sourceTokenAmount
|
|
50
50
|
};
|
|
51
|
-
l && (
|
|
51
|
+
l && (t.customRecipientMsg = l);
|
|
52
52
|
try {
|
|
53
|
-
switch (
|
|
53
|
+
switch (w) {
|
|
54
54
|
case "deposit": {
|
|
55
|
-
const
|
|
55
|
+
const o = y.find(
|
|
56
56
|
(c) => c.blockchain === "near" && c.symbol === e.sourceToken.symbol
|
|
57
57
|
);
|
|
58
|
-
if (!
|
|
59
|
-
throw new
|
|
58
|
+
if (!o)
|
|
59
|
+
throw new a({
|
|
60
60
|
code: "NO_NEAR_TOKEN_FOUND",
|
|
61
61
|
meta: { symbol: e.sourceToken.symbol }
|
|
62
62
|
});
|
|
63
|
-
|
|
64
|
-
...
|
|
65
|
-
recipient:
|
|
66
|
-
recipientType:
|
|
67
|
-
depositType:
|
|
68
|
-
destinationAsset:
|
|
63
|
+
n.current = I.getQuote({
|
|
64
|
+
...t,
|
|
65
|
+
recipient: d,
|
|
66
|
+
recipientType: r.recipientType.INTENTS,
|
|
67
|
+
depositType: r.depositType.ORIGIN_CHAIN,
|
|
68
|
+
destinationAsset: o.assetId,
|
|
69
69
|
// Refund
|
|
70
70
|
refundTo: m,
|
|
71
|
-
refundType:
|
|
72
|
-
}),
|
|
71
|
+
refundType: r.refundType.ORIGIN_CHAIN
|
|
72
|
+
}), i = await n.current;
|
|
73
73
|
break;
|
|
74
74
|
}
|
|
75
75
|
case "swap": {
|
|
76
76
|
if (e.sourceToken.isIntent && e.targetToken.isIntent) {
|
|
77
|
-
|
|
78
|
-
...
|
|
79
|
-
recipient:
|
|
80
|
-
recipientType:
|
|
77
|
+
n.current = I.getQuote({
|
|
78
|
+
...t,
|
|
79
|
+
recipient: d,
|
|
80
|
+
recipientType: r.recipientType.INTENTS,
|
|
81
81
|
destinationAsset: e.targetToken.assetId,
|
|
82
|
-
depositType:
|
|
82
|
+
depositType: r.depositType.INTENTS,
|
|
83
83
|
// Refund
|
|
84
|
-
refundTo:
|
|
85
|
-
refundType:
|
|
86
|
-
}),
|
|
84
|
+
refundTo: d,
|
|
85
|
+
refundType: r.refundType.INTENTS
|
|
86
|
+
}), i = await n.current;
|
|
87
87
|
break;
|
|
88
88
|
}
|
|
89
|
-
|
|
90
|
-
...
|
|
91
|
-
recipient: !e.targetToken.isIntent && e.sendAddress ? e.sendAddress :
|
|
92
|
-
recipientType: e.targetToken.isIntent ?
|
|
89
|
+
n.current = I.getQuote({
|
|
90
|
+
...t,
|
|
91
|
+
recipient: !e.targetToken.isIntent && e.sendAddress ? e.sendAddress : d,
|
|
92
|
+
recipientType: e.targetToken.isIntent ? r.recipientType.INTENTS : r.recipientType.DESTINATION_CHAIN,
|
|
93
93
|
destinationAsset: e.targetToken.assetId,
|
|
94
|
-
depositType: e.sourceToken.isIntent ?
|
|
94
|
+
depositType: e.sourceToken.isIntent ? r.depositType.INTENTS : r.depositType.ORIGIN_CHAIN,
|
|
95
95
|
// Refund
|
|
96
|
-
refundTo: e.sourceToken.isIntent ?
|
|
97
|
-
refundType: e.sourceToken.isIntent ?
|
|
98
|
-
}),
|
|
96
|
+
refundTo: e.sourceToken.isIntent ? d : m,
|
|
97
|
+
refundType: e.sourceToken.isIntent ? r.refundType.INTENTS : r.refundType.ORIGIN_CHAIN
|
|
98
|
+
}), i = await n.current;
|
|
99
99
|
break;
|
|
100
100
|
}
|
|
101
101
|
default: {
|
|
102
|
-
const
|
|
103
|
-
throw u.error(`[WIDGET] ${
|
|
102
|
+
const o = "Unknown swap variant (deposit or swap expected)";
|
|
103
|
+
throw u.error(`[WIDGET] ${o}`), new a({
|
|
104
104
|
code: "QUOTE_INVALID_INITIAL",
|
|
105
|
-
meta: { isDry:
|
|
105
|
+
meta: { isDry: s, message: o }
|
|
106
106
|
});
|
|
107
107
|
}
|
|
108
108
|
}
|
|
109
|
-
} catch (
|
|
110
|
-
if (
|
|
109
|
+
} catch (o) {
|
|
110
|
+
if (o instanceof g)
|
|
111
111
|
return;
|
|
112
|
-
throw
|
|
112
|
+
throw o instanceof A ? (u.error(o), o) : (u.error("[WIDGET] Failed to get a deposit address from the quote"), new a({
|
|
113
113
|
code: "QUOTE_INVALID",
|
|
114
|
-
meta: { isDry:
|
|
115
|
-
})) : (u.error("[WIDGET] Failed to get a deposit address from the quote"), new i({
|
|
116
|
-
code: "QUOTE_INVALID",
|
|
117
|
-
meta: { isDry: n }
|
|
114
|
+
meta: { isDry: s }
|
|
118
115
|
}));
|
|
119
116
|
}
|
|
120
|
-
if (!
|
|
117
|
+
if (!i.quote.depositAddress && !n.current.isCancelled && !s)
|
|
121
118
|
throw new Error("Failed to get a deposit address from the quote");
|
|
122
|
-
} catch (
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
119
|
+
} catch (t) {
|
|
120
|
+
if (t instanceof A) {
|
|
121
|
+
const o = t.body.message ?? t.message;
|
|
122
|
+
if (o.includes("Amount is too low")) {
|
|
123
|
+
const c = o.match(/\d+/), f = c ? c[0] : void 0;
|
|
124
|
+
throw new a({
|
|
125
|
+
code: "QUOTE_AMOUNT_IS_TOO_LOW",
|
|
126
|
+
meta: {
|
|
127
|
+
minAmount: (f && S(f, e.sourceToken.decimals)) ?? "0"
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
throw o.includes("recipient is not valid") ? new a({
|
|
132
|
+
code: "TOKEN_IS_NOT_SUPPORTED"
|
|
133
|
+
}) : o.includes("Failed to get a deposit address") ? new a({
|
|
134
|
+
code: "QUOTE_NO_ONE_TIME_ADDRESS"
|
|
135
|
+
}) : t;
|
|
132
136
|
}
|
|
133
|
-
throw
|
|
134
|
-
code: "TOKEN_IS_NOT_SUPPORTED"
|
|
135
|
-
}) : t.includes("Failed to get a deposit address") ? new i({
|
|
136
|
-
code: "QUOTE_NO_ONE_TIME_ADDRESS"
|
|
137
|
-
}) : new i({
|
|
137
|
+
throw new a({
|
|
138
138
|
code: "QUOTE_FAILED",
|
|
139
139
|
meta: {
|
|
140
|
-
|
|
140
|
+
// @ts-expect-error In case error has a message
|
|
141
|
+
message: t?.message ?? "Failed to fetch quote. Please try again."
|
|
141
142
|
}
|
|
142
143
|
});
|
|
143
144
|
}
|
|
144
145
|
let p;
|
|
145
|
-
if (
|
|
146
|
+
if (s)
|
|
146
147
|
p = {
|
|
147
148
|
dry: !0,
|
|
148
|
-
...
|
|
149
|
+
...i.quote,
|
|
149
150
|
deadline: void 0,
|
|
150
151
|
depositAddress: void 0
|
|
151
152
|
};
|
|
152
|
-
else if (
|
|
153
|
+
else if (i.quote.deadline && i.quote.depositAddress)
|
|
153
154
|
p = {
|
|
154
155
|
dry: !1,
|
|
155
|
-
...
|
|
156
|
-
deadline:
|
|
157
|
-
depositAddress:
|
|
156
|
+
...i.quote,
|
|
157
|
+
deadline: i.quote.deadline,
|
|
158
|
+
depositAddress: i.quote.depositAddress
|
|
158
159
|
};
|
|
159
160
|
else
|
|
160
|
-
throw new
|
|
161
|
+
throw new a({
|
|
161
162
|
code: "QUOTE_INVALID",
|
|
162
|
-
meta: { isDry:
|
|
163
|
+
meta: { isDry: s }
|
|
163
164
|
});
|
|
164
165
|
return p;
|
|
165
|
-
}, cancel: () =>
|
|
166
|
+
}, cancel: () => n.current?.cancel() };
|
|
166
167
|
};
|
|
167
168
|
export {
|
|
168
|
-
|
|
169
|
+
P as useMakeQuote
|
|
169
170
|
};
|
|
170
171
|
//# sourceMappingURL=useMakeQuote.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMakeQuote.js","sources":["../../src/hooks/useMakeQuote.ts"],"sourcesContent":["import { useRef } from 'react';\nimport {\n ApiError,\n CancelablePromise,\n CancelError,\n OneClickService,\n QuoteRequest,\n QuoteResponse,\n} from '@defuse-protocol/one-click-sdk-typescript';\n\nimport { logger } from '@/logger';\nimport { useConfig } from '@/config';\nimport { QuoteError } from '@/errors';\nimport { guardStates } from '@/machine/guards';\nimport { useUnsafeSnapshot } from '@/machine/snap';\nimport { getIntentsAccountId } from '@/utils/intents/getIntentsAccountId';\nimport { formatBigToHuman } from '@/utils/formatters/formatBigToHuman';\nimport { DRY_QUOTE_ADDRESSES } from '@/constants/chains';\nimport { Quote } from '@/types/quote';\n\nimport { useTokens } from './useTokens';\n\ntype Props = {\n variant: 'deposit' | 'swap';\n};\n\nexport const useMakeQuote = ({ variant }: Props) => {\n const { ctx } = useUnsafeSnapshot();\n const { intentsAccountType } = useConfig();\n\n const isDry = !ctx.walletAddress;\n\n const { tokens: tokenList } = useTokens();\n const intentsAccountId = getIntentsAccountId({\n addressType: intentsAccountType,\n walletAddress: isDry\n ? DRY_QUOTE_ADDRESSES[intentsAccountType]\n : (ctx.walletAddress ?? ''),\n });\n\n const request = useRef<CancelablePromise<QuoteResponse>>(null);\n\n const targetWalletAddress = isDry\n ? DRY_QUOTE_ADDRESSES[intentsAccountType]\n : (ctx.walletAddress ?? '');\n\n const make = async ({ message }: { message?: string } = {}) => {\n const guardCurrentState = guardStates(ctx, [\n 'input_valid_dry',\n 'input_valid_external',\n 'input_valid_internal',\n ]);\n\n if (!guardCurrentState) {\n const msg = `Unable to run quote in current state ${ctx.state}`;\n\n logger.error(`[WIDGET] ${msg}`);\n\n throw new QuoteError({\n code: 'QUOTE_INVALID_INITIAL',\n meta: { isDry, message: msg },\n });\n }\n\n if (!intentsAccountId) {\n const msg = 'No corresponding intents account to run a quote';\n\n logger.error(`[WIDGET] ${msg}`);\n\n throw new QuoteError({\n code: 'QUOTE_INVALID_INITIAL',\n meta: { isDry, message: msg },\n });\n }\n\n if (request.current && !request.current.isCancelled) {\n request.current.cancel();\n }\n\n let quoteResponse: QuoteResponse;\n\n try {\n const commonQuoteParams = {\n // Settings\n slippageTolerance: 100, // 1%\n swapType: QuoteRequest.swapType.EXACT_INPUT,\n deadline: new Date(Date.now() + 60 * 60 * 1000).toISOString(), // 1 hour\n dry: isDry,\n\n // Source\n originAsset: ctx.sourceToken.assetId,\n amount: ctx.sourceTokenAmount,\n };\n\n if (message) {\n // @ts-expect-error customRecipientMsg is not in the types\n commonQuoteParams.customRecipientMsg = message;\n }\n\n try {\n switch (variant) {\n case 'deposit': {\n const destinationAsset = tokenList.find(\n (t) =>\n t.blockchain === 'near' && t.symbol === ctx.sourceToken.symbol,\n );\n\n if (!destinationAsset) {\n throw new QuoteError({\n code: 'NO_NEAR_TOKEN_FOUND',\n meta: { symbol: ctx.sourceToken.symbol },\n });\n }\n\n request.current = OneClickService.getQuote({\n ...commonQuoteParams,\n recipient: intentsAccountId,\n recipientType: QuoteRequest.recipientType.INTENTS,\n depositType: QuoteRequest.depositType.ORIGIN_CHAIN,\n destinationAsset: destinationAsset.assetId,\n\n // Refund\n refundTo: targetWalletAddress,\n refundType: QuoteRequest.refundType.ORIGIN_CHAIN,\n });\n\n quoteResponse = await request.current;\n break;\n }\n\n case 'swap': {\n if (ctx.sourceToken.isIntent && ctx.targetToken.isIntent) {\n request.current = OneClickService.getQuote({\n ...commonQuoteParams,\n recipient: intentsAccountId,\n recipientType: QuoteRequest.recipientType.INTENTS,\n destinationAsset: ctx.targetToken.assetId,\n depositType: QuoteRequest.depositType.INTENTS,\n\n // Refund\n refundTo: intentsAccountId,\n refundType: QuoteRequest.refundType.INTENTS,\n });\n\n quoteResponse = await request.current;\n break;\n }\n\n request.current = OneClickService.getQuote({\n ...commonQuoteParams,\n recipient:\n !ctx.targetToken.isIntent && ctx.sendAddress\n ? ctx.sendAddress\n : intentsAccountId,\n recipientType: ctx.targetToken.isIntent\n ? QuoteRequest.recipientType.INTENTS\n : QuoteRequest.recipientType.DESTINATION_CHAIN,\n destinationAsset: ctx.targetToken.assetId,\n depositType: ctx.sourceToken.isIntent\n ? QuoteRequest.depositType.INTENTS\n : QuoteRequest.depositType.ORIGIN_CHAIN,\n\n // Refund\n refundTo: ctx.sourceToken.isIntent\n ? intentsAccountId\n : targetWalletAddress,\n refundType: ctx.sourceToken.isIntent\n ? QuoteRequest.refundType.INTENTS\n : QuoteRequest.refundType.ORIGIN_CHAIN,\n });\n\n quoteResponse = await request.current;\n break;\n }\n\n default: {\n const msg = 'Unknown swap variant (deposit or swap expected)';\n\n logger.error(`[WIDGET] ${msg}`);\n\n throw new QuoteError({\n code: 'QUOTE_INVALID_INITIAL',\n meta: { isDry, message: msg },\n });\n }\n }\n } catch (error: unknown) {\n if (error instanceof CancelError) {\n return;\n }\n\n if (error instanceof ApiError) {\n logger.error(error);\n\n throw new QuoteError({\n code: 'QUOTE_INVALID',\n meta: { isDry },\n });\n }\n\n logger.error('[WIDGET] Failed to get a deposit address from the quote');\n\n throw new QuoteError({\n code: 'QUOTE_INVALID',\n meta: { isDry },\n });\n }\n\n if (\n !quoteResponse.quote.depositAddress &&\n !request.current.isCancelled &&\n !isDry\n ) {\n throw new Error('Failed to get a deposit address from the quote');\n }\n } catch (error: unknown) {\n // @ts-expect-error In case error has a message\n const errorMessage: string = error?.message;\n\n if (errorMessage.includes('Amount is too low')) {\n const match = errorMessage.match(/\\d+/);\n const minAmount = match ? match[0] : undefined;\n\n throw new QuoteError({\n code: 'QUOTE_AMOUNT_IS_TOO_LOW',\n meta: {\n minAmount:\n (minAmount &&\n formatBigToHuman(minAmount, ctx.sourceToken.decimals)) ??\n '0',\n },\n });\n }\n\n if (errorMessage.includes('recipient is not valid')) {\n throw new QuoteError({\n code: 'TOKEN_IS_NOT_SUPPORTED',\n });\n }\n\n if (errorMessage.includes('Failed to get a deposit address')) {\n throw new QuoteError({\n code: 'QUOTE_NO_ONE_TIME_ADDRESS',\n });\n }\n\n throw new QuoteError({\n code: 'QUOTE_FAILED',\n meta: {\n message: errorMessage ?? 'Failed to fetch quote. Please try again.',\n },\n });\n }\n\n let result: Quote;\n\n if (isDry) {\n result = {\n dry: true,\n ...quoteResponse.quote,\n deadline: undefined,\n depositAddress: undefined,\n };\n } else if (\n quoteResponse.quote.deadline &&\n quoteResponse.quote.depositAddress\n ) {\n result = {\n dry: false,\n ...quoteResponse.quote,\n deadline: quoteResponse.quote.deadline,\n depositAddress: quoteResponse.quote.depositAddress,\n };\n } else {\n throw new QuoteError({\n code: 'QUOTE_INVALID',\n meta: { isDry },\n });\n }\n\n return result;\n };\n\n return { make, cancel: () => request.current?.cancel() };\n};\n"],"names":["useMakeQuote","variant","ctx","useUnsafeSnapshot","intentsAccountType","useConfig","isDry","tokenList","useTokens","intentsAccountId","getIntentsAccountId","DRY_QUOTE_ADDRESSES","request","useRef","targetWalletAddress","message","guardStates","msg","logger","QuoteError","quoteResponse","commonQuoteParams","QuoteRequest","destinationAsset","t","OneClickService","error","CancelError","ApiError","errorMessage","match","minAmount","formatBigToHuman","result"],"mappings":";;;;;;;;;;;AA0BO,MAAMA,IAAe,CAAC,EAAE,SAAAC,QAAqB;AAClD,QAAM,EAAE,KAAAC,EAAA,IAAQC,EAAA,GACV,EAAE,oBAAAC,EAAA,IAAuBC,EAAA,GAEzBC,IAAQ,CAACJ,EAAI,eAEb,EAAE,QAAQK,EAAA,IAAcC,EAAA,GACxBC,IAAmBC,EAAoB;AAAA,IAC3C,aAAaN;AAAA,IACb,eAAeE,IACXK,EAAoBP,CAAkB,IACrCF,EAAI,iBAAiB;AAAA,EAAA,CAC3B,GAEKU,IAAUC,EAAyC,IAAI,GAEvDC,IAAsBR,IACxBK,EAAoBP,CAAkB,IACrCF,EAAI,iBAAiB;AA+O1B,SAAO,EAAE,MA7OI,OAAO,EAAE,SAAAa,EAAA,IAAkC,CAAA,MAAO;AAO7D,QAAI,CANsBC,EAAYd,GAAK;AAAA,MACzC;AAAA,MACA;AAAA,MACA;AAAA,IAAA,CACD,GAEuB;AACtB,YAAMe,IAAM,wCAAwCf,EAAI,KAAK;AAE7D,YAAAgB,EAAO,M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;AAAA,QACP,KAAK;AAAA,QACL,GAAGb,EAAc;AAAA,QACjB,UAAUA,EAAc,MAAM;AAAA,QAC9B,gBAAgBA,EAAc,MAAM;AAAA,MAAA;AAAA;AAGtC,YAAM,IAAID,EAAW;AAAA,QACnB,MAAM;AAAA,QACN,MAAM,EAAE,OAAAb,EAAA;AAAA,MAAM,CACf;AAGH,WAAO2B;AAAA,EACT,GAEe,QAAQ,MAAMrB,EAAQ,SAAS,SAAO;AACvD;"}
|
|
1
|
+
{"version":3,"file":"useMakeQuote.js","sources":["../../src/hooks/useMakeQuote.ts"],"sourcesContent":["import { useRef } from 'react';\nimport {\n ApiError,\n CancelablePromise,\n CancelError,\n OneClickService,\n QuoteRequest,\n QuoteResponse,\n} from '@defuse-protocol/one-click-sdk-typescript';\n\nimport { logger } from '@/logger';\nimport { useConfig } from '@/config';\nimport { QuoteError } from '@/errors';\nimport { guardStates } from '@/machine/guards';\nimport { useUnsafeSnapshot } from '@/machine/snap';\nimport { getIntentsAccountId } from '@/utils/intents/getIntentsAccountId';\nimport { formatBigToHuman } from '@/utils/formatters/formatBigToHuman';\nimport { DRY_QUOTE_ADDRESSES } from '@/constants/chains';\nimport { Quote } from '@/types/quote';\n\nimport { useTokens } from './useTokens';\n\ntype Props = {\n variant: 'deposit' | 'swap';\n};\n\nexport const useMakeQuote = ({ variant }: Props) => {\n const { ctx } = useUnsafeSnapshot();\n const { intentsAccountType } = useConfig();\n\n const isDry = !ctx.walletAddress;\n\n const { tokens: tokenList } = useTokens();\n const intentsAccountId = getIntentsAccountId({\n addressType: intentsAccountType,\n walletAddress: isDry\n ? DRY_QUOTE_ADDRESSES[intentsAccountType]\n : (ctx.walletAddress ?? ''),\n });\n\n const request = useRef<CancelablePromise<QuoteResponse>>(null);\n\n const targetWalletAddress = isDry\n ? DRY_QUOTE_ADDRESSES[intentsAccountType]\n : (ctx.walletAddress ?? '');\n\n const make = async ({ message }: { message?: string } = {}) => {\n const guardCurrentState = guardStates(ctx, [\n 'input_valid_dry',\n 'input_valid_external',\n 'input_valid_internal',\n ]);\n\n if (!guardCurrentState) {\n const msg = `Unable to run quote in current state ${ctx.state}`;\n\n logger.error(`[WIDGET] ${msg}`);\n\n throw new QuoteError({\n code: 'QUOTE_INVALID_INITIAL',\n meta: { isDry, message: msg },\n });\n }\n\n if (!intentsAccountId) {\n const msg = 'No corresponding intents account to run a quote';\n\n logger.error(`[WIDGET] ${msg}`);\n\n throw new QuoteError({\n code: 'QUOTE_INVALID_INITIAL',\n meta: { isDry, message: msg },\n });\n }\n\n if (request.current && !request.current.isCancelled) {\n request.current.cancel();\n }\n\n let quoteResponse: QuoteResponse;\n\n try {\n const commonQuoteParams = {\n // Settings\n slippageTolerance: 100, // 1%\n swapType: QuoteRequest.swapType.EXACT_INPUT,\n deadline: new Date(Date.now() + 60 * 60 * 1000).toISOString(), // 1 hour\n dry: isDry,\n\n // Source\n originAsset: ctx.sourceToken.assetId,\n amount: ctx.sourceTokenAmount,\n };\n\n if (message) {\n // @ts-expect-error customRecipientMsg is not in the types\n commonQuoteParams.customRecipientMsg = message;\n }\n\n try {\n switch (variant) {\n case 'deposit': {\n const destinationAsset = tokenList.find(\n (t) =>\n t.blockchain === 'near' && t.symbol === ctx.sourceToken.symbol,\n );\n\n if (!destinationAsset) {\n throw new QuoteError({\n code: 'NO_NEAR_TOKEN_FOUND',\n meta: { symbol: ctx.sourceToken.symbol },\n });\n }\n\n request.current = OneClickService.getQuote({\n ...commonQuoteParams,\n recipient: intentsAccountId,\n recipientType: QuoteRequest.recipientType.INTENTS,\n depositType: QuoteRequest.depositType.ORIGIN_CHAIN,\n destinationAsset: destinationAsset.assetId,\n\n // Refund\n refundTo: targetWalletAddress,\n refundType: QuoteRequest.refundType.ORIGIN_CHAIN,\n });\n\n quoteResponse = await request.current;\n break;\n }\n\n case 'swap': {\n if (ctx.sourceToken.isIntent && ctx.targetToken.isIntent) {\n request.current = OneClickService.getQuote({\n ...commonQuoteParams,\n recipient: intentsAccountId,\n recipientType: QuoteRequest.recipientType.INTENTS,\n destinationAsset: ctx.targetToken.assetId,\n depositType: QuoteRequest.depositType.INTENTS,\n\n // Refund\n refundTo: intentsAccountId,\n refundType: QuoteRequest.refundType.INTENTS,\n });\n\n quoteResponse = await request.current;\n break;\n }\n\n request.current = OneClickService.getQuote({\n ...commonQuoteParams,\n recipient:\n !ctx.targetToken.isIntent && ctx.sendAddress\n ? ctx.sendAddress\n : intentsAccountId,\n recipientType: ctx.targetToken.isIntent\n ? QuoteRequest.recipientType.INTENTS\n : QuoteRequest.recipientType.DESTINATION_CHAIN,\n destinationAsset: ctx.targetToken.assetId,\n depositType: ctx.sourceToken.isIntent\n ? QuoteRequest.depositType.INTENTS\n : QuoteRequest.depositType.ORIGIN_CHAIN,\n\n // Refund\n refundTo: ctx.sourceToken.isIntent\n ? intentsAccountId\n : targetWalletAddress,\n refundType: ctx.sourceToken.isIntent\n ? QuoteRequest.refundType.INTENTS\n : QuoteRequest.refundType.ORIGIN_CHAIN,\n });\n\n quoteResponse = await request.current;\n break;\n }\n\n default: {\n const msg = 'Unknown swap variant (deposit or swap expected)';\n\n logger.error(`[WIDGET] ${msg}`);\n\n throw new QuoteError({\n code: 'QUOTE_INVALID_INITIAL',\n meta: { isDry, message: msg },\n });\n }\n }\n } catch (error: unknown) {\n if (error instanceof CancelError) {\n return;\n }\n\n if (error instanceof ApiError) {\n logger.error(error);\n throw error;\n }\n\n logger.error('[WIDGET] Failed to get a deposit address from the quote');\n\n throw new QuoteError({\n code: 'QUOTE_INVALID',\n meta: { isDry },\n });\n }\n\n if (\n !quoteResponse.quote.depositAddress &&\n !request.current.isCancelled &&\n !isDry\n ) {\n throw new Error('Failed to get a deposit address from the quote');\n }\n } catch (error: unknown) {\n if (error instanceof ApiError) {\n const errorMessage = error.body.message ?? error.message;\n\n if (errorMessage.includes('Amount is too low')) {\n const match = errorMessage.match(/\\d+/);\n const minAmount = match ? match[0] : undefined;\n\n throw new QuoteError({\n code: 'QUOTE_AMOUNT_IS_TOO_LOW',\n meta: {\n minAmount:\n (minAmount &&\n formatBigToHuman(minAmount, ctx.sourceToken.decimals)) ??\n '0',\n },\n });\n }\n\n if (errorMessage.includes('recipient is not valid')) {\n throw new QuoteError({\n code: 'TOKEN_IS_NOT_SUPPORTED',\n });\n }\n\n if (errorMessage.includes('Failed to get a deposit address')) {\n throw new QuoteError({\n code: 'QUOTE_NO_ONE_TIME_ADDRESS',\n });\n }\n\n throw error;\n }\n\n throw new QuoteError({\n code: 'QUOTE_FAILED',\n meta: {\n // @ts-expect-error In case error has a message\n message: error?.message ?? 'Failed to fetch quote. Please try again.',\n },\n });\n }\n\n let result: Quote;\n\n if (isDry) {\n result = {\n dry: true,\n ...quoteResponse.quote,\n deadline: undefined,\n depositAddress: undefined,\n };\n } else if (\n quoteResponse.quote.deadline &&\n quoteResponse.quote.depositAddress\n ) {\n result = {\n dry: false,\n ...quoteResponse.quote,\n deadline: quoteResponse.quote.deadline,\n depositAddress: quoteResponse.quote.depositAddress,\n };\n } else {\n throw new QuoteError({\n code: 'QUOTE_INVALID',\n meta: { isDry },\n });\n }\n\n return result;\n };\n\n return { make, cancel: () => request.current?.cancel() };\n};\n"],"names":["useMakeQuote","variant","ctx","useUnsafeSnapshot","intentsAccountType","useConfig","isDry","tokenList","useTokens","intentsAccountId","getIntentsAccountId","DRY_QUOTE_ADDRESSES","request","useRef","targetWalletAddress","message","guardStates","msg","logger","QuoteError","quoteResponse","commonQuoteParams","QuoteRequest","destinationAsset","t","OneClickService","error","CancelError","ApiError","errorMessage","match","minAmount","formatBigToHuman","result"],"mappings":";;;;;;;;;;;AA0BO,MAAMA,IAAe,CAAC,EAAE,SAAAC,QAAqB;AAClD,QAAM,EAAE,KAAAC,EAAA,IAAQC,EAAA,GACV,EAAE,oBAAAC,EAAA,IAAuBC,EAAA,GAEzBC,IAAQ,CAACJ,EAAI,eAEb,EAAE,QAAQK,EAAA,IAAcC,EAAA,GACxBC,IAAmBC,EAAoB;AAAA,IAC3C,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IV,EAAW;AAAA,UACnB,MAAM;AAAA,QAAA,CACP,IAGGO;AAAA,MACR;AAEA,YAAM,IAAIP,EAAW;AAAA,QACnB,MAAM;AAAA,QACN,MAAM;AAAA;AAAA,UAEJ,SAASO,GAAO,WAAW;AAAA,QAAA;AAAA,MAC7B,CACD;AAAA,IACH;AAEA,QAAIO;AAEJ,QAAI3B;AACF,MAAA2B,IAAS;AAAA,QACP,KAAK;AAAA,QACL,GAAGb,EAAc;AAAA,QACjB,UAAU;AAAA,QACV,gBAAgB;AAAA,MAAA;AAAA,aAGlBA,EAAc,MAAM,YACpBA,EAAc,MAAM;AAEpB,MAAAa,IAAS;AAAA,QACP,KAAK;AAAA,QACL,GAAGb,EAAc;AAAA,QACjB,UAAUA,EAAc,MAAM;AAAA,QAC9B,gBAAgBA,EAAc,MAAM;AAAA,MAAA;AAAA;AAGtC,YAAM,IAAID,EAAW;AAAA,QACnB,MAAM;AAAA,QACN,MAAM,EAAE,OAAAb,EAAA;AAAA,MAAM,CACf;AAGH,WAAO2B;AAAA,EACT,GAEe,QAAQ,MAAMrB,EAAQ,SAAS,SAAO;AACvD;"}
|
package/dist/machine/errors.d.ts
CHANGED
|
@@ -77,3 +77,7 @@ export type QuoteSuccessTransferError = {
|
|
|
77
77
|
};
|
|
78
78
|
export type QuoteSuccessError = QuoteSuccessDirectTransferError | QuoteSuccessTransferError;
|
|
79
79
|
export type ErrorType = InitialDryStateError | InitialInternalStateError | InitialExternalStateError | InputValidDryError | InputValidWalletError | QuoteSuccessError;
|
|
80
|
+
export declare const QUOTE_ERRORS: Array<InputValidDryError['code'] | InputValidWalletError['code']>;
|
|
81
|
+
export declare const TRANSFER_ERRORS: Array<QuoteSuccessDirectTransferError['code'] | QuoteSuccessTransferError['code']>;
|
|
82
|
+
export declare const isQuoteError: (error: ErrorType) => error is InputValidDryError | InputValidWalletError;
|
|
83
|
+
export declare const isTransferError: (error: ErrorType) => error is QuoteSuccessError;
|
package/dist/machine/errors.js
CHANGED
|
@@ -1,2 +1,24 @@
|
|
|
1
|
-
|
|
1
|
+
const _ = [
|
|
2
|
+
"NO_NEAR_TOKEN_FOUND",
|
|
3
|
+
"TOKEN_IS_NOT_SUPPORTED",
|
|
4
|
+
"QUOTE_FAILED",
|
|
5
|
+
"QUOTE_INVALID",
|
|
6
|
+
"QUOTE_INVALID_INITIAL",
|
|
7
|
+
"QUOTE_AMOUNT_IS_TOO_LOW",
|
|
8
|
+
"QUOTE_NO_ONE_TIME_ADDRESS"
|
|
9
|
+
], O = [
|
|
10
|
+
"DIRECT_TRANSFER_ERROR",
|
|
11
|
+
"BRIDGE_SDK_FAILED",
|
|
12
|
+
"FEES_NOT_ESTIMATED",
|
|
13
|
+
"NO_QUOTE_FOUND",
|
|
14
|
+
"NO_DEPOSIT_RESULT",
|
|
15
|
+
"QUOTE_ERROR",
|
|
16
|
+
"TRANSFER_INVALID_INITIAL"
|
|
17
|
+
], T = (E) => _.includes(E.code), R = (E) => O.includes(E.code);
|
|
18
|
+
export {
|
|
19
|
+
_ as QUOTE_ERRORS,
|
|
20
|
+
O as TRANSFER_ERRORS,
|
|
21
|
+
T as isQuoteError,
|
|
22
|
+
R as isTransferError
|
|
23
|
+
};
|
|
2
24
|
//# sourceMappingURL=errors.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errors.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}
|
|
1
|
+
{"version":3,"file":"errors.js","sources":["../../src/machine/errors.ts"],"sourcesContent":["export type InitialDryStateError =\n | { code: 'SOURCE_TOKEN_IS_EMPTY' }\n | { code: 'TARGET_TOKEN_IS_EMPTY' }\n | { code: 'SOURCE_TOKEN_AMOUNT_IS_EMPTY' };\n\nexport type InitialInternalStateError =\n | InitialDryStateError\n | { code: 'SOURCE_TOKEN_NOT_INTENT' }\n | { code: 'SOURCE_BALANCE_INSUFFICIENT' }\n | { code: 'INVALID_SOURCE_BALANCE' };\n\nexport type InitialExternalStateError =\n | InitialDryStateError\n | { code: 'SOURCE_TOKEN_IS_INTENT' }\n | { code: 'SOURCE_BALANCE_INSUFFICIENT' }\n | { code: 'INVALID_SOURCE_BALANCE' }\n | { code: 'SEND_ADDRESS_IS_EMPTY' };\n\nexport type InputValidDryError =\n | { code: 'NO_NEAR_TOKEN_FOUND'; meta: { symbol: string } }\n | { code: 'TOKEN_IS_NOT_SUPPORTED' }\n | { code: 'QUOTE_FAILED'; meta: { message: string } }\n | { code: 'QUOTE_INVALID'; meta: { isDry: boolean } }\n | { code: 'QUOTE_INVALID_INITIAL'; meta: { isDry: boolean; message: string } }\n | {\n code: 'QUOTE_AMOUNT_IS_TOO_LOW';\n meta: { minAmount?: string };\n };\n\nexport type InputValidWalletError =\n | InputValidDryError\n | { code: 'QUOTE_NO_ONE_TIME_ADDRESS' };\n\nexport type QuoteSuccessDirectTransferError =\n | { code: 'DIRECT_TRANSFER_ERROR' }\n | { code: 'BRIDGE_SDK_FAILED' }\n | { code: 'FEES_NOT_ESTIMATED' };\n\nexport type QuoteSuccessTransferError =\n | { code: 'NO_QUOTE_FOUND' }\n | { code: 'NO_DEPOSIT_RESULT' }\n | { code: 'QUOTE_ERROR'; meta: { message: string } }\n | { code: 'TRANSFER_INVALID_INITIAL'; meta: { message: string } };\n\nexport type QuoteSuccessError =\n | QuoteSuccessDirectTransferError\n | QuoteSuccessTransferError;\n\nexport type ErrorType =\n | InitialDryStateError\n | InitialInternalStateError\n | InitialExternalStateError\n | InputValidDryError\n | InputValidWalletError\n | QuoteSuccessError;\n\nexport const QUOTE_ERRORS: Array<\n InputValidDryError['code'] | InputValidWalletError['code']\n> = [\n 'NO_NEAR_TOKEN_FOUND',\n 'TOKEN_IS_NOT_SUPPORTED',\n 'QUOTE_FAILED',\n 'QUOTE_INVALID',\n 'QUOTE_INVALID_INITIAL',\n 'QUOTE_AMOUNT_IS_TOO_LOW',\n 'QUOTE_NO_ONE_TIME_ADDRESS',\n];\n\nexport const TRANSFER_ERRORS: Array<\n QuoteSuccessDirectTransferError['code'] | QuoteSuccessTransferError['code']\n> = [\n 'DIRECT_TRANSFER_ERROR',\n 'BRIDGE_SDK_FAILED',\n 'FEES_NOT_ESTIMATED',\n 'NO_QUOTE_FOUND',\n 'NO_DEPOSIT_RESULT',\n 'QUOTE_ERROR',\n 'TRANSFER_INVALID_INITIAL',\n];\n\nexport const isQuoteError = (\n error: ErrorType,\n): error is InputValidDryError | InputValidWalletError => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return QUOTE_ERRORS.includes(error.code as any);\n};\n\nexport const isTransferError = (\n error: ErrorType,\n): error is QuoteSuccessError => {\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n return TRANSFER_ERRORS.includes(error.code as any);\n};\n"],"names":["QUOTE_ERRORS","TRANSFER_ERRORS","isQuoteError","error","isTransferError"],"mappings":"AAwDO,MAAMA,IAET;AAAA,EACF;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAEaC,IAET;AAAA,EACF;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AACF,GAEaC,IAAe,CAC1BC,MAGOH,EAAa,SAASG,EAAM,IAAW,GAGnCC,IAAkB,CAC7BD,MAGOF,EAAgB,SAASE,EAAM,IAAW;"}
|
|
@@ -1,32 +1,33 @@
|
|
|
1
|
-
import { isNotEmptyAmount as
|
|
2
|
-
import { isValidBigint as
|
|
3
|
-
import { guardStates as
|
|
4
|
-
import { isBalanceSufficient as
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
import { isNotEmptyAmount as l } from "../../utils/checkers/isNotEmptyAmount.js";
|
|
2
|
+
import { isValidBigint as E } from "../../utils/checkers/isValidBigint.js";
|
|
3
|
+
import { guardStates as t } from "../guards/index.js";
|
|
4
|
+
import { isBalanceSufficient as T } from "../guards/checks/isBalanceSufficient.js";
|
|
5
|
+
import { isQuoteError as a, isTransferError as _ } from "../errors.js";
|
|
6
|
+
import { fireEvent as s } from "./utils/fireEvent.js";
|
|
7
|
+
const O = (e) => {
|
|
8
|
+
const n = t(e, ["initial_dry"]), i = t(e, ["input_valid_dry"]);
|
|
9
|
+
if (!n && !i)
|
|
9
10
|
return;
|
|
10
|
-
let
|
|
11
|
-
return
|
|
12
|
-
},
|
|
13
|
-
const
|
|
14
|
-
if (!
|
|
11
|
+
let o;
|
|
12
|
+
return n && (e.sourceToken ? e.targetToken ? l(e.sourceTokenAmount) || (o = { code: "SOURCE_TOKEN_AMOUNT_IS_EMPTY" }) : o = { code: "TARGET_TOKEN_IS_EMPTY" } : o = { code: "SOURCE_TOKEN_IS_EMPTY" }), !i && o ? s("errorSet", o) : e.error && !a(e.error) && !_(e.error) && s("errorSet", null), i;
|
|
13
|
+
}, A = (e) => {
|
|
14
|
+
const n = t(e, ["initial_wallet"]), i = t(e, ["input_valid_external"]);
|
|
15
|
+
if (!n && !i)
|
|
15
16
|
return;
|
|
16
|
-
const
|
|
17
|
-
let
|
|
18
|
-
return
|
|
19
|
-
},
|
|
20
|
-
const
|
|
21
|
-
if (!
|
|
17
|
+
const o = e.sourceTokenBalance;
|
|
18
|
+
let r;
|
|
19
|
+
return n && (e.sourceToken ? e.targetToken ? e.targetToken.isIntent ? r = { code: "SOURCE_TOKEN_IS_INTENT" } : l(e.sourceTokenAmount) ? e.sendAddress ? e.isDepositFromExternalWallet || (!o || !E(o) ? r = { code: "INVALID_SOURCE_BALANCE" } : T(e) || (r = { code: "SOURCE_BALANCE_INSUFFICIENT" })) : r = { code: "SEND_ADDRESS_IS_EMPTY" } : r = { code: "SOURCE_TOKEN_AMOUNT_IS_EMPTY" } : r = { code: "TARGET_TOKEN_IS_EMPTY" } : r = { code: "SOURCE_TOKEN_IS_EMPTY" }), !i && r ? s("errorSet", r) : e.error && !a(e.error) && !_(e.error) && s("errorSet", null), i;
|
|
20
|
+
}, m = (e) => {
|
|
21
|
+
const n = t(e, ["initial_wallet"]), i = t(e, ["input_valid_internal"]);
|
|
22
|
+
if (!n && !i)
|
|
22
23
|
return;
|
|
23
|
-
const
|
|
24
|
-
let
|
|
25
|
-
return
|
|
24
|
+
const o = e.sourceTokenBalance;
|
|
25
|
+
let r;
|
|
26
|
+
return n && (e.sourceToken ? e.targetToken ? e.targetToken.isIntent ? l(e.sourceTokenAmount) ? e.isDepositFromExternalWallet || (!o || !E(o) ? r = { code: "INVALID_SOURCE_BALANCE" } : T(e) || (r = { code: "SOURCE_BALANCE_INSUFFICIENT" })) : r = { code: "SOURCE_TOKEN_AMOUNT_IS_EMPTY" } : r = { code: "SOURCE_TOKEN_NOT_INTENT" } : r = { code: "TARGET_TOKEN_IS_EMPTY" } : r = { code: "SOURCE_TOKEN_IS_EMPTY" }), !i && r ? s("errorSet", r) : e.error && !a(e.error) && !_(e.error) && s("errorSet", null), i;
|
|
26
27
|
};
|
|
27
28
|
export {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
29
|
+
O as validateDryInputs,
|
|
30
|
+
A as validateExternalInputs,
|
|
31
|
+
m as validateInternalInputs
|
|
31
32
|
};
|
|
32
33
|
//# sourceMappingURL=validateInputs.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validateInputs.js","sources":["../../../src/machine/events/validateInputs.ts"],"sourcesContent":["import { isNotEmptyAmount } from '@/utils/checkers/isNotEmptyAmount';\nimport { isValidBigint } from '@/utils/checkers/isValidBigint';\n\nimport { guardStates } from '@/machine/guards';\nimport { isBalanceSufficient } from '@/machine/guards/checks/isBalanceSufficient';\nimport type { Context } from '@/machine/context';\nimport type {\n InitialDryStateError,\n InitialExternalStateError,\n InitialInternalStateError,\n} from '@/machine/errors';\n\nimport { fireEvent } from './utils/fireEvent';\n\nexport const validateDryInputs = (ctx: Context): boolean | undefined => {\n const isValidInitialState = guardStates(ctx, ['initial_dry']);\n const isValidInputsState = guardStates(ctx, ['input_valid_dry']);\n\n if (!isValidInitialState && !isValidInputsState) {\n return undefined;\n }\n\n let err: InitialDryStateError | undefined;\n\n if (isValidInitialState) {\n if (!ctx.sourceToken) {\n err = { code: 'SOURCE_TOKEN_IS_EMPTY' };\n } else if (!ctx.targetToken) {\n err = { code: 'TARGET_TOKEN_IS_EMPTY' };\n } else if (!isNotEmptyAmount(ctx.sourceTokenAmount)) {\n err = { code: 'SOURCE_TOKEN_AMOUNT_IS_EMPTY' };\n }\n }\n\n if (!isValidInputsState && err) {\n fireEvent('errorSet', err);\n } else {\n fireEvent('errorSet', null);\n }\n\n return isValidInputsState;\n};\n\nexport const validateExternalInputs = (ctx: Context): boolean | undefined => {\n const isValidInitialState = guardStates(ctx, ['initial_wallet']);\n const isValidInputsState = guardStates(ctx, ['input_valid_external']);\n\n if (!isValidInitialState && !isValidInputsState) {\n return undefined;\n }\n\n const sourceBalance = ctx.sourceTokenBalance;\n let err: InitialExternalStateError | undefined;\n\n if (isValidInitialState) {\n if (!ctx.sourceToken) {\n err = { code: 'SOURCE_TOKEN_IS_EMPTY' };\n } else if (!ctx.targetToken) {\n err = { code: 'TARGET_TOKEN_IS_EMPTY' };\n } else if (ctx.targetToken.isIntent) {\n err = { code: 'SOURCE_TOKEN_IS_INTENT' };\n } else if (!isNotEmptyAmount(ctx.sourceTokenAmount)) {\n err = { code: 'SOURCE_TOKEN_AMOUNT_IS_EMPTY' };\n } else if (!ctx.sendAddress) {\n err = { code: 'SEND_ADDRESS_IS_EMPTY' };\n } else if (!ctx.isDepositFromExternalWallet) {\n if (!sourceBalance || !isValidBigint(sourceBalance)) {\n err = { code: 'INVALID_SOURCE_BALANCE' };\n } else if (!isBalanceSufficient(ctx)) {\n err = { code: 'SOURCE_BALANCE_INSUFFICIENT' };\n }\n }\n }\n\n if (!isValidInputsState && err) {\n fireEvent('errorSet', err);\n } else {\n fireEvent('errorSet', null);\n }\n\n return isValidInputsState;\n};\n\nexport const validateInternalInputs = (ctx: Context): boolean | undefined => {\n const isValidInitialState = guardStates(ctx, ['initial_wallet']);\n const isValidInputsState = guardStates(ctx, ['input_valid_internal']);\n\n if (!isValidInitialState && !isValidInputsState) {\n return undefined;\n }\n\n const sourceBalance = ctx.sourceTokenBalance;\n let err: InitialInternalStateError | undefined;\n\n if (isValidInitialState) {\n if (!ctx.sourceToken) {\n err = { code: 'SOURCE_TOKEN_IS_EMPTY' };\n } else if (!ctx.targetToken) {\n err = { code: 'TARGET_TOKEN_IS_EMPTY' };\n } else if (!ctx.targetToken.isIntent) {\n err = { code: 'SOURCE_TOKEN_NOT_INTENT' };\n } else if (!isNotEmptyAmount(ctx.sourceTokenAmount)) {\n err = { code: 'SOURCE_TOKEN_AMOUNT_IS_EMPTY' };\n } else if (!ctx.isDepositFromExternalWallet) {\n if (!sourceBalance || !isValidBigint(sourceBalance)) {\n err = { code: 'INVALID_SOURCE_BALANCE' };\n } else if (!isBalanceSufficient(ctx)) {\n err = { code: 'SOURCE_BALANCE_INSUFFICIENT' };\n }\n }\n }\n\n if (!isValidInputsState && err) {\n fireEvent('errorSet', err);\n } else {\n fireEvent('errorSet', null);\n }\n\n return isValidInputsState;\n};\n"],"names":["validateDryInputs","ctx","isValidInitialState","guardStates","isValidInputsState","err","isNotEmptyAmount","fireEvent","validateExternalInputs","sourceBalance","isValidBigint","isBalanceSufficient","validateInternalInputs"],"mappings":"
|
|
1
|
+
{"version":3,"file":"validateInputs.js","sources":["../../../src/machine/events/validateInputs.ts"],"sourcesContent":["import { isNotEmptyAmount } from '@/utils/checkers/isNotEmptyAmount';\nimport { isValidBigint } from '@/utils/checkers/isValidBigint';\n\nimport { guardStates } from '@/machine/guards';\nimport { isBalanceSufficient } from '@/machine/guards/checks/isBalanceSufficient';\nimport { isQuoteError, isTransferError } from '@/machine/errors';\nimport type { Context } from '@/machine/context';\nimport type {\n InitialDryStateError,\n InitialExternalStateError,\n InitialInternalStateError,\n} from '@/machine/errors';\n\nimport { fireEvent } from './utils/fireEvent';\n\nexport const validateDryInputs = (ctx: Context): boolean | undefined => {\n const isValidInitialState = guardStates(ctx, ['initial_dry']);\n const isValidInputsState = guardStates(ctx, ['input_valid_dry']);\n\n if (!isValidInitialState && !isValidInputsState) {\n return undefined;\n }\n\n let err: InitialDryStateError | undefined;\n\n if (isValidInitialState) {\n if (!ctx.sourceToken) {\n err = { code: 'SOURCE_TOKEN_IS_EMPTY' };\n } else if (!ctx.targetToken) {\n err = { code: 'TARGET_TOKEN_IS_EMPTY' };\n } else if (!isNotEmptyAmount(ctx.sourceTokenAmount)) {\n err = { code: 'SOURCE_TOKEN_AMOUNT_IS_EMPTY' };\n }\n }\n\n if (!isValidInputsState && err) {\n fireEvent('errorSet', err);\n } else if (\n ctx.error &&\n !isQuoteError(ctx.error) &&\n !isTransferError(ctx.error)\n ) {\n fireEvent('errorSet', null);\n }\n\n return isValidInputsState;\n};\n\nexport const validateExternalInputs = (ctx: Context): boolean | undefined => {\n const isValidInitialState = guardStates(ctx, ['initial_wallet']);\n const isValidInputsState = guardStates(ctx, ['input_valid_external']);\n\n if (!isValidInitialState && !isValidInputsState) {\n return undefined;\n }\n\n const sourceBalance = ctx.sourceTokenBalance;\n let err: InitialExternalStateError | undefined;\n\n if (isValidInitialState) {\n if (!ctx.sourceToken) {\n err = { code: 'SOURCE_TOKEN_IS_EMPTY' };\n } else if (!ctx.targetToken) {\n err = { code: 'TARGET_TOKEN_IS_EMPTY' };\n } else if (ctx.targetToken.isIntent) {\n err = { code: 'SOURCE_TOKEN_IS_INTENT' };\n } else if (!isNotEmptyAmount(ctx.sourceTokenAmount)) {\n err = { code: 'SOURCE_TOKEN_AMOUNT_IS_EMPTY' };\n } else if (!ctx.sendAddress) {\n err = { code: 'SEND_ADDRESS_IS_EMPTY' };\n } else if (!ctx.isDepositFromExternalWallet) {\n if (!sourceBalance || !isValidBigint(sourceBalance)) {\n err = { code: 'INVALID_SOURCE_BALANCE' };\n } else if (!isBalanceSufficient(ctx)) {\n err = { code: 'SOURCE_BALANCE_INSUFFICIENT' };\n }\n }\n }\n\n if (!isValidInputsState && err) {\n fireEvent('errorSet', err);\n } else if (\n ctx.error &&\n !isQuoteError(ctx.error) &&\n !isTransferError(ctx.error)\n ) {\n fireEvent('errorSet', null);\n }\n\n return isValidInputsState;\n};\n\nexport const validateInternalInputs = (ctx: Context): boolean | undefined => {\n const isValidInitialState = guardStates(ctx, ['initial_wallet']);\n const isValidInputsState = guardStates(ctx, ['input_valid_internal']);\n\n if (!isValidInitialState && !isValidInputsState) {\n return undefined;\n }\n\n const sourceBalance = ctx.sourceTokenBalance;\n let err: InitialInternalStateError | undefined;\n\n if (isValidInitialState) {\n if (!ctx.sourceToken) {\n err = { code: 'SOURCE_TOKEN_IS_EMPTY' };\n } else if (!ctx.targetToken) {\n err = { code: 'TARGET_TOKEN_IS_EMPTY' };\n } else if (!ctx.targetToken.isIntent) {\n err = { code: 'SOURCE_TOKEN_NOT_INTENT' };\n } else if (!isNotEmptyAmount(ctx.sourceTokenAmount)) {\n err = { code: 'SOURCE_TOKEN_AMOUNT_IS_EMPTY' };\n } else if (!ctx.isDepositFromExternalWallet) {\n if (!sourceBalance || !isValidBigint(sourceBalance)) {\n err = { code: 'INVALID_SOURCE_BALANCE' };\n } else if (!isBalanceSufficient(ctx)) {\n err = { code: 'SOURCE_BALANCE_INSUFFICIENT' };\n }\n }\n }\n\n if (!isValidInputsState && err) {\n fireEvent('errorSet', err);\n } else if (\n ctx.error &&\n !isQuoteError(ctx.error) &&\n !isTransferError(ctx.error)\n ) {\n fireEvent('errorSet', null);\n }\n\n return isValidInputsState;\n};\n"],"names":["validateDryInputs","ctx","isValidInitialState","guardStates","isValidInputsState","err","isNotEmptyAmount","fireEvent","isQuoteError","isTransferError","validateExternalInputs","sourceBalance","isValidBigint","isBalanceSufficient","validateInternalInputs"],"mappings":";;;;;;AAeO,MAAMA,IAAoB,CAACC,MAAsC;AACtE,QAAMC,IAAsBC,EAAYF,GAAK,CAAC,aAAa,CAAC,GACtDG,IAAqBD,EAAYF,GAAK,CAAC,iBAAiB,CAAC;AAE/D,MAA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gBAAgB,CAAC,GACzDG,IAAqBD,EAAYF,GAAK,CAAC,sBAAsB,CAAC;AAEpE,MAAI,CAACC,KAAuB,CAACE;AAC3B;AAGF,QAAMO,IAAgBV,EAAI;AAC1B,MAAII;AAEJ,SAAIH,MACGD,EAAI,cAEGA,EAAI,cAEJA,EAAI,YAAY,WAEhBK,EAAiBL,EAAI,iBAAiB,IAEtCA,EAAI,gCACV,CAACU,KAAiB,CAACC,EAAcD,CAAa,IAChDN,IAAM,EAAE,MAAM,yBAAA,IACJQ,EAAoBZ,CAAG,MACjCI,IAAM,EAAE,MAAM,8BAAA,MALhBA,IAAM,EAAE,MAAM,+BAAA,IAFdA,IAAM,EAAE,MAAM,0BAAA,IAFdA,IAAM,EAAE,MAAM,wBAAA,IAFdA,IAAM,EAAE,MAAM,wBAAA,IAgBd,CAACD,KAAsBC,IACzBE,EAAU,YAAYF,CAAG,IAEzBJ,EAAI,SACJ,CAACO,EAAaP,EAAI,KAAK,KACvB,CAACQ,EAAgBR,EAAI,KAAK,KAE1BM,EAAU,YAAY,IAAI,GAGrBH;AACT;"}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
|
-
const
|
|
2
|
-
tokens:
|
|
3
|
-
walletSupportedChains:
|
|
4
|
-
}) =>
|
|
1
|
+
const e = ({
|
|
2
|
+
tokens: i,
|
|
3
|
+
walletSupportedChains: a
|
|
4
|
+
}) => a.includes("near") ? i.find(
|
|
5
5
|
(n) => !n.isIntent && n.name === "Near" && n.blockchain === "near"
|
|
6
|
-
) ??
|
|
6
|
+
) ?? i[0] : a.includes("sol") ? i.find(
|
|
7
|
+
(n) => !n.isIntent && n.name === "Solana" && n.blockchain === "sol"
|
|
8
|
+
) ?? i[0] : i.find(
|
|
7
9
|
(n) => !n.isIntent && n.name === "ETH" && n.blockchain === "eth"
|
|
8
|
-
) ??
|
|
10
|
+
) ?? i[0];
|
|
9
11
|
export {
|
|
10
|
-
|
|
12
|
+
e as getMainTokenByChain
|
|
11
13
|
};
|
|
12
14
|
//# sourceMappingURL=getMainTokenByChain.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"getMainTokenByChain.js","sources":["../../../src/utils/tokens/getMainTokenByChain.ts"],"sourcesContent":["import type { Chains } from '@/types/chain';\nimport type { Token } from '@/types/token';\n\ntype Args = {\n tokens: ReadonlyArray<Token>;\n walletSupportedChains: ReadonlyArray<Chains>;\n};\n\nexport const getMainTokenByChain = ({\n tokens,\n walletSupportedChains,\n}: Args) => {\n if (walletSupportedChains.includes('near')) {\n return (\n tokens.find(\n (t) => !t.isIntent && t.name === 'Near' && t.blockchain === 'near',\n ) ?? tokens[0]\n );\n }\n\n return (\n tokens.find(\n (t) => !t.isIntent && t.name === 'ETH' && t.blockchain === 'eth',\n ) ?? tokens[0]\n );\n};\n"],"names":["getMainTokenByChain","tokens","walletSupportedChains","t"],"mappings":"AAQO,MAAMA,IAAsB,CAAC;AAAA,EAClC,QAAAC;AAAA,EACA,uBAAAC;AACF,MACMA,EAAsB,SAAS,MAAM,IAErCD,EAAO;AAAA,EACL,CAACE,MAAM,CAACA,EAAE,YAAYA,EAAE,SAAS,UAAUA,EAAE,eAAe;AAAA,KACzDF,EAAO,CAAC,IAKfA,EAAO;AAAA,EACL,CAACE,MAAM,CAACA,EAAE,YAAYA,EAAE,SAAS,SAASA,EAAE,eAAe;AAAA,KACxDF,EAAO,CAAC;"}
|
|
1
|
+
{"version":3,"file":"getMainTokenByChain.js","sources":["../../../src/utils/tokens/getMainTokenByChain.ts"],"sourcesContent":["import type { Chains } from '@/types/chain';\nimport type { Token } from '@/types/token';\n\ntype Args = {\n tokens: ReadonlyArray<Token>;\n walletSupportedChains: ReadonlyArray<Chains>;\n};\n\nexport const getMainTokenByChain = ({\n tokens,\n walletSupportedChains,\n}: Args) => {\n if (walletSupportedChains.includes('near')) {\n return (\n tokens.find(\n (t) => !t.isIntent && t.name === 'Near' && t.blockchain === 'near',\n ) ?? tokens[0]\n );\n }\n\n if (walletSupportedChains.includes('sol')) {\n return (\n tokens.find(\n (t) => !t.isIntent && t.name === 'Solana' && t.blockchain === 'sol',\n ) ?? tokens[0]\n );\n }\n\n return (\n tokens.find(\n (t) => !t.isIntent && t.name === 'ETH' && t.blockchain === 'eth',\n ) ?? tokens[0]\n );\n};\n"],"names":["getMainTokenByChain","tokens","walletSupportedChains","t"],"mappings":"AAQO,MAAMA,IAAsB,CAAC;AAAA,EAClC,QAAAC;AAAA,EACA,uBAAAC;AACF,MACMA,EAAsB,SAAS,MAAM,IAErCD,EAAO;AAAA,EACL,CAACE,MAAM,CAACA,EAAE,YAAYA,EAAE,SAAS,UAAUA,EAAE,eAAe;AAAA,KACzDF,EAAO,CAAC,IAIbC,EAAsB,SAAS,KAAK,IAEpCD,EAAO;AAAA,EACL,CAACE,MAAM,CAACA,EAAE,YAAYA,EAAE,SAAS,YAAYA,EAAE,eAAe;AAAA,KAC3DF,EAAO,CAAC,IAKfA,EAAO;AAAA,EACL,CAACE,MAAM,CAACA,EAAE,YAAYA,EAAE,SAAS,SAASA,EAAE,eAAe;AAAA,KACxDF,EAAO,CAAC;"}
|
package/package.json
CHANGED
|
@@ -191,11 +191,7 @@ export const useMakeQuote = ({ variant }: Props) => {
|
|
|
191
191
|
|
|
192
192
|
if (error instanceof ApiError) {
|
|
193
193
|
logger.error(error);
|
|
194
|
-
|
|
195
|
-
throw new QuoteError({
|
|
196
|
-
code: 'QUOTE_INVALID',
|
|
197
|
-
meta: { isDry },
|
|
198
|
-
});
|
|
194
|
+
throw error;
|
|
199
195
|
}
|
|
200
196
|
|
|
201
197
|
logger.error('[WIDGET] Failed to get a deposit address from the quote');
|
|
@@ -214,40 +210,44 @@ export const useMakeQuote = ({ variant }: Props) => {
|
|
|
214
210
|
throw new Error('Failed to get a deposit address from the quote');
|
|
215
211
|
}
|
|
216
212
|
} catch (error: unknown) {
|
|
217
|
-
|
|
218
|
-
|
|
213
|
+
if (error instanceof ApiError) {
|
|
214
|
+
const errorMessage = error.body.message ?? error.message;
|
|
219
215
|
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
216
|
+
if (errorMessage.includes('Amount is too low')) {
|
|
217
|
+
const match = errorMessage.match(/\d+/);
|
|
218
|
+
const minAmount = match ? match[0] : undefined;
|
|
223
219
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
220
|
+
throw new QuoteError({
|
|
221
|
+
code: 'QUOTE_AMOUNT_IS_TOO_LOW',
|
|
222
|
+
meta: {
|
|
223
|
+
minAmount:
|
|
224
|
+
(minAmount &&
|
|
225
|
+
formatBigToHuman(minAmount, ctx.sourceToken.decimals)) ??
|
|
226
|
+
'0',
|
|
227
|
+
},
|
|
228
|
+
});
|
|
229
|
+
}
|
|
234
230
|
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
231
|
+
if (errorMessage.includes('recipient is not valid')) {
|
|
232
|
+
throw new QuoteError({
|
|
233
|
+
code: 'TOKEN_IS_NOT_SUPPORTED',
|
|
234
|
+
});
|
|
235
|
+
}
|
|
240
236
|
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
237
|
+
if (errorMessage.includes('Failed to get a deposit address')) {
|
|
238
|
+
throw new QuoteError({
|
|
239
|
+
code: 'QUOTE_NO_ONE_TIME_ADDRESS',
|
|
240
|
+
});
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
throw error;
|
|
245
244
|
}
|
|
246
245
|
|
|
247
246
|
throw new QuoteError({
|
|
248
247
|
code: 'QUOTE_FAILED',
|
|
249
248
|
meta: {
|
|
250
|
-
|
|
249
|
+
// @ts-expect-error In case error has a message
|
|
250
|
+
message: error?.message ?? 'Failed to fetch quote. Please try again.',
|
|
251
251
|
},
|
|
252
252
|
});
|
|
253
253
|
}
|
package/src/machine/errors.ts
CHANGED
|
@@ -53,3 +53,41 @@ export type ErrorType =
|
|
|
53
53
|
| InputValidDryError
|
|
54
54
|
| InputValidWalletError
|
|
55
55
|
| QuoteSuccessError;
|
|
56
|
+
|
|
57
|
+
export const QUOTE_ERRORS: Array<
|
|
58
|
+
InputValidDryError['code'] | InputValidWalletError['code']
|
|
59
|
+
> = [
|
|
60
|
+
'NO_NEAR_TOKEN_FOUND',
|
|
61
|
+
'TOKEN_IS_NOT_SUPPORTED',
|
|
62
|
+
'QUOTE_FAILED',
|
|
63
|
+
'QUOTE_INVALID',
|
|
64
|
+
'QUOTE_INVALID_INITIAL',
|
|
65
|
+
'QUOTE_AMOUNT_IS_TOO_LOW',
|
|
66
|
+
'QUOTE_NO_ONE_TIME_ADDRESS',
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
export const TRANSFER_ERRORS: Array<
|
|
70
|
+
QuoteSuccessDirectTransferError['code'] | QuoteSuccessTransferError['code']
|
|
71
|
+
> = [
|
|
72
|
+
'DIRECT_TRANSFER_ERROR',
|
|
73
|
+
'BRIDGE_SDK_FAILED',
|
|
74
|
+
'FEES_NOT_ESTIMATED',
|
|
75
|
+
'NO_QUOTE_FOUND',
|
|
76
|
+
'NO_DEPOSIT_RESULT',
|
|
77
|
+
'QUOTE_ERROR',
|
|
78
|
+
'TRANSFER_INVALID_INITIAL',
|
|
79
|
+
];
|
|
80
|
+
|
|
81
|
+
export const isQuoteError = (
|
|
82
|
+
error: ErrorType,
|
|
83
|
+
): error is InputValidDryError | InputValidWalletError => {
|
|
84
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
85
|
+
return QUOTE_ERRORS.includes(error.code as any);
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
export const isTransferError = (
|
|
89
|
+
error: ErrorType,
|
|
90
|
+
): error is QuoteSuccessError => {
|
|
91
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
92
|
+
return TRANSFER_ERRORS.includes(error.code as any);
|
|
93
|
+
};
|
|
@@ -3,6 +3,7 @@ import { isValidBigint } from '@/utils/checkers/isValidBigint';
|
|
|
3
3
|
|
|
4
4
|
import { guardStates } from '@/machine/guards';
|
|
5
5
|
import { isBalanceSufficient } from '@/machine/guards/checks/isBalanceSufficient';
|
|
6
|
+
import { isQuoteError, isTransferError } from '@/machine/errors';
|
|
6
7
|
import type { Context } from '@/machine/context';
|
|
7
8
|
import type {
|
|
8
9
|
InitialDryStateError,
|
|
@@ -34,7 +35,11 @@ export const validateDryInputs = (ctx: Context): boolean | undefined => {
|
|
|
34
35
|
|
|
35
36
|
if (!isValidInputsState && err) {
|
|
36
37
|
fireEvent('errorSet', err);
|
|
37
|
-
} else
|
|
38
|
+
} else if (
|
|
39
|
+
ctx.error &&
|
|
40
|
+
!isQuoteError(ctx.error) &&
|
|
41
|
+
!isTransferError(ctx.error)
|
|
42
|
+
) {
|
|
38
43
|
fireEvent('errorSet', null);
|
|
39
44
|
}
|
|
40
45
|
|
|
@@ -74,7 +79,11 @@ export const validateExternalInputs = (ctx: Context): boolean | undefined => {
|
|
|
74
79
|
|
|
75
80
|
if (!isValidInputsState && err) {
|
|
76
81
|
fireEvent('errorSet', err);
|
|
77
|
-
} else
|
|
82
|
+
} else if (
|
|
83
|
+
ctx.error &&
|
|
84
|
+
!isQuoteError(ctx.error) &&
|
|
85
|
+
!isTransferError(ctx.error)
|
|
86
|
+
) {
|
|
78
87
|
fireEvent('errorSet', null);
|
|
79
88
|
}
|
|
80
89
|
|
|
@@ -112,7 +121,11 @@ export const validateInternalInputs = (ctx: Context): boolean | undefined => {
|
|
|
112
121
|
|
|
113
122
|
if (!isValidInputsState && err) {
|
|
114
123
|
fireEvent('errorSet', err);
|
|
115
|
-
} else
|
|
124
|
+
} else if (
|
|
125
|
+
ctx.error &&
|
|
126
|
+
!isQuoteError(ctx.error) &&
|
|
127
|
+
!isTransferError(ctx.error)
|
|
128
|
+
) {
|
|
116
129
|
fireEvent('errorSet', null);
|
|
117
130
|
}
|
|
118
131
|
|
|
@@ -18,6 +18,14 @@ export const getMainTokenByChain = ({
|
|
|
18
18
|
);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
+
if (walletSupportedChains.includes('sol')) {
|
|
22
|
+
return (
|
|
23
|
+
tokens.find(
|
|
24
|
+
(t) => !t.isIntent && t.name === 'Solana' && t.blockchain === 'sol',
|
|
25
|
+
) ?? tokens[0]
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
21
29
|
return (
|
|
22
30
|
tokens.find(
|
|
23
31
|
(t) => !t.isIntent && t.name === 'ETH' && t.blockchain === 'eth',
|