@xswap-link/sdk 0.6.10 → 0.7.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/CHANGELOG.md +6 -0
- package/dist/index.d.mts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.global.js +108 -108
- package/dist/index.js +10 -10
- package/dist/index.mjs +10 -10
- package/package.json +1 -1
- package/src/components/Modal/index.tsx +48 -41
- package/src/components/Swap/SwapView/SwapButton/index.tsx +28 -21
- package/src/components/Swap/SwapView/SwapPanel/ChainPanel/ChainPicker/index.tsx +4 -5
- package/src/components/Swap/SwapView/SwapPanel/ChainPanel/index.tsx +16 -14
- package/src/components/Swap/SwapView/SwapPanel/TokenPanel/TokenPicker/TokenItem/index.tsx +9 -5
- package/src/components/Swap/SwapView/SwapPanel/TokenPanel/TokenPicker/index.tsx +2 -4
- package/src/components/Swap/SwapView/SwapPanel/TokenPanel/index.tsx +19 -18
- package/src/components/Swap/SwapView/WalletPicker/index.tsx +7 -6
- package/src/components/TxConfigForm/index.tsx +15 -18
- package/src/components/TxModal/index.tsx +59 -0
- package/src/components/index.ts +1 -0
- package/src/context/TxUIWrapper.tsx +5 -5
- package/src/hooks/index.ts +0 -1
- package/src/models/payloads/ModalIntegrationPayload.ts +2 -2
- package/src/services/integrations/transactions.ts +20 -17
- package/src/context/ModalProvider.tsx +0 -63
- package/src/hooks/usePortal.ts +0 -79
|
@@ -49,8 +49,8 @@ export const openTransactionModal = async ({
|
|
|
49
49
|
const validateSwapTxData = (
|
|
50
50
|
integratorId: string,
|
|
51
51
|
supportedChains: Chain[],
|
|
52
|
-
dstChain
|
|
53
|
-
dstToken
|
|
52
|
+
dstChain?: string,
|
|
53
|
+
dstToken?: string,
|
|
54
54
|
srcChain?: string,
|
|
55
55
|
srcToken?: string,
|
|
56
56
|
) => {
|
|
@@ -60,21 +60,24 @@ const validateSwapTxData = (
|
|
|
60
60
|
);
|
|
61
61
|
}
|
|
62
62
|
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
if (!supportedDstChain) {
|
|
67
|
-
throw new Error(`Provided chain '${dstChain}' is not supported`);
|
|
68
|
-
}
|
|
69
|
-
if (
|
|
70
|
-
!isETHAddressValid(dstToken) ||
|
|
71
|
-
supportedDstChain.tokens.findIndex(
|
|
72
|
-
({ address }) => address.toLowerCase() === dstToken.toLowerCase(),
|
|
73
|
-
) === -1
|
|
74
|
-
) {
|
|
75
|
-
throw new Error(
|
|
76
|
-
`Provided token address ${dstToken} on chain ${dstChain} is invalid`,
|
|
63
|
+
if (dstChain) {
|
|
64
|
+
const supportedDstChain = supportedChains.find(
|
|
65
|
+
({ chainId }) => chainId === dstChain,
|
|
77
66
|
);
|
|
67
|
+
if (!supportedDstChain) {
|
|
68
|
+
throw new Error(`Provided chain '${dstChain}' is not supported`);
|
|
69
|
+
}
|
|
70
|
+
if (
|
|
71
|
+
dstToken &&
|
|
72
|
+
(!isETHAddressValid(dstToken) ||
|
|
73
|
+
supportedDstChain.tokens.findIndex(
|
|
74
|
+
({ address }) => address.toLowerCase() === dstToken.toLowerCase(),
|
|
75
|
+
) === -1)
|
|
76
|
+
) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
`Provided token address ${dstToken} on chain ${dstChain} is invalid`,
|
|
79
|
+
);
|
|
80
|
+
}
|
|
78
81
|
}
|
|
79
82
|
|
|
80
83
|
if (srcChain) {
|
|
@@ -86,7 +89,7 @@ const validateSwapTxData = (
|
|
|
86
89
|
}
|
|
87
90
|
if (
|
|
88
91
|
srcToken &&
|
|
89
|
-
(!isETHAddressValid(
|
|
92
|
+
(!isETHAddressValid(srcToken) ||
|
|
90
93
|
supportedSrcChain.tokens.findIndex(
|
|
91
94
|
({ address }) => address.toLowerCase() === srcToken.toLowerCase(),
|
|
92
95
|
) === -1)
|
|
@@ -1,63 +0,0 @@
|
|
|
1
|
-
import { createContext, FC, ReactNode, useContext, useState } from "react";
|
|
2
|
-
import { useSwapContext } from "./SwapProvider";
|
|
3
|
-
|
|
4
|
-
interface ModalState {
|
|
5
|
-
openModal: (modalContent: ReactNode) => void;
|
|
6
|
-
closeModal: () => void;
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
const ModalContext = createContext<ModalState>({
|
|
10
|
-
openModal: () => {
|
|
11
|
-
throw new Error(`openModal is not implemented`);
|
|
12
|
-
},
|
|
13
|
-
closeModal: () => {
|
|
14
|
-
throw new Error(`closeModal is not implemented`);
|
|
15
|
-
},
|
|
16
|
-
});
|
|
17
|
-
|
|
18
|
-
export const ModalProvider: FC<{ children: ReactNode }> = ({ children }) => {
|
|
19
|
-
const { overlay } = useSwapContext();
|
|
20
|
-
const [content, setContent] = useState<ReactNode | null>(null);
|
|
21
|
-
|
|
22
|
-
const openModal = (modalContent: ReactNode) => {
|
|
23
|
-
setContent(modalContent);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
const closeModal = () => {
|
|
27
|
-
setContent(null);
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
return (
|
|
31
|
-
<ModalContext.Provider value={{ openModal, closeModal }}>
|
|
32
|
-
<div
|
|
33
|
-
className={`${
|
|
34
|
-
overlay ? "" : "relative min-w-x_modal w-x_modal max-w-x_modal"
|
|
35
|
-
}`}
|
|
36
|
-
>
|
|
37
|
-
{children}
|
|
38
|
-
{content && (
|
|
39
|
-
<div
|
|
40
|
-
className={`${
|
|
41
|
-
overlay ? "fixed bg-[rgba(0,0,0,0.8)]" : "absolute"
|
|
42
|
-
} top-0 left-0 right-0 bottom-0 flex items-center justify-center z-10`}
|
|
43
|
-
>
|
|
44
|
-
<div
|
|
45
|
-
className="flex flex-col bg-t_bg_primary x-border rounded-3xl p-4 w-full min-w-[340px]"
|
|
46
|
-
style={{
|
|
47
|
-
height: overlay ? "600px" : "100%",
|
|
48
|
-
maxWidth: "480px",
|
|
49
|
-
maxHeight: "100%",
|
|
50
|
-
}}
|
|
51
|
-
>
|
|
52
|
-
{content}
|
|
53
|
-
</div>
|
|
54
|
-
</div>
|
|
55
|
-
)}
|
|
56
|
-
</div>
|
|
57
|
-
</ModalContext.Provider>
|
|
58
|
-
);
|
|
59
|
-
};
|
|
60
|
-
|
|
61
|
-
export const useModal = () => {
|
|
62
|
-
return useContext(ModalContext);
|
|
63
|
-
};
|
package/src/hooks/usePortal.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { useEffect, useRef } from "react";
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* Creates DOM element to be used as React root.
|
|
5
|
-
*/
|
|
6
|
-
function createRootElement(id: string): HTMLElement {
|
|
7
|
-
const rootContainer = document.createElement("div");
|
|
8
|
-
rootContainer.setAttribute("id", id);
|
|
9
|
-
return rootContainer;
|
|
10
|
-
}
|
|
11
|
-
|
|
12
|
-
/**
|
|
13
|
-
* Appends element as last child of body.
|
|
14
|
-
*/
|
|
15
|
-
function addRootElement(rootElem: Element): void {
|
|
16
|
-
document.body.insertBefore(
|
|
17
|
-
rootElem,
|
|
18
|
-
document.body.lastElementChild!.nextElementSibling,
|
|
19
|
-
);
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
/**
|
|
23
|
-
* Hook to create a React Portal.
|
|
24
|
-
* Automatically handles creating and tearing-down the root elements (no SRR
|
|
25
|
-
* makes this trivial), so there is no need to ensure the parent target already
|
|
26
|
-
* exists.
|
|
27
|
-
* @example
|
|
28
|
-
* const target = usePortal(id, [id]);
|
|
29
|
-
* return createPortal(children, target);
|
|
30
|
-
* @param {string} id The id of the target container, e.g 'modal' or 'spotlight'
|
|
31
|
-
* @returns {HTMLElement} The DOM node to use as the Portal target.
|
|
32
|
-
*/
|
|
33
|
-
export function usePortal(id: string): HTMLElement {
|
|
34
|
-
const rootElemRef = useRef<HTMLElement | null>(null);
|
|
35
|
-
|
|
36
|
-
useEffect(
|
|
37
|
-
function setupElement() {
|
|
38
|
-
// Look for existing target dom element to append to
|
|
39
|
-
const existingParent = document.querySelector(`#${id}`);
|
|
40
|
-
// Parent is either a new root or the existing dom element
|
|
41
|
-
const parentElem = existingParent || createRootElement(id);
|
|
42
|
-
|
|
43
|
-
// If there is no existing DOM element, add a new one.
|
|
44
|
-
if (!existingParent) {
|
|
45
|
-
addRootElement(parentElem);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
// Add the detached element to the parent
|
|
49
|
-
parentElem.appendChild(rootElemRef.current!);
|
|
50
|
-
|
|
51
|
-
return function removeElement() {
|
|
52
|
-
rootElemRef.current!.remove();
|
|
53
|
-
if (!parentElem.childElementCount) {
|
|
54
|
-
parentElem.remove();
|
|
55
|
-
}
|
|
56
|
-
};
|
|
57
|
-
},
|
|
58
|
-
[id],
|
|
59
|
-
);
|
|
60
|
-
|
|
61
|
-
/**
|
|
62
|
-
* It's important we evaluate this lazily:
|
|
63
|
-
* - We need first render to contain the DOM element, so it shouldn't happen
|
|
64
|
-
* in useEffect. We would normally put this in the constructor().
|
|
65
|
-
* - We can't do 'const rootElemRef = useRef(document.createElement('div))',
|
|
66
|
-
* since this will run every single render (that's a lot).
|
|
67
|
-
* - We want the ref to consistently point to the same DOM element and only
|
|
68
|
-
* ever run once.
|
|
69
|
-
* @link https://reactjs.org/docs/hooks-faq.html#how-to-create-expensive-objects-lazily
|
|
70
|
-
*/
|
|
71
|
-
function getRootElem(): HTMLElement {
|
|
72
|
-
if (!rootElemRef.current) {
|
|
73
|
-
rootElemRef.current = document.createElement("div");
|
|
74
|
-
}
|
|
75
|
-
return rootElemRef.current;
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
return getRootElem();
|
|
79
|
-
}
|