Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions .changeset/tiny-pugs-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
"thirdweb": minor
---

ERC20 Token Paymaster support

You can now use ERC20 Token Paymasters with Smart Wallets.

```typescript
import { base } from "thirdweb/chains";
import { TokenPaymaster, smartWallet } from "thirdweb/wallets";

const wallet = smartWallet({
chain: base,
sponsorGas: true, // only sponsor gas for the first ERC20 approval
overrides: {
tokenPaymaster: TokenPaymaster.BASE_USDC,
},
});
```
1 change: 1 addition & 0 deletions packages/thirdweb/src/exports/wallets/smart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,5 @@ export {
ENTRYPOINT_ADDRESS_v0_7,
DEFAULT_ACCOUNT_FACTORY_V0_6,
DEFAULT_ACCOUNT_FACTORY_V0_7,
TokenPaymaster,
} from "../../wallets/smart/lib/constants.js";
54 changes: 37 additions & 17 deletions packages/thirdweb/src/wallets/smart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import { readContract } from "../../transaction/read-contract.js";
import { getAddress } from "../../utils/address.js";
import { isZkSyncChain } from "../../utils/any-evm/zksync/isZkSyncChain.js";
import { concatHex } from "../../utils/encoding/helpers/concat-hex.js";
import type { Hex } from "../../utils/encoding/hex.js";
import { parseTypedData } from "../../utils/signatures/helpers/parseTypedData.js";
import type {
Expand All @@ -45,7 +44,12 @@
prepareBatchExecute,
prepareExecute,
} from "./lib/calls.js";
import { getDefaultAccountFactory } from "./lib/constants.js";
import {
ENTRYPOINT_ADDRESS_v0_6,
ENTRYPOINT_ADDRESS_v0_7,
getDefaultAccountFactory,
getEntryPointVersion,
} from "./lib/constants.js";
import {
clearAccountDeploying,
createUnsignedUserOp,
Expand All @@ -58,6 +62,7 @@
SmartAccountOptions,
SmartWalletConnectionOptions,
SmartWalletOptions,
TokenPaymasterConfig,
UserOperationV06,
UserOperationV07,
} from "./types.js";
Expand Down Expand Up @@ -116,6 +121,17 @@
}
}

if (
options.overrides?.tokenPaymaster &&
!options.overrides?.entrypointAddress

Check warning on line 126 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L126

Added line #L126 was not covered by tests
) {
// if token paymaster is set, but no entrypoint address, set the entrypoint address to v0.7
options.overrides = {
...options.overrides,
entrypointAddress: ENTRYPOINT_ADDRESS_v0_7,
};
}

Check warning on line 133 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L129-L133

Added lines #L129 - L133 were not covered by tests

