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
5 changes: 5 additions & 0 deletions .changeset/tall-socks-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"thirdweb": patch
---

Get userOp prices from bundler
22 changes: 21 additions & 1 deletion packages/thirdweb/src/wallets/smart/lib/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { getClientFetch } from "../../../utils/fetch.js";
import { hexToBigInt, type Hex } from "../../../utils/encoding/hex.js";
import type {
EstimationResult,
GasPriceResult,
SmartWalletOptions,
UserOperation,
} from "../types.js";
Expand Down Expand Up @@ -58,6 +59,24 @@ export async function estimateUserOpGas(args: {
};
}

/**
* @internal
*/
export async function getUserOpGasPrice(args: {
options: SmartWalletOptions & { client: ThirdwebClient };
}): Promise<GasPriceResult> {
const res = await sendBundlerRequest({
...args,
operation: "thirdweb_getUserOperationGasPrice",
params: [],
});

return {
maxPriorityFeePerGas: hexToBigInt(res.maxPriorityFeePerGas),
maxFeePerGas: hexToBigInt(res.maxFeePerGas),
};
}

/**
* @internal
*/
Expand Down Expand Up @@ -98,7 +117,8 @@ async function sendBundlerRequest(args: {
operation:
| "eth_estimateUserOperationGas"
| "eth_sendUserOperation"
| "eth_getUserOperationReceipt";
| "eth_getUserOperationReceipt"
| "thirdweb_getUserOperationGasPrice";
params: any[];
}) {
const { options, operation, params } = args;
Expand Down
42 changes: 30 additions & 12 deletions packages/thirdweb/src/wallets/smart/lib/userop.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import { isContractDeployed } from "../../../utils/bytecode/is-contract-deployed
import type { ThirdwebContract } from "../../../contract/contract.js";
import { encode } from "../../../transaction/actions/encode.js";
import { getDefaultGasOverrides } from "../../../gas/fee-data.js";
import { DUMMY_SIGNATURE, ENTRYPOINT_ADDRESS } from "./constants.js";
import {
DUMMY_SIGNATURE,
ENTRYPOINT_ADDRESS,
getDefaultBundlerUrl,
} from "./constants.js";
import { getPaymasterAndData } from "./paymaster.js";
import { estimateUserOpGas } from "./bundler.js";
import { estimateUserOpGas, getUserOpGasPrice } from "./bundler.js";
import { randomNonce } from "./utils.js";
import { prepareCreateAccount } from "./calls.js";
import type { Account } from "../../interfaces/wallet.js";
Expand All @@ -17,6 +21,7 @@ import { hexToBytes } from "../../../utils/encoding/to-bytes.js";
import type { Hex } from "../../../utils/encoding/hex.js";
import { encodeAbiParameters } from "../../../utils/abi/encodeAbiParameters.js";
import type { ThirdwebClient } from "../../../client/client.js";
import { isThirdwebUrl } from "../../../utils/fetch.js";

/**
* Create an unsigned user operation
Expand Down Expand Up @@ -44,17 +49,30 @@ export async function createUnsignedUserOp(args: {
options,
});
const callData = await encode(executeTx);

let { maxFeePerGas, maxPriorityFeePerGas } = executeTx;
if (!maxFeePerGas || !maxPriorityFeePerGas) {
const feeData = await getDefaultGasOverrides(
factoryContract.client,
factoryContract.chain,
);
if (!maxPriorityFeePerGas) {
maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? undefined;
}
if (!maxFeePerGas) {
maxFeePerGas = feeData.maxFeePerGas ?? undefined;
const bundlerUrl =
options.overrides?.bundlerUrl ?? getDefaultBundlerUrl(options.chain);
if (isThirdwebUrl(bundlerUrl)) {
// get gas prices from bundler
const bundlerGasPrice = await getUserOpGasPrice({
options,
});
maxFeePerGas = bundlerGasPrice.maxFeePerGas;
maxPriorityFeePerGas = bundlerGasPrice.maxPriorityFeePerGas;
} else {
// otherwise fallback to RPC gas prices if not passed in explicitely
if (!maxFeePerGas || !maxPriorityFeePerGas) {
const feeData = await getDefaultGasOverrides(
factoryContract.client,
factoryContract.chain,
);
if (!maxPriorityFeePerGas) {
maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? undefined;
}
if (!maxFeePerGas) {
maxFeePerGas = feeData.maxFeePerGas ?? undefined;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions packages/thirdweb/src/wallets/smart/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,8 @@ export type EstimationResult = {
verificationGasLimit: bigint;
callGasLimit: bigint;
};

export type GasPriceResult = {
maxFeePerGas: bigint;
maxPriorityFeePerGas: bigint;
};