@swapkit/core 1.0.0-rc.125 → 1.0.0-rc.126
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 +4 -4
- package/package.json +15 -15
- package/src/client.ts +82 -79
- package/src/index.ts +0 -2
- package/src/types.ts +0 -89
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 ApproveMode,\n type ApproveReturnType,\n AssetValue,\n type
|
|
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 BaseWallet,\n Chain,\n type ConnectConfig,\n type ConnectWalletParams,\n SwapKitError,\n type SwapParams,\n} from \"@swapkit/helpers\";\nimport type { CosmosWallets, ThorchainWallets } from \"@swapkit/toolbox-cosmos\";\nimport type { BaseEVMWallet, EVMWallets } from \"@swapkit/toolbox-evm\";\nimport type { SubstrateWallets } from \"@swapkit/toolbox-substrate\";\nimport type { UTXOWallets } from \"@swapkit/toolbox-utxo\";\nimport {\n getExplorerAddressUrl as getAddressUrl,\n getExplorerTxUrl as getTxUrl,\n} from \"./helpers/explorerUrls.ts\";\n\nexport type Wallet = BaseWallet<\n EVMWallets & CosmosWallets & ThorchainWallets & UTXOWallets & SubstrateWallets\n>;\n\nexport type SwapKitWallet<ConnectParams extends Todo[]> = (\n params: ConnectWalletParams,\n) => (...connectParams: ConnectParams) => boolean | Promise<boolean>;\n\nexport type SwapKitPluginInterface<Methods = { [key in string]: Todo }> = {\n plugin: ({\n wallets,\n stagenet,\n config,\n }: { wallets: Wallet; stagenet?: boolean; config: Todo }) => Methods;\n config?: Todo;\n};\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?: { [key in Chain]?: Todo };\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\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 ApproveMode,\n type ApproveReturnType,\n AssetValue,\n type
|
|
8
|
+
"import {\n type AddChainWalletParams,\n ApproveMode,\n type ApproveReturnType,\n AssetValue,\n type BaseWallet,\n Chain,\n type ConnectConfig,\n type ConnectWalletParams,\n SwapKitError,\n type SwapParams,\n} from \"@swapkit/helpers\";\nimport type { CosmosWallets, ThorchainWallets } from \"@swapkit/toolbox-cosmos\";\nimport type { BaseEVMWallet, EVMWallets } from \"@swapkit/toolbox-evm\";\nimport type { SubstrateWallets } from \"@swapkit/toolbox-substrate\";\nimport type { UTXOWallets } from \"@swapkit/toolbox-utxo\";\nimport {\n getExplorerAddressUrl as getAddressUrl,\n getExplorerTxUrl as getTxUrl,\n} from \"./helpers/explorerUrls.ts\";\n\nexport type Wallet = BaseWallet<\n EVMWallets & CosmosWallets & ThorchainWallets & UTXOWallets & SubstrateWallets\n>;\n\nexport type SwapKitWallet<ConnectParams extends Todo[]> = (\n params: ConnectWalletParams,\n) => (...connectParams: ConnectParams) => boolean | Promise<boolean>;\n\nexport type SwapKitPluginInterface<Methods = { [key in string]: Todo }> = {\n plugin: ({\n wallets,\n stagenet,\n config,\n }: { wallets: Wallet; stagenet?: boolean; config: Todo }) => Methods;\n config?: Todo;\n};\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?: { [key in Chain]?: Todo };\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\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
10
|
"mappings": ";AAAA;AACA;;;ACDA;AAAA;AAAA;AAAA,SAME;AAAA;AAAA;;;ACNF;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": "
|
|
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.54",
|
|
5
|
+
"@swapkit/helpers": "1.0.0-rc.89"
|
|
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.49",
|
|
10
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.106",
|
|
11
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.95",
|
|
12
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.24",
|
|
13
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.97",
|
|
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.54",
|
|
18
|
+
"@swapkit/helpers": "1.0.0-rc.89",
|
|
19
|
+
"@swapkit/tokens": "1.0.0-rc.49",
|
|
20
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.106",
|
|
21
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.95",
|
|
22
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.24",
|
|
23
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.97"
|
|
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.126"
|
|
49
49
|
}
|
package/src/client.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import {
|
|
2
|
+
type AddChainWalletParams,
|
|
2
3
|
ApproveMode,
|
|
3
4
|
type ApproveReturnType,
|
|
4
5
|
AssetValue,
|
|
5
|
-
type AvailableProviders,
|
|
6
6
|
type BaseWallet,
|
|
7
7
|
Chain,
|
|
8
|
-
type
|
|
9
|
-
type
|
|
8
|
+
type ConnectConfig,
|
|
9
|
+
type ConnectWalletParams,
|
|
10
10
|
SwapKitError,
|
|
11
|
-
type SwapKitPlugin,
|
|
12
|
-
type SwapKitPlugins,
|
|
13
11
|
type SwapParams,
|
|
14
12
|
} from "@swapkit/helpers";
|
|
15
13
|
import type { CosmosWallets, ThorchainWallets } from "@swapkit/toolbox-cosmos";
|
|
@@ -20,84 +18,95 @@ import {
|
|
|
20
18
|
getExplorerAddressUrl as getAddressUrl,
|
|
21
19
|
getExplorerTxUrl as getTxUrl,
|
|
22
20
|
} from "./helpers/explorerUrls.ts";
|
|
23
|
-
import type { ConnectWalletParamsLocal as ConnectWalletParams } from "./types.ts";
|
|
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
21
|
|
|
44
22
|
export type Wallet = BaseWallet<
|
|
45
23
|
EVMWallets & CosmosWallets & ThorchainWallets & UTXOWallets & SubstrateWallets
|
|
46
24
|
>;
|
|
47
25
|
|
|
48
|
-
export type SwapKitWallet =
|
|
49
|
-
|
|
50
|
-
|
|
26
|
+
export type SwapKitWallet<ConnectParams extends Todo[]> = (
|
|
27
|
+
params: ConnectWalletParams,
|
|
28
|
+
) => (...connectParams: ConnectParams) => boolean | Promise<boolean>;
|
|
29
|
+
|
|
30
|
+
export type SwapKitPluginInterface<Methods = { [key in string]: Todo }> = {
|
|
31
|
+
plugin: ({
|
|
32
|
+
wallets,
|
|
33
|
+
stagenet,
|
|
34
|
+
config,
|
|
35
|
+
}: { wallets: Wallet; stagenet?: boolean; config: Todo }) => Methods;
|
|
36
|
+
config?: Todo;
|
|
51
37
|
};
|
|
52
38
|
|
|
53
39
|
export function SwapKit<
|
|
54
|
-
|
|
55
|
-
|
|
40
|
+
Plugins extends { [key in string]: SwapKitPluginInterface<{ [key in string]: Todo }> },
|
|
41
|
+
Wallets extends { [key in string]: SwapKitWallet<NotWorth[]> },
|
|
56
42
|
>({
|
|
57
|
-
stagenet,
|
|
58
|
-
wallets,
|
|
59
|
-
plugins,
|
|
60
|
-
config = {},
|
|
61
43
|
apis = {},
|
|
44
|
+
config = {},
|
|
45
|
+
plugins,
|
|
62
46
|
rpcUrls = {},
|
|
47
|
+
stagenet = false,
|
|
48
|
+
wallets,
|
|
63
49
|
}: {
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
50
|
+
apis?: { [key in Chain]?: Todo };
|
|
51
|
+
config?: ConnectConfig;
|
|
52
|
+
plugins: Plugins;
|
|
53
|
+
rpcUrls?: { [key in Chain]?: string };
|
|
54
|
+
stagenet?: boolean;
|
|
55
|
+
wallets: Wallets;
|
|
70
56
|
}) {
|
|
57
|
+
type PluginName = keyof Plugins;
|
|
58
|
+
|
|
59
|
+
/**
|
|
60
|
+
* @REMOVE (V1)
|
|
61
|
+
* Compatibility layer for plugins and wallets for easier migration and backwards compatibility
|
|
62
|
+
*/
|
|
63
|
+
const compatPlugins: Plugins = Array.isArray(plugins)
|
|
64
|
+
? plugins.reduce((acc, pluginInterface) => {
|
|
65
|
+
// @ts-expect-error Ignore until we remove the compatibility layer
|
|
66
|
+
const { name, plugin } = Object.values(pluginInterface)?.[0] || {};
|
|
67
|
+
acc[name] = plugin;
|
|
68
|
+
return acc;
|
|
69
|
+
}, {})
|
|
70
|
+
: plugins;
|
|
71
|
+
const compatWallets: Wallets = Array.isArray(wallets)
|
|
72
|
+
? wallets.reduce((acc, wallet) => {
|
|
73
|
+
// @ts-expect-error Ignore until we remove the compatibility layer
|
|
74
|
+
const [walletName, connectWallet] = Object.entries(wallet)?.[0] || {};
|
|
75
|
+
acc[walletName] = connectWallet;
|
|
76
|
+
return acc;
|
|
77
|
+
}, {})
|
|
78
|
+
: wallets;
|
|
79
|
+
|
|
71
80
|
const connectedWallets = {} as Wallet;
|
|
72
|
-
const availablePlugins: AvailableProviders<ExtendedProviders> = {};
|
|
73
81
|
|
|
74
|
-
|
|
75
|
-
|
|
82
|
+
const availablePlugins = Object.entries(compatPlugins).reduce(
|
|
83
|
+
(acc, [pluginName, { plugin, config }]) => {
|
|
84
|
+
const methods = plugin({ wallets: connectedWallets, stagenet, config });
|
|
76
85
|
|
|
77
|
-
|
|
78
|
-
|
|
86
|
+
// @ts-expect-error
|
|
87
|
+
acc[pluginName] = methods;
|
|
88
|
+
return acc;
|
|
89
|
+
},
|
|
90
|
+
{} as { [key in PluginName]: ReturnType<Plugins[key]["plugin"]> },
|
|
91
|
+
);
|
|
79
92
|
|
|
80
|
-
const connectWalletMethods =
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
// @ts-expect-error TODO
|
|
84
|
-
addChain,
|
|
85
|
-
config,
|
|
86
|
-
apis,
|
|
87
|
-
rpcUrls,
|
|
88
|
-
});
|
|
93
|
+
const connectWalletMethods = Object.entries(compatWallets).reduce(
|
|
94
|
+
(acc, [walletName, wallet]) => {
|
|
95
|
+
const connectWallet = wallet({ addChain, config, apis, rpcUrls });
|
|
89
96
|
|
|
90
|
-
|
|
91
|
-
|
|
97
|
+
// @ts-expect-error
|
|
98
|
+
acc[walletName] = connectWallet;
|
|
99
|
+
return acc;
|
|
100
|
+
},
|
|
101
|
+
{} as { [key in keyof Wallets]: ReturnType<Wallets[key]> },
|
|
102
|
+
);
|
|
92
103
|
|
|
93
104
|
/**
|
|
94
105
|
* @Private
|
|
95
106
|
* Internal helpers
|
|
96
107
|
*/
|
|
97
|
-
function getSwapKitPlugin(pluginName
|
|
98
|
-
const plugin =
|
|
99
|
-
(availablePlugins as SwapKitPlugins)[pluginName as PluginName] ||
|
|
100
|
-
Object.values(availablePlugins)[0];
|
|
108
|
+
function getSwapKitPlugin<T extends PluginName>(pluginName: T) {
|
|
109
|
+
const plugin = availablePlugins[pluginName] || Object.values(availablePlugins)[0];
|
|
101
110
|
|
|
102
111
|
if (!plugin) {
|
|
103
112
|
throw new SwapKitError("core_plugin_not_found", "Could not find the requested plugin");
|
|
@@ -106,15 +115,11 @@ export function SwapKit<
|
|
|
106
115
|
return plugin;
|
|
107
116
|
}
|
|
108
117
|
|
|
109
|
-
function addChain(connectWallet:
|
|
110
|
-
// @ts-expect-error TODO
|
|
118
|
+
function addChain<T extends Chain>(connectWallet: AddChainWalletParams<T>) {
|
|
119
|
+
// @ts-expect-error: TODO
|
|
111
120
|
connectedWallets[connectWallet.chain] = connectWallet;
|
|
112
121
|
}
|
|
113
122
|
|
|
114
|
-
/**
|
|
115
|
-
* @Private
|
|
116
|
-
* Wallet interaction helpers
|
|
117
|
-
*/
|
|
118
123
|
function approve<T extends ApproveMode>({
|
|
119
124
|
assetValue,
|
|
120
125
|
type = "checkOnly" as T,
|
|
@@ -123,7 +128,7 @@ export function SwapKit<
|
|
|
123
128
|
type: T;
|
|
124
129
|
assetValue: AssetValue;
|
|
125
130
|
contractAddress: string;
|
|
126
|
-
})
|
|
131
|
+
}) {
|
|
127
132
|
const { address, chain, isGasAsset, isSynthetic } = assetValue;
|
|
128
133
|
const isEVMChain = [Chain.Ethereum, Chain.Avalanche, Chain.BinanceSmartChain].includes(chain);
|
|
129
134
|
const isNativeEVM = isEVMChain && isGasAsset;
|
|
@@ -156,10 +161,10 @@ export function SwapKit<
|
|
|
156
161
|
function getWallet<T extends Chain>(chain: T) {
|
|
157
162
|
return connectedWallets[chain];
|
|
158
163
|
}
|
|
159
|
-
function getAddress(chain:
|
|
164
|
+
function getAddress<T extends Chain>(chain: T) {
|
|
160
165
|
return getWallet(chain)?.address || "";
|
|
161
166
|
}
|
|
162
|
-
async function getBalance(chain:
|
|
167
|
+
async function getBalance<T extends Chain>(chain: T, refresh?: boolean) {
|
|
163
168
|
if (refresh) {
|
|
164
169
|
const wallet = await getWalletWithBalance(chain, true);
|
|
165
170
|
return wallet.balance || [];
|
|
@@ -174,7 +179,7 @@ export function SwapKit<
|
|
|
174
179
|
return getWallet(chain)?.validateAddress?.(address);
|
|
175
180
|
}
|
|
176
181
|
|
|
177
|
-
async function getWalletWithBalance(chain:
|
|
182
|
+
async function getWalletWithBalance<T extends Chain>(chain: T, potentialScamFilter = true) {
|
|
178
183
|
const defaultBalance = [AssetValue.fromChainOrSignature(chain)];
|
|
179
184
|
const wallet = getWallet(chain);
|
|
180
185
|
|
|
@@ -190,10 +195,6 @@ export function SwapKit<
|
|
|
190
195
|
return wallet;
|
|
191
196
|
}
|
|
192
197
|
|
|
193
|
-
/**
|
|
194
|
-
* @Public
|
|
195
|
-
* Wallet interaction methods
|
|
196
|
-
*/
|
|
197
198
|
function approveAssetValue(assetValue: AssetValue, contractAddress: string) {
|
|
198
199
|
return approve({ assetValue, contractAddress, type: ApproveMode.Approve });
|
|
199
200
|
}
|
|
@@ -202,10 +203,14 @@ export function SwapKit<
|
|
|
202
203
|
return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });
|
|
203
204
|
}
|
|
204
205
|
|
|
205
|
-
function swap({
|
|
206
|
-
const plugin = getSwapKitPlugin(
|
|
206
|
+
function swap<T extends PluginName>({ pluginName, ...rest }: SwapParams<T>) {
|
|
207
|
+
const plugin = getSwapKitPlugin(pluginName);
|
|
207
208
|
|
|
208
|
-
|
|
209
|
+
if ("swap" in plugin) {
|
|
210
|
+
return plugin.swap(rest);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
throw new SwapKitError("core_plugin_swap_not_found");
|
|
209
214
|
}
|
|
210
215
|
|
|
211
216
|
return {
|
|
@@ -225,5 +230,3 @@ export function SwapKit<
|
|
|
225
230
|
validateAddress,
|
|
226
231
|
};
|
|
227
232
|
}
|
|
228
|
-
|
|
229
|
-
export type SwapKitClient<T extends {}, K> = ReturnType<typeof SwapKit<T, K>>;
|
package/src/index.ts
CHANGED
package/src/types.ts
DELETED
|
@@ -1,89 +0,0 @@
|
|
|
1
|
-
import type { ChainWallet } from "@swapkit/helpers";
|
|
2
|
-
import type {
|
|
3
|
-
Chain,
|
|
4
|
-
ConnectConfig,
|
|
5
|
-
CosmosChain,
|
|
6
|
-
EVMChain,
|
|
7
|
-
// type FeeOption,
|
|
8
|
-
UTXOChain,
|
|
9
|
-
} from "@swapkit/helpers";
|
|
10
|
-
// import type {
|
|
11
|
-
// BinanceToolbox,
|
|
12
|
-
// DepositParam,
|
|
13
|
-
// GaiaToolbox,
|
|
14
|
-
// KujiraToolbox,
|
|
15
|
-
// ThorchainToolboxType,
|
|
16
|
-
// } from "@swapkit/toolbox-cosmos";
|
|
17
|
-
// import type {
|
|
18
|
-
// ARBToolbox,
|
|
19
|
-
// AVAXToolbox,
|
|
20
|
-
// BSCToolbox,
|
|
21
|
-
// ETHToolbox,
|
|
22
|
-
// MATICToolbox,
|
|
23
|
-
// OPToolbox,
|
|
24
|
-
// } from "@swapkit/toolbox-evm";
|
|
25
|
-
// import type { ChainflipToolbox, PolkadotToolbox } from "@swapkit/toolbox-substrate";
|
|
26
|
-
// import type {
|
|
27
|
-
// BCHToolbox,
|
|
28
|
-
// BTCToolbox,
|
|
29
|
-
// DASHToolbox,
|
|
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;
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
export type ConnectWalletParamsLocal = {
|
|
85
|
-
addChain: (params: ChainWallet) => void;
|
|
86
|
-
apis: ApisType;
|
|
87
|
-
config: ConnectConfig;
|
|
88
|
-
rpcUrls: { [chain in Chain]?: string };
|
|
89
|
-
};
|