Skip to content

Commit

Permalink
renamed
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsoncusack committed Apr 4, 2024
1 parent e5efda2 commit fc38b23
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 75 deletions.
30 changes: 15 additions & 15 deletions .gas-snapshot
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ IsValidWithdrawalSignature:test_returnsTrueWithValidSignature() (gas: 51016)
OwnerWithdrawTest:test_revertsIfNotOwner() (gas: 15305)
OwnerWithdrawTest:test_transfersERC20Successfully(uint256) (runs: 256, μ: 97271, ~: 99711)
OwnerWithdrawTest:test_transfersETHSuccessfully(uint256) (runs: 256, μ: 51418, ~: 53415)
PostOpTest:test_RevertsIfPostOpFailed(uint256,uint256,uint256) (runs: 256, μ: 97229, ~: 99371)
PostOpTest:test_entryPointAddStake(uint112,uint32) (runs: 256, μ: 56278, ~: 56278)
PostOpTest:test_entryPointDeposit(uint112) (runs: 256, μ: 32457, ~: 32940)
PostOpTest:test_entryPointUnlockStake() (gas: 54933)
PostOpTest:test_entryPointWithdraw(uint112) (runs: 256, μ: 63371, ~: 65321)
PostOpTest:test_entryPointWithdrawStake() (gas: 71848)
PostOpTest:test_RevertsIfPostOpFailed(uint256,uint256,uint256) (runs: 256, μ: 97259, ~: 99371)
PostOpTest:test_entryPointAddStake(uint112,uint32) (runs: 256, μ: 56233, ~: 56233)
PostOpTest:test_entryPointDeposit(uint112) (runs: 256, μ: 32479, ~: 32962)
PostOpTest:test_entryPointUnlockStake() (gas: 54888)
PostOpTest:test_entryPointWithdraw(uint112) (runs: 256, μ: 63050, ~: 65300)
PostOpTest:test_entryPointWithdrawStake() (gas: 71803)
PostOpTest:test_paymasterPaysForOp() (gas: 210663)
PostOpTest:test_transfersExcess(uint256,uint256,uint256,uint256) (runs: 256, μ: 112452, ~: 113488)
SetMaxWithdrawPercent:test_emitsCorrectly(uint256) (runs: 256, μ: 19851, ~: 20191)
SetMaxWithdrawPercent:test_reverts_whenNotCalledByOwner() (gas: 12257)
SetMaxWithdrawPercent:test_setsMaxWithdrawPercent(uint256) (runs: 256, μ: 19209, ~: 19549)
PostOpTest:test_transfersExcess(uint256,uint256,uint256,uint256) (runs: 256, μ: 112442, ~: 113477)
SetMaxWithdrawDenominator:test_emitsCorrectly(uint256) (runs: 256, μ: 19851, ~: 20191)
SetMaxWithdrawDenominator:test_reverts_whenNotCalledByOwner() (gas: 12257)
SetMaxWithdrawDenominator:test_setsMaxWithdrawPercent(uint256) (runs: 256, μ: 19209, ~: 19549)
Simulate:test() (gas: 12114)
ValidatePaymasterUserOpTest:test_emitsCorrectly(address,uint256,uint256) (runs: 256, μ: 104847, ~: 106080)
ValidatePaymasterUserOpTest:test_recordsNonceUsed(uint256) (runs: 256, μ: 112158, ~: 113247)
Expand All @@ -23,16 +23,16 @@ ValidatePaymasterUserOpTest:test_returnsCorrectly() (gas: 91185)
ValidatePaymasterUserOpTest:test_revertsIfMaxCostMoreThanRequested() (gas: 39141)
ValidatePaymasterUserOpTest:test_revertsIfNonceUsed() (gas: 103502)
ValidatePaymasterUserOpTest:test_revertsIfWithdrawAssetNotZero() (gas: 55952)
ValidatePaymasterUserOpTest:test_reverts_whenWithdrawExceedsMaxAllowed(uint256,uint256,uint256) (runs: 256, μ: 100793, ~: 100992)
ValidatePaymasterUserOpTest:test_reverts_whenWithdrawExceedsMaxAllowed(uint256,uint256,uint256) (runs: 256, μ: 100806, ~: 100992)
ValidateTest:test_receive() (gas: 14687)
WithdrawGasExcess:test_RevertsIfNoExcess(uint256) (runs: 256, μ: 71133, ~: 71396)
WithdrawGasExcess:test_transferExcess(uint256,uint256,uint256) (runs: 256, μ: 104290, ~: 104290)
WithdrawTest:test_emitsCorrectly(address,uint256,uint256) (runs: 256, μ: 118193, ~: 121368)
WithdrawTest:test_emitsCorrectlyERC20Withdraw(address,uint256,uint256) (runs: 256, μ: 162446, ~: 164773)
WithdrawTest:test_emitsCorrectly(address,uint256,uint256) (runs: 256, μ: 118270, ~: 121368)
WithdrawTest:test_emitsCorrectlyERC20Withdraw(address,uint256,uint256) (runs: 256, μ: 162427, ~: 164773)
WithdrawTest:test_recordsNonceUsed(uint256) (runs: 256, μ: 111372, ~: 112461)
WithdrawTest:test_revertsIfExpired(uint48,uint256) (runs: 256, μ: 61675, ~: 61785)
WithdrawTest:test_revertsIfExpired(uint48,uint256) (runs: 256, μ: 61664, ~: 61785)
WithdrawTest:test_revertsIfNonceUsed() (gas: 95844)
WithdrawTest:test_revertsIfWrongSignature() (gas: 63830)
WithdrawTest:test_reverts_whenWithdrawExceedsMaxAllowed(uint256,uint256,uint256) (runs: 256, μ: 85154, ~: 85096)
WithdrawTest:test_reverts_whenWithdrawExceedsMaxAllowed(uint256,uint256,uint256) (runs: 256, μ: 85119, ~: 85096)
WithdrawTest:test_transfersERC20Successfully(uint256) (runs: 256, μ: 140626, ~: 143066)
WithdrawTest:test_transfersETHSuccessfully(uint256) (runs: 256, μ: 91692, ~: 93689)
53 changes: 26 additions & 27 deletions src/MagicSpend.sol
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,14 @@ contract MagicSpend is Ownable, IPaymaster {
uint48 expiry;
}

