@swapkit/core 1.0.0-rc.125 → 1.0.0-rc.127
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 +30 -21
- package/dist/index.js.map +5 -5
- package/package.json +15 -15
- package/src/client.ts +75 -93
- package/src/index.ts +0 -2
- package/src/types.ts +33 -81
package/dist/index.js
CHANGED
|
@@ -45,26 +45,32 @@ function getExplorerAddressUrl({ chain, address }) {
|
|
|
45
45
|
|
|
46
46
|
// src/client.ts
|
|
47
47
|
function SwapKit({
|
|
48
|
-
stagenet,
|
|
49
|
-
wallets,
|
|
50
|
-
plugins,
|
|
51
|
-
config = {},
|
|
52
48
|
apis = {},
|
|
53
|
-
|
|
49
|
+
config = {},
|
|
50
|
+
plugins,
|
|
51
|
+
rpcUrls = {},
|
|
52
|
+
stagenet = false,
|
|
53
|
+
wallets
|
|
54
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;
|
|
55
65
|
const connectedWallets = {};
|
|
56
|
-
const availablePlugins = {}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
}
|
|
61
|
-
const connectWalletMethods =
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
config,
|
|
65
|
-
apis,
|
|
66
|
-
rpcUrls
|
|
67
|
-
});
|
|
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;
|
|
68
74
|
return acc;
|
|
69
75
|
}, {});
|
|
70
76
|
function getSwapKitPlugin(pluginName) {
|
|
@@ -137,9 +143,12 @@ function SwapKit({
|
|
|
137
143
|
function isAssetValueApproved(assetValue, contractAddress) {
|
|
138
144
|
return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });
|
|
139
145
|
}
|
|
140
|
-
function swap({
|
|
141
|
-
const plugin = getSwapKitPlugin(
|
|
142
|
-
|
|
146
|
+
function swap({ pluginName, ...rest }) {
|
|
147
|
+
const plugin = getSwapKitPlugin(pluginName);
|
|
148
|
+
if ("swap" in plugin) {
|
|
149
|
+
return plugin.swap(rest);
|
|
150
|
+
}
|
|
151
|
+
throw new SwapKitError("core_plugin_swap_not_found");
|
|
143
152
|
}
|
|
144
153
|
return {
|
|
145
154
|
...availablePlugins,
|
|
@@ -160,4 +169,4 @@ export {
|
|
|
160
169
|
SwapKit
|
|
161
170
|
};
|
|
162
171
|
|
|
163
|
-
//# debugId=
|
|
172
|
+
//# debugId=BF450BFAA7D76CB564756e2164756e21
|
package/dist/index.js.map
CHANGED
|
@@ -2,12 +2,12 @@
|
|
|
2
2
|
"version": 3,
|
|
3
3
|
"sources": ["../src/index.ts", "../src/client.ts", "../src/helpers/explorerUrls.ts", "../src/client.ts"],
|
|
4
4
|
"sourcesContent": [
|
|
5
|
-
"export * from \"@swapkit/api\";\nexport * from \"@swapkit/helpers\";\n\nexport
|
|
6
|
-
"import {\n
|
|
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 Chain,\n type ConnectConfig,\n SwapKitError,\n type SwapParams,\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, Wallet } 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-expect-error 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-expect-error 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 * Internal helpers\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 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;\n }) {\n const { address, chain, isGasAsset, isSynthetic } = assetValue;\n const isEVMChain = [Chain.Ethereum, Chain.Avalanche, Chain.BinanceSmartChain].includes(chain);\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)) {\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 * Wallet helpers\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 async function getBalance<T extends Chain>(chain: T, refresh?: boolean) {\n if (refresh) {\n const wallet = await getWalletWithBalance(chain, true);\n return wallet.balance || [];\n }\n\n return getWallet(chain)?.balance || [];\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 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) {\n return approve({ assetValue, contractAddress, type: ApproveMode.Approve });\n }\n\n function isAssetValueApproved(assetValue: AssetValue, contractAddress: string) {\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 getExplorerAddressUrl: getAddressUrl,\n getExplorerTxUrl: getTxUrl,\n\n approveAssetValue,\n getAddress,\n getBalance,\n getWallet,\n getWalletWithBalance,\n isAssetValueApproved,\n swap,\n validateAddress,\n };\n}\n",
|
|
7
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
|
|
8
|
+
"import {\n type AddChainWalletParams,\n ApproveMode,\n type ApproveReturnType,\n AssetValue,\n Chain,\n type ConnectConfig,\n SwapKitError,\n type SwapParams,\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, Wallet } 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-expect-error 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-expect-error 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 * Internal helpers\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 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;\n }) {\n const { address, chain, isGasAsset, isSynthetic } = assetValue;\n const isEVMChain = [Chain.Ethereum, Chain.Avalanche, Chain.BinanceSmartChain].includes(chain);\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)) {\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 * Wallet helpers\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 async function getBalance<T extends Chain>(chain: T, refresh?: boolean) {\n if (refresh) {\n const wallet = await getWalletWithBalance(chain, true);\n return wallet.balance || [];\n }\n\n return getWallet(chain)?.balance || [];\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 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) {\n return approve({ assetValue, contractAddress, type: ApproveMode.Approve });\n }\n\n function isAssetValueApproved(assetValue: AssetValue, contractAddress: string) {\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 getExplorerAddressUrl: getAddressUrl,\n getExplorerTxUrl: getTxUrl,\n\n approveAssetValue,\n getAddress,\n getBalance,\n getWallet,\n getWalletWithBalance,\n isAssetValueApproved,\n swap,\n validateAddress,\n };\n}\n"
|
|
9
9
|
],
|
|
10
|
-
"mappings": ";AAAA;AACA;;;ACDA;AAAA;AAAA;AAAA,
|
|
11
|
-
"debugId": "
|
|
10
|
+
"mappings": ";AAAA;AACA;;;ACDA;AAAA;AAAA;AAAA,SAKE;AAAA;AAAA;;;ACLF;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": "BF450BFAA7D76CB564756e2164756e21",
|
|
12
12
|
"names": []
|
|
13
13
|
}
|
package/package.json
CHANGED
|
@@ -1,26 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"author": "swapkit-oss",
|
|
3
3
|
"dependencies": {
|
|
4
|
-
"@swapkit/api": "1.0.0-rc.
|
|
5
|
-
"@swapkit/helpers": "1.0.0-rc.
|
|
4
|
+
"@swapkit/api": "1.0.0-rc.55",
|
|
5
|
+
"@swapkit/helpers": "1.0.0-rc.90"
|
|
6
6
|
},
|
|
7
7
|
"description": "SwapKit - Core",
|
|
8
8
|
"devDependencies": {
|
|
9
|
-
"@swapkit/tokens": "1.0.0-rc.
|
|
10
|
-
"@swapkit/toolbox-cosmos": "1.0.0-rc.
|
|
11
|
-
"@swapkit/toolbox-evm": "1.0.0-rc.
|
|
12
|
-
"@swapkit/toolbox-substrate": "1.0.0-rc.
|
|
13
|
-
"@swapkit/toolbox-utxo": "1.0.0-rc.
|
|
9
|
+
"@swapkit/tokens": "1.0.0-rc.50",
|
|
10
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.107",
|
|
11
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.96",
|
|
12
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.25",
|
|
13
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.98",
|
|
14
14
|
"bun-types": "1.1.2"
|
|
15
15
|
},
|
|
16
16
|
"peerDependencies": {
|
|
17
|
-
"@swapkit/api": "1.0.0-rc.
|
|
18
|
-
"@swapkit/helpers": "1.0.0-rc.
|
|
19
|
-
"@swapkit/tokens": "1.0.0-rc.
|
|
20
|
-
"@swapkit/toolbox-cosmos": "1.0.0-rc.
|
|
21
|
-
"@swapkit/toolbox-evm": "1.0.0-rc.
|
|
22
|
-
"@swapkit/toolbox-substrate": "1.0.0-rc.
|
|
23
|
-
"@swapkit/toolbox-utxo": "1.0.0-rc.
|
|
17
|
+
"@swapkit/api": "1.0.0-rc.55",
|
|
18
|
+
"@swapkit/helpers": "1.0.0-rc.90",
|
|
19
|
+
"@swapkit/tokens": "1.0.0-rc.50",
|
|
20
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.107",
|
|
21
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.96",
|
|
22
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.25",
|
|
23
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.98"
|
|
24
24
|
},
|
|
25
25
|
"files": [
|
|
26
26
|
"src/",
|
|
@@ -45,5 +45,5 @@
|
|
|
45
45
|
},
|
|
46
46
|
"type": "module",
|
|
47
47
|
"types": "./src/index.ts",
|
|
48
|
-
"version": "1.0.0-rc.
|
|
48
|
+
"version": "1.0.0-rc.127"
|
|
49
49
|
}
|
package/src/client.ts
CHANGED
|
@@ -1,103 +1,91 @@
|
|
|
1
1
|
import {
|
|
2
|
+
type AddChainWalletParams,
|
|
2
3
|
ApproveMode,
|
|
3
4
|
type ApproveReturnType,
|
|
4
5
|
AssetValue,
|
|
5
|
-
type AvailableProviders,
|
|
6
|
-
type BaseWallet,
|
|
7
6
|
Chain,
|
|
8
|
-
type
|
|
9
|
-
type PluginName,
|
|
7
|
+
type ConnectConfig,
|
|
10
8
|
SwapKitError,
|
|
11
|
-
type SwapKitPlugin,
|
|
12
|
-
type SwapKitPlugins,
|
|
13
9
|
type SwapParams,
|
|
14
10
|
} from "@swapkit/helpers";
|
|
15
|
-
import type {
|
|
16
|
-
|
|
17
|
-
import type { SubstrateWallets } from "@swapkit/toolbox-substrate";
|
|
18
|
-
import type { UTXOWallets } from "@swapkit/toolbox-utxo";
|
|
11
|
+
import type { BaseEVMWallet } from "@swapkit/toolbox-evm";
|
|
12
|
+
|
|
19
13
|
import {
|
|
20
14
|
getExplorerAddressUrl as getAddressUrl,
|
|
21
15
|
getExplorerTxUrl as getTxUrl,
|
|
22
16
|
} from "./helpers/explorerUrls.ts";
|
|
23
|
-
import type {
|
|
24
|
-
|
|
25
|
-
export type SwapKitReturnType = SwapKitPlugins & {
|
|
26
|
-
getAddress: (chain: Chain) => string;
|
|
27
|
-
getWallet: (chain: Chain) => ChainWallet | undefined;
|
|
28
|
-
getWalletWithBalance: (chain: Chain, potentialScamFilter?: boolean) => Promise<ChainWallet>;
|
|
29
|
-
getBalance: (chain: Chain, refresh?: boolean) => Promise<AssetValue[]>;
|
|
30
|
-
getExplorerTxUrl: typeof getTxUrl;
|
|
31
|
-
getExplorerAddressUrl: typeof getAddressUrl;
|
|
32
|
-
swap: (params: SwapParams) => Promise<string>;
|
|
33
|
-
validateAddress: (params: { address: string; chain: Chain }) =>
|
|
34
|
-
| boolean
|
|
35
|
-
| Promise<boolean>
|
|
36
|
-
| undefined;
|
|
37
|
-
approveAssetValue: (assetValue: AssetValue, contractAddress: string) => boolean | Promise<string>;
|
|
38
|
-
isAssetValueApproved: (
|
|
39
|
-
assetValue: AssetValue,
|
|
40
|
-
contractAddress: string,
|
|
41
|
-
) => boolean | Promise<boolean>;
|
|
42
|
-
};
|
|
43
|
-
|
|
44
|
-
export type Wallet = BaseWallet<
|
|
45
|
-
EVMWallets & CosmosWallets & ThorchainWallets & UTXOWallets & SubstrateWallets
|
|
46
|
-
>;
|
|
47
|
-
|
|
48
|
-
export type SwapKitWallet = {
|
|
49
|
-
connectMethodName: string;
|
|
50
|
-
connect: (params: ConnectWalletParams) => (connectParams: Todo) => undefined | string;
|
|
51
|
-
};
|
|
17
|
+
import type { Apis, SwapKitPluginInterface, SwapKitWallet, Wallet } from "./types.ts";
|
|
52
18
|
|
|
53
19
|
export function SwapKit<
|
|
54
|
-
|
|
55
|
-
|
|
20
|
+
Plugins extends { [key in string]: SwapKitPluginInterface<{ [key in string]: Todo }> },
|
|
21
|
+
Wallets extends { [key in string]: SwapKitWallet<NotWorth[]> },
|
|
56
22
|
>({
|
|
57
|
-
stagenet,
|
|
58
|
-
wallets,
|
|
59
|
-
plugins,
|
|
60
|
-
config = {},
|
|
61
23
|
apis = {},
|
|
24
|
+
config = {},
|
|
25
|
+
plugins,
|
|
62
26
|
rpcUrls = {},
|
|
27
|
+
stagenet = false,
|
|
28
|
+
wallets,
|
|
63
29
|
}: {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
30
|
+
apis?: Apis;
|
|
31
|
+
config?: ConnectConfig;
|
|
32
|
+
plugins: Plugins;
|
|
33
|
+
rpcUrls?: { [key in Chain]?: string };
|
|
34
|
+
stagenet?: boolean;
|
|
35
|
+
wallets: Wallets;
|
|
70
36
|
}) {
|
|
71
|
-
|
|
72
|
-
const availablePlugins: AvailableProviders<ExtendedProviders> = {};
|
|
73
|
-
|
|
74
|
-
for (const plugin of plugins) {
|
|
75
|
-
const { name, methods } = plugin({ wallets: connectedWallets, stagenet });
|
|
37
|
+
type PluginName = keyof Plugins;
|
|
76
38
|
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
acc
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
39
|
+
/**
|
|
40
|
+
* @REMOVE (V1)
|
|
41
|
+
* Compatibility layer for plugins and wallets for easier migration and backwards compatibility
|
|
42
|
+
*/
|
|
43
|
+
const compatPlugins: Plugins = Array.isArray(plugins)
|
|
44
|
+
? plugins.reduce((acc, pluginInterface) => {
|
|
45
|
+
// @ts-expect-error Ignore until we remove the compatibility layer
|
|
46
|
+
const { name, plugin } = Object.values(pluginInterface)?.[0] || {};
|
|
47
|
+
acc[name] = plugin;
|
|
48
|
+
return acc;
|
|
49
|
+
}, {})
|
|
50
|
+
: plugins;
|
|
51
|
+
const compatWallets: Wallets = Array.isArray(wallets)
|
|
52
|
+
? wallets.reduce((acc, wallet) => {
|
|
53
|
+
// @ts-expect-error Ignore until we remove the compatibility layer
|
|
54
|
+
const [walletName, connectWallet] = Object.entries(wallet)?.[0] || {};
|
|
55
|
+
acc[walletName] = connectWallet;
|
|
56
|
+
return acc;
|
|
57
|
+
}, {})
|
|
58
|
+
: wallets;
|
|
89
59
|
|
|
90
|
-
|
|
91
|
-
|
|
60
|
+
const connectedWallets = {} as Wallet;
|
|
61
|
+
const availablePlugins = Object.entries(compatPlugins).reduce(
|
|
62
|
+
(acc, [pluginName, { plugin, config }]) => {
|
|
63
|
+
const methods = plugin({ wallets: connectedWallets, stagenet, config });
|
|
64
|
+
|
|
65
|
+
// @ts-expect-error
|
|
66
|
+
acc[pluginName] = methods;
|
|
67
|
+
return acc;
|
|
68
|
+
},
|
|
69
|
+
{} as { [key in PluginName]: ReturnType<Plugins[key]["plugin"]> },
|
|
70
|
+
);
|
|
71
|
+
|
|
72
|
+
const connectWalletMethods = Object.entries(compatWallets).reduce(
|
|
73
|
+
(acc, [walletName, wallet]) => {
|
|
74
|
+
const connectWallet = wallet({ addChain, config, apis, rpcUrls });
|
|
75
|
+
|
|
76
|
+
// @ts-expect-error
|
|
77
|
+
acc[walletName] = connectWallet;
|
|
78
|
+
return acc;
|
|
79
|
+
},
|
|
80
|
+
{} as { [key in keyof Wallets]: ReturnType<Wallets[key]> },
|
|
81
|
+
);
|
|
92
82
|
|
|
93
83
|
/**
|
|
94
84
|
* @Private
|
|
95
85
|
* Internal helpers
|
|
96
86
|
*/
|
|
97
|
-
function getSwapKitPlugin(pluginName
|
|
98
|
-
const plugin =
|
|
99
|
-
(availablePlugins as SwapKitPlugins)[pluginName as PluginName] ||
|
|
100
|
-
Object.values(availablePlugins)[0];
|
|
87
|
+
function getSwapKitPlugin<T extends PluginName>(pluginName: T) {
|
|
88
|
+
const plugin = availablePlugins[pluginName] || Object.values(availablePlugins)[0];
|
|
101
89
|
|
|
102
90
|
if (!plugin) {
|
|
103
91
|
throw new SwapKitError("core_plugin_not_found", "Could not find the requested plugin");
|
|
@@ -106,15 +94,11 @@ export function SwapKit<
|
|
|
106
94
|
return plugin;
|
|
107
95
|
}
|
|
108
96
|
|
|
109
|
-
function addChain(connectWallet:
|
|
110
|
-
// @ts-expect-error TODO
|
|
97
|
+
function addChain<T extends Chain>(connectWallet: AddChainWalletParams<T>) {
|
|
98
|
+
// @ts-expect-error: TODO
|
|
111
99
|
connectedWallets[connectWallet.chain] = connectWallet;
|
|
112
100
|
}
|
|
113
101
|
|
|
114
|
-
/**
|
|
115
|
-
* @Private
|
|
116
|
-
* Wallet interaction helpers
|
|
117
|
-
*/
|
|
118
102
|
function approve<T extends ApproveMode>({
|
|
119
103
|
assetValue,
|
|
120
104
|
type = "checkOnly" as T,
|
|
@@ -123,7 +107,7 @@ export function SwapKit<
|
|
|
123
107
|
type: T;
|
|
124
108
|
assetValue: AssetValue;
|
|
125
109
|
contractAddress: string;
|
|
126
|
-
})
|
|
110
|
+
}) {
|
|
127
111
|
const { address, chain, isGasAsset, isSynthetic } = assetValue;
|
|
128
112
|
const isEVMChain = [Chain.Ethereum, Chain.Avalanche, Chain.BinanceSmartChain].includes(chain);
|
|
129
113
|
const isNativeEVM = isEVMChain && isGasAsset;
|
|
@@ -156,10 +140,10 @@ export function SwapKit<
|
|
|
156
140
|
function getWallet<T extends Chain>(chain: T) {
|
|
157
141
|
return connectedWallets[chain];
|
|
158
142
|
}
|
|
159
|
-
function getAddress(chain:
|
|
143
|
+
function getAddress<T extends Chain>(chain: T) {
|
|
160
144
|
return getWallet(chain)?.address || "";
|
|
161
145
|
}
|
|
162
|
-
async function getBalance(chain:
|
|
146
|
+
async function getBalance<T extends Chain>(chain: T, refresh?: boolean) {
|
|
163
147
|
if (refresh) {
|
|
164
148
|
const wallet = await getWalletWithBalance(chain, true);
|
|
165
149
|
return wallet.balance || [];
|
|
@@ -174,7 +158,7 @@ export function SwapKit<
|
|
|
174
158
|
return getWallet(chain)?.validateAddress?.(address);
|
|
175
159
|
}
|
|
176
160
|
|
|
177
|
-
async function getWalletWithBalance(chain:
|
|
161
|
+
async function getWalletWithBalance<T extends Chain>(chain: T, potentialScamFilter = true) {
|
|
178
162
|
const defaultBalance = [AssetValue.fromChainOrSignature(chain)];
|
|
179
163
|
const wallet = getWallet(chain);
|
|
180
164
|
|
|
@@ -190,10 +174,6 @@ export function SwapKit<
|
|
|
190
174
|
return wallet;
|
|
191
175
|
}
|
|
192
176
|
|
|
193
|
-
/**
|
|
194
|
-
* @Public
|
|
195
|
-
* Wallet interaction methods
|
|
196
|
-
*/
|
|
197
177
|
function approveAssetValue(assetValue: AssetValue, contractAddress: string) {
|
|
198
178
|
return approve({ assetValue, contractAddress, type: ApproveMode.Approve });
|
|
199
179
|
}
|
|
@@ -202,10 +182,14 @@ export function SwapKit<
|
|
|
202
182
|
return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });
|
|
203
183
|
}
|
|
204
184
|
|
|
205
|
-
function swap({
|
|
206
|
-
const plugin = getSwapKitPlugin(
|
|
185
|
+
function swap<T extends PluginName>({ pluginName, ...rest }: SwapParams<T>) {
|
|
186
|
+
const plugin = getSwapKitPlugin(pluginName);
|
|
207
187
|
|
|
208
|
-
|
|
188
|
+
if ("swap" in plugin) {
|
|
189
|
+
return plugin.swap(rest);
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
throw new SwapKitError("core_plugin_swap_not_found");
|
|
209
193
|
}
|
|
210
194
|
|
|
211
195
|
return {
|
|
@@ -225,5 +209,3 @@ export function SwapKit<
|
|
|
225
209
|
validateAddress,
|
|
226
210
|
};
|
|
227
211
|
}
|
|
228
|
-
|
|
229
|
-
export type SwapKitClient<T extends {}, K> = ReturnType<typeof SwapKit<T, K>>;
|
package/src/index.ts
CHANGED
package/src/types.ts
CHANGED
|
@@ -1,89 +1,41 @@
|
|
|
1
|
-
import type { ChainWallet } from "@swapkit/helpers";
|
|
2
1
|
import type {
|
|
2
|
+
BaseWallet,
|
|
3
3
|
Chain,
|
|
4
|
-
|
|
4
|
+
ConnectWalletParams,
|
|
5
5
|
CosmosChain,
|
|
6
|
-
EVMChain,
|
|
7
|
-
// type FeeOption,
|
|
8
6
|
UTXOChain,
|
|
9
7
|
} from "@swapkit/helpers";
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
// DOGEToolbox,
|
|
31
|
-
// LTCToolbox,
|
|
32
|
-
// } from "@swapkit/toolbox-utxo";
|
|
33
|
-
|
|
34
|
-
// export type ThorchainWallet = ThorchainToolboxType & {
|
|
35
|
-
// transfer: (params: CoreTxParams) => Promise<string>;
|
|
36
|
-
// deposit: (params: DepositParam) => Promise<string>;
|
|
37
|
-
// };
|
|
38
|
-
|
|
39
|
-
// export type CosmosBasedWallet<T extends typeof BinanceToolbox | typeof GaiaToolbox> =
|
|
40
|
-
// ReturnType<T> & {
|
|
41
|
-
// transfer: (params: CoreTxParams) => Promise<string>;
|
|
42
|
-
// };
|
|
43
|
-
|
|
44
|
-
// export type SubstrateBasedWallet<T extends typeof PolkadotToolbox | typeof ChainflipToolbox> =
|
|
45
|
-
// Awaited<ReturnType<T>>;
|
|
46
|
-
|
|
47
|
-
// export type EVMWallet<
|
|
48
|
-
// T extends typeof AVAXToolbox | typeof BSCToolbox | typeof ETHToolbox | typeof OPToolbox,
|
|
49
|
-
// > = ReturnType<T> & {
|
|
50
|
-
// transfer: (params: CoreTxParams) => Promise<string>;
|
|
51
|
-
// };
|
|
52
|
-
|
|
53
|
-
// export type UTXOWallet<
|
|
54
|
-
// T extends typeof BCHToolbox | typeof BTCToolbox | typeof DOGEToolbox | typeof LTCToolbox,
|
|
55
|
-
// > = ReturnType<T> & {
|
|
56
|
-
// transfer: (prams: CoreTxParams) => Promise<string>;
|
|
57
|
-
// };
|
|
58
|
-
|
|
59
|
-
// export type WalletMethods = {
|
|
60
|
-
// [Chain.Arbitrum]: EVMWallet<typeof ARBToolbox> | null;
|
|
61
|
-
// [Chain.Avalanche]: EVMWallet<typeof AVAXToolbox> | null;
|
|
62
|
-
// [Chain.BinanceSmartChain]: EVMWallet<typeof BSCToolbox> | null;
|
|
63
|
-
// [Chain.Binance]: CosmosBasedWallet<typeof BinanceToolbox> | null;
|
|
64
|
-
// [Chain.BitcoinCash]: UTXOWallet<typeof BCHToolbox> | null;
|
|
65
|
-
// [Chain.Bitcoin]: UTXOWallet<typeof BTCToolbox> | null;
|
|
66
|
-
// [Chain.Chainflip]: SubstrateBasedWallet<typeof ChainflipToolbox> | null;
|
|
67
|
-
// [Chain.Cosmos]: CosmosBasedWallet<typeof GaiaToolbox> | null;
|
|
68
|
-
// [Chain.Dash]: UTXOWallet<typeof DASHToolbox> | null;
|
|
69
|
-
// [Chain.Dogecoin]: UTXOWallet<typeof DOGEToolbox> | null;
|
|
70
|
-
// [Chain.Ethereum]: EVMWallet<typeof ETHToolbox> | null;
|
|
71
|
-
// [Chain.Kujira]: CosmosBasedWallet<typeof KujiraToolbox> | null;
|
|
72
|
-
// [Chain.Litecoin]: UTXOWallet<typeof LTCToolbox> | null;
|
|
73
|
-
// [Chain.Maya]: ThorchainWallet | null;
|
|
74
|
-
// [Chain.Optimism]: EVMWallet<typeof OPToolbox> | null;
|
|
75
|
-
// [Chain.Polkadot]: SubstrateBasedWallet<typeof PolkadotToolbox> | null;
|
|
76
|
-
// [Chain.Polygon]: EVMWallet<typeof MATICToolbox> | null;
|
|
77
|
-
// [Chain.THORChain]: ThorchainWallet | null;
|
|
78
|
-
// };
|
|
79
|
-
|
|
80
|
-
type ApisType = { [key in UTXOChain]?: string } & { [key in EVMChain]?: string } & {
|
|
81
|
-
[key in CosmosChain]?: string;
|
|
8
|
+
import type { CosmosWallets, ThorchainWallets } from "@swapkit/toolbox-cosmos";
|
|
9
|
+
import type { CovalentApiType, EVMWallets, EthplorerApiType } from "@swapkit/toolbox-evm";
|
|
10
|
+
import type { SubstrateWallets } from "@swapkit/toolbox-substrate";
|
|
11
|
+
import type { BlockchairApiType, UTXOWallets } from "@swapkit/toolbox-utxo";
|
|
12
|
+
|
|
13
|
+
export type Wallet = BaseWallet<
|
|
14
|
+
EVMWallets & CosmosWallets & ThorchainWallets & UTXOWallets & SubstrateWallets
|
|
15
|
+
>;
|
|
16
|
+
|
|
17
|
+
export type SwapKitWallet<ConnectParams extends Todo[]> = (
|
|
18
|
+
params: ConnectWalletParams,
|
|
19
|
+
) => (...connectParams: ConnectParams) => boolean | Promise<boolean>;
|
|
20
|
+
|
|
21
|
+
export type SwapKitPluginInterface<Methods = { [key in string]: Todo }> = {
|
|
22
|
+
plugin: ({
|
|
23
|
+
wallets,
|
|
24
|
+
stagenet,
|
|
25
|
+
config,
|
|
26
|
+
}: { wallets: Wallet; stagenet?: boolean; config: Todo }) => Methods;
|
|
27
|
+
config?: Todo;
|
|
82
28
|
};
|
|
83
29
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
30
|
+
type CovalentChains =
|
|
31
|
+
| Chain.BinanceSmartChain
|
|
32
|
+
| Chain.Polygon
|
|
33
|
+
| Chain.Avalanche
|
|
34
|
+
| Chain.Arbitrum
|
|
35
|
+
| Chain.Optimism;
|
|
36
|
+
|
|
37
|
+
export type Apis = { [key in CovalentChains]?: CovalentApiType } & {
|
|
38
|
+
[key in Chain.Ethereum]?: EthplorerApiType;
|
|
39
|
+
} & { [key in CosmosChain]?: Todo } & {
|
|
40
|
+
[key in UTXOChain]?: BlockchairApiType;
|
|
89
41
|
};
|