@swapkit/core 1.0.0-rc.14 → 1.0.0-rc.140

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.
Files changed (45) hide show
  1. package/dist/index.js +175 -0
  2. package/dist/index.js.map +13 -0
  3. package/package.json +33 -47
  4. package/src/__tests__/helpers.test.ts +53 -0
  5. package/src/aggregator/contracts/avaxGeneric.ts +50 -50
  6. package/src/aggregator/contracts/avaxWoofi.ts +80 -80
  7. package/src/aggregator/contracts/bscGeneric.ts +59 -59
  8. package/src/aggregator/contracts/ethGeneric.ts +50 -50
  9. package/src/aggregator/contracts/index.ts +30 -28
  10. package/src/aggregator/contracts/pancakeV2.ts +80 -80
  11. package/src/aggregator/contracts/pangolin.ts +65 -65
  12. package/src/aggregator/contracts/routers/index.ts +58 -0
  13. package/src/aggregator/contracts/routers/kyber.ts +402 -0
  14. package/src/aggregator/contracts/routers/oneinch.ts +2188 -0
  15. package/src/aggregator/contracts/routers/pancakeswap.ts +340 -0
  16. package/src/aggregator/contracts/routers/pangolin.ts +340 -0
  17. package/src/aggregator/contracts/routers/sushiswap.ts +340 -0
  18. package/src/aggregator/contracts/routers/traderJoe.ts +340 -0
  19. package/src/aggregator/contracts/routers/uniswapv2.ts +340 -0
  20. package/src/aggregator/contracts/routers/uniswapv3.ts +254 -0
  21. package/src/aggregator/contracts/routers/woofi.ts +171 -0
  22. package/src/aggregator/contracts/sushiswap.ts +65 -65
  23. package/src/aggregator/contracts/traderJoe.ts +65 -65
  24. package/src/aggregator/contracts/uniswapV2.ts +65 -65
  25. package/src/aggregator/contracts/uniswapV2Leg.ts +70 -70
  26. package/src/aggregator/contracts/uniswapV3_100.ts +70 -70
  27. package/src/aggregator/contracts/uniswapV3_10000.ts +70 -70
  28. package/src/aggregator/contracts/uniswapV3_3000.ts +70 -70
  29. package/src/aggregator/contracts/uniswapV3_500.ts +70 -70
  30. package/src/aggregator/getSwapParams.ts +12 -12
  31. package/src/client.ts +218 -0
  32. package/src/helpers/explorerUrls.ts +38 -0
  33. package/src/index.ts +4 -4
  34. package/src/types.ts +29 -0
  35. package/LICENSE +0 -201
  36. package/dist/index-9e36735e.cjs +0 -1
  37. package/dist/index-cf1865cd.js +0 -649
  38. package/dist/index.cjs +0 -2
  39. package/dist/index.d.ts +0 -216
  40. package/dist/index.es.js +0 -4157
  41. package/src/client/__tests__/helpers.test.ts +0 -69
  42. package/src/client/explorerUrls.ts +0 -62
  43. package/src/client/index.ts +0 -699
  44. package/src/client/thornode.ts +0 -31
  45. package/src/client/types.ts +0 -103
