@swapkit/core 1.0.0-rc.166 → 1.0.0-rc.168

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/package.json CHANGED
@@ -1,26 +1,26 @@
1
1
  {
2
2
  "author": "swapkit-oss",
3
3
  "dependencies": {
4
- "@swapkit/api": "1.0.0-rc.79",
5
- "@swapkit/helpers": "1.0.0-rc.111"
4
+ "@swapkit/api": "1.0.0-rc.81",
5
+ "@swapkit/helpers": "1.0.0-rc.113"
6
6
  },
7
7
  "description": "SwapKit - Core",
8
8
  "devDependencies": {
9
- "@swapkit/tokens": "1.0.0-rc.53",
10
- "@swapkit/toolbox-cosmos": "1.0.0-rc.138",
11
- "@swapkit/toolbox-evm": "1.0.0-rc.118",
12
- "@swapkit/toolbox-substrate": "1.0.0-rc.46",
13
- "@swapkit/toolbox-utxo": "1.0.0-rc.122",
9
+ "@swapkit/tokens": "1.0.0-rc.55",
10
+ "@swapkit/toolbox-cosmos": "1.0.0-rc.140",
11
+ "@swapkit/toolbox-evm": "1.0.0-rc.120",
12
+ "@swapkit/toolbox-substrate": "1.0.0-rc.48",
13
+ "@swapkit/toolbox-utxo": "1.0.0-rc.124",
14
14
  "bun-types": "1.1.8"
15
15
  },
16
16
  "peerDependencies": {
17
- "@swapkit/api": "1.0.0-rc.79",
18
- "@swapkit/helpers": "1.0.0-rc.111",
19
- "@swapkit/tokens": "1.0.0-rc.53",
20
- "@swapkit/toolbox-cosmos": "1.0.0-rc.138",
21
- "@swapkit/toolbox-evm": "1.0.0-rc.118",
22
- "@swapkit/toolbox-substrate": "1.0.0-rc.46",
23
- "@swapkit/toolbox-utxo": "1.0.0-rc.122"
17
+ "@swapkit/api": "1.0.0-rc.81",
18
+ "@swapkit/helpers": "1.0.0-rc.113",
19
+ "@swapkit/tokens": "1.0.0-rc.55",
20
+ "@swapkit/toolbox-cosmos": "1.0.0-rc.140",
21
+ "@swapkit/toolbox-evm": "1.0.0-rc.120",
22
+ "@swapkit/toolbox-substrate": "1.0.0-rc.48",
23
+ "@swapkit/toolbox-utxo": "1.0.0-rc.124"
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.166"
48
+ "version": "1.0.0-rc.168"
49
49
  }
package/src/client.ts CHANGED
@@ -7,10 +7,13 @@ import {
7
7
  type ConnectConfig,
8
8
  type EVMChain,
9
9
  EVMChains,
10
- type ProviderName as PluginNameEnum,
10
+ type FeeOption,
11
+ ProviderName as PluginNameEnum,
11
12
  SwapKitError,
12
13
  type SwapParams,
14
+ type UTXOChain,
13
15
  type WalletChain,
16
+ isGasAsset,
14
17
  } from "@swapkit/helpers";
15
18
  import {
16
19
  type BaseEVMWallet,
@@ -20,10 +23,12 @@ import {
20
23
 
21
24
  import {
22
25
  type TransferParams as CosmosTransferParams,
26
+ estimateTransactionFee as cosmosTransactionFee,
23
27
  cosmosValidateAddress,
24
28
  } from "@swapkit/toolbox-cosmos";
25
29
  import { substrateValidateAddress } from "@swapkit/toolbox-substrate";
26
30
  import { type UTXOTransferParams, utxoValidateAddress } from "@swapkit/toolbox-utxo";
31
+ import { lowercasedContractAbiMapping } from "./aggregator/contracts/index.ts";
27
32
  import {
28
33
  getExplorerAddressUrl as getAddressUrl,
29
34
  getExplorerTxUrl as getTxUrl,
@@ -304,6 +309,111 @@ export function SwapKit<
304
309
  delete connectedWallets[chain];
305
310
  }
306
311
 
312
+ // biome-ignore lint/complexity/noExcessiveCognitiveComplexity: TODO clean this up
313
+ async function estimateTransactionFee<T extends PluginName>({
314
+ type,
315
+ feeOptionKey,
316
+ params,
317
+ }: (
318
+ | { type: "swap"; params: SwapParams<T> & { assetValue: AssetValue } }
319
+ | {
320
+ type: "transfer";
321
+ params: UTXOTransferParams | EVMTransferParams | CosmosTransferParams;
322
+ }
323
+ | {
324
+ type: "approve";
325
+ params: {
326
+ assetValue: AssetValue;
327
+ contractAddress: string | PluginName;
328
+ feeOptionKey?: FeeOption;
329
+ };
330
+ }
331
+ ) & {
332
+ feeOptionKey: FeeOption;
333
+ }): Promise<AssetValue | undefined> {
334
+ const { assetValue } = params;
335
+ const chain = params.assetValue.chain as WalletChain;
336
+ if (!connectedWallets[chain]) throw new SwapKitError("core_wallet_connection_not_found");
337
+ switch (chain) {
338
+ case Chain.Arbitrum:
339
+ case Chain.Avalanche:
340
+ case Chain.Ethereum:
341
+ case Chain.BinanceSmartChain:
342
+ case Chain.Polygon: {
343
+ const wallet = connectedWallets[chain as Exclude<EVMChain, Chain.Optimism>];
344
+ if (type === "transfer") {
345
+ const txObject = await wallet.createTransferTx(params);
346
+ return wallet.estimateTransactionFee(txObject, feeOptionKey);
347
+ }
348
+ if (type === "approve" && !isGasAsset(assetValue)) {
349
+ wallet.estimateTransactionFee(
350
+ await wallet.createApprovalTx({
351
+ assetAddress: assetValue.address as string,
352
+ spenderAddress: params.contractAddress as string,
353
+ amount: assetValue.getBaseValue("bigint"),
354
+ from: wallet.address,
355
+ }),
356
+ feeOptionKey,
357
+ );
358
+ }
359
+ if (type === "swap") {
360
+ const plugin = params.route.providers[0] as PluginNameEnum;
361
+ if (plugin === PluginNameEnum.CHAINFLIP) {
362
+ const txObject = await wallet.createTransferTx({
363
+ from: wallet.address,
364
+ recipient: wallet.address,
365
+ assetValue,
366
+ });
367
+ return wallet.estimateTransactionFee(txObject, feeOptionKey);
368
+ }
369
+ const {
370
+ route: { evmTransactionDetails },
371
+ } = params;
372
+ if (
373
+ !(
374
+ evmTransactionDetails &&
375
+ lowercasedContractAbiMapping[evmTransactionDetails.contractAddress]
376
+ )
377
+ )
378
+ return undefined;
379
+ wallet.estimateCall({
380
+ contractAddress: evmTransactionDetails.contractAddress,
381
+ // biome-ignore lint/style/noNonNullAssertion: TS cant infer the type
382
+ abi: lowercasedContractAbiMapping[evmTransactionDetails.contractAddress]!,
383
+ funcName: evmTransactionDetails.contractMethod,
384
+ funcParams: evmTransactionDetails.contractParams,
385
+ });
386
+ }
387
+ return AssetValue.fromChainOrSignature(chain, 0);
388
+ }
389
+ case Chain.Bitcoin:
390
+ case Chain.BitcoinCash:
391
+ case Chain.Dogecoin:
392
+ case Chain.Dash:
393
+ case Chain.Litecoin: {
394
+ const wallet = connectedWallets[chain as UTXOChain];
395
+ return wallet.estimateTransactionFee({
396
+ ...params,
397
+ feeOptionKey,
398
+ from: wallet.address,
399
+ recipient: wallet.address,
400
+ });
401
+ }
402
+ case Chain.THORChain:
403
+ case Chain.Maya:
404
+ case Chain.Kujira:
405
+ case Chain.Cosmos: {
406
+ return cosmosTransactionFee(params);
407
+ }
408
+ case Chain.Polkadot: {
409
+ const wallet = connectedWallets[chain as Chain.Polkadot];
410
+ return wallet.estimateTransactionFee({ ...params, recipient: wallet.address });
411
+ }
412
+ default:
413
+ return undefined;
414
+ }
415
+ }
416
+
307
417
  return {
308
418
  ...availablePlugins,
309
419
  ...connectWalletMethods,
@@ -317,6 +427,7 @@ export function SwapKit<
317
427
  getAllWallets,
318
428
  getWalletWithBalance,
319
429
  isAssetValueApproved,
430
+ estimateTransactionFee,
320
431
  swap,
321
432
  transfer,
322
433
  validateAddress,