@mycelium-sdk/core 2.0.0-alpha.0 → 2.0.0-alpha.1

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.cjs CHANGED
@@ -534,11 +534,17 @@ var DefaultSmartWallet = class extends SmartWallet {
534
534
  * @remarks
535
535
  * The protocol is selected on the SDK initialization step
536
536
  * @param amount Human-readable amount string
537
+ * @param options Optional parameters
538
+ * @param options.paymasterToken ERC-20 token address to use for gas payment (e.g., USDC)
537
539
  * @returns Transaction result for the deposit
538
540
  */
539
- async earn(vaultInfo, amount) {
540
- this.chainManager.getSupportedChain();
541
- const depositTransactionResult = this.protocolProvider.deposit(vaultInfo, amount, this);
541
+ async earn(vaultInfo, amount, options) {
542
+ const depositTransactionResult = this.protocolProvider.deposit(
543
+ vaultInfo,
544
+ amount,
545
+ this,
546
+ options
547
+ );
542
548
  return depositTransactionResult;
543
549
  }
544
550
  /**
@@ -558,12 +564,19 @@ var DefaultSmartWallet = class extends SmartWallet {
558
564
  * @public
559
565
  * @category Earn
560
566
  * @param amount Human-readable amount string
567
+ * @param options Optional parameters
568
+ * @param options.paymasterToken ERC-20 token address to use for gas payment (e.g., USDC)
561
569
  * @returns Transaction result for the withdrawal
562
570
  * @throws Error if the withdrawal fails
563
571
  * @throws Error a user didn't deposit anything
564
572
  */
565
- async withdraw(vaultInfo, amount) {
566
- const withdrawTransactionResult = await this.protocolProvider.withdraw(vaultInfo, amount, this);
573
+ async withdraw(vaultInfo, amount, options) {
574
+ const withdrawTransactionResult = await this.protocolProvider.withdraw(
575
+ vaultInfo,
576
+ amount,
577
+ this,
578
+ options
579
+ );
567
580
  return withdrawTransactionResult;
568
581
  }
569
582
  /**
@@ -2288,13 +2301,31 @@ var ProxyProtocol = class extends BaseProtocol {
2288
2301
  * @param smartWallet Smart wallet to use for the deposit
2289
2302
  * @returns Result of the deposit transaction
2290
2303
  */
2291
- async deposit(vaultInfo, amount, smartWallet) {
2304
+ async deposit(vaultInfo, amount, smartWallet, options) {
2292
2305
  const currentAddress = await smartWallet.getAddress();
2293
2306
  const operationsCallData = [];
2294
2307
  const depositTokenDecimals = vaultInfo.tokenDecimals;
2295
2308
  const depositTokenAddress = vaultInfo.tokenAddress;
2296
2309
  const vaultAddress = vaultInfo.vaultAddress;
2297
2310
  const rawDepositAmount = (0, import_viem13.parseUnits)(amount, depositTokenDecimals);
2311
+ if (options?.paymasterToken && options.paymasterToken.toLowerCase() === depositTokenAddress.toLowerCase()) {
2312
+ this.ensureInitialized();
2313
+ const publicClient = this.chainManager.getPublicClient(this.selectedChainId);
2314
+ const balance = await publicClient.readContract({
2315
+ address: depositTokenAddress,
2316
+ abi: import_viem13.erc20Abi,
2317
+ functionName: "balanceOf",
2318
+ args: [currentAddress]
2319
+ });
2320
+ const gasReserve = balance / 100n > 0n ? balance / 100n : 1n;
2321
+ const maxDepositAmount = balance > gasReserve ? balance - gasReserve : 0n;
2322
+ if (rawDepositAmount > maxDepositAmount) {
2323
+ const maxDepositFormatted = Number(maxDepositAmount) / 10 ** depositTokenDecimals;
2324
+ throw new Error(
2325
+ `Insufficient balance. Must reserve tokens for gas payment. Max deposit: ${maxDepositFormatted.toFixed(depositTokenDecimals)}`
2326
+ );
2327
+ }
2328
+ }
2298
2329
  const allowance = await this.checkAllowance(
2299
2330
  depositTokenAddress,
2300
2331
  vaultAddress,
@@ -2330,7 +2361,7 @@ var ProxyProtocol = class extends BaseProtocol {
2330
2361
  }
2331
2362
  const receivedOperationsCallData = apiResponse.data;
2332
2363
  operationsCallData.push(receivedOperationsCallData);
2333
- const hash = await smartWallet.sendBatch(operationsCallData, this.selectedChainId);
2364
+ const hash = await smartWallet.sendBatch(operationsCallData, this.selectedChainId, options);
2334
2365
  const operationStatus = hash ? "completed" : "failed";
2335
2366
  this.logOperation(
2336
2367
  currentAddress,
@@ -2350,7 +2381,7 @@ var ProxyProtocol = class extends BaseProtocol {
2350
2381
  * @param smartWallet Smart wallet to use for the withdrawal
2351
2382
  * @returns Result of the withdrawal transaction
2352
2383
  */
2353
- async withdraw(vaultInfo, amount, smartWallet) {
2384
+ async withdraw(vaultInfo, amount, smartWallet, options) {
2354
2385
  const currentAddress = await smartWallet.getAddress();
2355
2386
  const earningBalances = await smartWallet.getEarnBalances();
2356
2387
  if (!earningBalances) {
@@ -2362,6 +2393,24 @@ var ProxyProtocol = class extends BaseProtocol {
2362
2393
  }
2363
2394
  const balanceInfo = earningBalance.balance;
2364
2395
  const amountToWithdraw = amount ? amount : balanceInfo.currentBalance;
2396
+ if (options?.paymasterToken && options.paymasterToken.toLowerCase() === vaultInfo.tokenAddress.toLowerCase()) {
2397
+ this.ensureInitialized();
2398
+ const publicClient = this.chainManager.getPublicClient(this.selectedChainId);
2399
+ const walletBalance = await publicClient.readContract({
2400
+ address: vaultInfo.tokenAddress,
2401
+ abi: import_viem13.erc20Abi,
2402
+ functionName: "balanceOf",
2403
+ args: [currentAddress]
2404
+ });
2405
+ const gasReserve = walletBalance / 100n > 0n ? walletBalance / 100n : 1n;
2406
+ const minRequiredBalance = gasReserve;
2407
+ if (walletBalance < minRequiredBalance) {
2408
+ const minRequiredFormatted = Number(minRequiredBalance) / 10 ** vaultInfo.tokenDecimals;
2409
+ throw new Error(
2410
+ `Insufficient wallet balance for gas payment. Wallet needs at least ${minRequiredFormatted.toFixed(vaultInfo.tokenDecimals)} tokens to pay for gas before withdrawal.`
2411
+ );
2412
+ }
2413
+ }
2365
2414
  const apiResponse = await this.apiClient.sendRequest(
2366
2415
  "withdraw",
2367
2416
  void 0,
@@ -2369,14 +2418,14 @@ var ProxyProtocol = class extends BaseProtocol {
2369
2418
  {
2370
2419
  vaultInfo,
2371
2420
  amount: amountToWithdraw,
2372
- chainId: this.selectedChainId.toString()
2421
+ chainId: this.selectedChainId
2373
2422
  }
2374
2423
  );
2375
2424
  if (!apiResponse.success) {
2376
2425
  throw new Error(apiResponse.error || "Failed to receive withdraw operations call data");
2377
2426
  }
2378
2427
  const withdrawOperationCallData = apiResponse.data;
2379
- const hash = await smartWallet.send(withdrawOperationCallData, this.selectedChainId);
2428
+ const hash = await smartWallet.send(withdrawOperationCallData, this.selectedChainId, options);
2380
2429
  const operationStatus = hash ? "completed" : "failed";
2381
2430
  this.logOperation(
2382
2431
  currentAddress,