@swapkit/helpers 3.0.0-beta.13 → 3.0.0-beta.15

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
@@ -8,7 +8,7 @@
8
8
  },
9
9
  "description": "SwapKit - Helpers",
10
10
  "devDependencies": {
11
- "@swapkit/toolboxes": "1.0.0-beta.18"
11
+ "@swapkit/toolboxes": "1.0.0-beta.20"
12
12
  },
13
13
  "exports": {
14
14
  ".": {
@@ -55,5 +55,5 @@
55
55
  "type-check:go": "tsgo"
56
56
  },
57
57
  "type": "module",
58
- "version": "3.0.0-beta.13"
58
+ "version": "3.0.0-beta.15"
59
59
  }
package/src/index.ts CHANGED
@@ -7,6 +7,7 @@ export * from "./modules/requestClient";
7
7
  export * from "./modules/swapKitError";
8
8
  export * from "./modules/swapKitNumber";
9
9
  export * from "./modules/swapKitConfig";
10
+ export * from "./modules/feeMultiplier";
10
11
 
11
12
  /**
12
13
  * Utils
@@ -0,0 +1,87 @@
1
+ import { FeeOption } from "../types";
2
+ import { SKConfig } from "./swapKitConfig";
3
+
4
+ export interface FeeMultiplierConfig {
5
+ [FeeOption.Average]: number;
6
+ [FeeOption.Fast]: number;
7
+ [FeeOption.Fastest]: number;
8
+ }
9
+
10
+ const DEFAULT_FEE_MULTIPLIERS: FeeMultiplierConfig = {
11
+ [FeeOption.Average]: 1.0,
12
+ [FeeOption.Fast]: 1.5,
13
+ [FeeOption.Fastest]: 2.0,
14
+ };
15
+
16
+ /**
17
+ * Get fee multiplier for the given fee option.
18
+ * Checks SKConfig for custom multipliers first, then falls back to defaults.
19
+ *
20
+ * @param feeOption - The fee option (Average, Fast, Fastest)
21
+ * @returns The fee multiplier as a number
22
+ */
23
+ export function getFeeMultiplier(feeOption: FeeOption = FeeOption.Average): number {
24
+ const customMultipliers = SKConfig.get("feeMultipliers");
25
+
26
+ if (customMultipliers && customMultipliers[feeOption] !== undefined) {
27
+ return customMultipliers[feeOption];
28
+ }
29
+
30
+ return DEFAULT_FEE_MULTIPLIERS[feeOption];
31
+ }
32
+
33
+ /**
34
+ * Get fee multiplier as BigInt for EVM calculations.
35
+ * Returns numerator and denominator for precise BigInt arithmetic.
36
+ *
37
+ * @param feeOption - The fee option (Average, Fast, Fastest)
38
+ * @returns Object with numerator and denominator for BigInt calculations
39
+ */
40
+ export function getFeeMultiplierAsBigInt(feeOption: FeeOption = FeeOption.Average): {
41
+ numerator: bigint;
42
+ denominator: bigint;
43
+ } {
44
+ const multiplier = getFeeMultiplier(feeOption);
45
+
46
+ // Convert decimal multiplier to fraction for precise BigInt arithmetic
47
+ // e.g., 1.5 -> 15/10, 2.0 -> 20/10
48
+ const denominator = 10n;
49
+ const numerator = BigInt(Math.round(multiplier * 10));
50
+
51
+ return { numerator, denominator };
52
+ }
53
+
54
+ /**
55
+ * Apply fee multiplier to a BigInt value (for EVM chains).
56
+ *
57
+ * @param value - The base fee value as BigInt
58
+ * @param feeOption - The fee option (Average, Fast, Fastest)
59
+ * @returns The multiplied fee value as BigInt
60
+ */
61
+ export function applyFeeMultiplierToBigInt(
62
+ value: bigint,
63
+ feeOption: FeeOption = FeeOption.Average,
64
+ ): bigint {
65
+ const { numerator, denominator } = getFeeMultiplierAsBigInt(feeOption);
66
+ return (value * numerator) / denominator;
67
+ }
68
+
69
+ /**
70
+ * Apply fee multiplier to a number value (for non-EVM chains).
71
+ *
72
+ * @param value - The base fee value as number
73
+ * @param feeOption - The fee option (Average, Fast, Fastest)
74
+ * @param floor - Whether to floor the result (default: false)
75
+ * @returns The multiplied fee value as number
76
+ */
77
+ export function applyFeeMultiplier(
78
+ value: number,
79
+ feeOption: FeeOption = FeeOption.Average,
80
+ floor = false,
81
+ ): number {
82
+ const multiplier = getFeeMultiplier(feeOption);
83
+ const result = value * multiplier;
84
+ return floor ? Math.floor(result) : result;
85
+ }
86
+
87
+ export { DEFAULT_FEE_MULTIPLIERS };
@@ -1,5 +1,6 @@
1
1
  import { createStore } from "zustand/vanilla";
