@swapkit/helpers 1.0.0-rc.9 → 1.0.0-rc.90
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/index.js +2065 -0
- package/dist/index.js.map +28 -0
- package/package.json +24 -37
- package/src/helpers/__tests__/asset.test.ts +126 -108
- package/src/helpers/__tests__/memo.test.ts +52 -40
- package/src/helpers/__tests__/others.test.ts +42 -37
- package/src/helpers/__tests__/validators.test.ts +24 -0
- package/src/helpers/asset.ts +119 -87
- package/src/helpers/liquidity.ts +50 -43
- package/src/helpers/memo.ts +31 -28
- package/src/helpers/others.ts +29 -12
- package/src/helpers/validators.ts +15 -6
- package/src/helpers/web3wallets.ts +178 -0
- package/src/index.ts +13 -9
- package/src/modules/__tests__/assetValue.test.ts +325 -116
- package/src/modules/__tests__/bigIntArithmetics.test.ts +30 -0
- package/src/modules/__tests__/swapKitNumber.test.ts +306 -183
- package/src/modules/assetValue.ts +216 -152
- package/src/modules/bigIntArithmetics.ts +214 -147
- package/src/modules/requestClient.ts +29 -0
- package/src/modules/swapKitError.ts +32 -5
- package/src/modules/swapKitNumber.ts +8 -1
- package/src/types/abis/erc20.ts +99 -0
- package/src/types/abis/tcEthVault.ts +496 -0
- package/src/types/chains.ts +220 -0
- package/src/types/commonTypes.ts +119 -0
- package/src/types/derivationPath.ts +56 -0
- package/src/types/index.ts +10 -0
- package/src/types/network.ts +43 -0
- package/src/types/sdk.ts +11 -0
- package/src/types/tokens.ts +30 -0
- package/src/types/transactions.ts +45 -0
- package/src/types/wallet.ts +39 -0
- package/LICENSE +0 -201
- package/dist/index.cjs +0 -1
- package/dist/index.d.ts +0 -354
- package/dist/index.es.js +0 -1054
- package/src/helpers/request.ts +0 -16
|
@@ -1,17 +1,26 @@
|
|
|
1
|
-
import { Chain } from
|
|
1
|
+
import { Chain } from "../types/chains";
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
// Backward compatibility
|
|
4
|
+
const supportedChains = [...Object.values(Chain), "TERRA"];
|
|
4
5
|
|
|
5
|
-
export
|
|
6
|
+
export function validateIdentifier(identifier = "") {
|
|
6
7
|
const uppercasedIdentifier = identifier.toUpperCase();
|
|
7
8
|
|
|
8
|
-
const [chain] = uppercasedIdentifier.split(
|
|
9
|
+
const [chain] = uppercasedIdentifier.split(".") as [Chain, string];
|
|
9
10
|
if (supportedChains.includes(chain)) return true;
|
|
10
11
|
|
|
11
|
-
const [synthChain] = uppercasedIdentifier.split(
|
|
12
|
+
const [synthChain] = uppercasedIdentifier.split("/") as [Chain, string];
|
|
12
13
|
if (supportedChains.includes(synthChain)) return true;
|
|
13
14
|
|
|
14
15
|
throw new Error(
|
|
15
16
|
`Invalid identifier: ${identifier}. Expected format: <Chain>.<Ticker> or <Chain>.<Ticker>-<ContractAddress>`,
|
|
16
17
|
);
|
|
17
|
-
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export function validateTNS(name: string) {
|
|
21
|
+
if (name.length > 30) return false;
|
|
22
|
+
|
|
23
|
+
const regex = /^[a-zA-Z0-9+_-]+$/g;
|
|
24
|
+
|
|
25
|
+
return !!name.match(regex);
|
|
26
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
import type { BrowserProvider } from "ethers";
|
|
2
|
+
import { ChainId, WalletOption } from "../types";
|
|
3
|
+
|
|
4
|
+
export type EthereumWindowProvider = BrowserProvider & {
|
|
5
|
+
__XDEFI?: boolean;
|
|
6
|
+
isBraveWallet?: boolean;
|
|
7
|
+
isCoinbaseWallet?: boolean;
|
|
8
|
+
isMetaMask?: boolean;
|
|
9
|
+
isOkxWallet?: boolean;
|
|
10
|
+
isTrust?: boolean;
|
|
11
|
+
on: (event: string, callback?: () => void) => void;
|
|
12
|
+
overrideIsMetaMask?: boolean;
|
|
13
|
+
request: <T = unknown>(args: { method: string; params?: unknown[] }) => Promise<T>;
|
|
14
|
+
selectedProvider?: EthereumWindowProvider;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
declare const window: {
|
|
18
|
+
ethereum: EthereumWindowProvider;
|
|
19
|
+
trustwallet: EthereumWindowProvider;
|
|
20
|
+
coinbaseWalletExtension: EthereumWindowProvider;
|
|
21
|
+
braveSolana: Todo;
|
|
22
|
+
} & Window;
|
|
23
|
+
|
|
24
|
+
// declare global {
|
|
25
|
+
// interface Window {
|
|
26
|
+
// ethereum: EthereumWindowProvider;
|
|
27
|
+
// trustwallet: EthereumWindowProvider;
|
|
28
|
+
// coinbaseWalletExtension: EthereumWindowProvider;
|
|
29
|
+
// braveSolana: Todo;
|
|
30
|
+
// }
|
|
31
|
+
// }
|
|
32
|
+
|
|
33
|
+
type NetworkParams = {
|
|
34
|
+
chainId: ChainId;
|
|
35
|
+
chainName: string;
|
|
36
|
+
nativeCurrency: {
|
|
37
|
+
name: string;
|
|
38
|
+
symbol: string;
|
|
39
|
+
decimals: number;
|
|
40
|
+
};
|
|
41
|
+
rpcUrls: string[];
|
|
42
|
+
blockExplorerUrls: string[];
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
type ProviderRequestParams = {
|
|
46
|
+
provider?: BrowserProvider;
|
|
47
|
+
params?: Todo;
|
|
48
|
+
method:
|
|
49
|
+
| "wallet_addEthereumChain"
|
|
50
|
+
| "wallet_switchEthereumChain"
|
|
51
|
+
| "eth_requestAccounts"
|
|
52
|
+
| "eth_sendTransaction"
|
|
53
|
+
| "eth_signTransaction";
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const methodsToWrap = [
|
|
57
|
+
"approve",
|
|
58
|
+
"approvedAmount",
|
|
59
|
+
"call",
|
|
60
|
+
"sendTransaction",
|
|
61
|
+
"transfer",
|
|
62
|
+
"getBalance",
|
|
63
|
+
"isApproved",
|
|
64
|
+
"approvedAmount",
|
|
65
|
+
"EIP1193SendTransaction",
|
|
66
|
+
"getFeeData",
|
|
67
|
+
"broadcastTransaction",
|
|
68
|
+
"estimateCall",
|
|
69
|
+
"estimateGasLimit",
|
|
70
|
+
"estimateGasPrices",
|
|
71
|
+
"createContractTxObject",
|
|
72
|
+
];
|
|
73
|
+
|
|
74
|
+
export const wrapMethodWithNetworkSwitch = <T extends (...args: Todo[]) => Todo>(
|
|
75
|
+
func: T,
|
|
76
|
+
provider: BrowserProvider,
|
|
77
|
+
chainId: ChainId,
|
|
78
|
+
) =>
|
|
79
|
+
(async (...args: Todo[]) => {
|
|
80
|
+
try {
|
|
81
|
+
await switchEVMWalletNetwork(provider, chainId);
|
|
82
|
+
} catch (error) {
|
|
83
|
+
throw new Error(`Failed to switch network: ${error}`);
|
|
84
|
+
}
|
|
85
|
+
return func(...args);
|
|
86
|
+
}) as unknown as T;
|
|
87
|
+
|
|
88
|
+
const providerRequest = ({ provider, params, method }: ProviderRequestParams) => {
|
|
89
|
+
if (!provider?.send) throw new Error("Provider not found");
|
|
90
|
+
|
|
91
|
+
const providerParams = params ? (Array.isArray(params) ? params : [params]) : [];
|
|
92
|
+
return provider.send(method, providerParams);
|
|
93
|
+
};
|
|
94
|
+
|
|
95
|
+
export const prepareNetworkSwitch = <T extends { [key: string]: (...args: Todo[]) => Todo }>({
|
|
96
|
+
toolbox,
|
|
97
|
+
chainId,
|
|
98
|
+
provider = window.ethereum,
|
|
99
|
+
}: {
|
|
100
|
+
toolbox: T;
|
|
101
|
+
chainId: ChainId;
|
|
102
|
+
provider?: BrowserProvider;
|
|
103
|
+
}) => {
|
|
104
|
+
const wrappedMethods = methodsToWrap.reduce((object, methodName) => {
|
|
105
|
+
if (!toolbox[methodName]) return object;
|
|
106
|
+
const method = toolbox[methodName];
|
|
107
|
+
|
|
108
|
+
if (typeof method !== "function") return object;
|
|
109
|
+
|
|
110
|
+
return {
|
|
111
|
+
// biome-ignore lint/performance/noAccumulatingSpread: This is a valid use case
|
|
112
|
+
...object,
|
|
113
|
+
[methodName]: wrapMethodWithNetworkSwitch<typeof method>(method, provider, chainId),
|
|
114
|
+
};
|
|
115
|
+
}, {});
|
|
116
|
+
|
|
117
|
+
return { ...toolbox, ...wrappedMethods };
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
export const addEVMWalletNetwork = (provider: BrowserProvider, networkParams: NetworkParams) =>
|
|
121
|
+
providerRequest({ provider, method: "wallet_addEthereumChain", params: [networkParams] });
|
|
122
|
+
|
|
123
|
+
export const switchEVMWalletNetwork = (provider: BrowserProvider, chainId = ChainId.EthereumHex) =>
|
|
124
|
+
providerRequest({ provider, method: "wallet_switchEthereumChain", params: [{ chainId }] });
|
|
125
|
+
|
|
126
|
+
export const addAccountsChangedCallback = (callback: () => void) => {
|
|
127
|
+
window.ethereum?.on("accountsChanged", () => callback());
|
|
128
|
+
// @ts-expect-error that should be implemented in xdefi and hooked up via swapkit core
|
|
129
|
+
window.xfi?.ethereum.on("accountsChanged", () => callback());
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
export const getETHDefaultWallet = () => {
|
|
133
|
+
const { isTrust, isBraveWallet, __XDEFI, overrideIsMetaMask, selectedProvider } =
|
|
134
|
+
window?.ethereum || {};
|
|
135
|
+
if (isTrust) return WalletOption.TRUSTWALLET_WEB;
|
|
136
|
+
if (isBraveWallet) return WalletOption.BRAVE;
|
|
137
|
+
if (overrideIsMetaMask && selectedProvider?.isCoinbaseWallet) return WalletOption.COINBASE_WEB;
|
|
138
|
+
if (__XDEFI) WalletOption.XDEFI;
|
|
139
|
+
return WalletOption.METAMASK;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export const isDetected = (walletOption: WalletOption) => {
|
|
143
|
+
return listWeb3EVMWallets().includes(walletOption);
|
|
144
|
+
};
|
|
145
|
+
|
|
146
|
+
const listWeb3EVMWallets = () => {
|
|
147
|
+
const metamaskEnabled = window?.ethereum && !window.ethereum?.isBraveWallet;
|
|
148
|
+
// @ts-ignore that should be implemented in xdefi and hooked up via swapkit core
|
|
149
|
+
const xdefiEnabled = window?.xfi || window?.ethereum?.__XDEFI;
|
|
150
|
+
const braveEnabled = window?.ethereum?.isBraveWallet;
|
|
151
|
+
const trustEnabled = window?.ethereum?.isTrust || window?.trustwallet;
|
|
152
|
+
const coinbaseEnabled =
|
|
153
|
+
(window?.ethereum?.overrideIsMetaMask &&
|
|
154
|
+
window?.ethereum?.selectedProvider?.isCoinbaseWallet) ||
|
|
155
|
+
window?.coinbaseWalletExtension;
|
|
156
|
+
|
|
157
|
+
const wallets = [];
|
|
158
|
+
if (metamaskEnabled) wallets.push(WalletOption.METAMASK);
|
|
159
|
+
if (xdefiEnabled) wallets.push(WalletOption.XDEFI);
|
|
160
|
+
if (braveEnabled) wallets.push(WalletOption.BRAVE);
|
|
161
|
+
if (trustEnabled) wallets.push(WalletOption.TRUSTWALLET_WEB);
|
|
162
|
+
if (coinbaseEnabled) wallets.push(WalletOption.COINBASE_WEB);
|
|
163
|
+
if (okxMobileEnabled()) wallets.push(WalletOption.OKX_MOBILE);
|
|
164
|
+
|
|
165
|
+
return wallets;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
export const okxMobileEnabled = () => {
|
|
169
|
+
const ua = navigator.userAgent;
|
|
170
|
+
const isIOS = /iphone|ipad|ipod|ios/i.test(ua);
|
|
171
|
+
const isAndroid = /android|XiaoMi|MiuiBrowser/i.test(ua);
|
|
172
|
+
const isMobile = isIOS || isAndroid;
|
|
173
|
+
const isOKApp = /OKApp/i.test(ua);
|
|
174
|
+
|
|
175
|
+
return isMobile && isOKApp;
|
|
176
|
+
};
|
|
177
|
+
|
|
178
|
+
export const isWeb3Detected = () => typeof window.ethereum !== "undefined";
|
package/src/index.ts
CHANGED
|
@@ -1,16 +1,20 @@
|
|
|
1
|
+
export * from "./types/index.ts";
|
|
2
|
+
|
|
1
3
|
/**
|
|
2
4
|
* Helpers
|
|
3
5
|
*/
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
6
|
+
export * from "./helpers/asset.ts";
|
|
7
|
+
export * from "./helpers/liquidity.ts";
|
|
8
|
+
export * from "./helpers/memo.ts";
|
|
9
|
+
export * from "./helpers/others.ts";
|
|
10
|
+
export * from "./helpers/validators.ts";
|
|
11
|
+
export * from "./helpers/web3wallets.ts";
|
|
9
12
|
|
|
10
13
|
/**
|
|
11
14
|
* Modules
|
|
12
15
|
*/
|
|
13
|
-
export * from
|
|
14
|
-
export * from
|
|
15
|
-
export * from
|
|
16
|
-
export * from
|
|
16
|
+
export * from "./modules/assetValue.ts";
|
|
17
|
+
export * from "./modules/bigIntArithmetics.ts";
|
|
18
|
+
export * from "./modules/requestClient.ts";
|
|
19
|
+
export * from "./modules/swapKitError.ts";
|
|
20
|
+
export * from "./modules/swapKitNumber.ts";
|