/// @notice The maximum percentage of address.balance a single withdraw can use.
/// @notice address.balance divided by maxWithdrawDenominator expresses the max WithdrawRequest.amount
/// allowed for a native asset withdraw.
///
/// @dev Only applies to native asset withdraws.
/// @dev Helps prevent withdraws in the same transaction leading to reverts and hurting paymaster reputation.
/// @dev Percent expressed in whole units, e.g. 20 means no single withdraw
/// can exceed 20% of address.balance
uint256 public maxWithdrawPercent;
uint256 public maxWithdrawDenominator;

/// @notice Track the ETH available to be withdrawn per user.
mapping(address user => uint256 amount) internal _withdrawableETH;
/// @notice Track the amount of native asset available to be withdrawn per user.
mapping(address user => uint256 amount) internal _withdrawable;

/// @dev Mappings keeping track of already used nonces per user to prevent replays of withdraw requests.
mapping(uint256 nonce => mapping(address user => bool used)) internal _nonceUsed;
Expand All @@ -52,10 +50,10 @@ contract MagicSpend is Ownable, IPaymaster {
/// @param nonce The request nonce.
event MagicSpendWithdrawal(address indexed account, address indexed asset, uint256 amount, uint256 nonce);

/// @notice Emitted when the `maxWithdrawPercent` is set.
/// @notice Emitted when the `maxWithdrawDenominator` is set.
///
/// @param newPercent The new maxWithdrawPercent value.
event MaxWithdrawPercentSet(uint256 newPercent);
/// @param newDenominator The new maxWithdrawDenominator value.
event MaxWithdrawDenominatorSet(uint256 newDenominator);

/// @notice Thrown when the withdraw request signature is invalid.
///
Expand Down Expand Up @@ -85,7 +83,7 @@ contract MagicSpend is Ownable, IPaymaster {
/// @param asset The requested asset.
error UnsupportedPaymasterAsset(address asset);

/// @notice Thrown if WithdrawRequest.amount exceeds address(this).balance / maxWithdrawPercent.
/// @notice Thrown if WithdrawRequest.amount exceeds address(this).balance / maxWithdrawDenominator.
///
/// @param requestedAmount The requested amount excluding gas.
/// @param maxAllowed The current max allowed withdraw.
Expand All @@ -110,9 +108,10 @@ contract MagicSpend is Ownable, IPaymaster {
/// @notice Deploy the contract and set its initial owner.
///
/// @param owner_ The initial owner of this contract.
constructor(address owner_, uint256 maxWithdrawPercent_) {
/// @param maxWithdrawDenominator_ The initial maxWithdrawDenominator.
constructor(address owner_, uint256 maxWithdrawDenominator_) {
Ownable._initializeOwner(owner_);
_setMaxWithdrawPercent(maxWithdrawPercent_);
_setMaxWithdrawDenominator(maxWithdrawDenominator_);
}

/// @notice Receive function allowing ETH to be deposited in this contract.
Expand Down Expand Up @@ -141,7 +140,7 @@ contract MagicSpend is Ownable, IPaymaster {
validationData = (sigFailed ? 1 : 0) | (uint256(withdrawRequest.expiry) << 160);

// NOTE: Do not include the gas part in withdrawable funds as it will be handled in `postOp()`.
_withdrawableETH[userOp.sender] += withdrawAmount - maxCost;
_withdrawable[userOp.sender] += withdrawAmount - maxCost;
context = abi.encode(maxCost, userOp.sender);
}

Expand All @@ -151,7 +150,7 @@ contract MagicSpend is Ownable, IPaymaster {
onlyEntryPoint
{
// `PostOpMode.postOpReverted` should never happen.
// The flow here can only revert if there are > maxWithdrawPercent
// The flow here can only revert if there are > maxWithdrawDenominator
// withdraws in the same transaction, which should be highly unlikely.
// If the ETH transfer fails, the entire bundle will revert due an issue in the EntryPoint
// https://github.com/eth-infinitism/account-abstraction/pull/293
Expand All @@ -163,10 +162,10 @@ contract MagicSpend is Ownable, IPaymaster {

// Compute the total remaining funds available for the user accout.
// NOTE: Take into account the user operation gas that was not consumed.
uint256 withdrawable = _withdrawableETH[account] + (maxGasCost - actualGasCost);
uint256 withdrawable = _withdrawable[account] + (maxGasCost - actualGasCost);

// Send the all remaining funds to the user accout.
delete _withdrawableETH[account];
delete _withdrawable[account];
if (withdrawable > 0) {
SafeTransferLib.forceSafeTransferETH(account, withdrawable, SafeTransferLib.GAS_STIPEND_NO_STORAGE_WRITES);
}
Expand All @@ -177,11 +176,11 @@ contract MagicSpend is Ownable, IPaymaster {
/// @dev Can be called back during the `UserOperation` execution to sponsor funds for non-gas related
/// use cases (e.g., swap or mint).
function withdrawGasExcess() external {
uint256 amount = _withdrawableETH[msg.sender];
uint256 amount = _withdrawable[msg.sender];
// we could allow 0 value transfers, but prefer to be explicit
if (amount == 0) revert NoExcess();

delete _withdrawableETH[msg.sender];
delete _withdrawable[msg.sender];
_withdraw(address(0), msg.sender, amount);
}

Expand Down Expand Up @@ -259,13 +258,13 @@ contract MagicSpend is Ownable, IPaymaster {
IEntryPoint(entryPoint()).withdrawStake(to);
}

/// @notice sets maxWithdrawPercent
/// @notice Sets maxWithdrawDenominator.
///
/// @dev Reverts if not called by the owner of the contract.
///
/// @param newPercent The new value for maxWithdrawPercent.
function setMaxWithdrawPercent(uint256 newPercent) external onlyOwner {
_setMaxWithdrawPercent(newPercent);
/// @param newDenominator The new value for maxWithdrawDenominator.
function setMaxWithdrawDenominator(uint256 newDenominator) external onlyOwner {
_setMaxWithdrawDenominator(newDenominator);
}

/// @notice Returns whether the `withdrawRequest` signature is valid for the given `account`.
Expand Down Expand Up @@ -324,10 +323,10 @@ contract MagicSpend is Ownable, IPaymaster {
return 0x5FF137D4b0FDCD49DcA30c7CF57E578a026d2789;
}

function _setMaxWithdrawPercent(uint256 newPercent) internal {
maxWithdrawPercent = newPercent;
function _setMaxWithdrawDenominator(uint256 newDenominator) internal {
maxWithdrawDenominator = newDenominator;

emit MaxWithdrawPercentSet(newPercent);
emit MaxWithdrawDenominatorSet(newDenominator);
}

/// @notice Validate the `withdrawRequest` against the given `account`.
Expand All @@ -342,7 +341,7 @@ contract MagicSpend is Ownable, IPaymaster {
revert InvalidNonce(withdrawRequest.nonce);
}

uint256 maxAllowed = address(this).balance / maxWithdrawPercent;
uint256 maxAllowed = address(this).balance / maxWithdrawDenominator;
if (withdrawRequest.asset == address(0) && withdrawRequest.amount > maxAllowed) {
revert WithdrawTooLarge(withdrawRequest.amount, maxAllowed);
}
Expand Down
26 changes: 26 additions & 0 deletions test/SetMaxWithdrawDenominator.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.23;

import "./MagicSpend.t.sol";
import {Ownable} from "./MagicSpend.t.sol";

contract SetMaxWithdrawDenominator is MagicSpendTest {
function test_reverts_whenNotCalledByOwner() public {
vm.prank(makeAddr("fake"));
vm.expectRevert(Ownable.Unauthorized.selector);
magic.setMaxWithdrawDenominator(20);
}

function test_setsMaxWithdrawPercent(uint256 newDenominator) public {
vm.prank(owner);
magic.setMaxWithdrawDenominator(newDenominator);
assertEq(magic.maxWithdrawDenominator(), newDenominator);
}

function test_emitsCorrectly(uint256 newDenominator) public {
vm.expectEmit(false, false, false, true);
emit MagicSpend.MaxWithdrawDenominatorSet(newDenominator);
vm.prank(owner);
magic.setMaxWithdrawDenominator(newDenominator);
}
}
26 changes: 0 additions & 26 deletions test/SetMaxWithdrawPercent.t.sol

This file was deleted.

12 changes: 7 additions & 5 deletions test/Validate.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -41,22 +41,24 @@ abstract contract ValidateTest is MagicSpendTest {
function test_reverts_whenWithdrawExceedsMaxAllowed(
uint256 accountBalance,
uint256 withdraw,
uint256 maxWithdrawPercent
uint256 maxWithdrawDenominator
) public virtual {
vm.assume(maxWithdrawPercent > 0);
vm.assume(maxWithdrawDenominator > 0);
accountBalance = bound(accountBalance, 0, type(uint256).max - 1);
withdraw = bound(withdraw, accountBalance / maxWithdrawPercent + 1, type(uint256).max);
withdraw = bound(withdraw, accountBalance / maxWithdrawDenominator + 1, type(uint256).max);
amount = withdraw;

vm.stopPrank();
vm.startPrank(owner);
magic.setMaxWithdrawPercent(maxWithdrawPercent);
magic.setMaxWithdrawDenominator(maxWithdrawDenominator);
magic.ownerWithdraw(address(0), address(1), address(magic).balance);
vm.stopPrank();

vm.deal(address(magic), accountBalance);
vm.expectRevert(
abi.encodeWithSelector(MagicSpend.WithdrawTooLarge.selector, withdraw, accountBalance / maxWithdrawPercent)
abi.encodeWithSelector(
MagicSpend.WithdrawTooLarge.selector, withdraw, accountBalance / maxWithdrawDenominator
)
);
vm.startPrank(invoker);
_validateInvokingCall();
Expand Down
4 changes: 2 additions & 2 deletions test/ValidatePaymasterUserOp.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ contract ValidatePaymasterUserOpTest is PaymasterMagicSpendBaseTest, ValidateTes
function test_reverts_whenWithdrawExceedsMaxAllowed(
uint256 accountBalance,
uint256 withdraw,
uint256 maxWithdrawPercent
uint256 MaxWithdrawDenominator
) public override {
maxCost = withdraw;
super.test_reverts_whenWithdrawExceedsMaxAllowed(accountBalance, withdraw, maxWithdrawPercent);
super.test_reverts_whenWithdrawExceedsMaxAllowed(accountBalance, withdraw, MaxWithdrawDenominator);
}

function _validateInvokingCall() internal override {
Expand Down

0 comments on commit fc38b23

Please sign in to comment.