2
2
  import { Chain, EXPLORER_URLS, NODE_URLS, RPC_URLS, WalletOption } from "../types";
3
+ import type { FeeMultiplierConfig } from "./feeMultiplier";
3
4
 
4
5
  export type SKConfigIntegrations = {
5
6
  chainflip?: { useSDKBroker?: boolean; brokerUrl: string };
@@ -60,6 +61,8 @@ const initialState = {
60
61
  },
61
62
  },
62
63
  } as SKConfigIntegrations,
64
+
65
+ feeMultipliers: undefined as FeeMultiplierConfig | undefined,
63
66
  };
64
67
  type SKState = typeof initialState;
65
68
 
@@ -72,6 +75,7 @@ export type SKConfigState = {
72
75
  nodeUrls?: Partial<SKState["nodeUrls"]>;
73
76
  rpcUrls?: Partial<SKState["rpcUrls"]>;
74
77
  wallets?: SKState["wallets"];
78
+ feeMultipliers?: FeeMultiplierConfig;
75
79
  };
76
80
 
77
81
  type SwapKitConfigStore = SKState & {
@@ -85,6 +89,7 @@ type SwapKitConfigStore = SKState & {
85
89
  integration: keyof SKState["integrations"],
86
90
  config: SKConfigIntegrations[keyof SKConfigIntegrations],
87
91
  ) => void;
92
+ setFeeMultipliers: (multipliers: FeeMultiplierConfig) => void;
88
93
  };
89
94
 
