@swapkit/helpers 2.6.0 → 3.0.0-beta.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/dist/api/index.cjs +3 -0
- package/dist/api/index.cjs.map +16 -0
- package/dist/api/index.js +3 -0
- package/dist/api/index.js.map +16 -0
- package/dist/chunk-1gwdyyeh.js +4 -0
- package/dist/chunk-1gwdyyeh.js.map +10 -0
- package/dist/chunk-2bhvezjh.js +4 -0
- package/dist/chunk-2bhvezjh.js.map +10 -0
- package/dist/chunk-3ptwhtpd.js +3 -0
- package/dist/chunk-3ptwhtpd.js.map +10 -0
- package/dist/chunk-3wnfcm30.js +4 -0
- package/dist/chunk-3wnfcm30.js.map +9 -0
- package/dist/chunk-40epvqbm.js +3 -0
- package/dist/chunk-40epvqbm.js.map +10 -0
- package/dist/chunk-49nsjaay.js +3 -0
- package/dist/chunk-49nsjaay.js.map +10 -0
- package/dist/chunk-4b66tryh.js +4 -0
- package/dist/chunk-4b66tryh.js.map +10 -0
- package/dist/chunk-4kzjrrjv.js +3 -0
- package/dist/chunk-4kzjrrjv.js.map +10 -0
- package/dist/chunk-5m49s8z8.js +4 -0
- package/dist/chunk-5m49s8z8.js.map +10 -0
- package/dist/chunk-9b6y7r75.js +4 -0
- package/dist/chunk-9b6y7r75.js.map +10 -0
- package/dist/chunk-9zcbvqq2.js +3 -0
- package/dist/chunk-9zcbvqq2.js.map +10 -0
- package/dist/chunk-a0bpx1q3.js +4 -0
- package/dist/chunk-a0bpx1q3.js.map +10 -0
- package/dist/chunk-a0fxkkfh.js +4 -0
- package/dist/chunk-a0fxkkfh.js.map +10 -0
- package/dist/chunk-a6bjd9ar.js +3 -0
- package/dist/chunk-a6bjd9ar.js.map +10 -0
- package/dist/chunk-bg6pz5qh.js +3 -0
- package/dist/chunk-bg6pz5qh.js.map +10 -0
- package/dist/chunk-cpdcnp2x.js +4 -0
- package/dist/chunk-cpdcnp2x.js.map +10 -0
- package/dist/chunk-fazw0jvt.js +3 -0
- package/dist/chunk-fazw0jvt.js.map +9 -0
- package/dist/chunk-fr86y3rx.js +4 -0
- package/dist/chunk-fr86y3rx.js.map +10 -0
- package/dist/chunk-ftdtdkhk.js +3 -0
- package/dist/chunk-ftdtdkhk.js.map +10 -0
- package/dist/chunk-gjeaw024.js +4 -0
- package/dist/chunk-gjeaw024.js.map +10 -0
- package/dist/chunk-hdcdd2cf.js +4 -0
- package/dist/chunk-hdcdd2cf.js.map +10 -0
- package/dist/chunk-q2pb6ggs.js +3 -0
- package/dist/chunk-q2pb6ggs.js.map +10 -0
- package/dist/chunk-t9s9811k.js +3 -0
- package/dist/chunk-t9s9811k.js.map +10 -0
- package/dist/chunk-v4dvhh90.js +4 -0
- package/dist/chunk-v4dvhh90.js.map +10 -0
- package/dist/chunk-vzptz52h.js +4 -0
- package/dist/chunk-vzptz52h.js.map +10 -0
- package/dist/chunk-wyr5d8ad.js +3 -0
- package/dist/chunk-wyr5d8ad.js.map +10 -0
- package/dist/chunk-x0jsq6ag.js +3 -0
- package/dist/chunk-x0jsq6ag.js.map +10 -0
- package/dist/chunk-x2fe9scs.js +3 -0
- package/dist/chunk-x2fe9scs.js.map +10 -0
- package/dist/chunk-xm5jkehh.js +4 -0
- package/dist/chunk-xm5jkehh.js.map +10 -0
- package/dist/chunk-xzb1nshn.js +3 -0
- package/dist/chunk-xzb1nshn.js.map +10 -0
- package/dist/contracts/index.cjs +3 -0
- package/dist/contracts/index.cjs.map +38 -0
- package/dist/contracts/index.js +3 -0
- package/dist/contracts/index.js.map +38 -0
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +30 -0
- package/dist/index.js +3 -3
- package/dist/index.js.map +23 -23
- package/dist/tokens/index.cjs +3 -0
- package/dist/tokens/index.cjs.map +10 -0
- package/dist/tokens/index.js +3 -0
- package/dist/tokens/index.js.map +10 -0
- package/package.json +35 -16
- package/src/api/index.ts +19 -0
- package/src/api/microgard/endpoints.ts +83 -0
- package/src/api/microgard/types.ts +60 -0
- package/src/api/midgard/endpoints.ts +154 -0
- package/src/api/midgard/types.ts +155 -0
- package/src/api/swapkitApi/endpoints.ts +161 -0
- package/src/api/swapkitApi/types.ts +576 -0
- package/src/api/thornode/endpoints.ts +78 -0
- package/src/api/thornode/types.ts +241 -0
- package/src/api/thorswapStatic/endpoints.ts +46 -0
- package/src/api/thorswapStatic/types.ts +18 -0
- package/src/contracts/abis/avaxGeneric.ts +92 -0
- package/src/contracts/abis/avaxWoofi.ts +145 -0
- package/src/contracts/abis/bscGeneric.ts +106 -0
- package/src/contracts/abis/chainflipGateway.ts +330 -0
- package/src/contracts/abis/erc20.ts +99 -0
- package/src/contracts/abis/ethGeneric.ts +92 -0
- package/src/contracts/abis/mayaEvmVaults.ts +331 -0
- package/src/contracts/abis/pancakeV2.ts +145 -0
- package/src/contracts/abis/pangolin.ts +120 -0
- package/src/contracts/abis/sushiswap.ts +120 -0
- package/src/contracts/abis/tcEthVault.ts +650 -0
- package/src/contracts/abis/traderJoe.ts +120 -0
- package/src/contracts/abis/uniswapV2.ts +120 -0
- package/src/contracts/abis/uniswapV2Leg.ts +128 -0
- package/src/contracts/abis/uniswapV3_100.ts +128 -0
- package/src/contracts/abis/uniswapV3_10000.ts +128 -0
- package/src/contracts/abis/uniswapV3_3000.ts +128 -0
- package/src/contracts/abis/uniswapV3_500.ts +128 -0
- package/src/contracts/index.ts +95 -0
- package/src/contracts/routers/index.ts +58 -0
- package/src/contracts/routers/kyber.ts +402 -0
- package/src/contracts/routers/oneinch.ts +2188 -0
- package/src/contracts/routers/pancakeswap.ts +340 -0
- package/src/contracts/routers/pangolin.ts +340 -0
- package/src/contracts/routers/sushiswap.ts +340 -0
- package/src/contracts/routers/traderJoe.ts +340 -0
- package/src/contracts/routers/uniswapv2.ts +340 -0
- package/src/contracts/routers/uniswapv3.ts +254 -0
- package/src/contracts/routers/woofi.ts +171 -0
- package/src/index.ts +19 -15
- package/src/modules/__tests__/assetValue.test.ts +50 -24
- package/src/modules/__tests__/swapKitConfig.test.ts +37 -0
- package/src/modules/assetValue.ts +82 -141
- package/src/modules/bigIntArithmetics.ts +6 -2
- package/src/modules/requestClient.ts +53 -65
- package/src/modules/swapKitConfig.ts +134 -0
- package/src/modules/swapKitError.ts +19 -14
- package/src/tokens/index.ts +15 -0
- package/src/tokens/lists/camelot_v3.ts +15920 -0
- package/src/tokens/lists/caviar_v1.ts +1694 -0
- package/src/tokens/lists/chainflip.ts +104 -0
- package/src/tokens/lists/index.ts +14 -0
- package/src/tokens/lists/jupiter.ts +29606 -0
- package/src/tokens/lists/mayachain.ts +513 -0
- package/src/tokens/lists/oneinch.ts +14238 -0
- package/src/tokens/lists/openocean_v2.ts +11514 -0
- package/src/tokens/lists/pancakeswap.ts +4296 -0
- package/src/tokens/lists/pangolin_v1.ts +175 -0
- package/src/tokens/lists/sushiswap_v2.ts +965 -0
- package/src/tokens/lists/thorchain.ts +669 -0
- package/src/tokens/lists/traderjoe_v2.ts +1384 -0
- package/src/tokens/lists/uniswap_v2.ts +5596 -0
- package/src/tokens/lists/uniswap_v3.ts +5946 -0
- package/src/types/chains.ts +48 -51
- package/src/types/commonTypes.ts +3 -90
- package/src/types/derivationPath.ts +11 -24
- package/src/types/index.ts +3 -4
- package/src/types/quotes.ts +0 -1
- package/src/types/radix.ts +2 -2
- package/src/types/sdk.ts +0 -40
- package/src/types/tokens.ts +14 -17
- package/src/types/wallet.ts +70 -33
- package/src/{helpers → utils}/__tests__/asset.test.ts +0 -27
- package/src/utils/asset.ts +235 -0
- package/src/{helpers → utils}/derivationPath.ts +31 -9
- package/src/{helpers → utils}/memo.ts +0 -82
- package/src/{helpers → utils}/others.ts +12 -5
- package/src/utils/plugin.ts +13 -0
- package/src/utils/tokens.ts +73 -0
- package/src/{helpers → utils}/validators.ts +7 -5
- package/src/{helpers/web3wallets.ts → utils/wallets.ts} +189 -145
- package/src/helpers/asset.ts +0 -324
- package/src/modules/walletUtils.ts +0 -30
- package/src/types/network.ts +0 -49
- /package/src/{helpers/__tests__/derivationPath.ts → utils/__tests__/derivationPath.test.ts} +0 -0
- /package/src/{helpers → utils}/__tests__/memo.test.ts +0 -0
- /package/src/{helpers → utils}/__tests__/others.test.ts +0 -0
- /package/src/{helpers → utils}/__tests__/validators.test.ts +0 -0
- /package/src/{helpers → utils}/liquidity.ts +0 -0
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
import { match } from "ts-pattern";
|
|
2
|
+
import type * as tokenLists from "../tokens/lists";
|
|
3
|
+
|
|
4
|
+
export type TokenLists = {
|
|
5
|
+
camelot: typeof tokenLists.CamelotV3List;
|
|
6
|
+
caviar: typeof tokenLists.CaviarV1List;
|
|
7
|
+
chainflip: typeof tokenLists.ChainflipList;
|
|
8
|
+
jupiter: typeof tokenLists.JupiterList;
|
|
9
|
+
mayachain: typeof tokenLists.MayaList;
|
|
10
|
+
oneinch: typeof tokenLists.OneInchList;
|
|
11
|
+
openocean: typeof tokenLists.OpenOceanV2List;
|
|
12
|
+
pancakeswap: typeof tokenLists.PancakeswapList;
|
|
13
|
+
pangolin: typeof tokenLists.PangolinList;
|
|
14
|
+
sushiswap: typeof tokenLists.SushiswapList;
|
|
15
|
+
thorchain: typeof tokenLists.ThorchainList;
|
|
16
|
+
traderjoe: typeof tokenLists.TraderjoeV2List;
|
|
17
|
+
uniswap: typeof tokenLists.UniswapV2List;
|
|
18
|
+
uniswapv3: typeof tokenLists.UniswapV3List;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type TokenListName = keyof TokenLists;
|
|
22
|
+
|
|
23
|
+
const defaultLists = [
|
|
24
|
+
"camelot",
|
|
25
|
+
"caviar",
|
|
26
|
+
"chainflip",
|
|
27
|
+
"jupiter",
|
|
28
|
+
"mayachain",
|
|
29
|
+
"oneinch",
|
|
30
|
+
"openocean",
|
|
31
|
+
"pancakeswap",
|
|
32
|
+
"pangolin",
|
|
33
|
+
"sushiswap",
|
|
34
|
+
"thorchain",
|
|
35
|
+
"traderjoe",
|
|
36
|
+
"uniswap",
|
|
37
|
+
"uniswapv3",
|
|
38
|
+
] as TokenListName[];
|
|
39
|
+
|
|
40
|
+
export async function loadTokenLists<T extends TokenListName[]>(pickedLists?: T) {
|
|
41
|
+
const listsToLoad = pickedLists || defaultLists;
|
|
42
|
+
const lists = {} as { [key in T[number]]: TokenLists[key] };
|
|
43
|
+
|
|
44
|
+
for (const list of listsToLoad) {
|
|
45
|
+
const tokenList = await loadTokenList(list);
|
|
46
|
+
|
|
47
|
+
// @ts-expect-error - It's fine to do this because we know the type of the list
|
|
48
|
+
lists[list] = tokenList;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return lists;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async function loadTokenList<T extends TokenListName>(listName: T): Promise<TokenLists[T]> {
|
|
55
|
+
const { list } = await match(listName as TokenListName)
|
|
56
|
+
.with("camelot", () => import("../tokens/lists/camelot_v3"))
|
|
57
|
+
.with("caviar", () => import("../tokens/lists/caviar_v1"))
|
|
58
|
+
.with("chainflip", () => import("../tokens/lists/chainflip"))
|
|
59
|
+
.with("jupiter", () => import("../tokens/lists/jupiter"))
|
|
60
|
+
.with("mayachain", () => import("../tokens/lists/mayachain"))
|
|
61
|
+
.with("oneinch", () => import("../tokens/lists/oneinch"))
|
|
62
|
+
.with("openocean", () => import("../tokens/lists/openocean_v2"))
|
|
63
|
+
.with("pancakeswap", () => import("../tokens/lists/pancakeswap"))
|
|
64
|
+
.with("pangolin", () => import("../tokens/lists/pangolin_v1"))
|
|
65
|
+
.with("sushiswap", () => import("../tokens/lists/sushiswap_v2"))
|
|
66
|
+
.with("thorchain", () => import("../tokens/lists/thorchain"))
|
|
67
|
+
.with("traderjoe", () => import("../tokens/lists/traderjoe_v2"))
|
|
68
|
+
.with("uniswap", () => import("../tokens/lists/uniswap_v2"))
|
|
69
|
+
.with("uniswapv3", () => import("../tokens/lists/uniswap_v3"))
|
|
70
|
+
.exhaustive();
|
|
71
|
+
|
|
72
|
+
return list as unknown as TokenLists[T];
|
|
73
|
+
}
|
|
@@ -1,17 +1,19 @@
|
|
|
1
|
+
import { SKConfig } from "../modules/swapKitConfig";
|
|
1
2
|
import { SwapKitError } from "../modules/swapKitError";
|
|
2
|
-
import { Chain } from "../types/chains";
|
|
3
|
+
import type { Chain } from "../types/chains";
|
|
3
4
|
|
|
4
5
|
// Backward compatibility
|
|
5
|
-
const supportedChains = [...
|
|
6
|
+
const supportedChains = ["TERRA", ...SKConfig.get("chains")];
|
|
6
7
|
|
|
7
8
|
export function validateIdentifier(identifier = "") {
|
|
8
9
|
const uppercasedIdentifier = identifier.toUpperCase();
|
|
9
10
|
|
|
10
|
-
const
|
|
11
|
-
const [chain] = uppercasedIdentifier.split(supportedThorchainSeperatorRegex) as [Chain, string];
|
|
12
|
-
|
|
11
|
+
const [chain] = uppercasedIdentifier.split(".") as [Chain, string];
|
|
13
12
|
if (supportedChains.includes(chain)) return true;
|
|
14
13
|
|
|
14
|
+
const [synthChain] = uppercasedIdentifier.split("/") as [Chain, string];
|
|
15
|
+
if (supportedChains.includes(synthChain)) return true;
|
|
16
|
+
|
|
15
17
|
throw new SwapKitError({
|
|
16
18
|
errorKey: "helpers_invalid_identifier",
|
|
17
19
|
info: {
|
|
@@ -1,27 +1,17 @@
|
|
|
1
|
-
import type {
|
|
1
|
+
import type { getEvmToolbox } from "@swapkit/toolboxes/evm";
|
|
2
|
+
import type { BrowserProvider, JsonRpcProvider } from "ethers";
|
|
2
3
|
import { SwapKitError } from "../modules/swapKitError";
|
|
3
4
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
type AddChainType,
|
|
6
|
+
type Chain,
|
|
7
|
+
ChainToHexChainId,
|
|
6
8
|
type EIP6963AnnounceProviderEvent,
|
|
7
9
|
type EIP6963Provider,
|
|
10
|
+
type EthereumWindowProvider,
|
|
11
|
+
type NetworkParams,
|
|
8
12
|
WalletOption,
|
|
9
13
|
} from "../types";
|
|
10
|
-
|
|
11
|
-
export type EthereumWindowProvider = BrowserProvider & {
|
|
12
|
-
__XDEFI?: boolean;
|
|
13
|
-
isBraveWallet?: boolean;
|
|
14
|
-
isCoinbaseWallet?: boolean;
|
|
15
|
-
isMetaMask?: boolean;
|
|
16
|
-
isOkxWallet?: boolean;
|
|
17
|
-
isKeepKeyWallet?: boolean;
|
|
18
|
-
isTrust?: boolean;
|
|
19
|
-
isTalisman?: boolean;
|
|
20
|
-
on: (event: string, callback?: () => void) => void;
|
|
21
|
-
overrideIsMetaMask?: boolean;
|
|
22
|
-
request: <T = unknown>(args: { method: string; params?: unknown[] }) => Promise<T>;
|
|
23
|
-
selectedProvider?: EthereumWindowProvider;
|
|
24
|
-
};
|
|
14
|
+
import { warnOnce } from "./others";
|
|
25
15
|
|
|
26
16
|
declare const window: {
|
|
27
17
|
ethereum: EthereumWindowProvider;
|
|
@@ -30,57 +20,96 @@ declare const window: {
|
|
|
30
20
|
braveSolana: any;
|
|
31
21
|
bitkeep?: { ethereum: EthereumWindowProvider };
|
|
32
22
|
xfi?: { ethereum: EthereumWindowProvider };
|
|
23
|
+
$onekey?: { ethereum: EthereumWindowProvider };
|
|
33
24
|
} & Window;
|
|
34
25
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
nativeCurrency: {
|
|
39
|
-
name: string;
|
|
40
|
-
symbol: string;
|
|
41
|
-
decimals: number;
|
|
42
|
-
};
|
|
43
|
-
rpcUrls: string[];
|
|
44
|
-
blockExplorerUrls: string[];
|
|
45
|
-
};
|
|
26
|
+
export function isWeb3Detected() {
|
|
27
|
+
return typeof window.ethereum !== "undefined";
|
|
28
|
+
}
|
|
46
29
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
30
|
+
export function isDetected(walletOption: WalletOption) {
|
|
31
|
+
return listWeb3EVMWallets().includes(walletOption);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function listWeb3EVMWallets() {
|
|
35
|
+
const metamaskEnabled = window?.ethereum && !window.ethereum?.isBraveWallet;
|
|
36
|
+
// @ts-ignore that should be implemented in ctrl and hooked up via swapkit core
|
|
37
|
+
const ctrlEnabled = window?.xfi || window?.ethereum?.__XDEFI;
|
|
38
|
+
const braveEnabled = window?.ethereum?.isBraveWallet;
|
|
39
|
+
const trustEnabled = window?.ethereum?.isTrust || window?.trustwallet;
|
|
40
|
+
const coinbaseEnabled =
|
|
41
|
+
(window?.ethereum?.overrideIsMetaMask &&
|
|
42
|
+
window?.ethereum?.selectedProvider?.isCoinbaseWallet) ||
|
|
43
|
+
window?.coinbaseWalletExtension;
|
|
44
|
+
const bitgetEnabled = window?.bitkeep?.ethereum;
|
|
45
|
+
const onekeyEnabled = window?.$onekey?.ethereum;
|
|
46
|
+
|
|
47
|
+
const wallets = [];
|
|
48
|
+
if (metamaskEnabled) wallets.push(WalletOption.METAMASK);
|
|
49
|
+
if (ctrlEnabled) wallets.push(WalletOption.CTRL);
|
|
50
|
+
if (braveEnabled) wallets.push(WalletOption.BRAVE);
|
|
51
|
+
if (trustEnabled) wallets.push(WalletOption.TRUSTWALLET_WEB);
|
|
52
|
+
if (coinbaseEnabled) wallets.push(WalletOption.COINBASE_WEB);
|
|
53
|
+
if (okxMobileEnabled()) wallets.push(WalletOption.OKX_MOBILE);
|
|
54
|
+
if (bitgetEnabled) wallets.push(WalletOption.BITGET);
|
|
55
|
+
if (onekeyEnabled) wallets.push(WalletOption.ONEKEY);
|
|
56
|
+
|
|
57
|
+
return wallets;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export async function switchEVMWalletNetwork(
|
|
61
|
+
provider: BrowserProvider,
|
|
62
|
+
chain: Chain,
|
|
63
|
+
networkParams?: NetworkParams,
|
|
64
|
+
) {
|
|
65
|
+
try {
|
|
66
|
+
await providerRequest({
|
|
67
|
+
provider,
|
|
68
|
+
method: "wallet_switchEthereumChain",
|
|
69
|
+
params: [{ chainId: ChainToHexChainId[chain] }],
|
|
70
|
+
});
|
|
71
|
+
} catch (_error) {
|
|
72
|
+
if (!networkParams) {
|
|
73
|
+
throw new Error("Failed to switch network, networkParams not provided");
|
|
74
|
+
}
|
|
75
|
+
await addEVMWalletNetwork(provider, networkParams);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function filterSupportedChains<T extends string[]>({
|
|
80
|
+
chains,
|
|
81
|
+
supportedChains,
|
|
82
|
+
walletType,
|
|
83
|
+
}: { chains: Chain[]; supportedChains: T; walletType?: WalletOption }) {
|
|
84
|
+
const supported = chains.filter((chain) => supportedChains.includes(chain));
|
|
85
|
+
|
|
86
|
+
if (supported.length === 0) {
|
|
87
|
+
throw new SwapKitError("wallet_chain_not_supported", {
|
|
88
|
+
wallet: walletType,
|
|
89
|
+
chain: chains.join(", "),
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const unsupported = chains.filter((chain) => !supportedChains.includes(chain));
|
|
94
|
+
|
|
95
|
+
warnOnce(
|
|
96
|
+
unsupported.length > 0,
|
|
97
|
+
`${walletType} wallet does not support the following chains: ${unsupported.join(
|
|
98
|
+
", ",
|
|
99
|
+
)}. These chains will be ignored.`,
|
|
100
|
+
);
|
|
101
|
+
|
|
102
|
+
return supported as T;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
export function wrapMethodWithNetworkSwitch<T extends (...args: any[]) => any>(
|
|
77
106
|
func: T,
|
|
78
107
|
provider: BrowserProvider,
|
|
79
|
-
|
|
80
|
-
)
|
|
108
|
+
chain: Chain,
|
|
109
|
+
) {
|
|
81
110
|
(async (...args: any[]) => {
|
|
82
111
|
try {
|
|
83
|
-
await switchEVMWalletNetwork(provider,
|
|
112
|
+
await switchEVMWalletNetwork(provider, chain);
|
|
84
113
|
} catch (error) {
|
|
85
114
|
throw new SwapKitError({
|
|
86
115
|
errorKey: "helpers_failed_to_switch_network",
|
|
@@ -89,110 +118,69 @@ export const wrapMethodWithNetworkSwitch = <T extends (...args: any[]) => any>(
|
|
|
89
118
|
}
|
|
90
119
|
return func(...args);
|
|
91
120
|
}) as unknown as T;
|
|
121
|
+
}
|
|
92
122
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
const providerParams = params ? (Array.isArray(params) ? params : [params]) : [];
|
|
99
|
-
return provider.send(method, providerParams);
|
|
100
|
-
};
|
|
101
|
-
|
|
102
|
-
export const prepareNetworkSwitch = <T extends { [key: string]: (...args: any[]) => any }>({
|
|
123
|
+
export function prepareNetworkSwitch<
|
|
124
|
+
T extends Awaited<ReturnType<typeof getEvmToolbox>>,
|
|
125
|
+
M extends keyof T,
|
|
126
|
+
>({
|
|
103
127
|
toolbox,
|
|
104
|
-
|
|
128
|
+
chain,
|
|
105
129
|
provider = window.ethereum,
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
130
|
+
methodNames = [],
|
|
131
|
+
}: { toolbox: T; chain: Chain; provider?: BrowserProvider | JsonRpcProvider; methodNames?: M[] }) {
|
|
132
|
+
const methodsToWrap = [
|
|
133
|
+
...methodNames,
|
|
134
|
+
"approve",
|
|
135
|
+
"approvedAmount",
|
|
136
|
+
"call",
|
|
137
|
+
"sendTransaction",
|
|
138
|
+
"transfer",
|
|
139
|
+
"isApproved",
|
|
140
|
+
"approvedAmount",
|
|
141
|
+
"EIP1193SendTransaction",
|
|
142
|
+
"getFeeData",
|
|
143
|
+
"broadcastTransaction",
|
|
144
|
+
"estimateCall",
|
|
145
|
+
"estimateGasLimit",
|
|
146
|
+
"estimateGasPrices",
|
|
147
|
+
"createContractTxObject",
|
|
148
|
+
] as M[];
|
|
111
149
|
const wrappedMethods = methodsToWrap.reduce((object, methodName) => {
|
|
112
150
|
if (!toolbox[methodName]) return object;
|
|
151
|
+
|
|
113
152
|
const method = toolbox[methodName];
|
|
114
153
|
|
|
115
154
|
if (typeof method !== "function") return object;
|
|
116
155
|
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
};
|
|
156
|
+
// @ts-expect-error
|
|
157
|
+
const wrappedMethod = wrapMethodWithNetworkSwitch(method, provider, chain);
|
|
158
|
+
|
|
159
|
+
// biome-ignore lint/performance/noAccumulatingSpread: valid use case
|
|
160
|
+
return { ...object, [methodName]: wrappedMethod };
|
|
122
161
|
}, {});
|
|
123
162
|
|
|
124
163
|
return { ...toolbox, ...wrappedMethods };
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
export const addEVMWalletNetwork = (provider: BrowserProvider, networkParams: NetworkParams) =>
|
|
128
|
-
providerRequest({ provider, method: "wallet_addEthereumChain", params: [networkParams] });
|
|
164
|
+
}
|
|
129
165
|
|
|
130
|
-
export
|
|
131
|
-
provider:
|
|
132
|
-
|
|
133
|
-
networkParams?: NetworkParams,
|
|
134
|
-
) => {
|
|
135
|
-
try {
|
|
136
|
-
providerRequest({ provider, method: "wallet_switchEthereumChain", params: [{ chainId }] });
|
|
137
|
-
} catch (_error) {
|
|
138
|
-
if (!networkParams) {
|
|
139
|
-
throw new Error("Failed to switch network, networkParams not provided");
|
|
140
|
-
}
|
|
141
|
-
addEVMWalletNetwork(provider, networkParams);
|
|
142
|
-
}
|
|
143
|
-
};
|
|
166
|
+
export function addEVMWalletNetwork(provider: BrowserProvider, networkParams: NetworkParams) {
|
|
167
|
+
return providerRequest({ provider, method: "wallet_addEthereumChain", params: [networkParams] });
|
|
168
|
+
}
|
|
144
169
|
|
|
145
|
-
export
|
|
170
|
+
export function addAccountsChangedCallback(callback: () => void) {
|
|
146
171
|
window.ethereum?.on("accountsChanged", () => callback());
|
|
147
172
|
window.xfi?.ethereum.on("accountsChanged", () => callback());
|
|
148
|
-
}
|
|
173
|
+
}
|
|
149
174
|
|
|
150
|
-
export
|
|
175
|
+
export function getETHDefaultWallet() {
|
|
151
176
|
const { isTrust, isBraveWallet, __XDEFI, overrideIsMetaMask, selectedProvider } =
|
|
152
177
|
window?.ethereum || {};
|
|
153
178
|
if (isTrust) return WalletOption.TRUSTWALLET_WEB;
|
|
154
179
|
if (isBraveWallet) return WalletOption.BRAVE;
|
|
155
180
|
if (overrideIsMetaMask && selectedProvider?.isCoinbaseWallet) return WalletOption.COINBASE_WEB;
|
|
156
181
|
if (__XDEFI) WalletOption.CTRL;
|
|
182
|
+
if (window?.$onekey?.ethereum) return WalletOption.ONEKEY;
|
|
157
183
|
return WalletOption.METAMASK;
|
|
158
|
-
};
|
|
159
|
-
|
|
160
|
-
export const isDetected = (walletOption: WalletOption) => {
|
|
161
|
-
return listWeb3EVMWallets().includes(walletOption);
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
export const listWeb3EVMWallets = () => {
|
|
165
|
-
const metamaskEnabled = window?.ethereum && !window.ethereum?.isBraveWallet;
|
|
166
|
-
// @ts-ignore that should be implemented in ctrl and hooked up via swapkit core
|
|
167
|
-
const ctrlEnabled = window?.xfi || window?.ethereum?.__XDEFI;
|
|
168
|
-
const braveEnabled = window?.ethereum?.isBraveWallet;
|
|
169
|
-
const trustEnabled = window?.ethereum?.isTrust || window?.trustwallet;
|
|
170
|
-
const coinbaseEnabled =
|
|
171
|
-
(window?.ethereum?.overrideIsMetaMask &&
|
|
172
|
-
window?.ethereum?.selectedProvider?.isCoinbaseWallet) ||
|
|
173
|
-
window?.coinbaseWalletExtension;
|
|
174
|
-
const bitgetEnabled = window?.bitkeep?.ethereum;
|
|
175
|
-
|
|
176
|
-
const wallets = [];
|
|
177
|
-
if (metamaskEnabled) wallets.push(WalletOption.METAMASK);
|
|
178
|
-
if (ctrlEnabled) wallets.push(WalletOption.CTRL);
|
|
179
|
-
if (braveEnabled) wallets.push(WalletOption.BRAVE);
|
|
180
|
-
if (trustEnabled) wallets.push(WalletOption.TRUSTWALLET_WEB);
|
|
181
|
-
if (coinbaseEnabled) wallets.push(WalletOption.COINBASE_WEB);
|
|
182
|
-
if (okxMobileEnabled()) wallets.push(WalletOption.OKX_MOBILE);
|
|
183
|
-
if (bitgetEnabled) wallets.push(WalletOption.BITGET);
|
|
184
|
-
|
|
185
|
-
return wallets;
|
|
186
|
-
};
|
|
187
|
-
|
|
188
|
-
export function pickEvmApiKey({
|
|
189
|
-
chain,
|
|
190
|
-
nonEthApiKey,
|
|
191
|
-
ethApiKey,
|
|
192
|
-
}: { chain: Chain; nonEthApiKey?: string; ethApiKey?: string }) {
|
|
193
|
-
const apiKey = chain === Chain.Ethereum ? ethApiKey : nonEthApiKey;
|
|
194
|
-
|
|
195
|
-
return apiKey;
|
|
196
184
|
}
|
|
197
185
|
|
|
198
186
|
export function getEIP6963Wallets() {
|
|
@@ -213,7 +201,7 @@ export function getEIP6963Wallets() {
|
|
|
213
201
|
return { providers, removeEIP6963EventListener };
|
|
214
202
|
}
|
|
215
203
|
|
|
216
|
-
export
|
|
204
|
+
export function okxMobileEnabled() {
|
|
217
205
|
const ua = navigator.userAgent;
|
|
218
206
|
const isIOS = /iphone|ipad|ipod|ios/i.test(ua);
|
|
219
207
|
const isAndroid = /android|XiaoMi|MiuiBrowser/i.test(ua);
|
|
@@ -221,6 +209,62 @@ export const okxMobileEnabled = () => {
|
|
|
221
209
|
const isOKApp = /OKApp/i.test(ua);
|
|
222
210
|
|
|
223
211
|
return isMobile && isOKApp;
|
|
224
|
-
}
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
export function createWallet<
|
|
215
|
+
ConnectParams extends any[],
|
|
216
|
+
SupportedChains extends Chain[],
|
|
217
|
+
const Name extends string,
|
|
218
|
+
WalletType extends WalletOption,
|
|
219
|
+
>({
|
|
220
|
+
connect,
|
|
221
|
+
name,
|
|
222
|
+
supportedChains,
|
|
223
|
+
walletType,
|
|
224
|
+
}: {
|
|
225
|
+
connect: (connectParams: {
|
|
226
|
+
addChain: AddChainType;
|
|
227
|
+
walletType: WalletType;
|
|
228
|
+
supportedChains: SupportedChains;
|
|
229
|
+
}) => (...params: ConnectParams) => Promise<boolean>;
|
|
230
|
+
name: Name;
|
|
231
|
+
supportedChains: SupportedChains;
|
|
232
|
+
walletType?: WalletType | string;
|
|
233
|
+
}) {
|
|
234
|
+
function connectWallet(connectParams: {
|
|
235
|
+
addChain: AddChainType;
|
|
236
|
+
}) {
|
|
237
|
+
return connect({ ...connectParams, walletType: walletType as WalletType, supportedChains });
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
return {
|
|
241
|
+
[name]: { supportedChains, connectWallet },
|
|
242
|
+
} as unknown as {
|
|
243
|
+
[key in Name]: {
|
|
244
|
+
connectWallet: typeof connectWallet;
|
|
245
|
+
supportedChains: SupportedChains;
|
|
246
|
+
};
|
|
247
|
+
};
|
|
248
|
+
}
|
|
225
249
|
|
|
226
|
-
|
|
250
|
+
function providerRequest({
|
|
251
|
+
provider,
|
|
252
|
+
params,
|
|
253
|
+
method,
|
|
254
|
+
}: {
|
|
255
|
+
provider?: BrowserProvider;
|
|
256
|
+
params?: any;
|
|
257
|
+
method:
|
|
258
|
+
| "wallet_addEthereumChain"
|
|
259
|
+
| "wallet_switchEthereumChain"
|
|
260
|
+
| "eth_requestAccounts"
|
|
261
|
+
| "eth_sendTransaction"
|
|
262
|
+
| "eth_signTransaction";
|
|
263
|
+
}) {
|
|
264
|
+
if (!provider?.send) {
|
|
265
|
+
throw new SwapKitError("helpers_not_found_provider");
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
const providerParams = params ? (Array.isArray(params) ? params : [params]) : [];
|
|
269
|
+
return provider.send(method, providerParams);
|
|
270
|
+
}
|