@swapkit/core 1.0.0-rc.131 → 1.0.0-rc.132
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 +6 -2
- package/dist/index.js.map +3 -3
- package/package.json +13 -13
- package/src/client.ts +11 -4
package/dist/index.js
CHANGED
|
@@ -88,6 +88,10 @@ function SwapKit({
|
|
|
88
88
|
type = "checkOnly",
|
|
89
89
|
contractAddress: spenderAddress
|
|
90
90
|
}) {
|
|
91
|
+
const plugin = availablePlugins[spenderAddress];
|
|
92
|
+
if ("approve" in plugin) {
|
|
93
|
+
return plugin.approve({ assetValue, type, spenderAddress });
|
|
94
|
+
}
|
|
91
95
|
const { address, chain, isGasAsset, isSynthetic } = assetValue;
|
|
92
96
|
const isEVMChain = [Chain2.Ethereum, Chain2.Avalanche, Chain2.BinanceSmartChain].includes(chain);
|
|
93
97
|
const isNativeEVM = isEVMChain && isGasAsset;
|
|
@@ -99,7 +103,7 @@ function SwapKit({
|
|
|
99
103
|
if (!walletAction)
|
|
100
104
|
throw new SwapKitError("core_wallet_connection_not_found");
|
|
101
105
|
const from = getAddress(chain);
|
|
102
|
-
if (!(address && from)) {
|
|
106
|
+
if (!(address && from && typeof spenderAddress === "string")) {
|
|
103
107
|
throw new SwapKitError("core_approve_asset_address_or_from_not_found");
|
|
104
108
|
}
|
|
105
109
|
return walletAction({
|
|
@@ -168,4 +172,4 @@ export {
|
|
|
168
172
|
SwapKit
|
|
169
173
|
};
|
|
170
174
|
|
|
171
|
-
//# debugId=
|
|
175
|
+
//# debugId=5F55B916EC553AF964756e2164756e21
|
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 Chain,\n type ConnectConfig,\n SwapKitError,\n type SwapParams,\n type Wallet,\n} from \"@swapkit/helpers\";\nimport type { BaseEVMWallet } from \"@swapkit/toolbox-evm\";\n\nimport {\n getExplorerAddressUrl as getAddressUrl,\n getExplorerTxUrl as getTxUrl,\n} from \"./helpers/explorerUrls.ts\";\nimport type { Apis, SwapKitPluginInterface, SwapKitWallet } from \"./types.ts\";\n\nexport function SwapKit<\n Plugins extends { [key in string]: SwapKitPluginInterface<{ [key in string]: Todo }> },\n Wallets extends { [key in string]: SwapKitWallet<NotWorth[]> },\n>({\n apis = {},\n config = {},\n plugins,\n rpcUrls = {},\n stagenet = false,\n wallets,\n}: {\n apis?: Apis;\n config?: ConnectConfig;\n plugins: Plugins;\n rpcUrls?: { [key in Chain]?: string };\n stagenet?: boolean;\n wallets: Wallets;\n}) {\n type PluginName = keyof Plugins;\n\n /**\n * @REMOVE (V1)\n * Compatibility layer for plugins and wallets for easier migration and backwards compatibility\n */\n const compatPlugins: Plugins = Array.isArray(plugins)\n ? plugins.reduce((acc, pluginInterface) => {\n // @ts-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 */\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 */\n function getWallet<T extends Chain>(chain: T) {\n return connectedWallets[chain];\n }\n function getAddress<T extends Chain>(chain: T) {\n return getWallet(chain)?.address || \"\";\n }\n /**\n * TODO: Figure out validation without connecting to wallet\n */\n function validateAddress({ address, chain }: { address: string; chain: Chain }) {\n return getWallet(chain)?.validateAddress?.(address);\n }\n\n function getBalance<T extends Chain>(chain: T, refresh?: boolean) {\n if (refresh) {\n return getWalletWithBalance(chain).then((wallet) => wallet.balance);\n }\n\n return getWallet(chain)?.balance || [];\n }\n\n async function getWalletWithBalance<T extends Chain>(chain: T, potentialScamFilter = true) {\n const defaultBalance = [AssetValue.fromChainOrSignature(chain)];\n const wallet = getWallet(chain);\n\n if (!wallet) {\n throw new SwapKitError(\"core_wallet_connection_not_found\");\n }\n\n if (\"getBalance\" in wallet) {\n const balance = await wallet.getBalance(wallet.address, potentialScamFilter);\n wallet.balance = balance?.length ? balance : defaultBalance;\n }\n\n return wallet;\n }\n\n function approveAssetValue(assetValue: AssetValue, contractAddress: string) {\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 approveAssetValue,\n getAddress,\n getBalance,\n getExplorerAddressUrl: getAddressUrl,\n getExplorerTxUrl: getTxUrl,\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 type Wallet,\n} from \"@swapkit/helpers\";\nimport type { BaseEVMWallet } from \"@swapkit/toolbox-evm\";\n\nimport {\n getExplorerAddressUrl as getAddressUrl,\n getExplorerTxUrl as getTxUrl,\n} from \"./helpers/explorerUrls.ts\";\nimport type { Apis, SwapKitPluginInterface, SwapKitWallet } from \"./types.ts\";\n\nexport function SwapKit<\n Plugins extends { [key in string]: SwapKitPluginInterface<{ [key in string]: Todo }> },\n Wallets extends { [key in string]: SwapKitWallet<NotWorth[]> },\n>({\n apis = {},\n config = {},\n plugins,\n rpcUrls = {},\n stagenet = false,\n wallets,\n}: {\n apis?: Apis;\n config?: ConnectConfig;\n plugins: Plugins;\n rpcUrls?: { [key in Chain]?: string };\n stagenet?: boolean;\n wallets: Wallets;\n}) {\n type PluginName = keyof Plugins;\n\n /**\n * @REMOVE (V1)\n * Compatibility layer for plugins and wallets for easier migration and backwards compatibility\n */\n const compatPlugins: Plugins = Array.isArray(plugins)\n ? plugins.reduce((acc, pluginInterface) => {\n // @ts-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 */\n function getSwapKitPlugin<T extends PluginName>(pluginName: T) {\n const plugin = availablePlugins[pluginName] || Object.values(availablePlugins)[0];\n\n if (!plugin) {\n throw new SwapKitError(\"core_plugin_not_found\", \"Could not find the requested plugin\");\n }\n\n return plugin;\n }\n\n function addChain<T extends Chain>(connectWallet: AddChainWalletParams<T>) {\n // @ts-expect-error: TODO\n connectedWallets[connectWallet.chain] = connectWallet;\n }\n\n // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>\n function approve<T extends ApproveMode>({\n assetValue,\n type = \"checkOnly\" as T,\n contractAddress: spenderAddress,\n }: {\n type: T;\n assetValue: AssetValue;\n contractAddress: string | PluginName;\n }) {\n const plugin = availablePlugins[spenderAddress];\n\n if (\"approve\" in plugin) {\n return plugin.approve({ assetValue, type, spenderAddress }) as ApproveReturnType<T>;\n }\n\n const { address, chain, isGasAsset, isSynthetic } = assetValue;\n const isEVMChain = [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 && typeof spenderAddress === \"string\")) {\n throw new SwapKitError(\"core_approve_asset_address_or_from_not_found\");\n }\n\n return walletAction({\n amount: assetValue.getBaseValue(\"bigint\"),\n assetAddress: address,\n from,\n spenderAddress,\n }) as ApproveReturnType<T>;\n }\n\n /**\n * @Public\n */\n function getWallet<T extends Chain>(chain: T) {\n return connectedWallets[chain];\n }\n function getAddress<T extends Chain>(chain: T) {\n return getWallet(chain)?.address || \"\";\n }\n /**\n * TODO: Figure out validation without connecting to wallet\n */\n function validateAddress({ address, chain }: { address: string; chain: Chain }) {\n return getWallet(chain)?.validateAddress?.(address);\n }\n\n function getBalance<T extends Chain>(chain: T, refresh?: boolean) {\n if (refresh) {\n return getWalletWithBalance(chain).then((wallet) => wallet.balance);\n }\n\n return getWallet(chain)?.balance || [];\n }\n\n async function getWalletWithBalance<T extends Chain>(chain: T, potentialScamFilter = true) {\n const defaultBalance = [AssetValue.fromChainOrSignature(chain)];\n const wallet = getWallet(chain);\n\n if (!wallet) {\n throw new SwapKitError(\"core_wallet_connection_not_found\");\n }\n\n if (\"getBalance\" in wallet) {\n const balance = await wallet.getBalance(wallet.address, potentialScamFilter);\n wallet.balance = balance?.length ? balance : defaultBalance;\n }\n\n return wallet;\n }\n\n function approveAssetValue(assetValue: AssetValue, contractAddress: string | PluginName) {\n return approve({ assetValue, contractAddress, type: ApproveMode.Approve });\n }\n\n function isAssetValueApproved(assetValue: AssetValue, contractAddress: string | PluginName) {\n return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });\n }\n\n function swap<T extends PluginName>({ pluginName, ...rest }: SwapParams<T>) {\n const plugin = getSwapKitPlugin(pluginName);\n\n if (\"swap\" in plugin) {\n return plugin.swap(rest);\n }\n\n throw new SwapKitError(\"core_plugin_swap_not_found\");\n }\n\n return {\n ...availablePlugins,\n ...connectWalletMethods,\n\n approveAssetValue,\n getAddress,\n getBalance,\n getExplorerAddressUrl: getAddressUrl,\n getExplorerTxUrl: getTxUrl,\n getWallet,\n getWalletWithBalance,\n isAssetValueApproved,\n swap,\n validateAddress,\n };\n}\n",
|
|
7
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 Chain,\n type ConnectConfig,\n SwapKitError,\n type SwapParams,\n type Wallet,\n} from \"@swapkit/helpers\";\nimport type { BaseEVMWallet } from \"@swapkit/toolbox-evm\";\n\nimport {\n getExplorerAddressUrl as getAddressUrl,\n getExplorerTxUrl as getTxUrl,\n} from \"./helpers/explorerUrls.ts\";\nimport type { Apis, SwapKitPluginInterface, SwapKitWallet } from \"./types.ts\";\n\nexport function SwapKit<\n Plugins extends { [key in string]: SwapKitPluginInterface<{ [key in string]: Todo }> },\n Wallets extends { [key in string]: SwapKitWallet<NotWorth[]> },\n>({\n apis = {},\n config = {},\n plugins,\n rpcUrls = {},\n stagenet = false,\n wallets,\n}: {\n apis?: Apis;\n config?: ConnectConfig;\n plugins: Plugins;\n rpcUrls?: { [key in Chain]?: string };\n stagenet?: boolean;\n wallets: Wallets;\n}) {\n type PluginName = keyof Plugins;\n\n /**\n * @REMOVE (V1)\n * Compatibility layer for plugins and wallets for easier migration and backwards compatibility\n */\n const compatPlugins: Plugins = Array.isArray(plugins)\n ? plugins.reduce((acc, pluginInterface) => {\n // @ts-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 */\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 */\n function getWallet<T extends Chain>(chain: T) {\n return connectedWallets[chain];\n }\n function getAddress<T extends Chain>(chain: T) {\n return getWallet(chain)?.address || \"\";\n }\n /**\n * TODO: Figure out validation without connecting to wallet\n */\n function validateAddress({ address, chain }: { address: string; chain: Chain }) {\n return getWallet(chain)?.validateAddress?.(address);\n }\n\n function getBalance<T extends Chain>(chain: T, refresh?: boolean) {\n if (refresh) {\n return getWalletWithBalance(chain).then((wallet) => wallet.balance);\n }\n\n return getWallet(chain)?.balance || [];\n }\n\n async function getWalletWithBalance<T extends Chain>(chain: T, potentialScamFilter = true) {\n const defaultBalance = [AssetValue.fromChainOrSignature(chain)];\n const wallet = getWallet(chain);\n\n if (!wallet) {\n throw new SwapKitError(\"core_wallet_connection_not_found\");\n }\n\n if (\"getBalance\" in wallet) {\n const balance = await wallet.getBalance(wallet.address, potentialScamFilter);\n wallet.balance = balance?.length ? balance : defaultBalance;\n }\n\n return wallet;\n }\n\n function approveAssetValue(assetValue: AssetValue, contractAddress: string) {\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 approveAssetValue,\n getAddress,\n getBalance,\n getExplorerAddressUrl: getAddressUrl,\n getExplorerTxUrl: getTxUrl,\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 type Wallet,\n} from \"@swapkit/helpers\";\nimport type { BaseEVMWallet } from \"@swapkit/toolbox-evm\";\n\nimport {\n getExplorerAddressUrl as getAddressUrl,\n getExplorerTxUrl as getTxUrl,\n} from \"./helpers/explorerUrls.ts\";\nimport type { Apis, SwapKitPluginInterface, SwapKitWallet } from \"./types.ts\";\n\nexport function SwapKit<\n Plugins extends { [key in string]: SwapKitPluginInterface<{ [key in string]: Todo }> },\n Wallets extends { [key in string]: SwapKitWallet<NotWorth[]> },\n>({\n apis = {},\n config = {},\n plugins,\n rpcUrls = {},\n stagenet = false,\n wallets,\n}: {\n apis?: Apis;\n config?: ConnectConfig;\n plugins: Plugins;\n rpcUrls?: { [key in Chain]?: string };\n stagenet?: boolean;\n wallets: Wallets;\n}) {\n type PluginName = keyof Plugins;\n\n /**\n * @REMOVE (V1)\n * Compatibility layer for plugins and wallets for easier migration and backwards compatibility\n */\n const compatPlugins: Plugins = Array.isArray(plugins)\n ? plugins.reduce((acc, pluginInterface) => {\n // @ts-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 */\n function getSwapKitPlugin<T extends PluginName>(pluginName: T) {\n const plugin = availablePlugins[pluginName] || Object.values(availablePlugins)[0];\n\n if (!plugin) {\n throw new SwapKitError(\"core_plugin_not_found\", \"Could not find the requested plugin\");\n }\n\n return plugin;\n }\n\n function addChain<T extends Chain>(connectWallet: AddChainWalletParams<T>) {\n // @ts-expect-error: TODO\n connectedWallets[connectWallet.chain] = connectWallet;\n }\n\n // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>\n function approve<T extends ApproveMode>({\n assetValue,\n type = \"checkOnly\" as T,\n contractAddress: spenderAddress,\n }: {\n type: T;\n assetValue: AssetValue;\n contractAddress: string | PluginName;\n }) {\n const plugin = availablePlugins[spenderAddress];\n\n if (\"approve\" in plugin) {\n return plugin.approve({ assetValue, type, spenderAddress }) as ApproveReturnType<T>;\n }\n\n const { address, chain, isGasAsset, isSynthetic } = assetValue;\n const isEVMChain = [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 && typeof spenderAddress === \"string\")) {\n throw new SwapKitError(\"core_approve_asset_address_or_from_not_found\");\n }\n\n return walletAction({\n amount: assetValue.getBaseValue(\"bigint\"),\n assetAddress: address,\n from,\n spenderAddress,\n }) as ApproveReturnType<T>;\n }\n\n /**\n * @Public\n */\n function getWallet<T extends Chain>(chain: T) {\n return connectedWallets[chain];\n }\n function getAddress<T extends Chain>(chain: T) {\n return getWallet(chain)?.address || \"\";\n }\n /**\n * TODO: Figure out validation without connecting to wallet\n */\n function validateAddress({ address, chain }: { address: string; chain: Chain }) {\n return getWallet(chain)?.validateAddress?.(address);\n }\n\n function getBalance<T extends Chain>(chain: T, refresh?: boolean) {\n if (refresh) {\n return getWalletWithBalance(chain).then((wallet) => wallet.balance);\n }\n\n return getWallet(chain)?.balance || [];\n }\n\n async function getWalletWithBalance<T extends Chain>(chain: T, potentialScamFilter = true) {\n const defaultBalance = [AssetValue.fromChainOrSignature(chain)];\n const wallet = getWallet(chain);\n\n if (!wallet) {\n throw new SwapKitError(\"core_wallet_connection_not_found\");\n }\n\n if (\"getBalance\" in wallet) {\n const balance = await wallet.getBalance(wallet.address, potentialScamFilter);\n wallet.balance = balance?.length ? balance : defaultBalance;\n }\n\n return wallet;\n }\n\n function approveAssetValue(assetValue: AssetValue, contractAddress: string | PluginName) {\n return approve({ assetValue, contractAddress, type: ApproveMode.Approve });\n }\n\n function isAssetValueApproved(assetValue: AssetValue, contractAddress: string | PluginName) {\n return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });\n }\n\n function swap<T extends PluginName>({ pluginName, ...rest }: SwapParams<T>) {\n const plugin = getSwapKitPlugin(pluginName);\n\n if (\"swap\" in plugin) {\n return plugin.swap(rest);\n }\n\n throw new SwapKitError(\"core_plugin_swap_not_found\");\n }\n\n return {\n ...availablePlugins,\n ...connectWalletMethods,\n\n approveAssetValue,\n getAddress,\n getBalance,\n getExplorerAddressUrl: getAddressUrl,\n getExplorerTxUrl: getTxUrl,\n getWallet,\n getWalletWithBalance,\n isAssetValueApproved,\n swap,\n validateAddress,\n };\n}\n"
|
|
9
9
|
],
|
|
10
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": "
|
|
11
|
+
"debugId": "5F55B916EC553AF964756e2164756e21",
|
|
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.58",
|
|
5
|
+
"@swapkit/helpers": "1.0.0-rc.92"
|
|
6
6
|
},
|
|
7
7
|
"description": "SwapKit - Core",
|
|
8
8
|
"devDependencies": {
|
|
9
9
|
"@swapkit/tokens": "1.0.0-rc.50",
|
|
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.
|
|
10
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.111",
|
|
11
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.98",
|
|
12
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.27",
|
|
13
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.101",
|
|
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.
|
|
17
|
+
"@swapkit/api": "1.0.0-rc.58",
|
|
18
|
+
"@swapkit/helpers": "1.0.0-rc.92",
|
|
19
19
|
"@swapkit/tokens": "1.0.0-rc.50",
|
|
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.
|
|
20
|
+
"@swapkit/toolbox-cosmos": "1.0.0-rc.111",
|
|
21
|
+
"@swapkit/toolbox-evm": "1.0.0-rc.98",
|
|
22
|
+
"@swapkit/toolbox-substrate": "1.0.0-rc.27",
|
|
23
|
+
"@swapkit/toolbox-utxo": "1.0.0-rc.101"
|
|
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.132"
|
|
49
49
|
}
|
package/src/client.ts
CHANGED
|
@@ -99,6 +99,7 @@ export function SwapKit<
|
|
|
99
99
|
connectedWallets[connectWallet.chain] = connectWallet;
|
|
100
100
|
}
|
|
101
101
|
|
|
102
|
+
// biome-ignore lint/complexity/noExcessiveCognitiveComplexity: <explanation>
|
|
102
103
|
function approve<T extends ApproveMode>({
|
|
103
104
|
assetValue,
|
|
104
105
|
type = "checkOnly" as T,
|
|
@@ -106,8 +107,14 @@ export function SwapKit<
|
|
|
106
107
|
}: {
|
|
107
108
|
type: T;
|
|
108
109
|
assetValue: AssetValue;
|
|
109
|
-
contractAddress: string;
|
|
110
|
+
contractAddress: string | PluginName;
|
|
110
111
|
}) {
|
|
112
|
+
const plugin = availablePlugins[spenderAddress];
|
|
113
|
+
|
|
114
|
+
if ("approve" in plugin) {
|
|
115
|
+
return plugin.approve({ assetValue, type, spenderAddress }) as ApproveReturnType<T>;
|
|
116
|
+
}
|
|
117
|
+
|
|
111
118
|
const { address, chain, isGasAsset, isSynthetic } = assetValue;
|
|
112
119
|
const isEVMChain = [Chain.Ethereum, Chain.Avalanche, Chain.BinanceSmartChain].includes(chain);
|
|
113
120
|
const isNativeEVM = isEVMChain && isGasAsset;
|
|
@@ -121,7 +128,7 @@ export function SwapKit<
|
|
|
121
128
|
if (!walletAction) throw new SwapKitError("core_wallet_connection_not_found");
|
|
122
129
|
|
|
123
130
|
const from = getAddress(chain);
|
|
124
|
-
if (!(address && from)) {
|
|
131
|
+
if (!(address && from && typeof spenderAddress === "string")) {
|
|
125
132
|
throw new SwapKitError("core_approve_asset_address_or_from_not_found");
|
|
126
133
|
}
|
|
127
134
|
|
|
@@ -173,11 +180,11 @@ export function SwapKit<
|
|
|
173
180
|
return wallet;
|
|
174
181
|
}
|
|
175
182
|
|
|
176
|
-
function approveAssetValue(assetValue: AssetValue, contractAddress: string) {
|
|
183
|
+
function approveAssetValue(assetValue: AssetValue, contractAddress: string | PluginName) {
|
|
177
184
|
return approve({ assetValue, contractAddress, type: ApproveMode.Approve });
|
|
178
185
|
}
|
|
179
186
|
|
|
180
|
-
function isAssetValueApproved(assetValue: AssetValue, contractAddress: string) {
|
|
187
|
+
function isAssetValueApproved(assetValue: AssetValue, contractAddress: string | PluginName) {
|
|
181
188
|
return approve({ assetValue, contractAddress, type: ApproveMode.CheckOnly });
|
|
182
189
|
}
|
|
183
190
|
|