package/dist/index.js ADDED
@@ -0,0 +1,175 @@
1
+ // src/index.ts
2
+ export * from "@swapkit/api";
3
+ export * from "@swapkit/helpers";
4
+
5
+ // src/client.ts
6
+ import {
7
+ ApproveMode,
8
+ AssetValue,
9
+ EVMChains,
10
+ SwapKitError
11
+ } from "@swapkit/helpers";
12
+
13
+ // src/helpers/explorerUrls.ts
14
+ import {Chain, ChainToExplorerUrl} from "@swapkit/helpers";
15
+ function getExplorerTxUrl({ chain, txHash }) {
16
+ const baseUrl = ChainToExplorerUrl[chain];
17
+ switch (chain) {
18
+ case Chain.Binance:
19
+ case Chain.Maya:
20
+ case Chain.Kujira:
21
+ case Chain.Cosmos:
22
+ case Chain.THORChain:
23
+ return `${baseUrl}/tx/${txHash.startsWith("0x") ? txHash.slice(2) : txHash}`;
24
+ case Chain.Arbitrum:
25
+ case Chain.Avalanche:
26
+ case Chain.BinanceSmartChain:
27
+ case Chain.Ethereum:
28
+ case Chain.Optimism:
29
+ case Chain.Polkadot:
30
+ case Chain.Polygon:
31
+ return `${baseUrl}/tx/${txHash.startsWith("0x") ? txHash : `0x${txHash}`}`;
32
+ case Chain.Litecoin:
33
+ case Chain.Bitcoin:
34
+ case Chain.BitcoinCash:
35
+ case Chain.Dogecoin:
36
+ return `${baseUrl}/transaction/${txHash.toLowerCase()}`;
37
+ default:
38
+ throw new Error(`Unsupported chain: ${chain}`);
39
+ }
40
+ }
41
+ function getExplorerAddressUrl({ chain, address }) {
42
+ const baseUrl = ChainToExplorerUrl[chain];
43
+ return `${baseUrl}/address/${address}`;
44
+ }
45
+
46
+ // src/client.ts
47
+ function SwapKit({
48
+ apis = {},
49
+ config = {},
50
+ plugins,
51
+ rpcUrls = {},
52
+ stagenet = false,
53
+ wallets
54
+ }) {
55
+ const compatPlugins = Array.isArray(plugins) ? plugins.reduce((acc, pluginInterface) => {
56
+ const { name, plugin } = Object.values(pluginInterface)?.[0] || {};
57
+ acc[name] = plugin;
58
+ return acc;
59
+ }, {}) : plugins;
60
+ const compatWallets = Array.isArray(wallets) ? wallets.reduce((acc, wallet) => {
61
+ const [walletName, connectWallet] = Object.entries(wallet)?.[0] || {};
62
+ acc[walletName] = connectWallet;
63
+ return acc;
64
+ }, {}) : wallets;
65
+ const connectedWallets = {};
66
+ const availablePlugins = Object.entries(compatPlugins).reduce((acc, [pluginName, { plugin, config: config2 }]) => {
67
+ const methods = plugin({ wallets: connectedWallets, stagenet, config: config2 });
68
+ acc[pluginName] = methods;
69
+ return acc;
70
+ }, {});
71
+ const connectWalletMethods = Object.entries(compatWallets).reduce((acc, [walletName, wallet]) => {
72
+ const connectWallet = wallet({ addChain, config, apis, rpcUrls });
73
+ acc[walletName] = connectWallet;
74
+ return acc;
75
+ }, {});
76
+ function getSwapKitPlugin(pluginName) {
77
+ const plugin = availablePlugins[pluginName] || Object.values(availablePlugins)[0];
78
+ if (!plugin) {
79
+ throw new SwapKitError("core_plugin_not_found", "Could not find the requested plugin");
80
+ }
81
+ return plugin;
82
+ }
83
+ function addChain(connectWallet) {
84
+ connectedWallets[connectWallet.chain] = connectWallet;
85
+ }
86
+ function approve({
87
+ assetValue,
88
+ type = "checkOnly",
89
+ contractAddress: spenderAddress
90
+ }) {
91
+ const plugin = availablePlugins[spenderAddress];
92
+ if ("approve" in plugin) {
93
+ return plugin.approve({ assetValue, type, spenderAddress });
94
+ }
95
+ const { address, chain, isGasAsset, isSynthetic } = assetValue;
96
+ const isEVMChain = EVMChains.includes(chain);
97
+ const isNativeEVM = isEVMChain && isGasAsset;
98
+ if (isNativeEVM || !isEVMChain || isSynthetic) {
99
+ return Promise.resolve(type === "checkOnly" ? true : "approved");
100
+ }
101
+ const walletMethods = connectedWallets[chain];
102
+ const walletAction = type === "checkOnly" ? walletMethods?.isApproved : walletMethods?.approve;
103
+ if (!walletAction)
104
+ throw new SwapKitError("core_wallet_connection_not_found");
105
+ const from = getAddress(chain);
106
+ if (!(address && from && typeof spenderAddress === "string")) {
107
+ throw new SwapKitError("core_approve_asset_address_or_from_not_found");
108
+ }
109
+ return walletAction({
110
+ amount: assetValue.getBaseValue("bigint"),
111
+ assetAddress: address,
112
+ from,
113
+ spenderAddress
114
+ });
115
+ }
116
+ function getWallet(chain) {
117
+ return connectedWallets[chain];
118
+ }
119
+ function getAddress(chain) {
120
+ return getWallet(chain)?.address || "";
121
+ }
122
+ function validateAddress({ address, chain }) {
123
+ return getWallet(chain)?.validateAddress?.(address);
124
+ }
125
+ function getBalance(chain, refresh) {
126
+ if (refresh) {
127
+ return getWalletWithBalance(chain).then((wallet) => wallet.balance);
128
+ }
129
+ return getWallet(chain)?.balance || [];
130
+ }
131
+ async function getWalletWithBalance(chain, potentialScamFilter = true) {
132
+ const defaultBalance = [AssetValue.fromChainOrSignature(chain)];
133
+ const wallet = getWallet(chain);
134
+ if (!wallet) {
135
+ throw new SwapKitError("core_wallet_connection_not_found");
136
+ }
137
+ if ("getBalance" in wallet) {
138
+ const balance = await wallet.getBalance(wallet.address, potentialScamFilter);
139
+ wallet.balance = balance?.length ? balance : defaultBalance;
140
+ }
141
+ return wallet;
142
+ }
143
+ function approveAssetValue(assetValue, contractAddress) {
144
+ return approve({ assetValue, contractAddress, type: ApproveMode.Approve });
145
+ }
146
+ function isAssetValueApproved(assetValue, contractAddress) {
147
+ return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });
148
+ }
149
+ function swap({ pluginName, ...rest }) {
150
+ const plugin = getSwapKitPlugin(pluginName);
151
+ if ("swap" in plugin) {
152
+ return plugin.swap(rest);
153
+ }
154
+ throw new SwapKitError("core_plugin_swap_not_found");
155
+ }
156
+ return {
157
+ ...availablePlugins,
158
+ ...connectWalletMethods,
159
+ approveAssetValue,
160
+ getAddress,
161
+ getBalance,
162
+ getExplorerAddressUrl,
163
+ getExplorerTxUrl,
164
+ getWallet,
165
+ getWalletWithBalance,
166
+ isAssetValueApproved,
167
+ swap,
168
+ validateAddress
169
+ };
170
+ }
171
+ export {
172
+ SwapKit
173
+ };
174
+
175
+ //# debugId=B5580E1E21E220C364756e2164756e21
@@ -0,0 +1,13 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../src/index.ts", "../src/client.ts", "../src/helpers/explorerUrls.ts", "../src/client.ts"],
4
+ "sourcesContent": [
5
+ "export * from \"@swapkit/api\";\nexport * from \"@swapkit/helpers\";\n\nexport * from \"./client.ts\";\n",
6
+ "import {\n type AddChainWalletParams,\n ApproveMode,\n type ApproveReturnType,\n AssetValue,\n type Chain,\n type ConnectConfig,\n type EVMChain,\n EVMChains,\n SwapKitError,\n type SwapParams,\n type Wallet,\n} from \"@swapkit/helpers\";\nimport type { BaseEVMWallet } from \"@swapkit/toolbox-evm\";\n\nimport {\n getExplorerAddressUrl as getAddressUrl,\n getExplorerTxUrl as getTxUrl,\n} from \"./helpers/explorerUrls.ts\";\nimport type { Apis, SwapKitPluginInterface, SwapKitWallet } from \"./types.ts\";\n\nexport function SwapKit<\n Plugins extends { [key in string]: SwapKitPluginInterface<{ [key in string]: Todo }> },\n Wallets extends { [key in string]: SwapKitWallet<NotWorth[]> },\n>({\n apis = {},\n config = {},\n plugins,\n rpcUrls = {},\n stagenet = false,\n wallets,\n}: {\n apis?: Apis;\n config?: ConnectConfig;\n plugins: Plugins;\n rpcUrls?: { [key in Chain]?: string };\n stagenet?: boolean;\n wallets: Wallets;\n}) {\n type PluginName = keyof Plugins;\n\n /**\n * @REMOVE (V1)\n * Compatibility layer for plugins and wallets for easier migration and backwards compatibility\n */\n const compatPlugins: Plugins = Array.isArray(plugins)\n ? plugins.reduce((acc, pluginInterface) => {\n // @ts-ignore Ignore until we remove the compatibility layer\n const { name, plugin } = Object.values(pluginInterface)?.[0] || {};\n acc[name] = plugin;\n return acc;\n }, {})\n : plugins;\n const compatWallets: Wallets = Array.isArray(wallets)\n ? wallets.reduce((acc, wallet) => {\n // @ts-ignore Ignore until we remove the compatibility layer\n const [walletName, connectWallet] = Object.entries(wallet)?.[0] || {};\n acc[walletName] = connectWallet;\n return acc;\n }, {})\n : wallets;\n\n const connectedWallets = {} as Wallet;\n const availablePlugins = Object.entries(compatPlugins).reduce(\n (acc, [pluginName, { plugin, config }]) => {\n const methods = plugin({ wallets: connectedWallets, stagenet, config });\n\n // @ts-expect-error\n acc[pluginName] = methods;\n return acc;\n },\n {} as { [key in PluginName]: ReturnType<Plugins[key][\"plugin\"]> },\n );\n\n const connectWalletMethods = Object.entries(compatWallets).reduce(\n (acc, [walletName, wallet]) => {\n const connectWallet = wallet({ addChain, config, apis, rpcUrls });\n\n // @ts-expect-error\n acc[walletName] = connectWallet;\n return acc;\n },\n {} as { [key in keyof Wallets]: ReturnType<Wallets[key]> },\n );\n\n /**\n * @Private\n */\n function getSwapKitPlugin<T extends PluginName>(pluginName: T) {\n const plugin = availablePlugins[pluginName] || Object.values(availablePlugins)[0];\n\n if (!plugin) {\n throw new SwapKitError(\"core_plugin_not_found\", \"Could not find the requested plugin\");\n }\n\n return plugin;\n }\n\n function addChain<T extends Chain>(connectWallet: AddChainWalletParams<T>) {\n // @ts-expect-error: TODO\n connectedWallets[connectWallet.chain] = connectWallet;\n }\n\n // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>\n function approve<T extends ApproveMode>({\n assetValue,\n type = \"checkOnly\" as T,\n contractAddress: spenderAddress,\n }: {\n type: T;\n assetValue: AssetValue;\n contractAddress: string | PluginName;\n }) {\n const plugin = availablePlugins[spenderAddress];\n\n if (\"approve\" in plugin) {\n return plugin.approve({ assetValue, type, spenderAddress }) as ApproveReturnType<T>;\n }\n\n const { address, chain, isGasAsset, isSynthetic } = assetValue;\n const isEVMChain = EVMChains.includes(chain as EVMChain);\n const isNativeEVM = isEVMChain && isGasAsset;\n\n if (isNativeEVM || !isEVMChain || isSynthetic) {\n return Promise.resolve(type === \"checkOnly\" ? true : \"approved\") as ApproveReturnType<T>;\n }\n\n const walletMethods = connectedWallets[chain] as BaseEVMWallet;\n const walletAction = type === \"checkOnly\" ? walletMethods?.isApproved : walletMethods?.approve;\n if (!walletAction) throw new SwapKitError(\"core_wallet_connection_not_found\");\n\n const from = getAddress(chain);\n if (!(address && from && typeof spenderAddress === \"string\")) {\n throw new SwapKitError(\"core_approve_asset_address_or_from_not_found\");\n }\n\n return walletAction({\n amount: assetValue.getBaseValue(\"bigint\"),\n assetAddress: address,\n from,\n spenderAddress,\n }) as ApproveReturnType<T>;\n }\n\n /**\n * @Public\n */\n function getWallet<T extends Chain>(chain: T) {\n return connectedWallets[chain];\n }\n function getAddress<T extends Chain>(chain: T) {\n return getWallet(chain)?.address || \"\";\n }\n /**\n * TODO: Figure out validation without connecting to wallet\n */\n function validateAddress({ address, chain }: { address: string; chain: Chain }) {\n return getWallet(chain)?.validateAddress?.(address);\n }\n\n function getBalance<T extends Chain>(chain: T, refresh?: boolean) {\n if (refresh) {\n return getWalletWithBalance(chain).then((wallet) => wallet.balance);\n }\n\n return getWallet(chain)?.balance || [];\n }\n\n async function getWalletWithBalance<T extends Chain>(chain: T, potentialScamFilter = true) {\n const defaultBalance = [AssetValue.fromChainOrSignature(chain)];\n const wallet = getWallet(chain);\n\n if (!wallet) {\n throw new SwapKitError(\"core_wallet_connection_not_found\");\n }\n\n if (\"getBalance\" in wallet) {\n const balance = await wallet.getBalance(wallet.address, potentialScamFilter);\n wallet.balance = balance?.length ? balance : defaultBalance;\n }\n\n return wallet;\n }\n\n function approveAssetValue(assetValue: AssetValue, contractAddress: string | PluginName) {\n return approve({ assetValue, contractAddress, type: ApproveMode.Approve });\n }\n\n function isAssetValueApproved(assetValue: AssetValue, contractAddress: string | PluginName) {\n return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });\n }\n\n function swap<T extends PluginName>({ pluginName, ...rest }: SwapParams<T>) {\n const plugin = getSwapKitPlugin(pluginName);\n\n if (\"swap\" in plugin) {\n return plugin.swap(rest);\n }\n\n throw new SwapKitError(\"core_plugin_swap_not_found\");\n }\n\n return {\n ...availablePlugins,\n ...connectWalletMethods,\n\n approveAssetValue,\n getAddress,\n getBalance,\n getExplorerAddressUrl: getAddressUrl,\n getExplorerTxUrl: getTxUrl,\n getWallet,\n getWalletWithBalance,\n isAssetValueApproved,\n swap,\n validateAddress,\n };\n}\n",
7
+ "import { Chain, ChainToExplorerUrl } from \"@swapkit/helpers\";\n\nexport function getExplorerTxUrl({ chain, txHash }: { txHash: string; chain: Chain }) {\n const baseUrl = ChainToExplorerUrl[chain];\n\n switch (chain) {\n case Chain.Binance:\n case Chain.Maya:\n case Chain.Kujira:\n case Chain.Cosmos:\n case Chain.THORChain:\n return `${baseUrl}/tx/${txHash.startsWith(\"0x\") ? txHash.slice(2) : txHash}`;\n\n case Chain.Arbitrum:\n case Chain.Avalanche:\n case Chain.BinanceSmartChain:\n case Chain.Ethereum:\n case Chain.Optimism:\n case Chain.Polkadot:\n case Chain.Polygon:\n return `${baseUrl}/tx/${txHash.startsWith(\"0x\") ? txHash : `0x${txHash}`}`;\n\n case Chain.Litecoin:\n case Chain.Bitcoin:\n case Chain.BitcoinCash:\n case Chain.Dogecoin:\n return `${baseUrl}/transaction/${txHash.toLowerCase()}`;\n\n default:\n throw new Error(`Unsupported chain: ${chain}`);\n }\n}\n\nexport function getExplorerAddressUrl({ chain, address }: { address: string; chain: Chain }) {\n const baseUrl = ChainToExplorerUrl[chain];\n\n return `${baseUrl}/address/${address}`;\n}\n",
8
+ "import {\n type AddChainWalletParams,\n ApproveMode,\n type ApproveReturnType,\n AssetValue,\n type Chain,\n type ConnectConfig,\n type EVMChain,\n EVMChains,\n SwapKitError,\n type SwapParams,\n type Wallet,\n} from \"@swapkit/helpers\";\nimport type { BaseEVMWallet } from \"@swapkit/toolbox-evm\";\n\nimport {\n getExplorerAddressUrl as getAddressUrl,\n getExplorerTxUrl as getTxUrl,\n} from \"./helpers/explorerUrls.ts\";\nimport type { Apis, SwapKitPluginInterface, SwapKitWallet } from \"./types.ts\";\n\nexport function SwapKit<\n Plugins extends { [key in string]: SwapKitPluginInterface<{ [key in string]: Todo }> },\n Wallets extends { [key in string]: SwapKitWallet<NotWorth[]> },\n>({\n apis = {},\n config = {},\n plugins,\n rpcUrls = {},\n stagenet = false,\n wallets,\n}: {\n apis?: Apis;\n config?: ConnectConfig;\n plugins: Plugins;\n rpcUrls?: { [key in Chain]?: string };\n stagenet?: boolean;\n wallets: Wallets;\n}) {\n type PluginName = keyof Plugins;\n\n /**\n * @REMOVE (V1)\n * Compatibility layer for plugins and wallets for easier migration and backwards compatibility\n */\n const compatPlugins: Plugins = Array.isArray(plugins)\n ? plugins.reduce((acc, pluginInterface) => {\n // @ts-ignore Ignore until we remove the compatibility layer\n const { name, plugin } = Object.values(pluginInterface)?.[0] || {};\n acc[name] = plugin;\n return acc;\n }, {})\n : plugins;\n const compatWallets: Wallets = Array.isArray(wallets)\n ? wallets.reduce((acc, wallet) => {\n // @ts-ignore Ignore until we remove the compatibility layer\n const [walletName, connectWallet] = Object.entries(wallet)?.[0] || {};\n acc[walletName] = connectWallet;\n return acc;\n }, {})\n : wallets;\n\n const connectedWallets = {} as Wallet;\n const availablePlugins = Object.entries(compatPlugins).reduce(\n (acc, [pluginName, { plugin, config }]) => {\n const methods = plugin({ wallets: connectedWallets, stagenet, config });\n\n // @ts-expect-error\n acc[pluginName] = methods;\n return acc;\n },\n {} as { [key in PluginName]: ReturnType<Plugins[key][\"plugin\"]> },\n );\n\n const connectWalletMethods = Object.entries(compatWallets).reduce(\n (acc, [walletName, wallet]) => {\n const connectWallet = wallet({ addChain, config, apis, rpcUrls });\n\n // @ts-expect-error\n acc[walletName] = connectWallet;\n return acc;\n },\n {} as { [key in keyof Wallets]: ReturnType<Wallets[key]> },\n );\n\n /**\n * @Private\n */\n function getSwapKitPlugin<T extends PluginName>(pluginName: T) {\n const plugin = availablePlugins[pluginName] || Object.values(availablePlugins)[0];\n\n if (!plugin) {\n throw new SwapKitError(\"core_plugin_not_found\", \"Could not find the requested plugin\");\n }\n\n return plugin;\n }\n\n function addChain<T extends Chain>(connectWallet: AddChainWalletParams<T>) {\n // @ts-expect-error: TODO\n connectedWallets[connectWallet.chain] = connectWallet;\n }\n\n // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>\n function approve<T extends ApproveMode>({\n assetValue,\n type = \"checkOnly\" as T,\n contractAddress: spenderAddress,\n }: {\n type: T;\n assetValue: AssetValue;\n contractAddress: string | PluginName;\n }) {\n const plugin = availablePlugins[spenderAddress];\n\n if (\"approve\" in plugin) {\n return plugin.approve({ assetValue, type, spenderAddress }) as ApproveReturnType<T>;\n }\n\n const { address, chain, isGasAsset, isSynthetic } = assetValue;\n const isEVMChain = EVMChains.includes(chain as EVMChain);\n const isNativeEVM = isEVMChain && isGasAsset;\n\n if (isNativeEVM || !isEVMChain || isSynthetic) {\n return Promise.resolve(type === \"checkOnly\" ? true : \"approved\") as ApproveReturnType<T>;\n }\n\n const walletMethods = connectedWallets[chain] as BaseEVMWallet;\n const walletAction = type === \"checkOnly\" ? walletMethods?.isApproved : walletMethods?.approve;\n if (!walletAction) throw new SwapKitError(\"core_wallet_connection_not_found\");\n\n const from = getAddress(chain);\n if (!(address && from && typeof spenderAddress === \"string\")) {\n throw new SwapKitError(\"core_approve_asset_address_or_from_not_found\");\n }\n\n return walletAction({\n amount: assetValue.getBaseValue(\"bigint\"),\n assetAddress: address,\n from,\n spenderAddress,\n }) as ApproveReturnType<T>;\n }\n\n /**\n * @Public\n */\n function getWallet<T extends Chain>(chain: T) {\n return connectedWallets[chain];\n }\n function getAddress<T extends Chain>(chain: T) {\n return getWallet(chain)?.address || \"\";\n }\n /**\n * TODO: Figure out validation without connecting to wallet\n */\n function validateAddress({ address, chain }: { address: string; chain: Chain }) {\n return getWallet(chain)?.validateAddress?.(address);\n }\n\n function getBalance<T extends Chain>(chain: T, refresh?: boolean) {\n if (refresh) {\n return getWalletWithBalance(chain).then((wallet) => wallet.balance);\n }\n\n return getWallet(chain)?.balance || [];\n }\n\n async function getWalletWithBalance<T extends Chain>(chain: T, potentialScamFilter = true) {\n const defaultBalance = [AssetValue.fromChainOrSignature(chain)];\n const wallet = getWallet(chain);\n\n if (!wallet) {\n throw new SwapKitError(\"core_wallet_connection_not_found\");\n }\n\n if (\"getBalance\" in wallet) {\n const balance = await wallet.getBalance(wallet.address, potentialScamFilter);\n wallet.balance = balance?.length ? balance : defaultBalance;\n }\n\n return wallet;\n }\n\n function approveAssetValue(assetValue: AssetValue, contractAddress: string | PluginName) {\n return approve({ assetValue, contractAddress, type: ApproveMode.Approve });\n }\n\n function isAssetValueApproved(assetValue: AssetValue, contractAddress: string | PluginName) {\n return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });\n }\n\n function swap<T extends PluginName>({ pluginName, ...rest }: SwapParams<T>) {\n const plugin = getSwapKitPlugin(pluginName);\n\n if (\"swap\" in plugin) {\n return plugin.swap(rest);\n }\n\n throw new SwapKitError(\"core_plugin_swap_not_found\");\n }\n\n return {\n ...availablePlugins,\n ...connectWalletMethods,\n\n approveAssetValue,\n getAddress,\n getBalance,\n getExplorerAddressUrl: getAddressUrl,\n getExplorerTxUrl: getTxUrl,\n getWallet,\n getWalletWithBalance,\n isAssetValueApproved,\n swap,\n validateAddress,\n };\n}\n"
9
+ ],
10
+ "mappings": ";AAAA;AACA;;;ACDA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA;AAEO,SAAS,gBAAgB,GAAG,OAAO,UAA4C;AACpF,QAAM,UAAU,mBAAmB;AAEnC,UAAQ;AAAA,SACD,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AACT,aAAO,GAAG,cAAc,OAAO,WAAW,IAAI,IAAI,OAAO,MAAM,CAAC,IAAI;AAAA,SAEjE,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AACT,aAAO,GAAG,cAAc,OAAO,WAAW,IAAI,IAAI,SAAS,KAAK;AAAA,SAE7D,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AAAA,SACN,MAAM;AACT,aAAO,GAAG,uBAAuB,OAAO,YAAY;AAAA;AAGpD,YAAM,IAAI,MAAM,sBAAsB,OAAO;AAAA;AAAA;AAI5C,SAAS,qBAAqB,GAAG,OAAO,WAA8C;AAC3F,QAAM,UAAU,mBAAmB;AAEnC,SAAO,GAAG,mBAAmB;AAAA;",
11
+ "debugId": "B5580E1E21E220C364756e2164756e21",
12
+ "names": []
13
+ }
package/package.json CHANGED
@@ -1,40 +1,26 @@
1
1
  {
2
- "author": "swapkit-oss-team",
2
+ "author": "swapkit-oss",
3
3
  "dependencies": {
4
- "@swapkit/types": "1.0.0-rc.6",
5
- "@swapkit/helpers": "1.0.0-rc.9"
4
+ "@swapkit/api": "1.0.0-rc.62",
5
+ "@swapkit/helpers": "1.0.0-rc.96"
6
6
  },
7
- "description": "SwapKit Lib core",
7
+ "description": "SwapKit - Core",
8
8
  "devDependencies": {
9
- "@vitest/coverage-istanbul": "0.34.4",
10
- "ethers": "6.7.1",
11
- "vite": "4.4.9",
12
- "vite-plugin-top-level-await": "1.3.1",
13
- "vite-plugin-wasm": "3.2.2",
14
- "vitest": "0.34.4",
15
- "@internal/config": "0.0.0-internal.0",
16
- "@swapkit/api": "1.0.0-rc.6",
17
- "@swapkit/tokens": "1.0.0-rc.6",
18
- "@swapkit/toolbox-cosmos": "1.0.0-rc.11",
19
- "@swapkit/toolbox-evm": "1.0.0-rc.10",
20
- "@swapkit/toolbox-utxo": "1.0.0-rc.10"
9
+ "@swapkit/tokens": "1.0.0-rc.51",
10
+ "@swapkit/toolbox-cosmos": "1.0.0-rc.117",
11
+ "@swapkit/toolbox-evm": "1.0.0-rc.102",
12
+ "@swapkit/toolbox-substrate": "1.0.0-rc.31",
13
+ "@swapkit/toolbox-utxo": "1.0.0-rc.105",
14
+ "bun-types": "1.1.2"
21
15
  },
22
16
  "peerDependencies": {
23
- "@swapkit/api": "1.0.0-rc.6",
24
- "@swapkit/tokens": "1.0.0-rc.6",
25
- "@swapkit/toolbox-cosmos": "1.0.0-rc.11",
26
- "@swapkit/toolbox-evm": "1.0.0-rc.10",
27
- "@swapkit/toolbox-utxo": "1.0.0-rc.10"
28
- },
29
- "eslintConfig": {
30
- "extends": "../../../internal/eslint-config"
31
- },
32
- "exports": {
33
- ".": {
34
- "import": "./dist/index.es.js",
35
- "require": "./dist/index.cjs",
36
- "types": "./dist/index.d.ts"
37
- }
17
+ "@swapkit/api": "1.0.0-rc.62",
18
+ "@swapkit/helpers": "1.0.0-rc.96",
19
+ "@swapkit/tokens": "1.0.0-rc.51",
20
+ "@swapkit/toolbox-cosmos": "1.0.0-rc.117",
21
+ "@swapkit/toolbox-evm": "1.0.0-rc.102",
22
+ "@swapkit/toolbox-substrate": "1.0.0-rc.31",
23
+ "@swapkit/toolbox-utxo": "1.0.0-rc.105"
38
24
  },
39
25
  "files": [
40
26
  "src/",
@@ -42,22 +28,22 @@
42
28
  ],
43
29
  "homepage": "https://github.com/thorswap/SwapKit",
44
30
  "license": "Apache-2.0",
45
- "main": "./dist/index.cjs",
46
- "module": "./dist/index.es.js",
31
+ "main": "./dist/index.js",
47
32
  "name": "@swapkit/core",
48
- "publishConfig": {
49
- "access": "public"
50
- },
51
33
  "react-native": "./src/index.ts",
52
- "repository": "https://github.com/thorswap/SwapKit.git",
53
- "type": "module",
54
- "types": "./dist/index.d.ts",
55
- "version": "1.0.0-rc.14",
34
+ "repository": {
35
+ "type": "git",
36
+ "url": "git+https://github.com/thorswap/SwapKit.git"
37
+ },
56
38
  "scripts": {
57
- "build": "NODE_OPTIONS=--max_old_space_size=16384 vite build",
58
- "clean": "rm -rf dist vite.config.ts.* .turbo node_modules",
59
- "lint": "eslint ./ --ext .ts,.tsx --fix; tsc --noEmit",
60
- "test": "vitest --run",
61
- "test:coverage": "vitest run --coverage"
62
- }
63
- }
39
+ "build": "bun run ./build.ts",
40
+ "clean": "rm -rf .turbo dist node_modules *.tsbuildinfo",
41
+ "lint": "biome check --apply ./src",
42
+ "test": "echo 'bun test'",
43
+ "test:coverage": "echo 'bun test --coverage'",
44
+ "type-check": "tsc --noEmit"
45
+ },
46
+ "type": "module",
47
+ "types": "./src/index.ts",
48
+ "version": "1.0.0-rc.140"
49
+ }
@@ -0,0 +1,53 @@
1
+ import { describe, expect, test } from "bun:test";
2
+ import { Chain, ChainToExplorerUrl, CosmosChains, EVMChains, UTXOChains } from "@swapkit/helpers";
3
+ import { getExplorerAddressUrl, getExplorerTxUrl } from "../helpers/explorerUrls.ts";
4
+
5
+ describe("Explorer URLs", () => {
6
+ describe("CosmosChains", () => {
7
+ for (const chain of CosmosChains) {
8
+ test(`getExplorerTxUrl returns correct URL for ${chain}`, () => {
9
+ expect(getExplorerTxUrl({ chain, txHash: "0x123456789" })).toBe(
10
+ `${ChainToExplorerUrl[chain]}/tx/123456789`,
11
+ );
12
+
13
+ expect(getExplorerAddressUrl({ chain, address: "asdfg" })).toBe(
14
+ `${ChainToExplorerUrl[chain]}/address/asdfg`,
15
+ );
16
+ });
17
+ }
18
+ });
19
+
20
+ describe("EVMChains & SubstrateChains", () => {
21
+ for (const chain of [...EVMChains, Chain.Polkadot]) {
22
+ test(`getExplorerTxUrl returns correct URL for ${chain}`, () => {
23
+ expect(getExplorerTxUrl({ chain, txHash: "0x123456789" })).toBe(
24
+ `${ChainToExplorerUrl[chain]}/tx/0x123456789`,
25
+ );
26
+
27
+ expect(getExplorerAddressUrl({ chain, address: "asdfg" })).toBe(
28
+ `${ChainToExplorerUrl[chain]}/address/asdfg`,
29
+ );
30
+ });
31
+ }
32
+
33
+ test("getExplorerTxUrl adds 0x for EVM like chains", () => {
34
+ expect(getExplorerTxUrl({ chain: Chain.Ethereum, txHash: "12345" })).toBe(
35
+ "https://etherscan.io/tx/0x12345",
36
+ );
37
+ });
38
+ });
39
+
40
+ describe("UTXOChains", () => {
41
+ for (const chain of UTXOChains.filter((c) => c !== Chain.Dash)) {
42
+ test(`getExplorerTxUrl returns correct URL for ${chain}`, () => {
43
+ expect(getExplorerTxUrl({ chain, txHash: "0x123456789" })).toBe(
44
+ `${ChainToExplorerUrl[chain]}/transaction/0x123456789`,
45
+ );
46
+
47
+ expect(getExplorerAddressUrl({ chain, address: "asdfg" })).toBe(
48
+ `${ChainToExplorerUrl[chain]}/address/asdfg`,
49
+ );
50
+ });
51
+ }
52
+ });
53
+ });
@@ -1,92 +1,92 @@
1
1
  export const avaxGeneric = [
2
2
  {
3
- inputs: [{ internalType: 'address', name: '_ttp', type: 'address' }],
4
- stateMutability: 'nonpayable',
5
- type: 'constructor',
3
+ inputs: [{ internalType: "address", name: "_ttp", type: "address" }],
4
+ stateMutability: "nonpayable",
5
+ type: "constructor",
6
6
  },
7
7
  {
8
8
  anonymous: false,
9
9
  inputs: [
10
- { indexed: false, internalType: 'uint256', name: 'fee', type: 'uint256' },
11
- { indexed: false, internalType: 'address', name: 'feeRecipient', type: 'address' },
10
+ { indexed: false, internalType: "uint256", name: "fee", type: "uint256" },
11
+ { indexed: false, internalType: "address", name: "feeRecipient", type: "address" },
12
12
  ],
13
- name: 'FeeSet',
14
- type: 'event',
13
+ name: "FeeSet",
14
+ type: "event",
15
15
  },
16
16
  {
17
17
  anonymous: false,
18
18
  inputs: [
19
- { indexed: true, internalType: 'address', name: 'owner', type: 'address' },
20
- { indexed: false, internalType: 'bool', name: 'active', type: 'bool' },
19
+ { indexed: true, internalType: "address", name: "owner", type: "address" },
20
+ { indexed: false, internalType: "bool", name: "active", type: "bool" },
21
21
  ],
22
- name: 'OwnerSet',
23
- type: 'event',
22
+ name: "OwnerSet",
23
+ type: "event",
24
24
  },
25
25
  {
26
26
  inputs: [],
27
- name: 'fee',
28
- outputs: [{ internalType: 'uint256', name: '', type: 'uint256' }],
29
- stateMutability: 'view',
30
- type: 'function',
27
+ name: "fee",
28
+ outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
29
+ stateMutability: "view",
30
+ type: "function",
31
31
  },
32
32
  {
33
33
  inputs: [],
34
- name: 'feeRecipient',
35
- outputs: [{ internalType: 'address', name: '', type: 'address' }],
36
- stateMutability: 'view',
37
- type: 'function',
34
+ name: "feeRecipient",
35
+ outputs: [{ internalType: "address", name: "", type: "address" }],
36
+ stateMutability: "view",
37
+ type: "function",
38
38
  },
39
39
  {
40
- inputs: [{ internalType: 'address', name: '', type: 'address' }],
41
- name: 'owners',
42
- outputs: [{ internalType: 'bool', name: '', type: 'bool' }],
43
- stateMutability: 'view',
44
- type: 'function',
40
+ inputs: [{ internalType: "address", name: "", type: "address" }],
41
+ name: "owners",
42
+ outputs: [{ internalType: "bool", name: "", type: "bool" }],
43
+ stateMutability: "view",
44
+ type: "function",
45
45
  },
46
46
  {
47
47
  inputs: [
48
- { internalType: 'uint256', name: '_fee', type: 'uint256' },
49
- { internalType: 'address', name: '_feeRecipient', type: 'address' },
48
+ { internalType: "uint256", name: "_fee", type: "uint256" },
49
+ { internalType: "address", name: "_feeRecipient", type: "address" },
50
50
  ],
51
- name: 'setFee',
51
+ name: "setFee",
52
52
  outputs: [],
53
- stateMutability: 'nonpayable',
54
- type: 'function',
53
+ stateMutability: "nonpayable",
54
+ type: "function",
55
55
  },
56
56
  {
57
57
  inputs: [
58
- { internalType: 'address', name: 'owner', type: 'address' },
59
- { internalType: 'bool', name: 'active', type: 'bool' },
58
+ { internalType: "address", name: "owner", type: "address" },
59
+ { internalType: "bool", name: "active", type: "bool" },
60
60
  ],
61
- name: 'setOwner',
61
+ name: "setOwner",
62
62
  outputs: [],
63
- stateMutability: 'nonpayable',
64
- type: 'function',
63
+ stateMutability: "nonpayable",
64
+ type: "function",
65
65
  },
66
66
  {
67
67
  inputs: [
68
- { internalType: 'address', name: 'tcRouter', type: 'address' },
69
- { internalType: 'address', name: 'tcVault', type: 'address' },
70
- { internalType: 'string', name: 'tcMemo', type: 'string' },
71
- { internalType: 'address', name: 'token', type: 'address' },
72
- { internalType: 'uint256', name: 'amount', type: 'uint256' },
73
- { internalType: 'address', name: 'router', type: 'address' },
74
- { internalType: 'bytes', name: 'data', type: 'bytes' },
75
- { internalType: 'uint256', name: 'deadline', type: 'uint256' },
68
+ { internalType: "address", name: "tcRouter", type: "address" },
69
+ { internalType: "address", name: "tcVault", type: "address" },
70
+ { internalType: "string", name: "tcMemo", type: "string" },
71
+ { internalType: "address", name: "token", type: "address" },
72
+ { internalType: "uint256", name: "amount", type: "uint256" },
73
+ { internalType: "address", name: "router", type: "address" },
74
+ { internalType: "bytes", name: "data", type: "bytes" },
75
+ { internalType: "uint256", name: "deadline", type: "uint256" },
76
76
  ],
77
- name: 'swapIn',
77
+ name: "swapIn",
78
78
  outputs: [],
79
- stateMutability: 'nonpayable',
80
- type: 'function',
79
+ stateMutability: "nonpayable",
80
+ type: "function",
81
81
  },
82
82
  {
83
83
  inputs: [],
84
- name: 'tokenTransferProxy',
84
+ name: "tokenTransferProxy",
85
85
  outputs: [
86
- { internalType: 'contract TSAggregatorTokenTransferProxy', name: '', type: 'address' },
86
+ { internalType: "contract TSAggregatorTokenTransferProxy", name: "", type: "address" },
87
87
  ],
88
- stateMutability: 'view',
89
- type: 'function',
88
+ stateMutability: "view",
89
+ type: "function",
90
90
  },
91
- { stateMutability: 'payable', type: 'receive' },
91
+ { stateMutability: "payable", type: "receive" },
92
92
  ];