@1llet.xyz/erc4337-gasless-sdk 0.4.32 → 0.4.33

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.mjs CHANGED
@@ -352,6 +352,16 @@ var TokenService = class {
352
352
  args: [recipient, amount]
353
353
  });
354
354
  }
355
+ /**
356
+ * Encode transferFrom data
357
+ */
358
+ encodeTransferFrom(sender, recipient, amount) {
359
+ return encodeFunctionData({
360
+ abi: erc20Abi,
361
+ functionName: "transferFrom",
362
+ args: [sender, recipient, amount]
363
+ });
364
+ }
355
365
  /**
356
366
  * Encode approve data
357
367
  */
@@ -606,6 +616,30 @@ var AccountAbstraction = class {
606
616
  if (!this.owner || !this.smartAccountAddress) throw new Error("Not connected");
607
617
  return this.tokenService.getAllowance(token, this.owner, this.smartAccountAddress);
608
618
  }
619
+ /**
620
+ * Get comprehensive state of the account for a specific token
621
+ * Useful for UI initialization
622
+ */
623
+ async getAccountState(token) {
624
+ if (!this.owner || !this.smartAccountAddress) throw new Error("Not connected");
625
+ const tokenAddress = this.getTokenAddress(token);
626
+ const isNative = tokenAddress === "0x0000000000000000000000000000000000000000";
627
+ const [balance, eoaBalance, allowance, isDeployed] = await Promise.all([
628
+ this.getBalance(token),
629
+ this.getEoaBalance(token),
630
+ isNative ? 0n : this.getAllowance(token).catch(() => 0n),
631
+ // Handle native/error gracefully
632
+ this.isAccountDeployed()
633
+ ]);
634
+ return {
635
+ owner: this.owner,
636
+ smartAccount: this.smartAccountAddress,
637
+ balance,
638
+ eoaBalance,
639
+ allowance,
640
+ isDeployed
641
+ };
642
+ }
609
643
  // --- Transactions ---
610
644
  async sendTransaction(tx) {
611
645
  return this.sendBatchTransaction([tx]);
@@ -649,6 +683,77 @@ var AccountAbstraction = class {
649
683
  }]
650
684
  });
651
685
  }
686
+ /**
687
+ * Smart Transfer: Automatically chooses best method (SA vs EOA) based on balances.
688
+ * Supports strict fee handling.
689
+ */
690
+ async smartTransfer(token, recipient, amount, fee) {
691
+ if (!this.owner || !this.smartAccountAddress) throw new Error("Not connected");
692
+ const tokenAddress = this.getTokenAddress(token);
693
+ const isNative = tokenAddress === "0x0000000000000000000000000000000000000000";
694
+ const [saBal, eoaBal, allowance] = await Promise.all([
695
+ this.getBalance(token),
696
+ this.getEoaBalance(token),
697
+ isNative ? 0n : this.getAllowance(token).catch(() => 0n)
698
+ ]);
699
+ const totalNeeded = amount + (fee?.amount || 0n);
700
+ if (saBal >= totalNeeded) {
701
+ const txs = [];
702
+ if (isNative) {
703
+ txs.push({ target: recipient, value: amount, data: "0x" });
704
+ } else {
705
+ txs.push({ target: tokenAddress, value: 0n, data: this.tokenService.encodeTransfer(recipient, amount) });
706
+ }
707
+ if (fee && fee.amount > 0n) {
708
+ if (isNative) {
709
+ txs.push({ target: fee.recipient, value: fee.amount, data: "0x" });
710
+ } else {
711
+ txs.push({ target: tokenAddress, value: 0n, data: this.tokenService.encodeTransfer(fee.recipient, fee.amount) });
712
+ }
713
+ }
714
+ console.log("SmartTransfer: Using Smart Account");
715
+ return this.sendBatchTransaction(txs);
716
+ }
717
+ if (eoaBal >= totalNeeded) {
718
+ if (isNative) {
719
+ console.log("SmartTransfer: Using EOA (Native)");
720
+ if (this.walletClient) {
721
+ const hash2 = await this.walletClient.sendTransaction({
722
+ account: this.walletClient.account,
723
+ to: recipient,
724
+ value: amount,
725
+ chain: this.chainConfig.chain
726
+ });
727
+ return { receipt: { transactionHash: hash2 } };
728
+ }
729
+ const hash = await window.ethereum.request({
730
+ method: "eth_sendTransaction",
731
+ params: [{ from: this.owner, to: recipient, value: "0x" + amount.toString(16) }]
732
+ });
733
+ return { receipt: { transactionHash: hash } };
734
+ } else {
735
+ console.log("SmartTransfer: Using EOA (Pull)");
736
+ if (allowance < totalNeeded) {
737
+ throw new Error(`Approval required. Please approve ${token} usage.`);
738
+ }
739
+ const txs = [];
740
+ txs.push({
741
+ target: tokenAddress,
742
+ value: 0n,
743
+ data: this.tokenService.encodeTransferFrom(this.owner, recipient, amount)
744
+ });
745
+ if (fee && fee.amount > 0n) {
746
+ txs.push({
747
+ target: tokenAddress,
748
+ value: 0n,
749
+ data: this.tokenService.encodeTransferFrom(this.owner, fee.recipient, fee.amount)
750
+ });
751
+ }
752
+ return this.sendBatchTransaction(txs);
753
+ }
754
+ }
755
+ throw new Error(`Insufficient funds.`);
756
+ }
652
757
  async transfer(token, recipient, amount) {
653
758
  const tokenAddress = this.getTokenAddress(token);
654
759
  if (tokenAddress === "0x0000000000000000000000000000000000000000") {