@@ -22,7 +22,6 @@ import type { PreparedTransaction } from "../../transaction/prepare-transaction.
2222import { readContract } from "../../transaction/read-contract.js" ;
2323import { getAddress } from "../../utils/address.js" ;
2424import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js" ;
25- import { concatHex } from "../../utils/encoding/helpers/concat-hex.js" ;
2625import type { Hex } from "../../utils/encoding/hex.js" ;
2726import { parseTypedData } from "../../utils/signatures/helpers/parseTypedData.js" ;
2827import type {
@@ -45,7 +44,12 @@ import {
4544 prepareBatchExecute ,
4645 prepareExecute ,
4746} from "./lib/calls.js" ;
48- import { getDefaultAccountFactory } from "./lib/constants.js" ;
47+ import {
48+ ENTRYPOINT_ADDRESS_v0_6 ,
49+ ENTRYPOINT_ADDRESS_v0_7 ,
50+ getDefaultAccountFactory ,
51+ getEntryPointVersion ,
52+ } from "./lib/constants.js" ;
4953import {
5054 clearAccountDeploying ,
5155 createUnsignedUserOp ,
@@ -58,6 +62,7 @@ import type {
5862 SmartAccountOptions ,
5963 SmartWalletConnectionOptions ,
6064 SmartWalletOptions ,
65+ TokenPaymasterConfig ,
6166 UserOperationV06 ,
6267 UserOperationV07 ,
6368} from "./types.js" ;
@@ -116,6 +121,17 @@ export async function connectSmartWallet(
116121 }
117122 }
118123
124+ if (
125+ options . overrides ?. tokenPaymaster &&
126+ ! options . overrides ?. entrypointAddress
127+ ) {
128+ // if token paymaster is set, but no entrypoint address, set the entrypoint address to v0.7
129+ options . overrides = {
130+ ...options . overrides ,
131+ entrypointAddress : ENTRYPOINT_ADDRESS_v0_7 ,
132+ } ;
133+ }
134+
119135 const factoryAddress =
120136 options . factoryAddress ??
121137 getDefaultAccountFactory ( options . overrides ?. entrypointAddress ) ;
@@ -196,12 +212,24 @@ export async function disconnectSmartWallet(
196212async function createSmartAccount (
197213 options : SmartAccountOptions ,
198214) : Promise < Account > {
215+ const erc20Paymaster = options . overrides ?. tokenPaymaster ;
216+ if ( erc20Paymaster ) {
217+ if (
218+ getEntryPointVersion (
219+ options . overrides ?. entrypointAddress || ENTRYPOINT_ADDRESS_v0_6 ,
220+ ) !== "v0.7"
221+ ) {
222+ throw new Error (
223+ "Token paymaster is only supported for entrypoint version v0.7" ,
224+ ) ;
225+ }
226+ }
227+
199228 const { accountContract } = options ;
200229 const account : Account = {
201230 address : getAddress ( accountContract . address ) ,
202231 async sendTransaction ( transaction : SendTransactionOption ) {
203232 // if erc20 paymaster - check allowance and approve if needed
204- const erc20Paymaster = options . overrides ?. erc20Paymaster ;
205233 let paymasterOverride :
206234 | undefined
207235 | ( (
@@ -215,12 +243,7 @@ async function createSmartAccount(
215243 } ) ;
216244 const paymasterCallback = async ( ) : Promise < PaymasterResult > => {
217245 return {
218- paymasterAndData : concatHex ( [
219- erc20Paymaster . address as Hex ,
220- erc20Paymaster ?. token as Hex ,
221- ] ) ,
222- // for 0.7 compatibility
223- paymaster : erc20Paymaster . address as Hex ,
246+ paymaster : erc20Paymaster . paymasterAddress as Hex ,
224247 paymasterData : "0x" ,
225248 } ;
226249 } ;
@@ -436,13 +459,10 @@ async function createSmartAccount(
436459async function approveERC20 ( args : {
437460 accountContract : ThirdwebContract ;
438461 options : SmartAccountOptions ;
439- erc20Paymaster : {
440- address : string ;
441- token : string ;
442- } ;
462+ erc20Paymaster : TokenPaymasterConfig ;
443463} ) {
444464 const { accountContract, erc20Paymaster, options } = args ;
445- const tokenAddress = erc20Paymaster . token ;
465+ const tokenAddress = erc20Paymaster . tokenAddress ;
446466 const tokenContract = getContract ( {
447467 address : tokenAddress ,
448468 chain : accountContract . chain ,
@@ -451,7 +471,7 @@ async function approveERC20(args: {
451471 const accountAllowance = await allowance ( {
452472 contract : tokenContract ,
453473 owner : accountContract . address ,
454- spender : erc20Paymaster . address ,
474+ spender : erc20Paymaster . paymasterAddress ,
455475 } ) ;
456476
457477 if ( accountAllowance > 0n ) {
@@ -460,7 +480,7 @@ async function approveERC20(args: {
460480
461481 const approveTx = approve ( {
462482 contract : tokenContract ,
463- spender : erc20Paymaster . address ,
483+ spender : erc20Paymaster . paymasterAddress ,
464484 amountWei : maxUint96 - 1n ,
465485 } ) ;
466486 const transaction = await toSerializableTransaction ( {
@@ -478,7 +498,7 @@ async function approveERC20(args: {
478498 ...options ,
479499 overrides : {
480500 ...options . overrides ,
481- erc20Paymaster : undefined ,
501+ tokenPaymaster : undefined ,
482502 } ,
483503 } ,
484504 } ) ;
0 commit comments