Skip to content

Commit

Permalink
fix op max cost calculations
Browse files Browse the repository at this point in the history
  • Loading branch information
mouseless0x committed Oct 14, 2024
1 parent e1c8a0a commit 3345aae
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ To find your userOperation's max cost, we will need to sum up all it's gas value
- `paymasterVerificationGasLimit` (v0.7 only)
- `paymasterPostOpGasLimit` (v0.7 only)

Once the gas values are summed up, we multiply it by the maxFeePerGas to get the max cost for the userOperation.

```ts
// [!include ~/snippets/erc20-paymaster/calculate-costs.ts:calculateMaxCost]
```
Expand Down
4 changes: 3 additions & 1 deletion docs/snippets/erc20-paymaster/calculate-costs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,14 @@ const op = await smartAccountClient.prepareUserOperation({
},
})

const userOperationMaxCost =
const userOperationMaxGas =
op.preVerificationGas +
op.verificationGasLimit +
op.callGasLimit +
(op.paymasterVerificationGasLimit || 0n) +
(op.paymasterPostOpGasLimit || 0n)

const userOperationMaxCost = userOperationMaxGas * op.maxFeePerGas
// [!endregion calculateMaxCost]

// [!region calculateCostInToken]
Expand Down
4 changes: 3 additions & 1 deletion docs/snippets/erc20-paymaster/how-to.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,11 @@ const userOperationMaxGas =
(userOperation.paymasterPostOpGasLimit || 0n) +
(userOperation.paymasterVerificationGasLimit || 0n)

const userOperationMaxCost = userOperationMaxGas * userOperation.maxFeePerGas

// using formula here https://github.com/pimlicolabs/singleton-paymaster/blob/main/src/base/BaseSingletonPaymaster.sol#L334-L341
const maxCostInToken =
((userOperationMaxGas + postOpGas * userOperation.maxFeePerGas) * exchangeRate) / BigInt(1e18)
((userOperationMaxCost + postOpGas * userOperation.maxFeePerGas) * exchangeRate) / BigInt(1e18)

// [!endregion getTokenQuotes]

Expand Down

0 comments on commit 3345aae

Please sign in to comment.