From 2bde7af87ee8a30dcaf0d78fb07b8bf5597ea497 Mon Sep 17 00:00:00 2001 From: tommyrharper Date: Tue, 9 Jul 2024 09:39:05 +0100 Subject: [PATCH] =?UTF-8?q?=E2=9C=85=20=F0=9F=A7=B9=20Move=20deployment=20?= =?UTF-8?q?user=20op=20into=20setUp=20func?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- script/utils/parameters/BaseParameters.sol | 29 ++++++++- test/MarginPaymaster.t.sol | 76 +++++++++++++++++++++- test/utils/Bootstrap.sol | 73 ++------------------- 3 files changed, 108 insertions(+), 70 deletions(-) diff --git a/script/utils/parameters/BaseParameters.sol b/script/utils/parameters/BaseParameters.sol index 2e1f179..2704889 100644 --- a/script/utils/parameters/BaseParameters.sol +++ b/script/utils/parameters/BaseParameters.sol @@ -1,4 +1,31 @@ // SPDX-License-Identifier: GPL-3.0-or-later pragma solidity 0.8.25; -contract BaseParameters {} +contract BaseParameters { + // https://app.safe.global/home?safe=base:0x2f4004Bc32cc5D18a62fE26E35A0881d5397c549 + address public constant PDAO = 0x2f4004Bc32cc5D18a62fE26E35A0881d5397c549; + + address public constant PERPS_MARKET_PROXY = address(0); + + address public constant SPOT_MARKET_PROXY = address(0); + + address public constant USD_PROXY = address(0); + + // https://usecannon.com/packages/synthetix-perps-market/3.3.5/8453-andromeda + address public constant PERPS_MARKET_PROXY_ANDROMEDA = + 0x0A2AF931eFFd34b81ebcc57E3d3c9B1E1dE1C9Ce; + + // https://usecannon.com/packages/synthetix-spot-market/3.3.5/8453-andromeda + address public constant SPOT_MARKET_PROXY_ANDROMEDA = + 0x18141523403e2595D31b22604AcB8Fc06a4CaA61; + + // https://usecannon.com/packages/synthetix/3.3.5/8453-andromeda + address public constant USD_PROXY_ANDROMEDA = + 0x09d51516F38980035153a554c26Df3C6f51a23C3; + + // https://basescan.org/token/0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913 + address public constant USDC = 0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913; + + // https://usecannon.com/packages/synthetix-spot-market/3.3.5/84531-andromeda + uint128 public constant SUSDC_SPOT_MARKET_ID = 1; +} diff --git a/test/MarginPaymaster.t.sol b/test/MarginPaymaster.t.sol index cae57ae..a57d883 100644 --- a/test/MarginPaymaster.t.sol +++ b/test/MarginPaymaster.t.sol @@ -2,16 +2,90 @@ pragma solidity 0.8.25; import {Bootstrap} from "test/utils/Bootstrap.sol"; +import {EntryPoint, UserOperation} from "lib/account-abstraction/contracts/core/EntryPoint.sol"; +import {AccountFactory, Account} from "src/Account.sol"; contract MarginPaymasterTest is Bootstrap { uint256 constant BASE_BLOCK_NUMBER = 16841532; + function setUp() public { /// @dev uncomment the following line to test in a forked environment /// at a specific block number vm.rollFork(BASE_BLOCK_NUMBER); initializeLocal(); + + entryPoint = new EntryPoint(); + accountFactory = new AccountFactory(); + vm.deal(address(this), initialPaymasterBalance); + entryPoint.depositTo{value: initialPaymasterBalance}( + marginPaymasterAddress + ); + + bytes memory initCode = abi.encodePacked( + address(accountFactory), + abi.encodeCall(accountFactory.createAccount, (address(this))) + ); + + try entryPoint.getSenderAddress(initCode) { + assert(false); + } catch (bytes memory reason) { + bytes memory result = new bytes(20); + assembly { + // Copy the last 20 bytes from `reason` to `result` + mstore( + add(result, 32), + mload(add(add(reason, 32), sub(mload(reason), 20))) + ) + } + sender = bytesToAddress(result); + } + account = Account(sender); + + uint256 nonce = entryPoint.getNonce(sender, 0); + bytes memory signature; + UserOperation memory userOp = UserOperation({ + sender: sender, + nonce: nonce, + initCode: initCode, + callData: abi.encodeWithSelector(Account.execute.selector), + callGasLimit: 800_000, + verificationGasLimit: 800_000, + preVerificationGas: 200_000, + maxFeePerGas: 10 gwei, + maxPriorityFeePerGas: 10 gwei, + paymasterAndData: abi.encodePacked(address(marginPaymaster)), + signature: signature + }); + + ops.push(userOp); + + assertEq(sender.code.length, 0); + assertEq(sender.balance, 0); + uint256 balanceOfPaymasterBefore = entryPoint.balanceOf( + address(marginPaymaster) + ); + assertEq(balanceOfPaymasterBefore, initialPaymasterBalance); + + vm.prank(bundler); + entryPoint.handleOps(ops, bundler); + + uint256 balanceOfPaymasterAfter = entryPoint.balanceOf( + address(marginPaymaster) + ); + assertLt(balanceOfPaymasterAfter, balanceOfPaymasterBefore); } - function testSetupWorks() public {} + function testAccountDeployed() public { + assertGt(sender.code.length, 0); + assertEq(account.count(), 1); + } + + function bytesToAddress( + bytes memory bys + ) private pure returns (address addr) { + assembly { + addr := mload(add(bys, 20)) + } + } } diff --git a/test/utils/Bootstrap.sol b/test/utils/Bootstrap.sol index 00d2d15..93b0e39 100644 --- a/test/utils/Bootstrap.sol +++ b/test/utils/Bootstrap.sol @@ -13,6 +13,7 @@ contract Bootstrap is Test { error SenderAddressResult(address sender); MarginPaymaster internal marginPaymaster; + address internal marginPaymasterAddress; EntryPoint internal entryPoint; AccountFactory internal accountFactory; Account internal account; @@ -21,90 +22,26 @@ contract Bootstrap is Test { address payable user = payable(vm.addr(0x1234)); address payable bundler = payable(vm.addr(0x12345)); uint256 internal initialPaymasterBalance = 10 ether; + address internal sender; UserOperation[] ops; function initializeLocal() internal { BootstrapLocal bootstrap = new BootstrapLocal(); - address marginPaymasterAddress = bootstrap.init(); - + marginPaymasterAddress = bootstrap.init(); marginPaymaster = MarginPaymaster(marginPaymasterAddress); - entryPoint = new EntryPoint(); - accountFactory = new AccountFactory(); - vm.deal(address(this), initialPaymasterBalance); - entryPoint.depositTo{value: initialPaymasterBalance}(marginPaymasterAddress); - - bytes memory initCode = abi.encodePacked( - address(accountFactory), - abi.encodeCall(accountFactory.createAccount, (address(this))) - ); - - address sender; - try entryPoint.getSenderAddress(initCode) { - assert(false); - } catch (bytes memory reason) { - bytes memory result = new bytes(20); - assembly { - // Copy the last 20 bytes from `reason` to `result` - mstore( - add(result, 32), - mload(add(add(reason, 32), sub(mload(reason), 20))) - ) - } - sender = bytesToAddress(result); - } - account = Account(sender); - - uint256 nonce = entryPoint.getNonce(sender, 0); - bytes memory signature; - UserOperation memory userOp = UserOperation({ - sender: sender, - nonce: nonce, - initCode: initCode, - callData: abi.encodeWithSelector(Account.execute.selector), - callGasLimit: 800_000, - verificationGasLimit: 800_000, - preVerificationGas: 200_000, - maxFeePerGas: 10 gwei, - maxPriorityFeePerGas: 10 gwei, - paymasterAndData: abi.encodePacked(address(marginPaymaster)), - signature: signature - }); - - ops.push(userOp); - - assertEq(sender.code.length, 0); - assertEq(sender.balance, 0); - uint256 balanceOfPaymasterBefore = entryPoint.balanceOf(address(marginPaymaster)); - assertEq(balanceOfPaymasterBefore, initialPaymasterBalance); - - vm.prank(bundler); - entryPoint.handleOps(ops, bundler); - - - uint256 balanceOfPaymasterAfter = entryPoint.balanceOf(address(marginPaymaster)); - assertLt(balanceOfPaymasterAfter, balanceOfPaymasterBefore); - - assertGt(sender.code.length, 0); - assertEq(account.count(), 1); - } - - function bytesToAddress(bytes memory bys) private pure returns (address addr) { - assembly { - addr := mload(add(bys, 20)) - } } function initializeOptimismGoerli() internal { BootstrapOptimismGoerli bootstrap = new BootstrapOptimismGoerli(); - address marginPaymasterAddress = bootstrap.init(); + marginPaymasterAddress = bootstrap.init(); marginPaymaster = MarginPaymaster(marginPaymasterAddress); } function initializeOptimism() internal { BootstrapOptimismGoerli bootstrap = new BootstrapOptimismGoerli(); - address marginPaymasterAddress = bootstrap.init(); + marginPaymasterAddress = bootstrap.init(); marginPaymaster = MarginPaymaster(marginPaymasterAddress); }