@aurora-is-near/intents-swap-widget 2.7.0 → 2.8.0
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/README.md +35 -2
- package/dist/features/TokensModal.js +3 -2
- package/dist/features/TokensModal.js.map +1 -1
- package/dist/hooks/useMakeQuoteTransfer.d.ts +2 -6
- package/dist/hooks/useMakeQuoteTransfer.js +24 -17
- package/dist/hooks/useMakeQuoteTransfer.js.map +1 -1
- package/dist/types/transfer.d.ts +9 -1
- package/package.json +1 -1
- package/src/hooks/useMakeQuoteTransfer.ts +8 -6
- package/src/types/transfer.ts +11 -1
package/README.md
CHANGED
|
@@ -4,7 +4,40 @@ Swap Widget frontend that uses [1Click API](https://docs.near-intents.org/near-i
|
|
|
4
4
|
|
|
5
5
|

|
|
6
6
|
|
|
7
|
-
##
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
### Widgets
|
|
10
|
+
|
|
11
|
+
This package exports a number of pre-built widget components, `WidgetSwap`,
|
|
12
|
+
`WidgetDeposit` and `WidgetWithdraw`.
|
|
13
|
+
|
|
14
|
+
See the `/demo` folder for some examples of how to use these widgets.
|
|
15
|
+
|
|
16
|
+
#### Making a transfer
|
|
17
|
+
|
|
18
|
+
To each of these components we should pass in a `makeTransfer` function. When
|
|
19
|
+
the user clicks the relevant submit button this function is called with an
|
|
20
|
+
`args` object that has the following shape:
|
|
21
|
+
|
|
22
|
+
```ts
|
|
23
|
+
type MakeTransferArgs = {
|
|
24
|
+
amount: string;
|
|
25
|
+
address: string;
|
|
26
|
+
tokenAddress?: string;
|
|
27
|
+
chain: Chains;
|
|
28
|
+
evmChainId: number | null;
|
|
29
|
+
};
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
It is the responsibility of the consuming application to handle this transfer.
|
|
33
|
+
|
|
34
|
+
## Custom implementation
|
|
35
|
+
|
|
36
|
+
If your use cases cannot be met using the pre-built widgets described above you
|
|
37
|
+
have the option to roll your own, using the components and hooks exposed by this
|
|
38
|
+
package.
|
|
39
|
+
|
|
40
|
+
For this purpose, the package structure is as follows:
|
|
8
41
|
|
|
9
42
|
### Components
|
|
10
43
|
|
|
@@ -82,7 +115,7 @@ variable you can find in `src/theme.css`):
|
|
|
82
115
|
}
|
|
83
116
|
```
|
|
84
117
|
|
|
85
|
-
##
|
|
118
|
+
## Localisation
|
|
86
119
|
|
|
87
120
|
To change copy within the widget you can pass a `localisation` object to the
|
|
88
121
|
`WidgetConfigProvider`, for example:
|
|
@@ -24,7 +24,8 @@ import "axios";
|
|
|
24
24
|
import "../network.js";
|
|
25
25
|
import "ethers";
|
|
26
26
|
import "../machine/machine.js";
|
|
27
|
-
|
|
27
|
+
import "viem";
|
|
28
|
+
const ne = ({
|
|
28
29
|
variant: r,
|
|
29
30
|
showBalances: f,
|
|
30
31
|
showChainsSelector: u,
|
|
@@ -121,6 +122,6 @@ const se = ({
|
|
|
121
122
|
);
|
|
122
123
|
};
|
|
123
124
|
export {
|
|
124
|
-
|
|
125
|
+
ne as TokensModal
|
|
125
126
|
};
|
|
126
127
|
//# sourceMappingURL=TokensModal.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"TokensModal.js","sources":["../../src/features/TokensModal.tsx"],"sourcesContent":["import { useState } from 'react';\nimport * as Icons from 'lucide-react';\n\nimport { Card } from '@/components/Card';\nimport { Input } from '@/components/Input';\nimport { Banner } from '@/components/Banner';\n\nimport { cn } from '@/utils/cn';\nimport { useConfig } from '@/config';\nimport { useUnsafeSnapshot } from '@/machine/snap';\nimport { notReachable } from '@/utils/notReachable';\nimport { useTypedTranslation } from '@/localisation';\nimport type { Chains, DefaultChainsFilter } from '@/types/chain';\nimport type { Token } from '@/types/token';\n\nimport { TokensList } from './TokensList';\nimport { ChainsDropdown } from './ChainsDropdown';\nimport { useChains } from '../hooks';\n\ntype Msg =\n | { type: 'on_select_token'; token: Token }\n | { type: 'on_dismiss_tokens_modal' };\n\ntype Props = {\n variant: 'source' | 'target';\n groupTokens: boolean;\n showBalances: boolean;\n showChainsSelector: boolean;\n chainsFilter: DefaultChainsFilter;\n className?: string;\n onMsg: (msg: Msg) => void;\n};\n\nexport const TokensModal = ({\n variant,\n showBalances,\n showChainsSelector,\n chainsFilter,\n groupTokens,\n className,\n onMsg,\n}: Props) => {\n const { t } = useTypedTranslation();\n const { ctx } = useUnsafeSnapshot();\n const { walletSupportedChains } = useConfig();\n\n const [search, setSearch] = useState('');\n const chains = useChains(variant);\n\n // If there is only one chain available, select it by default\n const defaultChain =\n chains.length === 1 && chains[0]?.id ? chains[0]?.id : 'all';\n\n const [selectedChain, setSelectedChain] = useState<\n 'all' | 'intents' | Chains\n >(defaultChain);\n\n // selected chain is not supported by connected wallet\n const chainIsNotSupported =\n selectedChain !== 'all' &&\n selectedChain !== 'intents' &&\n chainsFilter.external !== 'all' &&\n !walletSupportedChains.includes(selectedChain);\n\n return (\n <Card\n padding=\"none\"\n className={cn('gap-sw-2xl flex flex-col px-sw-2xl pt-sw-2xl', className)}>\n <header className=\"py-sw-md flex items-center justify-between\">\n <h2 className=\"text-sw-label-l\">Select token</h2>\n <button\n type=\"button\"\n className=\"flex cursor-pointer items-center justify-center text-sw-gray-100 transition-colors hover:text-sw-gray-50\"\n onClick={() => onMsg({ type: 'on_dismiss_tokens_modal' })}>\n <Icons.X size={22} />\n </button>\n </header>\n\n <div className=\"gap-sw-xl flex items-center\">\n <Input\n focusOnMount\n icon={Icons.Search}\n defaultValue={search}\n className=\"w-full\"\n placeholder=\"Search or paste address\"\n onChange={(e) => setSearch(e.target.value.trim())}\n />\n {showChainsSelector && (\n <ChainsDropdown\n variant={variant}\n selected={selectedChain}\n chainsFilter={chainsFilter}\n onMsg={(msg) => {\n switch (msg.type) {\n case 'on_click_chain':\n setSelectedChain(msg.chain);\n break;\n default:\n notReachable(msg.type, { throwError: false });\n }\n }}\n />\n )}\n </div>\n\n {chainIsNotSupported && !!ctx.walletAddress && (\n <Banner\n variant=\"info\"\n message={t(\n 'wallet.connected.error.notSupportedChain',\n \"Your connected wallet doesn't support this chain\",\n )}\n />\n )}\n\n <TokensList\n variant={variant}\n search={search}\n groupTokens={groupTokens}\n showBalances={showBalances}\n chainsFilter={chainsFilter}\n selectedChain={selectedChain}\n chainIsNotSupported={chainIsNotSupported}\n onMsg={(msg) => {\n switch (msg.type) {\n case 'on_reset_search':\n setSearch('');\n break;\n case 'on_select_token':\n onMsg(msg);\n break;\n default:\n notReachable(msg, { throwError: false });\n }\n }}\n />\n </Card>\n );\n};\n"],"names":["TokensModal","variant","showBalances","showChainsSelector","chainsFilter","groupTokens","className","onMsg","t","useTypedTranslation","ctx","useUnsafeSnapshot","walletSupportedChains","useConfig","search","setSearch","useState","chains","useChains","defaultChain","selectedChain","setSelectedChain","chainIsNotSupported","jsxs","Card","cn","jsx","Icons","Input","ChainsDropdown","msg","notReachable","Banner","TokensList"],"mappings":"
|
|
1
|
+
{"version":3,"file":"TokensModal.js","sources":["../../src/features/TokensModal.tsx"],"sourcesContent":["import { useState } from 'react';\nimport * as Icons from 'lucide-react';\n\nimport { Card } from '@/components/Card';\nimport { Input } from '@/components/Input';\nimport { Banner } from '@/components/Banner';\n\nimport { cn } from '@/utils/cn';\nimport { useConfig } from '@/config';\nimport { useUnsafeSnapshot } from '@/machine/snap';\nimport { notReachable } from '@/utils/notReachable';\nimport { useTypedTranslation } from '@/localisation';\nimport type { Chains, DefaultChainsFilter } from '@/types/chain';\nimport type { Token } from '@/types/token';\n\nimport { TokensList } from './TokensList';\nimport { ChainsDropdown } from './ChainsDropdown';\nimport { useChains } from '../hooks';\n\ntype Msg =\n | { type: 'on_select_token'; token: Token }\n | { type: 'on_dismiss_tokens_modal' };\n\ntype Props = {\n variant: 'source' | 'target';\n groupTokens: boolean;\n showBalances: boolean;\n showChainsSelector: boolean;\n chainsFilter: DefaultChainsFilter;\n className?: string;\n onMsg: (msg: Msg) => void;\n};\n\nexport const TokensModal = ({\n variant,\n showBalances,\n showChainsSelector,\n chainsFilter,\n groupTokens,\n className,\n onMsg,\n}: Props) => {\n const { t } = useTypedTranslation();\n const { ctx } = useUnsafeSnapshot();\n const { walletSupportedChains } = useConfig();\n\n const [search, setSearch] = useState('');\n const chains = useChains(variant);\n\n // If there is only one chain available, select it by default\n const defaultChain =\n chains.length === 1 && chains[0]?.id ? chains[0]?.id : 'all';\n\n const [selectedChain, setSelectedChain] = useState<\n 'all' | 'intents' | Chains\n >(defaultChain);\n\n // selected chain is not supported by connected wallet\n const chainIsNotSupported =\n selectedChain !== 'all' &&\n selectedChain !== 'intents' &&\n chainsFilter.external !== 'all' &&\n !walletSupportedChains.includes(selectedChain);\n\n return (\n <Card\n padding=\"none\"\n className={cn('gap-sw-2xl flex flex-col px-sw-2xl pt-sw-2xl', className)}>\n <header className=\"py-sw-md flex items-center justify-between\">\n <h2 className=\"text-sw-label-l\">Select token</h2>\n <button\n type=\"button\"\n className=\"flex cursor-pointer items-center justify-center text-sw-gray-100 transition-colors hover:text-sw-gray-50\"\n onClick={() => onMsg({ type: 'on_dismiss_tokens_modal' })}>\n <Icons.X size={22} />\n </button>\n </header>\n\n <div className=\"gap-sw-xl flex items-center\">\n <Input\n focusOnMount\n icon={Icons.Search}\n defaultValue={search}\n className=\"w-full\"\n placeholder=\"Search or paste address\"\n onChange={(e) => setSearch(e.target.value.trim())}\n />\n {showChainsSelector && (\n <ChainsDropdown\n variant={variant}\n selected={selectedChain}\n chainsFilter={chainsFilter}\n onMsg={(msg) => {\n switch (msg.type) {\n case 'on_click_chain':\n setSelectedChain(msg.chain);\n break;\n default:\n notReachable(msg.type, { throwError: false });\n }\n }}\n />\n )}\n </div>\n\n {chainIsNotSupported && !!ctx.walletAddress && (\n <Banner\n variant=\"info\"\n message={t(\n 'wallet.connected.error.notSupportedChain',\n \"Your connected wallet doesn't support this chain\",\n )}\n />\n )}\n\n <TokensList\n variant={variant}\n search={search}\n groupTokens={groupTokens}\n showBalances={showBalances}\n chainsFilter={chainsFilter}\n selectedChain={selectedChain}\n chainIsNotSupported={chainIsNotSupported}\n onMsg={(msg) => {\n switch (msg.type) {\n case 'on_reset_search':\n setSearch('');\n break;\n case 'on_select_token':\n onMsg(msg);\n break;\n default:\n notReachable(msg, { throwError: false });\n }\n }}\n />\n </Card>\n );\n};\n"],"names":["TokensModal","variant","showBalances","showChainsSelector","chainsFilter","groupTokens","className","onMsg","t","useTypedTranslation","ctx","useUnsafeSnapshot","walletSupportedChains","useConfig","search","setSearch","useState","chains","useChains","defaultChain","selectedChain","setSelectedChain","chainIsNotSupported","jsxs","Card","cn","jsx","Icons","Input","ChainsDropdown","msg","notReachable","Banner","TokensList"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAiCO,MAAMA,KAAc,CAAC;AAAA,EAC1B,SAAAC;AAAA,EACA,cAAAC;AAAA,EACA,oBAAAC;AAAA,EACA,cAAAC;AAAA,EACA,aAAAC;AAAA,EACA,WAAAC;AAAA,EACA,OAAAC;AACF,MAAa;AACX,QAAM,EAAE,GAAAC,EAAA,IAAMC,EAAA,GACR,EAAE,KAAAC,EAAA,IAAQC,EAAA,GACV,EAAE,uBAAAC,EAAA,IAA0BC,EAAA,GAE5B,CAACC,GAAQC,CAAS,IAAIC,EAAS,EAAE,GACjCC,IAASC,EAAUjB,CAAO,GAG1BkB,IACJF,EAAO,WAAW,KAAKA,EAAO,CAAC,GAAG,KAAKA,EAAO,CAAC,GAAG,KAAK,OAEnD,CAACG,GAAeC,CAAgB,IAAIL,EAExCG,CAAY,GAGRG,IACJF,MAAkB,SAClBA,MAAkB,aAClBhB,EAAa,aAAa,SAC1B,CAACQ,EAAsB,SAASQ,CAAa;AAE/C,SACE,gBAAAG;AAAA,IAACC;AAAA,IAAA;AAAA,MACC,SAAQ;AAAA,MACR,WAAWC,EAAG,gDAAgDnB,CAAS;AAAA,MACvE,UAAA;AAAA,QAAA,gBAAAiB,EAAC,UAAA,EAAO,WAAU,8CAChB,UAAA;AAAA,UAAA,gBAAAG,EAAC,MAA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AAAA,YACA,eAAAgB;AAAA,YACA,qBAAAE;AAAA,YACA,OAAO,CAACQ,MAAQ;AACd,sBAAQA,EAAI,MAAA;AAAA,gBACV,KAAK;AACH,kBAAAf,EAAU,EAAE;AACZ;AAAA,gBACF,KAAK;AACH,kBAAAR,EAAMuB,CAAG;AACT;AAAA,gBACF;AACE,kBAAAC,EAAaD,GAAK,EAAE,YAAY,GAAA,CAAO;AAAA,cAAA;AAAA,YAE7C;AAAA,UAAA;AAAA,QAAA;AAAA,MACF;AAAA,IAAA;AAAA,EAAA;AAGN;"}
|
|
@@ -1,15 +1,11 @@
|
|
|
1
|
-
import { TransferResult } from '../types/transfer';
|
|
1
|
+
import { MakeTransferArgs, TransferResult } from '../types/transfer';
|
|
2
2
|
type Result = {
|
|
3
3
|
hash: string;
|
|
4
4
|
transactionLink: string;
|
|
5
5
|
intent?: string;
|
|
6
6
|
};
|
|
7
7
|
export type QuoteTransferArgs = {
|
|
8
|
-
makeTransfer: (args:
|
|
9
|
-
amount: string;
|
|
10
|
-
address: string;
|
|
11
|
-
tokenAddress: string | undefined;
|
|
12
|
-
}) => Promise<Result | undefined | null>;
|
|
8
|
+
makeTransfer: (args: MakeTransferArgs) => Promise<Result | undefined | null>;
|
|
13
9
|
};
|
|
14
10
|
export declare const useMakeQuoteTransfer: ({ makeTransfer }: QuoteTransferArgs) => {
|
|
15
11
|
make: () => Promise<TransferResult | undefined>;
|
|
@@ -1,28 +1,33 @@
|
|
|
1
|
-
import
|
|
1
|
+
import m from "axios";
|
|
2
2
|
import { logger as a } from "../logger.js";
|
|
3
|
-
import { b as
|
|
3
|
+
import { b as c, T as r } from "../config-haLfG4Cu.js";
|
|
4
4
|
import { NATIVE_NEAR_DUMB_ASSET_ID as n } from "../constants/tokens.js";
|
|
5
|
-
import { isUserDeniedSigning as
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import { isUserDeniedSigning as d } from "../utils/checkers/isUserDeniedSigning.js";
|
|
6
|
+
import { EVM_CHAIN_IDS_MAP as u } from "../constants/chains.js";
|
|
7
|
+
import { isEvmChain as R } from "../utils/evm/isEvmChain.js";
|
|
8
|
+
import "viem";
|
|
9
|
+
import "ethers";
|
|
10
|
+
import "../utils/cn.js";
|
|
11
|
+
const p = ({ makeTransfer: i }) => {
|
|
12
|
+
const { ctx: o } = c();
|
|
8
13
|
return { make: async () => {
|
|
9
14
|
if (!o.sourceToken) {
|
|
10
15
|
const e = "No token selected to transfer.";
|
|
11
|
-
throw new
|
|
16
|
+
throw new r({
|
|
12
17
|
code: "TRANSFER_INVALID_INITIAL",
|
|
13
18
|
meta: { message: e }
|
|
14
19
|
});
|
|
15
20
|
}
|
|
16
21
|
if (!o.quote) {
|
|
17
22
|
const e = "Quote is required for a transfer.";
|
|
18
|
-
throw new
|
|
23
|
+
throw new r({
|
|
19
24
|
code: "TRANSFER_INVALID_INITIAL",
|
|
20
25
|
meta: { message: e }
|
|
21
26
|
});
|
|
22
27
|
}
|
|
23
28
|
if (!o.quote.depositAddress) {
|
|
24
29
|
const e = "Quote has no deposit address. Make sure not dry.";
|
|
25
|
-
throw new
|
|
30
|
+
throw new r({
|
|
26
31
|
code: "TRANSFER_INVALID_INITIAL",
|
|
27
32
|
meta: { message: e }
|
|
28
33
|
});
|
|
@@ -31,10 +36,12 @@ const g = ({ makeTransfer: i }) => {
|
|
|
31
36
|
const e = await i({
|
|
32
37
|
amount: o.quote.amountIn,
|
|
33
38
|
address: o.quote.depositAddress,
|
|
39
|
+
chain: o.sourceToken.blockchain,
|
|
40
|
+
evmChainId: R(o.sourceToken.blockchain) ? u[o.sourceToken.blockchain] : null,
|
|
34
41
|
tokenAddress: o.sourceToken.assetId === n ? n : o.sourceToken.contractAddress
|
|
35
42
|
});
|
|
36
43
|
if (!e)
|
|
37
|
-
throw a.error("[TRANSFER ERROR]", "No deposit result"), new
|
|
44
|
+
throw a.error("[TRANSFER ERROR]", "No deposit result"), new r({
|
|
38
45
|
code: "NO_DEPOSIT_RESULT"
|
|
39
46
|
});
|
|
40
47
|
return {
|
|
@@ -44,19 +51,19 @@ const g = ({ makeTransfer: i }) => {
|
|
|
44
51
|
};
|
|
45
52
|
} catch (e) {
|
|
46
53
|
a.error("[TRANSFER ERROR]", e, { error: e });
|
|
47
|
-
let
|
|
48
|
-
const t = e instanceof Error &&
|
|
54
|
+
let s = "Failed to make a transfer. Please try again.";
|
|
55
|
+
const t = e instanceof Error && d(e.message);
|
|
49
56
|
if (t)
|
|
50
57
|
return;
|
|
51
|
-
throw
|
|
58
|
+
throw m.isAxiosError(e) ? (s = e.response?.data?.detail ?? e.message ?? "Failed to make deposit. Please try again.", new r({
|
|
52
59
|
code: "QUOTE_ERROR",
|
|
53
|
-
meta: { message:
|
|
54
|
-
})) :
|
|
60
|
+
meta: { message: s }
|
|
61
|
+
})) : s === "No quotes found" ? new r({
|
|
55
62
|
code: "NO_QUOTE_FOUND"
|
|
56
|
-
}) : e instanceof Error && !t ? new
|
|
63
|
+
}) : e instanceof Error && !t ? new r({
|
|
57
64
|
code: "QUOTE_ERROR",
|
|
58
65
|
meta: { message: e.message }
|
|
59
|
-
}) : new
|
|
66
|
+
}) : new r({
|
|
60
67
|
code: "QUOTE_ERROR",
|
|
61
68
|
meta: { message: "Unknown quote transfer error" }
|
|
62
69
|
});
|
|
@@ -64,6 +71,6 @@ const g = ({ makeTransfer: i }) => {
|
|
|
64
71
|
} };
|
|
65
72
|
};
|
|
66
73
|
export {
|
|
67
|
-
|
|
74
|
+
p as useMakeQuoteTransfer
|
|
68
75
|
};
|
|
69
76
|
//# sourceMappingURL=useMakeQuoteTransfer.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMakeQuoteTransfer.js","sources":["../../src/hooks/useMakeQuoteTransfer.ts"],"sourcesContent":["import axios from 'axios';\n\nimport { logger } from '@/logger';\nimport { TransferError } from '@/errors';\nimport { useUnsafeSnapshot } from '@/machine/snap';\nimport { NATIVE_NEAR_DUMB_ASSET_ID } from '@/constants/tokens';\nimport { isUserDeniedSigning } from '@/utils/checkers/isUserDeniedSigning';\nimport type { TransferResult } from '@/types/transfer';\n\ntype Result = {\n hash: string;\n transactionLink: string;\n intent?: string;\n};\n\nexport type QuoteTransferArgs = {\n makeTransfer: (args:
|
|
1
|
+
{"version":3,"file":"useMakeQuoteTransfer.js","sources":["../../src/hooks/useMakeQuoteTransfer.ts"],"sourcesContent":["import axios from 'axios';\n\nimport { logger } from '@/logger';\nimport { TransferError } from '@/errors';\nimport { useUnsafeSnapshot } from '@/machine/snap';\nimport { NATIVE_NEAR_DUMB_ASSET_ID } from '@/constants/tokens';\nimport { isUserDeniedSigning } from '@/utils/checkers/isUserDeniedSigning';\nimport type { MakeTransferArgs, TransferResult } from '@/types/transfer';\nimport { EVM_CHAIN_IDS_MAP } from '../constants/chains';\nimport { isEvmChain } from '../utils';\n\ntype Result = {\n hash: string;\n transactionLink: string;\n intent?: string;\n};\n\nexport type QuoteTransferArgs = {\n makeTransfer: (args: MakeTransferArgs) => Promise<Result | undefined | null>;\n};\n\nexport const useMakeQuoteTransfer = ({ makeTransfer }: QuoteTransferArgs) => {\n const { ctx } = useUnsafeSnapshot();\n\n const make = async (): Promise<TransferResult | undefined> => {\n if (!ctx.sourceToken) {\n const msg = 'No token selected to transfer.';\n\n throw new TransferError({\n code: 'TRANSFER_INVALID_INITIAL',\n meta: { message: msg },\n });\n }\n\n if (!ctx.quote) {\n const msg = 'Quote is required for a transfer.';\n\n throw new TransferError({\n code: 'TRANSFER_INVALID_INITIAL',\n meta: { message: msg },\n });\n }\n\n if (!ctx.quote.depositAddress) {\n const msg = 'Quote has no deposit address. Make sure not dry.';\n\n throw new TransferError({\n code: 'TRANSFER_INVALID_INITIAL',\n meta: { message: msg },\n });\n }\n\n try {\n const depositResult = await makeTransfer({\n amount: ctx.quote.amountIn,\n address: ctx.quote.depositAddress,\n chain: ctx.sourceToken.blockchain,\n evmChainId: isEvmChain(ctx.sourceToken.blockchain)\n ? EVM_CHAIN_IDS_MAP[ctx.sourceToken.blockchain]\n : null,\n tokenAddress:\n ctx.sourceToken.assetId === NATIVE_NEAR_DUMB_ASSET_ID\n ? NATIVE_NEAR_DUMB_ASSET_ID\n : ctx.sourceToken.contractAddress,\n });\n\n if (!depositResult) {\n logger.error('[TRANSFER ERROR]', 'No deposit result');\n throw new TransferError({\n code: 'NO_DEPOSIT_RESULT',\n });\n }\n\n return {\n hash: depositResult.hash,\n transactionLink: depositResult.transactionLink,\n intent: undefined,\n };\n } catch (error: unknown) {\n logger.error('[TRANSFER ERROR]', error, { error });\n let errorMessage = 'Failed to make a transfer. Please try again.';\n\n const userCancelledTx =\n error instanceof Error && isUserDeniedSigning(error.message);\n\n if (userCancelledTx) {\n return;\n }\n\n if (axios.isAxiosError<{ detail?: string }>(error)) {\n errorMessage =\n error.response?.data?.detail ??\n error.message ??\n 'Failed to make deposit. Please try again.';\n\n throw new TransferError({\n code: 'QUOTE_ERROR',\n meta: { message: errorMessage },\n });\n } else if (errorMessage === 'No quotes found') {\n throw new TransferError({\n code: 'NO_QUOTE_FOUND',\n });\n } else if (error instanceof Error) {\n if (!userCancelledTx) {\n throw new TransferError({\n code: 'QUOTE_ERROR',\n meta: { message: error.message },\n });\n }\n }\n\n throw new TransferError({\n code: 'QUOTE_ERROR',\n meta: { message: 'Unknown quote transfer error' },\n });\n }\n };\n\n return { make };\n};\n"],"names":["useMakeQuoteTransfer","makeTransfer","ctx","useUnsafeSnapshot","msg","TransferError","depositResult","isEvmChain","EVM_CHAIN_IDS_MAP","NATIVE_NEAR_DUMB_ASSET_ID","logger","error","errorMessage","userCancelledTx","isUserDeniedSigning","axios"],"mappings":";;;;;;;;;;AAqBO,MAAMA,IAAuB,CAAC,EAAE,cAAAC,QAAsC;AAC3E,QAAM,EAAE,KAAAC,EAAA,IAAQC,EAAA;AAiGhB,SAAO,EAAE,MA/FI,YAAiD;AAC5D,QAAI,CAACD,EAAI,aAAa;AACpB,YAAME,IAAM;AAEZ,YAAM,IAAIC,EAAc;AA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AAE,SAASO,EAAA;AAAA,MAAa,CAC/B,KACQA,MAAiB,oBACpB,IAAIP,EAAc;AAAA,QACtB,MAAM;AAAA,MAAA,CACP,IACQM,aAAiB,SACtB,CAACE,IACG,IAAIR,EAAc;AAAA,QACtB,MAAM;AAAA,QACN,MAAM,EAAE,SAASM,EAAM,QAAA;AAAA,MAAQ,CAChC,IAIC,IAAIN,EAAc;AAAA,QACtB,MAAM;AAAA,QACN,MAAM,EAAE,SAAS,+BAAA;AAAA,MAA+B,CACjD;AAAA,IACH;AAAA,EACF,EAES;AACX;"}
|
package/dist/types/transfer.d.ts
CHANGED
|
@@ -1,5 +1,13 @@
|
|
|
1
|
+
import { Chains } from './chain';
|
|
1
2
|
export type TransferResult = {
|
|
2
3
|
hash: string;
|
|
3
4
|
transactionLink: string;
|
|
4
|
-
intent
|
|
5
|
+
intent?: string;
|
|
6
|
+
};
|
|
7
|
+
export type MakeTransferArgs = {
|
|
8
|
+
amount: string;
|
|
9
|
+
address: string;
|
|
10
|
+
tokenAddress?: string;
|
|
11
|
+
chain: Chains;
|
|
12
|
+
evmChainId: number | null;
|
|
5
13
|
};
|
package/package.json
CHANGED
|
@@ -5,7 +5,9 @@ import { TransferError } from '@/errors';
|
|
|
5
5
|
import { useUnsafeSnapshot } from '@/machine/snap';
|
|
6
6
|
import { NATIVE_NEAR_DUMB_ASSET_ID } from '@/constants/tokens';
|
|
7
7
|
import { isUserDeniedSigning } from '@/utils/checkers/isUserDeniedSigning';
|
|
8
|
-
import type { TransferResult } from '@/types/transfer';
|
|
8
|
+
import type { MakeTransferArgs, TransferResult } from '@/types/transfer';
|
|
9
|
+
import { EVM_CHAIN_IDS_MAP } from '../constants/chains';
|
|
10
|
+
import { isEvmChain } from '../utils';
|
|
9
11
|
|
|
10
12
|
type Result = {
|
|
11
13
|
hash: string;
|
|
@@ -14,11 +16,7 @@ type Result = {
|
|
|
14
16
|
};
|
|
15
17
|
|
|
16
18
|
export type QuoteTransferArgs = {
|
|
17
|
-
makeTransfer: (args:
|
|
18
|
-
amount: string;
|
|
19
|
-
address: string;
|
|
20
|
-
tokenAddress: string | undefined;
|
|
21
|
-
}) => Promise<Result | undefined | null>;
|
|
19
|
+
makeTransfer: (args: MakeTransferArgs) => Promise<Result | undefined | null>;
|
|
22
20
|
};
|
|
23
21
|
|
|
24
22
|
export const useMakeQuoteTransfer = ({ makeTransfer }: QuoteTransferArgs) => {
|
|
@@ -56,6 +54,10 @@ export const useMakeQuoteTransfer = ({ makeTransfer }: QuoteTransferArgs) => {
|
|
|
56
54
|
const depositResult = await makeTransfer({
|
|
57
55
|
amount: ctx.quote.amountIn,
|
|
58
56
|
address: ctx.quote.depositAddress,
|
|
57
|
+
chain: ctx.sourceToken.blockchain,
|
|
58
|
+
evmChainId: isEvmChain(ctx.sourceToken.blockchain)
|
|
59
|
+
? EVM_CHAIN_IDS_MAP[ctx.sourceToken.blockchain]
|
|
60
|
+
: null,
|
|
59
61
|
tokenAddress:
|
|
60
62
|
ctx.sourceToken.assetId === NATIVE_NEAR_DUMB_ASSET_ID
|
|
61
63
|
? NATIVE_NEAR_DUMB_ASSET_ID
|
package/src/types/transfer.ts
CHANGED
|
@@ -1,5 +1,15 @@
|
|
|
1
|
+
import { Chains } from './chain';
|
|
2
|
+
|
|
1
3
|
export type TransferResult = {
|
|
2
4
|
hash: string;
|
|
3
5
|
transactionLink: string;
|
|
4
|
-
intent
|
|
6
|
+
intent?: string;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type MakeTransferArgs = {
|
|
10
|
+
amount: string;
|
|
11
|
+
address: string;
|
|
12
|
+
tokenAddress?: string;
|
|
13
|
+
chain: Chains;
|
|
14
|
+
evmChainId: number | null;
|
|
5
15
|
};
|