@1llet.xyz/erc4337-gasless-sdk 0.4.31 → 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 +26 -0
- package/dist/index.d.ts +26 -0
- package/dist/index.js +119 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +119 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -75,6 +75,8 @@ declare class AccountAbstraction {
|
|
|
75
75
|
owner: Address;
|
|
76
76
|
smartAccount: Address;
|
|
77
77
|
}>;
|
|
78
|
+
isAccountDeployed(): Promise<boolean>;
|
|
79
|
+
deployAccount(): Promise<UserOpReceipt>;
|
|
78
80
|
/**
|
|
79
81
|
* Get the Smart Account address for an owner
|
|
80
82
|
*/
|
|
@@ -83,6 +85,18 @@ declare class AccountAbstraction {
|
|
|
83
85
|
getBalance(token: string | Address): Promise<bigint>;
|
|
84
86
|
getEoaBalance(token: string | Address): Promise<bigint>;
|
|
85
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
|
+
}>;
|
|
86
100
|
sendTransaction(tx: {
|
|
87
101
|
target: Address;
|
|
88
102
|
value?: bigint;
|
|
@@ -94,6 +108,18 @@ declare class AccountAbstraction {
|
|
|
94
108
|
data?: Hex;
|
|
95
109
|
}[]): Promise<UserOpReceipt>;
|
|
96
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
|
+
}>;
|
|
97
123
|
transfer(token: Address | string, recipient: Address, amount: bigint): Promise<UserOpReceipt>;
|
|
98
124
|
/**
|
|
99
125
|
* Approve a token for the Smart Account
|
package/dist/index.d.ts
CHANGED
|
@@ -75,6 +75,8 @@ declare class AccountAbstraction {
|
|
|
75
75
|
owner: Address;
|
|
76
76
|
smartAccount: Address;
|
|
77
77
|
}>;
|
|
78
|
+
isAccountDeployed(): Promise<boolean>;
|
|
79
|
+
deployAccount(): Promise<UserOpReceipt>;
|
|
78
80
|
/**
|
|
79
81
|
* Get the Smart Account address for an owner
|
|
80
82
|
*/
|
|
@@ -83,6 +85,18 @@ declare class AccountAbstraction {
|
|
|
83
85
|
getBalance(token: string | Address): Promise<bigint>;
|
|
84
86
|
getEoaBalance(token: string | Address): Promise<bigint>;
|
|
85
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
|
+
}>;
|
|
86
100
|
sendTransaction(tx: {
|
|
87
101
|
target: Address;
|
|
88
102
|
value?: bigint;
|
|
@@ -94,6 +108,18 @@ declare class AccountAbstraction {
|
|
|
94
108
|
data?: Hex;
|
|
95
109
|
}[]): Promise<UserOpReceipt>;
|
|
96
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
|
+
}>;
|
|
97
123
|
transfer(token: Address | string, recipient: Address, amount: bigint): Promise<UserOpReceipt>;
|
|
98
124
|
/**
|
|
99
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
|
*/
|
|
@@ -570,6 +580,20 @@ var AccountAbstraction = class {
|
|
|
570
580
|
smartAccount: this.smartAccountAddress
|
|
571
581
|
};
|
|
572
582
|
}
|
|
583
|
+
// --- Account Management ---
|
|
584
|
+
async isAccountDeployed() {
|
|
585
|
+
if (!this.smartAccountAddress) return false;
|
|
586
|
+
const code = await this.publicClient.getBytecode({ address: this.smartAccountAddress });
|
|
587
|
+
return code !== void 0;
|
|
588
|
+
}
|
|
589
|
+
async deployAccount() {
|
|
590
|
+
if (!this.owner || !this.smartAccountAddress) throw new Error("Not connected");
|
|
591
|
+
return this.sendTransaction({
|
|
592
|
+
target: this.smartAccountAddress,
|
|
593
|
+
value: 0n,
|
|
594
|
+
data: "0x"
|
|
595
|
+
});
|
|
596
|
+
}
|
|
573
597
|
/**
|
|
574
598
|
* Get the Smart Account address for an owner
|
|
575
599
|
*/
|
|
@@ -598,6 +622,30 @@ var AccountAbstraction = class {
|
|
|
598
622
|
if (!this.owner || !this.smartAccountAddress) throw new Error("Not connected");
|
|
599
623
|
return this.tokenService.getAllowance(token, this.owner, this.smartAccountAddress);
|
|
600
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
|
+
}
|
|
601
649
|
// --- Transactions ---
|
|
602
650
|
async sendTransaction(tx) {
|
|
603
651
|
return this.sendBatchTransaction([tx]);
|
|
@@ -641,6 +689,77 @@ var AccountAbstraction = class {
|
|
|
641
689
|
}]
|
|
642
690
|
});
|
|
643
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
|
+
}
|
|
644
763
|
async transfer(token, recipient, amount) {
|
|
645
764
|
const tokenAddress = this.getTokenAddress(token);
|
|
646
765
|
if (tokenAddress === "0x0000000000000000000000000000000000000000") {
|