@swapkit/core 1.0.0-rc.126 → 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.map CHANGED
@@ -3,11 +3,11 @@
3
3
  "sources": ["../src/index.ts", "../src/client.ts", "../src/helpers/explorerUrls.ts", "../src/client.ts"],
4
4
  "sourcesContent": [
5
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",
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 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"
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,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;",
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
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.54",
5
- "@swapkit/helpers": "1.0.0-rc.89"
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.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",
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.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"
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.126"
48
+ "version": "1.0.0-rc.127"
49
49
  }
package/src/client.ts CHANGED
@@ -3,38 +3,18 @@ import {
3
3
  ApproveMode,
4
4
  type ApproveReturnType,
5
5
  AssetValue,
6
- type BaseWallet,
7
6
  Chain,
8
7
  type ConnectConfig,
9
- type ConnectWalletParams,
10
8
  SwapKitError,
11
9
  type SwapParams,
12
10
  } from "@swapkit/helpers";
13
- import type { CosmosWallets, ThorchainWallets } from "@swapkit/toolbox-cosmos";
14
- import type { BaseEVMWallet, EVMWallets } from "@swapkit/toolbox-evm";
15
- import type { SubstrateWallets } from "@swapkit/toolbox-substrate";
16
- import type { UTXOWallets } from "@swapkit/toolbox-utxo";
11
+ import type { BaseEVMWallet } from "@swapkit/toolbox-evm";
12
+
17
13
  import {
18
14
  getExplorerAddressUrl as getAddressUrl,
19
15
  getExplorerTxUrl as getTxUrl,
20
16
  } from "./helpers/explorerUrls.ts";
21
-
22
- export type Wallet = BaseWallet<
23
- EVMWallets & CosmosWallets & ThorchainWallets & UTXOWallets & SubstrateWallets
24
- >;
25
-
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;
37
- };
17
+ import type { Apis, SwapKitPluginInterface, SwapKitWallet, Wallet } from "./types.ts";
38
18
 
39
19
  export function SwapKit<
40
20
  Plugins extends { [key in string]: SwapKitPluginInterface<{ [key in string]: Todo }> },
@@ -47,7 +27,7 @@ export function SwapKit<
47
27
  stagenet = false,
48
28
  wallets,
49
29
  }: {
50
- apis?: { [key in Chain]?: Todo };
30
+ apis?: Apis;
51
31
  config?: ConnectConfig;
52
32
  plugins: Plugins;
53
33
  rpcUrls?: { [key in Chain]?: string };
@@ -78,7 +58,6 @@ export function SwapKit<
78
58
  : wallets;
79
59
 
80
60
  const connectedWallets = {} as Wallet;
81
-
82
61
  const availablePlugins = Object.entries(compatPlugins).reduce(
83
62
  (acc, [pluginName, { plugin, config }]) => {
84
63
  const methods = plugin({ wallets: connectedWallets, stagenet, config });
package/src/types.ts ADDED
@@ -0,0 +1,41 @@
1
+ import type {
2
+ BaseWallet,
3
+ Chain,
4
+ ConnectWalletParams,
5
+ CosmosChain,
6
+ UTXOChain,
7
+ } from "@swapkit/helpers";
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;
28
+ };
29
+
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;
41
+ };