const factoryAddress =
options.factoryAddress ??
getDefaultAccountFactory(options.overrides?.entrypointAddress);
Expand Down Expand Up @@ -196,12 +212,24 @@
async function createSmartAccount(
options: SmartAccountOptions,
): Promise<Account> {
const erc20Paymaster = options.overrides?.tokenPaymaster;
if (erc20Paymaster) {
if (
getEntryPointVersion(
options.overrides?.entrypointAddress || ENTRYPOINT_ADDRESS_v0_6,
) !== "v0.7"
) {
throw new Error(
"Token paymaster is only supported for entrypoint version v0.7",
);
}
}

Check warning on line 226 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L217-L226

Added lines #L217 - L226 were not covered by tests

const { accountContract } = options;
const account: Account = {
address: getAddress(accountContract.address),
async sendTransaction(transaction: SendTransactionOption) {
// if erc20 paymaster - check allowance and approve if needed
const erc20Paymaster = options.overrides?.erc20Paymaster;
let paymasterOverride:
| undefined
| ((
Expand All @@ -215,12 +243,7 @@
});
const paymasterCallback = async (): Promise<PaymasterResult> => {
return {
paymasterAndData: concatHex([
erc20Paymaster.address as Hex,
erc20Paymaster?.token as Hex,
]),
// for 0.7 compatibility
paymaster: erc20Paymaster.address as Hex,
paymaster: erc20Paymaster.paymasterAddress as Hex,

Check warning on line 246 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L246

Added line #L246 was not covered by tests
paymasterData: "0x",
};
};
Expand Down Expand Up @@ -436,13 +459,10 @@
async function approveERC20(args: {
accountContract: ThirdwebContract;
options: SmartAccountOptions;
erc20Paymaster: {
address: string;
token: string;
};
erc20Paymaster: TokenPaymasterConfig;
}) {
const { accountContract, erc20Paymaster, options } = args;
const tokenAddress = erc20Paymaster.token;
const tokenAddress = erc20Paymaster.tokenAddress;

Check warning on line 465 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L465

Added line #L465 was not covered by tests
const tokenContract = getContract({
address: tokenAddress,
chain: accountContract.chain,
Expand All @@ -451,7 +471,7 @@
const accountAllowance = await allowance({
contract: tokenContract,
owner: accountContract.address,
spender: erc20Paymaster.address,
spender: erc20Paymaster.paymasterAddress,

Check warning on line 474 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L474

Added line #L474 was not covered by tests
});

if (accountAllowance > 0n) {
Expand All @@ -460,7 +480,7 @@

const approveTx = approve({
contract: tokenContract,
spender: erc20Paymaster.address,
spender: erc20Paymaster.paymasterAddress,

Check warning on line 483 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L483

Added line #L483 was not covered by tests
amountWei: maxUint96 - 1n,
});
const transaction = await toSerializableTransaction({
Expand All @@ -478,7 +498,7 @@
...options,
overrides: {
...options.overrides,
erc20Paymaster: undefined,
tokenPaymaster: undefined,

Check warning on line 501 in packages/thirdweb/src/wallets/smart/index.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/index.ts#L501

Added line #L501 was not covered by tests
},
},
});
Expand Down
18 changes: 14 additions & 4 deletions packages/thirdweb/src/wallets/smart/lib/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,26 @@
* ```
* @walletUtils
*/
export async function estimateUserOpGas(args: {
userOp: UserOperationV06 | UserOperationV07;
options: BundlerOptions;
}): Promise<EstimationResult> {
export async function estimateUserOpGas(
args: {

Check warning on line 71 in packages/thirdweb/src/wallets/smart/lib/bundler.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/lib/bundler.ts#L70-L71

Added lines #L70 - L71 were not covered by tests
userOp: UserOperationV06 | UserOperationV07;
options: BundlerOptions;
},
stateOverrides?: {

Check warning on line 75 in packages/thirdweb/src/wallets/smart/lib/bundler.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/lib/bundler.ts#L75

Added line #L75 was not covered by tests
[x: string]: {
stateDiff: {
[x: string]: `0x${string}`;
};
};
},
): Promise<EstimationResult> {

Check warning on line 82 in packages/thirdweb/src/wallets/smart/lib/bundler.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/lib/bundler.ts#L82

Added line #L82 was not covered by tests
const res = await sendBundlerRequest({
...args,
operation: "eth_estimateUserOperationGas",
params: [
hexlifyUserOp(args.userOp),
args.options.entrypointAddress ?? ENTRYPOINT_ADDRESS_v0_6,
stateOverrides,

Check warning on line 89 in packages/thirdweb/src/wallets/smart/lib/bundler.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/lib/bundler.ts#L89

Added line #L89 was not covered by tests
],
});

Expand Down
23 changes: 23 additions & 0 deletions packages/thirdweb/src/wallets/smart/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Chain } from "../../../chains/types.js";
import { getAddress } from "../../../utils/address.js";
import { getThirdwebDomains } from "../../../utils/domains.js";
import type { TokenPaymasterConfig } from "../types.js";

export const DUMMY_SIGNATURE =
"0xfffffffffffffffffffffffffffffff0000000000000000000000000000000007aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1c";
Expand All @@ -17,6 +18,28 @@ export const ENTRYPOINT_ADDRESS_v0_7 =

export const MANAGED_ACCOUNT_GAS_BUFFER = 50000n;

type PAYMASTERS = "BASE_USDC" | "CELO_CUSD" | "LISK_LSK";
export const TokenPaymaster: Record<PAYMASTERS, TokenPaymasterConfig> = {
BASE_USDC: {
chainId: 8453,
paymasterAddress: "0x2222f2738BE6bB7aA0Bfe4AEeAf2908172CF5539",
tokenAddress: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
balanceStorageSlot: 9n,
},
CELO_CUSD: {
chainId: 42220,
paymasterAddress: "0x3feA3c5744D715ff46e91C4e5C9a94426DfF2aF9",
tokenAddress: "0x765DE816845861e75A25fCA122bb6898B8B1282a",
balanceStorageSlot: 9n,
},
LISK_LSK: {
chainId: 1135,
paymasterAddress: "0x9eb8cf7fBa5ed9EeDCC97a0d52254cc0e9B1AC25",
tokenAddress: "0xac485391EB2d7D88253a7F1eF18C37f4242D1A24",
balanceStorageSlot: 9n,
},
};

/*
* @internal
*/
Expand Down
39 changes: 31 additions & 8 deletions packages/thirdweb/src/wallets/smart/lib/userop.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { concat } from "viem";
import { maxUint96 } from "ox/Solidity";
import { concat, keccak256, toHex } from "viem";
import type { Chain } from "../../../chains/types.js";
import type { ThirdwebClient } from "../../../client/client.js";
import {
Expand All @@ -13,6 +14,7 @@
import { toSerializableTransaction } from "../../../transaction/actions/to-serializable-transaction.js";
import type { PreparedTransaction } from "../../../transaction/prepare-transaction.js";
import type { TransactionReceipt } from "../../../transaction/types.js";
import { encodeAbiParameters } from "../../../utils/abi/encodeAbiParameters.js";
import { isContractDeployed } from "../../../utils/bytecode/is-contract-deployed.js";
import type { Hex } from "../../../utils/encoding/hex.js";
import { hexToBytes } from "../../../utils/encoding/to-bytes.js";
Expand Down Expand Up @@ -361,17 +363,38 @@
paymasterResult.paymasterVerificationGasLimit;
} else {
// otherwise fallback to bundler for gas limits
const estimates = await estimateUserOpGas({
userOp: partialOp,
options: bundlerOptions,
});
const stateOverrides = overrides?.tokenPaymaster
? {
[overrides.tokenPaymaster.tokenAddress]: {
stateDiff: {
[keccak256(
encodeAbiParameters(
[{ type: "address" }, { type: "uint256" }],
[
accountContract.address,
overrides.tokenPaymaster.balanceStorageSlot,
],
),
)]: toHex(maxUint96, { size: 32 }),
},
},
}
: undefined;
const estimates = await estimateUserOpGas(
{
userOp: partialOp,
options: bundlerOptions,
},
stateOverrides,
);

Check warning on line 389 in packages/thirdweb/src/wallets/smart/lib/userop.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/lib/userop.ts#L366-L389

Added lines #L366 - L389 were not covered by tests
partialOp.callGasLimit = estimates.callGasLimit;
partialOp.verificationGasLimit = estimates.verificationGasLimit;
partialOp.preVerificationGas = estimates.preVerificationGas;
partialOp.paymasterPostOpGasLimit =
paymasterResult.paymasterPostOpGasLimit || 0n;
partialOp.paymasterPostOpGasLimit = overrides?.tokenPaymaster
? 500000n // TODO: estimate this better, needed if there's an extra swap needed in the paymaster
: estimates.paymasterPostOpGasLimit || 0n;

Check warning on line 395 in packages/thirdweb/src/wallets/smart/lib/userop.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/lib/userop.ts#L393-L395

Added lines #L393 - L395 were not covered by tests
partialOp.paymasterVerificationGasLimit =
paymasterResult.paymasterVerificationGasLimit || 0n;
estimates.paymasterVerificationGasLimit || 0n;

Check warning on line 397 in packages/thirdweb/src/wallets/smart/lib/userop.ts

View check run for this annotation

Codecov / codecov/patch

packages/thirdweb/src/wallets/smart/lib/userop.ts#L397

Added line #L397 was not covered by tests
// need paymaster to re-sign after estimates
const paymasterResult2 = (await getPaymasterAndData({
userOp: partialOp,
Expand Down
Loading
Loading