@xswap-link/sdk 0.3.5 → 0.4.1
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/.eslintrc.json +9 -5
- package/CHANGELOG.md +16 -0
- package/README.md +2 -1
- package/dist/index.d.mts +106 -20
- package/dist/index.d.ts +106 -20
- package/dist/index.global.js +856 -307
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +105 -3564
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +105 -3534
- package/dist/index.mjs.map +1 -0
- package/nodemon.json +1 -1
- package/package.json +16 -9
- package/postcss.config.js +8 -1
- package/src/{components → assets}/icons/ArrowDownIcon.tsx +1 -2
- package/src/assets/icons/ArrowDownLongIcon.tsx +12 -0
- package/src/{components → assets}/icons/ArrowLeftIcon.tsx +1 -5
- package/src/{components → assets}/icons/ArrowRightIcon.tsx +1 -5
- package/src/assets/icons/ArrowUpIcon.tsx +12 -0
- package/src/{components → assets}/icons/ArrowUpRightIcon.tsx +1 -5
- package/src/assets/icons/CashbacklIcon.tsx +28 -0
- package/src/{components → assets}/icons/CheckIcon.tsx +1 -5
- package/src/assets/icons/ChevronDownIcon.tsx +12 -0
- package/src/assets/icons/ChevronUpIcon.tsx +7 -0
- package/src/{components → assets}/icons/CircularProgressIcon.tsx +1 -2
- package/src/assets/icons/CloseIcon.tsx +15 -0
- package/src/{components → assets}/icons/CoinsIcon.tsx +0 -2
- package/src/assets/icons/CopyIcon.tsx +15 -0
- package/src/assets/icons/DotGreenIcon.tsx +12 -0
- package/src/{components → assets}/icons/DownArrorIcon.tsx +0 -2
- package/src/{components → assets}/icons/ErrorIcon.tsx +0 -1
- package/src/assets/icons/ErrorModalStatusIcon.tsx +31 -0
- package/src/assets/icons/HelpIcon.tsx +21 -0
- package/src/assets/icons/HistoryGradientBgIcon.tsx +32 -0
- package/src/assets/icons/HistoryIcon.tsx +12 -0
- package/src/{components → assets}/icons/HourGlassIcon.tsx +1 -5
- package/src/assets/icons/Icon.tsx +5 -0
- package/src/{components → assets}/icons/InfoIcon.tsx +0 -1
- package/src/assets/icons/LinkExternalIcon.tsx +15 -0
- package/src/{components → assets}/icons/PercentageIcon.tsx +0 -1
- package/src/assets/icons/RankIcon.tsx +28 -0
- package/src/assets/icons/SettingsGradientBgIcon.tsx +32 -0
- package/src/assets/icons/SettingsIcon.tsx +12 -0
- package/src/{components → assets}/icons/SignIcon.tsx +0 -2
- package/src/{components → assets}/icons/SuccessIcon.tsx +0 -1
- package/src/assets/icons/SuccessModalStatusIcon.tsx +31 -0
- package/src/assets/icons/ThumbUpRoundedIcon.tsx +15 -0
- package/src/{components → assets}/icons/TimerIcon.tsx +0 -1
- package/src/assets/icons/TokensIcon.tsx +16 -0
- package/src/assets/icons/WaitingIcon.tsx +15 -0
- package/src/{components → assets}/icons/XMarkIcon.tsx +1 -5
- package/src/assets/icons/XPowerIcon.tsx +28 -0
- package/src/{components → assets}/icons/index.ts +16 -0
- package/src/components/Alert/index.tsx +1 -1
- package/src/components/Button/index.tsx +57 -0
- package/src/components/MessageBar/SnackMessage.tsx +32 -0
- package/src/components/MessageBar/index.tsx +44 -0
- package/src/components/Modal/index.tsx +52 -0
- package/src/components/PoweredBy/index.tsx +12 -0
- package/src/components/SafeInput/index.tsx +51 -0
- package/src/components/Skeleton/index.tsx +5 -9
- package/src/components/Spinner/index.tsx +6 -8
- package/src/components/Swap/Header/Controls/index.tsx +57 -0
- package/src/components/Swap/Header/index.tsx +16 -0
- package/src/components/Swap/HistoryView/index.tsx +34 -0
- package/src/components/Swap/SettingsView/Delivery/index.tsx +42 -0
- package/src/components/Swap/SettingsView/Slippage/index.tsx +80 -0
- package/src/components/Swap/SettingsView/index.tsx +11 -0
- package/src/components/Swap/SwapView/ConfirmationView/TxOverview/ArrowIcon.tsx +13 -0
- package/src/components/Swap/SwapView/ConfirmationView/TxOverview/Header/index.tsx +37 -0
- package/src/components/Swap/SwapView/ConfirmationView/TxOverview/Steps/index.tsx +145 -0
- package/src/components/Swap/SwapView/ConfirmationView/TxOverview/SwapPanel/index.tsx +61 -0
- package/src/components/Swap/SwapView/ConfirmationView/TxOverview/index.tsx +291 -0
- package/src/components/Swap/SwapView/ConfirmationView/TxResult/TokenItem/index.tsx +24 -0
- package/src/components/Swap/SwapView/ConfirmationView/TxResult/index.tsx +84 -0
- package/src/components/Swap/SwapView/ConfirmationView/index.tsx +17 -0
- package/src/components/Swap/SwapView/FeesPanel/DeliveryInfo/index.tsx +25 -0
- package/src/components/Swap/SwapView/FeesPanel/Fee/index.tsx +18 -0
- package/src/components/Swap/SwapView/FeesPanel/Fees/index.tsx +71 -0
- package/src/components/Swap/SwapView/FeesPanel/index.tsx +67 -0
- package/src/components/Swap/SwapView/SwapButton/index.tsx +154 -0
- package/src/components/Swap/SwapView/SwapPanel/AmountPanel/Balance/index.tsx +23 -0
- package/src/components/Swap/SwapView/SwapPanel/AmountPanel/index.tsx +56 -0
- package/src/components/Swap/SwapView/SwapPanel/ChainPanel/ChainPicker/ChainItem/index.tsx +70 -0
- package/src/components/Swap/SwapView/SwapPanel/ChainPanel/ChainPicker/index.tsx +62 -0
- package/src/components/Swap/SwapView/SwapPanel/ChainPanel/index.tsx +74 -0
- package/src/components/Swap/SwapView/SwapPanel/MaxPanel/index.tsx +35 -0
- package/src/components/Swap/SwapView/SwapPanel/TokenPanel/TokenPicker/QuickPickTokenItem/index.tsx +40 -0
- package/src/components/Swap/SwapView/SwapPanel/TokenPanel/TokenPicker/TokenItem/index.tsx +62 -0
- package/src/components/Swap/SwapView/SwapPanel/TokenPanel/TokenPicker/index.tsx +399 -0
- package/src/components/Swap/SwapView/SwapPanel/TokenPanel/index.tsx +104 -0
- package/src/components/Swap/SwapView/SwapPanel/index.tsx +42 -0
- package/src/components/Swap/SwapView/index.tsx +20 -0
- package/src/components/Swap/index.tsx +34 -0
- package/src/components/TextInput/index.tsx +67 -0
- package/src/components/Timer/index.tsx +57 -0
- package/src/components/ToggleButton/index.tsx +24 -0
- package/src/components/{UnknownTokenLogo/UnknownTokenLogo.tsx → TokenLogo/Unknown/index.tsx} +3 -4
- package/src/components/TokenLogo/WithChain/index.tsx +34 -0
- package/src/components/TokenLogo/index.tsx +22 -0
- package/src/components/Tooltip/index.tsx +108 -0
- package/src/components/TxConfigForm/index.tsx +63 -42
- package/src/components/TxDataCard/TokenDetails/index.tsx +30 -0
- package/src/components/TxDataCard/TxDataCardUI/index.tsx +133 -0
- package/src/components/TxDataCard/index.tsx +30 -0
- package/src/components/TxStatusButton/index.tsx +64 -108
- package/src/components/global.css +115 -4
- package/src/components/index.ts +18 -1
- package/src/config/fonts.ts +114 -0
- package/src/config/global.d.ts +4 -0
- package/src/config/init.tsx +35 -31
- package/src/constants/crosschainEstimationTimes.ts +52 -0
- package/src/constants/index.ts +8 -0
- package/src/context/GlobalDataProvider.tsx +165 -0
- package/src/context/HistoryProvider.tsx +339 -0
- package/src/context/ModalProvider.tsx +47 -0
- package/src/context/SwapProvider.tsx +664 -0
- package/src/context/TransactionProvider.tsx +86 -0
- package/src/context/TxUIWrapper.tsx +388 -0
- package/src/context/index.ts +5 -0
- package/src/contracts/abi/CustomXSwapRouter.json +953 -0
- package/src/contracts/abi/StakingStableAPR.json +1438 -0
- package/src/contracts/abi/XSwapRouter.json +1242 -0
- package/src/contracts/abi/XSwapTokenFeeDistributor.json +350 -0
- package/src/contracts/abi/index.ts +8 -0
- package/src/contracts/addresses.ts +24 -6
- package/src/fonts/Satoshi-Black.woff2 +0 -0
- package/src/fonts/Satoshi-BlackItalic.woff2 +0 -0
- package/src/fonts/Satoshi-Bold.woff2 +0 -0
- package/src/fonts/Satoshi-BoldItalic.woff2 +0 -0
- package/src/fonts/Satoshi-Light.woff2 +0 -0
- package/src/fonts/Satoshi-LightItalic.woff2 +0 -0
- package/src/fonts/Satoshi-Medium.woff2 +0 -0
- package/src/fonts/Satoshi-MediumItalic.woff2 +0 -0
- package/src/fonts/Satoshi-Regular.woff2 +0 -0
- package/src/fonts/Satoshi-RegularItalic.woff2 +0 -0
- package/src/fonts/Satoshi-Variable.woff2 +0 -0
- package/src/fonts/Satoshi-VariableItalic.woff2 +0 -0
- package/src/hooks/index.ts +5 -0
- package/src/hooks/networkManagement/clientToSigner.tsx +17 -0
- package/src/hooks/networkManagement/useNetworks.ts +51 -0
- package/src/hooks/useBatchQuery.ts +46 -0
- package/src/hooks/useDebounce.tsx +28 -0
- package/src/hooks/useEvmContractApi.ts +85 -0
- package/src/hooks/usePortal.ts +79 -0
- package/src/hooks/useXPower.ts +58 -0
- package/src/models/Addresses.ts +8 -2
- package/src/models/TokenData.ts +13 -0
- package/src/models/TransactionHistory.ts +2 -0
- package/src/models/TxUIWrapper.ts +51 -0
- package/src/models/XSwapConfig.ts +1 -0
- package/src/models/index.ts +1 -0
- package/src/models/payloads/GetBalancesPayload.ts +3 -0
- package/src/models/payloads/GetFinishedLeaderboardQuestsPayload.ts +3 -0
- package/src/models/payloads/GetLeaderboardChangePayload.ts +4 -0
- package/src/models/payloads/{GetSwapTxPayload.ts → ModalIntegrationPayload.ts} +3 -1
- package/src/models/payloads/index.ts +5 -2
- package/src/services/api.ts +34 -5
- package/src/services/integrations/monitoring.ts +49 -37
- package/src/services/integrations/transactions.ts +50 -6
- package/src/utils/contracts.ts +29 -0
- package/src/utils/index.ts +3 -3
- package/src/utils/numbers.ts +14 -0
- package/src/utils/parseWeb3Error.ts +7 -3
- package/tailwind.config.js +15 -5
- package/tsconfig.json +7 -7
- package/tsup.config.ts +62 -0
- package/xswap.config.ts +1 -0
- package/dist/index.css +0 -1
- package/src/components/TxConfigForm/BalanceComponent.tsx +0 -52
- package/src/components/TxConfigForm/Button.tsx +0 -20
- package/src/components/TxConfigForm/ChainListElement.tsx +0 -36
- package/src/components/TxConfigForm/ConfirmationAmount.tsx +0 -91
- package/src/components/TxConfigForm/ConfirmationView/ErrorView.tsx +0 -38
- package/src/components/TxConfigForm/ConfirmationView/SuccessView.tsx +0 -159
- package/src/components/TxConfigForm/ConfirmationView/index.tsx +0 -149
- package/src/components/TxConfigForm/ConnectWalletPrompt.tsx +0 -21
- package/src/components/TxConfigForm/Description.tsx +0 -17
- package/src/components/TxConfigForm/ErrorField.tsx +0 -19
- package/src/components/TxConfigForm/FeesDetails.tsx +0 -117
- package/src/components/TxConfigForm/Form.tsx +0 -574
- package/src/components/TxConfigForm/History.tsx +0 -131
- package/src/components/TxConfigForm/HistoryCard.tsx +0 -182
- package/src/components/TxConfigForm/PoweredBy.tsx +0 -12
- package/src/components/TxConfigForm/Settings.tsx +0 -143
- package/src/components/TxConfigForm/Summary.tsx +0 -105
- package/src/components/TxConfigForm/SwapPanel.tsx +0 -185
- package/src/components/TxConfigForm/TokenPicker.tsx +0 -176
- package/src/components/TxConfigForm/TopBar.tsx +0 -58
- package/src/components/TxConfigForm/UsdPrice.tsx +0 -44
- package/src/components/icons/ChevronDownIcon.tsx +0 -15
- package/src/components/icons/ChevronUpIcon.tsx +0 -10
- package/src/components/icons/CloseIcon.tsx +0 -15
- package/src/components/icons/HistoryIcon.tsx +0 -16
- package/src/components/icons/SettingsIcon.tsx +0 -16
- /package/src/{components → assets}/icons/ChainlinkCCIPIcon.tsx +0 -0
- /package/src/{components → assets}/icons/SearchIcon.tsx +0 -0
- /package/src/{components → assets}/icons/XSwapBadgeIcon.tsx +0 -0
- /package/src/{components → assets}/icons/XSwapLogo.tsx +0 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { Ecosystem, EnqueueTxProps, TxStatus } from "@src/models";
|
|
2
|
+
import { ContractReceipt, ContractTransaction } from "ethers";
|
|
3
|
+
import {
|
|
4
|
+
Context,
|
|
5
|
+
ReactNode,
|
|
6
|
+
createContext,
|
|
7
|
+
useCallback,
|
|
8
|
+
useContext,
|
|
9
|
+
useMemo,
|
|
10
|
+
} from "react";
|
|
11
|
+
|
|
12
|
+
export interface TransactionState {
|
|
13
|
+
enqueueTransaction: (props: EnqueueTxProps) => number;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
const TransactionContext: Context<TransactionState> =
|
|
17
|
+
createContext<TransactionState>({} as TransactionState);
|
|
18
|
+
|
|
19
|
+
interface TransactionProviderProps {
|
|
20
|
+
children: ReactNode;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface EvmHandlers {
|
|
24
|
+
onSuccess?: (data: ContractReceipt) => void;
|
|
25
|
+
onError?: <T>(data: T) => void;
|
|
26
|
+
onConfirm?: (data?: ContractTransaction) => void;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export const TransactionProvider = ({ children }: TransactionProviderProps) => {
|
|
30
|
+
const evmTransactionExecutor = useCallback(
|
|
31
|
+
async ({ executeTransaction, handlers }) => {
|
|
32
|
+
try {
|
|
33
|
+
const tx = await executeTransaction();
|
|
34
|
+
if (!tx) {
|
|
35
|
+
throw new Error("Transaction is undefined");
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
handlers?.onConfirm?.(tx);
|
|
39
|
+
|
|
40
|
+
const result = await tx.wait();
|
|
41
|
+
|
|
42
|
+
handlers?.onSuccess?.(result);
|
|
43
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
44
|
+
} catch (err: any) {
|
|
45
|
+
console.error(err);
|
|
46
|
+
handlers?.onError?.(err);
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
[],
|
|
50
|
+
);
|
|
51
|
+
|
|
52
|
+
const enqueueTransaction = useCallback(
|
|
53
|
+
({
|
|
54
|
+
executeTransaction,
|
|
55
|
+
handlers,
|
|
56
|
+
network = Ecosystem.EVM,
|
|
57
|
+
}: EnqueueTxProps) => {
|
|
58
|
+
const currentKey = Date.now();
|
|
59
|
+
if (network === Ecosystem.EVM) {
|
|
60
|
+
evmTransactionExecutor({
|
|
61
|
+
executeTransaction,
|
|
62
|
+
handlers,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
return currentKey;
|
|
66
|
+
},
|
|
67
|
+
[evmTransactionExecutor],
|
|
68
|
+
);
|
|
69
|
+
|
|
70
|
+
const state = useMemo<TransactionState>(
|
|
71
|
+
() => ({
|
|
72
|
+
enqueueTransaction,
|
|
73
|
+
}),
|
|
74
|
+
[enqueueTransaction],
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
return (
|
|
78
|
+
<TransactionContext.Provider value={state}>
|
|
79
|
+
{children}
|
|
80
|
+
</TransactionContext.Provider>
|
|
81
|
+
);
|
|
82
|
+
};
|
|
83
|
+
|
|
84
|
+
export function useTransaction(): TransactionState {
|
|
85
|
+
return useContext(TransactionContext);
|
|
86
|
+
}
|
|
@@ -0,0 +1,388 @@
|
|
|
1
|
+
import { CloseIcon } from "@src/assets/icons";
|
|
2
|
+
import {
|
|
3
|
+
Button,
|
|
4
|
+
Modal,
|
|
5
|
+
ModalType,
|
|
6
|
+
SnackMessage,
|
|
7
|
+
Spinner,
|
|
8
|
+
} from "@src/components";
|
|
9
|
+
import { ConfirmationView } from "@src/components/Swap/SwapView/ConfirmationView";
|
|
10
|
+
import { CCIP_EXPLORER } from "@src/constants";
|
|
11
|
+
import {
|
|
12
|
+
Ecosystem,
|
|
13
|
+
EnqueueTxProps,
|
|
14
|
+
TxStatus,
|
|
15
|
+
TxUIWrapperState,
|
|
16
|
+
} from "@src/models";
|
|
17
|
+
import { parseWeb3Error, trimTxHash } from "@src/utils";
|
|
18
|
+
import { ContractReceipt } from "ethers";
|
|
19
|
+
import { useSnackbar } from "notistack";
|
|
20
|
+
import {
|
|
21
|
+
Context,
|
|
22
|
+
createContext,
|
|
23
|
+
ReactNode,
|
|
24
|
+
useCallback,
|
|
25
|
+
useContext,
|
|
26
|
+
useEffect,
|
|
27
|
+
useMemo,
|
|
28
|
+
useRef,
|
|
29
|
+
useState,
|
|
30
|
+
} from "react";
|
|
31
|
+
import { useAccount } from "wagmi";
|
|
32
|
+
import { useSwapContext } from "./SwapProvider";
|
|
33
|
+
import { EvmHandlers, useTransaction } from "./TransactionProvider";
|
|
34
|
+
|
|
35
|
+
const TxUIWrapperContext: Context<TxUIWrapperState> =
|
|
36
|
+
createContext<TxUIWrapperState>({} as TxUIWrapperState);
|
|
37
|
+
|
|
38
|
+
type Props = {
|
|
39
|
+
children: ReactNode;
|
|
40
|
+
};
|
|
41
|
+
export const TxUIWrapper = ({ children }: Props) => {
|
|
42
|
+
const { chainId } = useAccount();
|
|
43
|
+
const { enqueueTransaction: nativeEnqueueTransaction } = useTransaction();
|
|
44
|
+
const { enqueueSnackbar, closeSnackbar } = useSnackbar();
|
|
45
|
+
const [currentNetwork, setCurrentNetwork] = useState(Ecosystem.EVM);
|
|
46
|
+
const isTxModalOpenRef = useRef(false);
|
|
47
|
+
const [isTxModalOpen, setTxModalOpen] = useState(false);
|
|
48
|
+
const [txHash, setTxHash] = useState("");
|
|
49
|
+
const txKey = useRef<number>();
|
|
50
|
+
const [txError, setTxError] = useState("");
|
|
51
|
+
const [txMsg, setTxMsg] = useState("");
|
|
52
|
+
const [txReceipt, setTxReceipt] = useState<ContractReceipt | undefined>(
|
|
53
|
+
undefined,
|
|
54
|
+
);
|
|
55
|
+
|
|
56
|
+
const [loading, setLoading] = useState(false);
|
|
57
|
+
const [txNeedsApproval, setTxNeedsApproval] = useState(false);
|
|
58
|
+
const [txStatus, setTxStatus] = useState(TxStatus.SWAP_INIT);
|
|
59
|
+
|
|
60
|
+
const { supportedChains, setSrcValue } = useSwapContext();
|
|
61
|
+
|
|
62
|
+
const closeTransactionModal = (transactionKey: number) => {
|
|
63
|
+
if (txKey.current === transactionKey) {
|
|
64
|
+
isTxModalOpenRef.current = false;
|
|
65
|
+
setTxModalOpen(false);
|
|
66
|
+
}
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
useEffect(() => {
|
|
70
|
+
if (!isTxModalOpen) {
|
|
71
|
+
if (txError) {
|
|
72
|
+
enqueueSnackbar(
|
|
73
|
+
<SnackMessage
|
|
74
|
+
message={{
|
|
75
|
+
text: txError,
|
|
76
|
+
variant: "error",
|
|
77
|
+
}}
|
|
78
|
+
/>,
|
|
79
|
+
);
|
|
80
|
+
setTxError("");
|
|
81
|
+
}
|
|
82
|
+
if (loading) {
|
|
83
|
+
if (txHash) {
|
|
84
|
+
const snackbarKey = txHash;
|
|
85
|
+
enqueueSnackbar(
|
|
86
|
+
<SnackMessage
|
|
87
|
+
message={{
|
|
88
|
+
text: `Transaction ${trimTxHash(txHash)} is about to be mined!`,
|
|
89
|
+
variant: "warning",
|
|
90
|
+
action: () => (
|
|
91
|
+
<div className="flex items-center flex-nowrap">
|
|
92
|
+
<Button
|
|
93
|
+
type="button"
|
|
94
|
+
onClick={() => {
|
|
95
|
+
window.open(
|
|
96
|
+
`${
|
|
97
|
+
supportedChains.find(
|
|
98
|
+
(chain) =>
|
|
99
|
+
chain.chainId === chainId?.toString() &&
|
|
100
|
+
chain.ecosystem === currentNetwork,
|
|
101
|
+
)?.transactionExplorer
|
|
102
|
+
}/${txHash}` || "",
|
|
103
|
+
);
|
|
104
|
+
}}
|
|
105
|
+
>
|
|
106
|
+
Check
|
|
107
|
+
</Button>
|
|
108
|
+
|
|
109
|
+
<div
|
|
110
|
+
className="fill-white w-[14px] h-[14px] cursor-pointer p-[14px] leading-[0px]"
|
|
111
|
+
onClick={() => closeSnackbar(snackbarKey)}
|
|
112
|
+
>
|
|
113
|
+
<CloseIcon />
|
|
114
|
+
</div>
|
|
115
|
+
</div>
|
|
116
|
+
),
|
|
117
|
+
animation: () => <Spinner className="w-4 h-4" />,
|
|
118
|
+
}}
|
|
119
|
+
/>,
|
|
120
|
+
{ persist: true, key: snackbarKey },
|
|
121
|
+
);
|
|
122
|
+
setTxHash("");
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}, [
|
|
127
|
+
loading,
|
|
128
|
+
currentNetwork,
|
|
129
|
+
txHash,
|
|
130
|
+
txError,
|
|
131
|
+
enqueueSnackbar,
|
|
132
|
+
closeSnackbar,
|
|
133
|
+
chainId,
|
|
134
|
+
isTxModalOpen,
|
|
135
|
+
supportedChains,
|
|
136
|
+
]);
|
|
137
|
+
|
|
138
|
+
const getTxExplorerUrl = useCallback(
|
|
139
|
+
(txHash: string, txExplorer: string, txReceipt?: ContractReceipt) => {
|
|
140
|
+
const defaultUrl = `${txExplorer}/${txHash}`;
|
|
141
|
+
if (!txReceipt) {
|
|
142
|
+
return defaultUrl;
|
|
143
|
+
} else {
|
|
144
|
+
const msgSendEvent = txReceipt.events?.find(
|
|
145
|
+
(event) => event.event === "MessageSent",
|
|
146
|
+
);
|
|
147
|
+
const messageId = msgSendEvent?.args?.messageId;
|
|
148
|
+
if (messageId) {
|
|
149
|
+
return `${CCIP_EXPLORER}${messageId}`;
|
|
150
|
+
} else {
|
|
151
|
+
return defaultUrl;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
},
|
|
155
|
+
[],
|
|
156
|
+
);
|
|
157
|
+
|
|
158
|
+
const addUILogicToHandlersEVM = useCallback(
|
|
159
|
+
(
|
|
160
|
+
currentKey: number,
|
|
161
|
+
handlers?: EvmHandlers,
|
|
162
|
+
showDefaultSuccessMessage = true,
|
|
163
|
+
): EvmHandlers => {
|
|
164
|
+
const newHandlers: EvmHandlers = { ...handlers };
|
|
165
|
+
|
|
166
|
+
newHandlers.onConfirm = (tx) => {
|
|
167
|
+
if (txKey.current === currentKey) {
|
|
168
|
+
if (handlers?.onConfirm) {
|
|
169
|
+
handlers?.onConfirm(tx);
|
|
170
|
+
}
|
|
171
|
+
setTxHash(tx?.hash || "");
|
|
172
|
+
setTxStatus((prevStatus) => prevStatus + 1);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
|
|
176
|
+
newHandlers.onSuccess = (result) => {
|
|
177
|
+
closeSnackbar(result?.transactionHash);
|
|
178
|
+
if (txKey.current === currentKey) {
|
|
179
|
+
setTxReceipt(result);
|
|
180
|
+
if (!isTxModalOpenRef.current) {
|
|
181
|
+
if (showDefaultSuccessMessage) {
|
|
182
|
+
const snackbarKey = `${txHash}-success`;
|
|
183
|
+
enqueueSnackbar(
|
|
184
|
+
<SnackMessage
|
|
185
|
+
message={{
|
|
186
|
+
text: `Transaction mined successfully!`,
|
|
187
|
+
variant: "success",
|
|
188
|
+
action: () => (
|
|
189
|
+
<div className="flex items-center flex-nowrap">
|
|
190
|
+
<Button
|
|
191
|
+
type="button"
|
|
192
|
+
onClick={() => {
|
|
193
|
+
window.open(
|
|
194
|
+
getTxExplorerUrl(
|
|
195
|
+
txHash,
|
|
196
|
+
supportedChains.find(
|
|
197
|
+
(chain) =>
|
|
198
|
+
chain.chainId === chainId?.toString() &&
|
|
199
|
+
chain.ecosystem === currentNetwork,
|
|
200
|
+
)?.transactionExplorer || "",
|
|
201
|
+
result,
|
|
202
|
+
),
|
|
203
|
+
);
|
|
204
|
+
}}
|
|
205
|
+
>
|
|
206
|
+
View
|
|
207
|
+
</Button>
|
|
208
|
+
|
|
209
|
+
<div
|
|
210
|
+
className="fill-white w-[14px] h-[14px] cursor-pointer p-[14px] leading-[0px]"
|
|
211
|
+
onClick={() => closeSnackbar(snackbarKey)}
|
|
212
|
+
>
|
|
213
|
+
<CloseIcon />
|
|
214
|
+
</div>
|
|
215
|
+
</div>
|
|
216
|
+
),
|
|
217
|
+
}}
|
|
218
|
+
/>,
|
|
219
|
+
{ persist: false, key: snackbarKey },
|
|
220
|
+
);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (handlers?.onSuccess) {
|
|
224
|
+
handlers?.onSuccess(result);
|
|
225
|
+
}
|
|
226
|
+
setTxError("");
|
|
227
|
+
setLoading(false);
|
|
228
|
+
}
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
newHandlers.onError = (err) => {
|
|
232
|
+
if (txKey.current === currentKey) {
|
|
233
|
+
setTxError(parseWeb3Error(err));
|
|
234
|
+
handlers?.onError?.(err);
|
|
235
|
+
setLoading(false);
|
|
236
|
+
}
|
|
237
|
+
};
|
|
238
|
+
|
|
239
|
+
return newHandlers;
|
|
240
|
+
},
|
|
241
|
+
[
|
|
242
|
+
closeSnackbar,
|
|
243
|
+
txHash,
|
|
244
|
+
enqueueSnackbar,
|
|
245
|
+
getTxExplorerUrl,
|
|
246
|
+
supportedChains,
|
|
247
|
+
chainId,
|
|
248
|
+
currentNetwork,
|
|
249
|
+
],
|
|
250
|
+
);
|
|
251
|
+
|
|
252
|
+
const enqueueTransaction = useCallback(
|
|
253
|
+
({
|
|
254
|
+
executeTransaction,
|
|
255
|
+
txStatus,
|
|
256
|
+
txMsg = "",
|
|
257
|
+
handlers,
|
|
258
|
+
network = Ecosystem.EVM,
|
|
259
|
+
showDefaultSuccessMessage = true,
|
|
260
|
+
}: EnqueueTxProps) => {
|
|
261
|
+
const currentKey = Date.now();
|
|
262
|
+
|
|
263
|
+
let newHandlers = handlers;
|
|
264
|
+
if (network === Ecosystem.EVM) {
|
|
265
|
+
newHandlers = addUILogicToHandlersEVM(
|
|
266
|
+
currentKey,
|
|
267
|
+
handlers as EvmHandlers,
|
|
268
|
+
showDefaultSuccessMessage,
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// extracted tx logic
|
|
273
|
+
nativeEnqueueTransaction({
|
|
274
|
+
executeTransaction,
|
|
275
|
+
txStatus,
|
|
276
|
+
handlers: newHandlers,
|
|
277
|
+
network,
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
txKey.current = currentKey;
|
|
281
|
+
isTxModalOpenRef.current = true;
|
|
282
|
+
setTxHash("");
|
|
283
|
+
setTxError("");
|
|
284
|
+
setTxMsg(txMsg);
|
|
285
|
+
setTxStatus(txStatus);
|
|
286
|
+
setTxReceipt(undefined);
|
|
287
|
+
setLoading(true);
|
|
288
|
+
setCurrentNetwork(network);
|
|
289
|
+
setTxModalOpen(true);
|
|
290
|
+
|
|
291
|
+
return currentKey;
|
|
292
|
+
},
|
|
293
|
+
[addUILogicToHandlersEVM, nativeEnqueueTransaction],
|
|
294
|
+
);
|
|
295
|
+
|
|
296
|
+
const txExplorerUrl = useMemo(
|
|
297
|
+
() =>
|
|
298
|
+
getTxExplorerUrl(
|
|
299
|
+
txHash,
|
|
300
|
+
supportedChains.find(
|
|
301
|
+
(chain) =>
|
|
302
|
+
chain.chainId === chainId?.toString() &&
|
|
303
|
+
chain.ecosystem === currentNetwork,
|
|
304
|
+
)?.transactionExplorer || "",
|
|
305
|
+
txReceipt,
|
|
306
|
+
),
|
|
307
|
+
[
|
|
308
|
+
chainId,
|
|
309
|
+
currentNetwork,
|
|
310
|
+
getTxExplorerUrl,
|
|
311
|
+
supportedChains,
|
|
312
|
+
txHash,
|
|
313
|
+
txReceipt,
|
|
314
|
+
],
|
|
315
|
+
);
|
|
316
|
+
|
|
317
|
+
const setModalOpen = useCallback((flag: boolean) => {
|
|
318
|
+
setTxModalOpen(flag);
|
|
319
|
+
isTxModalOpenRef.current = flag;
|
|
320
|
+
}, []);
|
|
321
|
+
|
|
322
|
+
const modalType: ModalType = useMemo(
|
|
323
|
+
() =>
|
|
324
|
+
txError
|
|
325
|
+
? "error"
|
|
326
|
+
: txStatus === TxStatus.COMPLETED
|
|
327
|
+
? "success"
|
|
328
|
+
: "neutral",
|
|
329
|
+
[txError, txStatus],
|
|
330
|
+
);
|
|
331
|
+
|
|
332
|
+
const state = useMemo<TxUIWrapperState>(
|
|
333
|
+
() => ({
|
|
334
|
+
enqueueTransaction,
|
|
335
|
+
closeTransactionModal,
|
|
336
|
+
isTxModalOpen,
|
|
337
|
+
setTxModalOpen,
|
|
338
|
+
loading,
|
|
339
|
+
txStatus,
|
|
340
|
+
setTxStatus,
|
|
341
|
+
txExplorerUrl,
|
|
342
|
+
txHash,
|
|
343
|
+
txError,
|
|
344
|
+
txMsg,
|
|
345
|
+
setTxMsg,
|
|
346
|
+
setTxError,
|
|
347
|
+
txNeedsApproval,
|
|
348
|
+
setTxNeedsApproval,
|
|
349
|
+
}),
|
|
350
|
+
[
|
|
351
|
+
enqueueTransaction,
|
|
352
|
+
isTxModalOpen,
|
|
353
|
+
loading,
|
|
354
|
+
txStatus,
|
|
355
|
+
txExplorerUrl,
|
|
356
|
+
txHash,
|
|
357
|
+
txError,
|
|
358
|
+
txMsg,
|
|
359
|
+
txNeedsApproval,
|
|
360
|
+
],
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
const closeTxModal = useCallback(() => {
|
|
364
|
+
setTxError("");
|
|
365
|
+
setSrcValue("");
|
|
366
|
+
setModalOpen(false);
|
|
367
|
+
}, [setTxError, setSrcValue, setModalOpen]);
|
|
368
|
+
|
|
369
|
+
return (
|
|
370
|
+
<TxUIWrapperContext.Provider value={state}>
|
|
371
|
+
{isTxModalOpen && (
|
|
372
|
+
<Modal
|
|
373
|
+
id="tx_modal"
|
|
374
|
+
className="max-w-x_modal"
|
|
375
|
+
type={modalType}
|
|
376
|
+
onCloseClick={closeTxModal}
|
|
377
|
+
>
|
|
378
|
+
<ConfirmationView onCloseClick={closeTxModal} />
|
|
379
|
+
</Modal>
|
|
380
|
+
)}
|
|
381
|
+
{children}
|
|
382
|
+
</TxUIWrapperContext.Provider>
|
|
383
|
+
);
|
|
384
|
+
};
|
|
385
|
+
|
|
386
|
+
export function useTxUIWrapper(): TxUIWrapperState {
|
|
387
|
+
return useContext(TxUIWrapperContext);
|
|
388
|
+
}
|