Skip to content

Commit

Permalink
✅ 🧹 Move deployment user op into setUp func
Browse files Browse the repository at this point in the history
  • Loading branch information
tommyrharper committed Jul 9, 2024
1 parent af8f6b0 commit 2bde7af
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 70 deletions.
29 changes: 28 additions & 1 deletion script/utils/parameters/BaseParameters.sol
Original file line number Diff line number Diff line change
@@ -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;
}
76 changes: 75 additions & 1 deletion test/MarginPaymaster.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
}
73 changes: 5 additions & 68 deletions test/utils/Bootstrap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}
Expand Down

0 comments on commit 2bde7af

Please sign in to comment.