@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.d.mts +24 -0
- package/dist/index.d.ts +24 -0
- package/dist/index.js +105 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +105 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -85,6 +85,18 @@ declare class AccountAbstraction {
|
|
|
85
85
|
getBalance(token: string | Address): Promise<bigint>;
|
|
86
86
|
getEoaBalance(token: string | Address): Promise<bigint>;
|
|
87
87
|
getAllowance(token?: string | Address): Promise<bigint>;
|
|
88
|
+
/**
|
|
89
|
+
* Get comprehensive state of the account for a specific token
|
|
90
|
+
* Useful for UI initialization
|
|
91
|
+
*/
|
|
92
|
+
getAccountState(token: string | Address): Promise<{
|
|
93
|
+
owner: `0x${string}`;
|
|
94
|
+
smartAccount: `0x${string}`;
|
|
95
|
+
balance: bigint;
|
|
96
|
+
eoaBalance: bigint;
|
|
97
|
+
allowance: bigint;
|
|
98
|
+
isDeployed: boolean;
|
|
99
|
+
}>;
|
|
88
100
|
sendTransaction(tx: {
|
|
89
101
|
target: Address;
|
|
90
102
|
value?: bigint;
|
|
@@ -96,6 +108,18 @@ declare class AccountAbstraction {
|
|
|
96
108
|
data?: Hex;
|
|
97
109
|
}[]): Promise<UserOpReceipt>;
|
|
98
110
|
deposit(amount: bigint): Promise<Hash>;
|
|
111
|
+
/**
|
|
112
|
+
* Smart Transfer: Automatically chooses best method (SA vs EOA) based on balances.
|
|
113
|
+
* Supports strict fee handling.
|
|
114
|
+
*/
|
|
115
|
+
smartTransfer(token: Address | string, recipient: Address, amount: bigint, fee?: {
|
|
116
|
+
amount: bigint;
|
|
117
|
+
recipient: Address;
|
|
118
|
+
}): Promise<UserOpReceipt | {
|
|
119
|
+
receipt: {
|
|
120
|
+
transactionHash: Hash;
|
|
121
|
+
};
|
|
122
|
+
}>;
|
|
99
123
|
transfer(token: Address | string, recipient: Address, amount: bigint): Promise<UserOpReceipt>;
|
|
100
124
|
/**
|
|
101
125
|
* Approve a token for the Smart Account
|
package/dist/index.d.ts
CHANGED
|
@@ -85,6 +85,18 @@ declare class AccountAbstraction {
|
|
|
85
85
|
getBalance(token: string | Address): Promise<bigint>;
|
|
86
86
|
getEoaBalance(token: string | Address): Promise<bigint>;
|
|
87
87
|
getAllowance(token?: string | Address): Promise<bigint>;
|
|
88
|
+
/**
|
|
89
|
+
* Get comprehensive state of the account for a specific token
|
|
90
|
+
* Useful for UI initialization
|
|
91
|
+
*/
|
|
92
|
+
getAccountState(token: string | Address): Promise<{
|
|
93
|
+
owner: `0x${string}`;
|
|
94
|
+
smartAccount: `0x${string}`;
|
|
95
|
+
balance: bigint;
|
|
96
|
+
eoaBalance: bigint;
|
|
97
|
+
allowance: bigint;
|
|
98
|
+
isDeployed: boolean;
|
|
99
|
+
}>;
|
|
88
100
|
sendTransaction(tx: {
|
|
89
101
|
target: Address;
|
|
90
102
|
value?: bigint;
|
|
@@ -96,6 +108,18 @@ declare class AccountAbstraction {
|
|
|
96
108
|
data?: Hex;
|
|
97
109
|
}[]): Promise<UserOpReceipt>;
|
|
98
110
|
deposit(amount: bigint): Promise<Hash>;
|
|
111
|
+
/**
|
|
112
|
+
* Smart Transfer: Automatically chooses best method (SA vs EOA) based on balances.
|
|
113
|
+
* Supports strict fee handling.
|
|
114
|
+
*/
|
|
115
|
+
smartTransfer(token: Address | string, recipient: Address, amount: bigint, fee?: {
|
|
116
|
+
amount: bigint;
|
|
117
|
+
recipient: Address;
|
|
118
|
+
}): Promise<UserOpReceipt | {
|
|
119
|
+
receipt: {
|
|
120
|
+
transactionHash: Hash;
|
|
121
|
+
};
|
|
122
|
+
}>;
|
|
99
123
|
transfer(token: Address | string, recipient: Address, amount: bigint): Promise<UserOpReceipt>;
|
|
100
124
|
/**
|
|
101
125
|
* Approve a token for the Smart Account
|
package/dist/index.js
CHANGED
|
@@ -358,6 +358,16 @@ var TokenService = class {
|
|
|
358
358
|
args: [recipient, amount]
|
|
359
359
|
});
|
|
360
360
|
}
|
|
361
|
+
/**
|
|
362
|
+
* Encode transferFrom data
|
|
363
|
+
*/
|
|
364
|
+
encodeTransferFrom(sender, recipient, amount) {
|
|
365
|
+
return viem.encodeFunctionData({
|
|
366
|
+
abi: erc20Abi,
|
|
367
|
+
functionName: "transferFrom",
|
|
368
|
+
args: [sender, recipient, amount]
|
|
369
|
+
});
|
|
370
|
+
}
|
|
361
371
|
/**
|
|
362
372
|
* Encode approve data
|
|
363
373
|
*/
|
|
@@ -612,6 +622,30 @@ var AccountAbstraction = class {
|
|
|
612
622
|
if (!this.owner || !this.smartAccountAddress) throw new Error("Not connected");
|
|
613
623
|
return this.tokenService.getAllowance(token, this.owner, this.smartAccountAddress);
|
|
614
624
|
}
|
|
625
|
+
/**
|
|
626
|
+
* Get comprehensive state of the account for a specific token
|
|
627
|
+
* Useful for UI initialization
|
|
628
|
+
*/
|
|
629
|
+
async getAccountState(token) {
|
|
630
|
+
if (!this.owner || !this.smartAccountAddress) throw new Error("Not connected");
|
|
631
|
+
const tokenAddress = this.getTokenAddress(token);
|
|
632
|
+
const isNative = tokenAddress === "0x0000000000000000000000000000000000000000";
|
|
633
|
+
const [balance, eoaBalance, allowance, isDeployed] = await Promise.all([
|
|
634
|
+
this.getBalance(token),
|
|
635
|
+
this.getEoaBalance(token),
|
|
636
|
+
isNative ? 0n : this.getAllowance(token).catch(() => 0n),
|
|
637
|
+
// Handle native/error gracefully
|
|
638
|
+
this.isAccountDeployed()
|
|
639
|
+
]);
|
|
640
|
+
return {
|
|
641
|
+
owner: this.owner,
|
|
642
|
+
smartAccount: this.smartAccountAddress,
|
|
643
|
+
balance,
|
|
644
|
+
eoaBalance,
|
|
645
|
+
allowance,
|
|
646
|
+
isDeployed
|
|
647
|
+
};
|
|
648
|
+
}
|
|
615
649
|
// --- Transactions ---
|
|
616
650
|
async sendTransaction(tx) {
|
|
617
651
|
return this.sendBatchTransaction([tx]);
|
|
@@ -655,6 +689,77 @@ var AccountAbstraction = class {
|
|
|
655
689
|
}]
|
|
656
690
|
});
|
|
657
691
|
}
|
|
692
|
+
/**
|
|
693
|
+
* Smart Transfer: Automatically chooses best method (SA vs EOA) based on balances.
|
|
694
|
+
* Supports strict fee handling.
|
|
695
|
+
*/
|
|
696
|
+
async smartTransfer(token, recipient, amount, fee) {
|
|
697
|
+
if (!this.owner || !this.smartAccountAddress) throw new Error("Not connected");
|
|
698
|
+
const tokenAddress = this.getTokenAddress(token);
|
|
699
|
+
const isNative = tokenAddress === "0x0000000000000000000000000000000000000000";
|
|
700
|
+
const [saBal, eoaBal, allowance] = await Promise.all([
|
|
701
|
+
this.getBalance(token),
|
|
702
|
+
this.getEoaBalance(token),
|
|
703
|
+
isNative ? 0n : this.getAllowance(token).catch(() => 0n)
|
|
704
|
+
]);
|
|
705
|
+
const totalNeeded = amount + (fee?.amount || 0n);
|
|
706
|
+
if (saBal >= totalNeeded) {
|
|
707
|
+
const txs = [];
|
|
708
|
+
if (isNative) {
|
|
709
|
+
txs.push({ target: recipient, value: amount, data: "0x" });
|
|
710
|
+
} else {
|
|
711
|
+
txs.push({ target: tokenAddress, value: 0n, data: this.tokenService.encodeTransfer(recipient, amount) });
|
|
712
|
+
}
|
|
713
|
+
if (fee && fee.amount > 0n) {
|
|
714
|
+
if (isNative) {
|
|
715
|
+
txs.push({ target: fee.recipient, value: fee.amount, data: "0x" });
|
|
716
|
+
} else {
|
|
717
|
+
txs.push({ target: tokenAddress, value: 0n, data: this.tokenService.encodeTransfer(fee.recipient, fee.amount) });
|
|
718
|
+
}
|
|
719
|
+
}
|
|
720
|
+
console.log("SmartTransfer: Using Smart Account");
|
|
721
|
+
return this.sendBatchTransaction(txs);
|
|
722
|
+
}
|
|
723
|
+
if (eoaBal >= totalNeeded) {
|
|
724
|
+
if (isNative) {
|
|
725
|
+
console.log("SmartTransfer: Using EOA (Native)");
|
|
726
|
+
if (this.walletClient) {
|
|
727
|
+
const hash2 = await this.walletClient.sendTransaction({
|
|
728
|
+
account: this.walletClient.account,
|
|
729
|
+
to: recipient,
|
|
730
|
+
value: amount,
|
|
731
|
+
chain: this.chainConfig.chain
|
|
732
|
+
});
|
|
733
|
+
return { receipt: { transactionHash: hash2 } };
|
|
734
|
+
}
|
|
735
|
+
const hash = await window.ethereum.request({
|
|
736
|
+
method: "eth_sendTransaction",
|
|
737
|
+
params: [{ from: this.owner, to: recipient, value: "0x" + amount.toString(16) }]
|
|
738
|
+
});
|
|
739
|
+
return { receipt: { transactionHash: hash } };
|
|
740
|
+
} else {
|
|
741
|
+
console.log("SmartTransfer: Using EOA (Pull)");
|
|
742
|
+
if (allowance < totalNeeded) {
|
|
743
|
+
throw new Error(`Approval required. Please approve ${token} usage.`);
|
|
744
|
+
}
|
|
745
|
+
const txs = [];
|
|
746
|
+
txs.push({
|
|
747
|
+
target: tokenAddress,
|
|
748
|
+
value: 0n,
|
|
749
|
+
data: this.tokenService.encodeTransferFrom(this.owner, recipient, amount)
|
|
750
|
+
});
|
|
751
|
+
if (fee && fee.amount > 0n) {
|
|
752
|
+
txs.push({
|
|
753
|
+
target: tokenAddress,
|
|
754
|
+
value: 0n,
|
|
755
|
+
data: this.tokenService.encodeTransferFrom(this.owner, fee.recipient, fee.amount)
|
|
756
|
+
});
|
|
757
|
+
}
|
|
758
|
+
return this.sendBatchTransaction(txs);
|
|
759
|
+
}
|
|
760
|
+
}
|
|
761
|
+
throw new Error(`Insufficient funds.`);
|
|
762
|
+
}
|
|
658
763
|
async transfer(token, recipient, amount) {
|
|
659
764
|
const tokenAddress = this.getTokenAddress(token);
|
|
660
765
|
if (tokenAddress === "0x0000000000000000000000000000000000000000") {
|