90
95
  const swapKitState = createStore<SwapKitConfigStore>((set) => ({
@@ -98,6 +103,7 @@ const swapKitState = createStore<SwapKitConfigStore>((set) => ({
98
103
  setRpcUrl: (chain, url) => set((s) => ({ rpcUrls: { ...s.rpcUrls, [chain]: url } })),
99
104
  setIntegrationConfig: (integration, config) =>
100
105
  set((s) => ({ integrations: { ...s.integrations, [integration]: config } })),
106
+ setFeeMultipliers: (multipliers) => set(() => ({ feeMultipliers: multipliers })),
101
107
  setConfig: (config) =>
102
108
  set((s) => ({
103
109
  apiKeys: { ...s.apiKeys, ...config.apiKeys },
@@ -106,6 +112,7 @@ const swapKitState = createStore<SwapKitConfigStore>((set) => ({
106
112
  integrations: { ...s.integrations, ...config.integrations },
107
113
  nodeUrls: { ...s.nodeUrls, ...config.nodeUrls },
108
114
  rpcUrls: { ...s.rpcUrls, ...config.rpcUrls },
115
+ feeMultipliers: config.feeMultipliers || s.feeMultipliers,
109
116
  chains: s.chains.concat(config.chains || []),
110
117
  wallets: s.wallets.concat(config.wallets || []),
111
118
  })),
@@ -130,4 +137,6 @@ export const SKConfig = {
130
137
  integration: T,
131
138
  config: SKConfigIntegrations[T],
132
139
  ) => swapKitState.getState().setIntegrationConfig(integration, config),
140
+ setFeeMultipliers: (multipliers: FeeMultiplierConfig) =>
141
+ swapKitState.getState().setFeeMultipliers(multipliers),
133
142
  };
@@ -230,6 +230,13 @@ const errorCodes = {
230
230
  */
231
231
  wallet_keystore_invalid_password: 21901,
232
232
  wallet_keystore_unsupported_version: 21902,
233
+ /**
234
+ * Wallets - Near Extensions
235
+ */
236
+ wallet_near_extensions_failed_to_switch_network: 22001,
237
+ wallet_near_extensions_no_provider: 22002,
238
+ wallet_near_extensions_not_found: 22003,
239
+ wallet_near_method_not_supported: 22003,
233
240
  /**
234
241
  * Chainflip
235
242
  */
@@ -326,10 +333,33 @@ const errorCodes = {
326
333
  toolbox_tron_no_signer: 50801,
327
334
  toolbox_tron_invalid_token_identifier: 50802,
328
335
  toolbox_tron_token_transfer_failed: 50803,
336
+ /**
337
+ * Toolboxes - Near
338
+ */
339
+ toolbox_near_no_signer: 90601,
340
+ toolbox_near_invalid_address: 90602,
341
+ toolbox_near_invalid_amount: 90603,
342
+ toolbox_near_transfer_failed: 90604,
343
+ toolbox_near_access_key_error: 90605,
344
+ toolbox_near_no_rpc_url: 90606,
345
+ toolbox_near_empty_batch: 90607,
346
+ toolbox_near_balance_failed: 90608,
347
+ toolbox_near_invalid_name: 90609,
348
+ toolbox_near_missing_contract_address: 90610,
349
+ toolbox_near_no_account: 90611,
350
+ toolbox_near_invalid_gas_params: 90612,
329
351
  /**
330
352
  * Toolboxes - General
331
353
  */
332
- toolbox_not_supported: 50801,
354
+ toolbox_not_supported: 59901,
355
+ /**
356
+ * NEAR Plugin
357
+ */
358
+ plugin_near_invalid_name: 41001,
359
+ plugin_near_no_connection: 41002,
360
+ plugin_near_name_unavailable: 41003,
361
+ plugin_near_registration_failed: 41004,
362
+ plugin_near_transfer_failed: 41005,
333
363
  /**
334
364
  * SwapKit API
335
365
  */
@@ -245,7 +245,7 @@ export const RPC_URLS: Record<Chain | StagenetChain, string> = {
245
245
  [Chain.Kujira]: "https://kujira-rpc.ibs.team",
246
246
  [Chain.Litecoin]: "https://node-router.thorswap.net/litecoin",
247
247
  [Chain.Maya]: "https://tendermint.mayachain.info",
248
- [Chain.Near]: "https://rpc.ankr.com/near",
248
+ [Chain.Near]: "https://rpc.mainnet.near.org",
249
249
  [Chain.Optimism]: "https://mainnet.optimism.io",
250
250
  [Chain.Polkadot]: "wss://rpc.polkadot.io",
251
251
  [Chain.Polygon]: "https://polygon-rpc.com",
@@ -288,27 +288,34 @@ export const FALLBACK_URLS: Record<Chain | StagenetChain, string[]> = {
288
288
  "wss://archive-1.mainnet.chainflip.io",
289
289
  "wss://archive-2.mainnet.chainflip.io",
290
290
  ],
291
- [Chain.Cosmos]: ["https://cosmos-rpc.quickapi.com", "https://cosmos-rpc.publicnode.com"],
291
+ [Chain.Cosmos]: ["https://cosmos-rpc.publicnode.com"],
292
292
  [Chain.Dash]: ["https://dash-rpc.publicnode.com"],
293
293
  [Chain.Dogecoin]: ["https://doge.getblock.io/mainnet", "https://dogecoin.publicnode.com"],
294
- [Chain.Ethereum]: ["https://eth.llamarpc.com", "https://rpc.ankr.com/eth"],
294
+ [Chain.Ethereum]: ["https://eth.llamarpc.com", "https://cloudflare-eth.com"],
295
295
  [Chain.Fiat]: [],
296
296
  [Chain.Kujira]: ["https://kujira-rpc.polkachu.com", "https://rpc-kujira.synergynodes.com/"],
297
297
  [Chain.Litecoin]: ["https://ltc.getblock.io/mainnet", "https://litecoin.publicnode.com"],
298
298
  [Chain.Maya]: ["https://tendermint.mayachain.info", "https://maya-tendermint.publicnode.com"],
299
299
  [StagenetChain.Maya]: [],
300
- [Chain.Near]: ["https://1rpc.io/near"],
300
+ [Chain.Near]: [
301
+ "https://1rpc.io/near",
302
+ "https://near.lava.build",
303
+ "https://near-mainnet.infura.io/v3/3cbfcafa5e1e48b7bb0ea41f2fbc4abf",
304
+ ],
301
305
  [Chain.Optimism]: ["https://optimism.llamarpc.com", "https://1rpc.io/op"],
302
306
  [Chain.Polkadot]: [
303
307
  "wss://polkadot-rpc.dwellir.com",
304
308
  "wss://polkadot.api.onfinality.io/public-ws",
305
309
  ],
306
- [Chain.Polygon]: ["https://polygon.llamarpc.com", "https://rpc.ankr.com/polygon"],
310
+ [Chain.Polygon]: ["https://polygon.llamarpc.com", "https://polygon-bor-rpc.publicnode.com"],
307
311
  [Chain.Radix]: ["https://mainnet.radixdlt.com", "https://radix-mainnet.rpc.grove.city/v1"],
308
312
  [Chain.Ripple]: ["wss://s1.ripple.com/", "wss://s2.ripple.com/"],
309
313
  [Chain.THORChain]: ["https://thornode.ninerealms.com", NODE_URLS[Chain.THORChain]],
310
314
  [StagenetChain.THORChain]: [],
311
- [Chain.Solana]: ["https://api.mainnet-beta.solana.com", "https://rpc.ankr.com/solana"],
315
+ [Chain.Solana]: [
316
+ "https://api.mainnet-beta.solana.com",
317
+ "https://solana-mainnet.rpc.extrnode.com",
318
+ ],
312
319
  [Chain.Tron]: ["https://api.tronstack.io", "https://api.tron.network"],
313
320
  };
314
321
 
@@ -1,5 +1,6 @@
1
1
  import type { CosmosWallets, ThorchainWallets } from "@swapkit/toolboxes/cosmos";
2
2
  import type { EVMToolboxes } from "@swapkit/toolboxes/evm";
3
+ import type { NearWallet } from "@swapkit/toolboxes/near";
3
4
  import type { RadixWallet } from "@swapkit/toolboxes/radix";
4
5
  import type { RippleWallet } from "@swapkit/toolboxes/ripple";
5
6
  import type { SolanaWallet } from "@swapkit/toolboxes/solana";
@@ -110,6 +111,7 @@ export type FullWallet = BaseWallet<
110
111
  [Chain.Ripple]: RippleWallet;
111
112
  [Chain.Solana]: SolanaWallet;
112
113
  [Chain.Tron]: TronWallet;
114
+ [Chain.Near]: NearWallet;
113
115
  }
114
116
  >;
115
117
 
@@ -140,6 +140,7 @@ export const getCommonAssetInfo = (assetString: CommonAssetString) => {
140
140
  Chain.Ripple,
141
141
  Chain.Polkadot,
142
142
  Chain.Tron,
143
+ Chain.Near,
143
144
  (asset) => ({ identifier: `${asset}.${asset}`, decimal }),
144
145
  )
145
146
  .with(Chain.Radix, "XRD.XRD", () => ({ identifier: "XRD.XRD", decimal }))
@@ -36,9 +36,15 @@ export function updateDerivationPath(
36
36
  }
37
37
 
38
38
  export function derivationPathToString([network, chainId, account, change, index]:
39
+ | [number, number, number, number?, number?]
39
40
  | [number, number, number, number, number?]
40
41
  | DerivationPathArray) {
41
42
  const shortPath = typeof index !== "number";
43
+ const accountPath = typeof change !== "number";
44
+
45
+ if (accountPath) {
46
+ return `m/${network}'/${chainId}'/${account}'`;
47
+ }
42
48
 
43
49
  return `m/${network}'/${chainId}'/${account}'/${change}${shortPath ? "" : `/${index}`}`;
44
50
  }
@@ -110,7 +110,7 @@ export function wrapMethodWithNetworkSwitch<T extends (...args: any[]) => any>(
110
110
  provider: BrowserProvider,
111
111
  chain: Chain,
112
112
  ) {
113
- (async (...args: any[]) => {
113
+ return (async (...args: any[]) => {
114
114
  try {
115
115
  await switchEVMWalletNetwork(provider, chain);
116
116
  } catch (error) {