Skip to content

Commit

Permalink
πŸ‘·πŸ» βœ… Add withdraw eth and usdc helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyrharper committed Jul 11, 2024
1 parent 7a4da1c commit 9b1f36f
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/MarginPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,20 @@ contract MarginPaymaster is IPaymaster, Zap, Ownable {
entryPoint.withdrawTo(withdrawAddress, withdrawAmount);
}

function withdrawETH(
address payable withdrawAddress,
uint256 amount
) external onlyOwner {
withdrawAddress.call{value: amount}("");
}

function withdrawUSDC(
address withdrawAddress,
uint256 amount
) external onlyOwner {
_USDC.transfer(withdrawAddress, amount);
}

/*//////////////////////////////////////////////////////////////
HELPERS
//////////////////////////////////////////////////////////////*/
Expand Down
47 changes: 47 additions & 0 deletions test/MarginPaymaster.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,53 @@ contract MarginPaymasterTest is Bootstrap {
marginPaymaster.withdrawTo(withdrawAddress, depositAmount);
}

function testWithdrawETH() public {
uint256 depositAmount = 1e18; // 1 ETH
address payable withdrawAddress = payable(address(0x321));

vm.deal(marginPaymasterAddress, depositAmount);

// Withdraw ETH from the contract
marginPaymaster.withdrawETH(withdrawAddress, depositAmount);

// Check if the funds were transferred to the withdrawAddress
uint256 withdrawAddressBalance = withdrawAddress.balance;
assertEq(withdrawAddressBalance, depositAmount);
}

function testWithdrawETH_onlyOwner() public {
uint256 depositAmount = 1e18; // 1 ETH
address payable withdrawAddress = payable(address(0x321));
// Attempt to withdraw ETH as a non-owner
vm.prank(withdrawAddress); // some non-owner address
vm.expectRevert("Ownable: caller is not the owner");
marginPaymaster.withdrawETH(withdrawAddress, depositAmount);
}

function testWithdrawUSDC() public {
uint256 depositAmount = 1000 * 1e6; // 1000 USDC
address withdrawAddress = address(0x321);

// Mint USDC to the contract
mintUSDC(address(marginPaymaster), depositAmount);

// Withdraw USDC from the contract
marginPaymaster.withdrawUSDC(withdrawAddress, depositAmount);

// Check if the funds were transferred to the withdrawAddress
uint256 withdrawAddressBalance = usdc.balanceOf(withdrawAddress);
assertEq(withdrawAddressBalance, depositAmount);
}

function testWithdrawUSDC_onlyOwner() public {
uint256 depositAmount = 1000 * 1e6; // 1000 USDC
address withdrawAddress = address(0x321);
// Attempt to withdraw USDC as a non-owner
vm.prank(withdrawAddress); // some non-owner address
vm.expectRevert("Ownable: caller is not the owner");
marginPaymaster.withdrawUSDC(withdrawAddress, depositAmount);
}

/*//////////////////////////////////////////////////////////////
USER OP TESTS
//////////////////////////////////////////////////////////////*/
Expand Down

0 comments on commit 9b1f36f

Please sign in to comment.