Skip to content

Commit

Permalink
refactor:push updated test and logs
Browse files Browse the repository at this point in the history
  • Loading branch information
livingrockrises committed Oct 14, 2024
1 parent a45615b commit 0be09ad
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 47 deletions.
61 changes: 27 additions & 34 deletions test/base/TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {

function createUserOp(
Vm.Wallet memory sender,
bytes memory callData,
BiconomySponsorshipPaymaster paymaster,
uint32 priceMarkup
)
Expand All @@ -139,41 +140,33 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
uint48 validUntil = uint48(block.timestamp + 1 days);
uint48 validAfter = uint48(block.timestamp);

userOp = buildUserOpWithCalldata(sender, "", address(VALIDATOR_MODULE));
userOp = buildUserOpWithCalldata(sender, callData, address(VALIDATOR_MODULE));


// Note: Maybe only do this once during the setup and allow to pass estimated limits in createUserOp call

// PaymasterData memory pmData = PaymasterData(3e6, 8e3, DAPP_ACCOUNT.addr, validUntil, validAfter, priceMarkup);
// (userOp.paymasterAndData,) = generateAndSignPaymasterData(
// userOp, PAYMASTER_SIGNER, paymaster, pmData
// );
// userOp.signature = signUserOp(sender, userOp);

// (,, uint256 verificationGasLimit, uint256 callGasLimit) = estimateUserOpGasCosts(userOp);
// // Estimate paymaster gas limits
// (, uint256 postopGasUsed, uint256 validationGasLimit, uint256 postopGasLimit) =
// estimatePaymasterGasCosts(paymaster, userOp, 5e4);

// // console2.log("postOpGasUsed");
// // console2.logUint(postopGasUsed);

// // uint256 prevValUnaccountedGas = paymaster.unaccountedGas();
// // console2.logUint(prevValUnaccountedGas);

// // Below may not be needed if unaccountedGas is set correctly
// vm.startPrank(paymaster.owner());
// // Set unaccounted gas to be gas used in postop + 1000 for EP overhead and penalty
// paymaster.setUnaccountedGas(postopGasUsed + 500);
// vm.stopPrank();

// // uint256 currentValUnaccountedGas = paymaster.unaccountedGas();
// // console2.logUint(currentValUnaccountedGas);

// // Ammend the userop to have new gas limits and signature
PaymasterData memory pmData = PaymasterData(24000, uint128(35000), DAPP_ACCOUNT.addr, validUntil, validAfter, priceMarkup);
(userOp.paymasterAndData,) = generateAndSignPaymasterData(
userOp, PAYMASTER_SIGNER, paymaster, pmData
);
userOp.signature = signUserOp(sender, userOp);

userOp.accountGasLimits = bytes32(abi.encodePacked(uint128(4e5), uint128(2e4)));
PaymasterData memory pmDataNew = PaymasterData(uint128(2e5), uint128(1e5), DAPP_ACCOUNT.addr, validUntil, validAfter, priceMarkup);
(,, uint256 verificationGasLimit, uint256 callGasLimit) = estimateUserOpGasCosts(userOp);
// Estimate paymaster gas limits
(, uint256 postopGasUsed, uint256 validationGasLimit, uint256 postopGasLimit) =
estimatePaymasterGasCosts(paymaster, userOp, 5e4);
console2.log("verificationGasLimit");
console2.logUint(verificationGasLimit);
console2.log("callGasLimit");
console2.logUint(callGasLimit);
console2.log("validationGasLimit");
console2.logUint(validationGasLimit);
console2.log("postopGasLimit");
console2.logUint(postopGasLimit);
console2.log("postopGasUsed");
console2.logUint(postopGasUsed);


userOp.accountGasLimits = bytes32(abi.encodePacked(uint128(55000), uint128(0)));
PaymasterData memory pmDataNew = PaymasterData(uint128(24000), uint128(35000), DAPP_ACCOUNT.addr, validUntil, validAfter, priceMarkup);
(userOp.paymasterAndData,) = generateAndSignPaymasterData(
userOp,
PAYMASTER_SIGNER,
Expand Down Expand Up @@ -382,11 +375,11 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {

// TODO
// Review: fix this properly. avoid out of stack errors
// assertGt(gasPaidByDapp, BUNDLER.addr.balance - initialBundlerBalance);
assertGt(gasPaidByDapp, BUNDLER.addr.balance - initialBundlerBalance);

// Ensure that max 1% difference between total gas paid + the adjustment premium and gas paid by dapp (from
// paymaster)
// assertApproxEqRel(totalGasFeePaid + actualPriceMarkup, gasPaidByDapp, 0.01e18);
assertApproxEqRel(totalGasFeePaid + actualPriceMarkup, gasPaidByDapp, 0.01e18);
}

function _toSingletonArray(address addr) internal pure returns (address[] memory) {
Expand Down
27 changes: 16 additions & 11 deletions test/unit/concrete/TestSponsorshipPaymaster.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,44 @@ contract TestSponsorshipPaymasterWithPriceMarkup is TestBase {
setupPaymasterTestEnvironment();
// Deploy Sponsorship Paymaster
bicoPaymaster = new BiconomySponsorshipPaymaster(
PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, PAYMASTER_FEE_COLLECTOR.addr, 7e3
PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, PAYMASTER_FEE_COLLECTOR.addr, 27000
);
}

function test_Deploy() external {
BiconomySponsorshipPaymaster testArtifact = new BiconomySponsorshipPaymaster(
PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, PAYMASTER_FEE_COLLECTOR.addr, 7e3
PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, PAYMASTER_FEE_COLLECTOR.addr, 27000
);
assertEq(testArtifact.owner(), PAYMASTER_OWNER.addr);
assertEq(address(testArtifact.entryPoint()), ENTRYPOINT_ADDRESS);
assertEq(testArtifact.verifyingSigner(), PAYMASTER_SIGNER.addr);
assertEq(testArtifact.feeCollector(), PAYMASTER_FEE_COLLECTOR.addr);
assertEq(testArtifact.unaccountedGas(), 7e3);
assertEq(testArtifact.unaccountedGas(), 27000);
}

function test_RevertIf_DeployWithSignerSetToZero() external {
vm.expectRevert(abi.encodeWithSelector(VerifyingSignerCanNotBeZero.selector));
new BiconomySponsorshipPaymaster(
PAYMASTER_OWNER.addr, ENTRYPOINT, address(0), PAYMASTER_FEE_COLLECTOR.addr, 7e3
PAYMASTER_OWNER.addr, ENTRYPOINT, address(0), PAYMASTER_FEE_COLLECTOR.addr, 27000
);
}

function test_RevertIf_DeployWithSignerAsContract() external {
vm.expectRevert(abi.encodeWithSelector(VerifyingSignerCanNotBeContract.selector));
new BiconomySponsorshipPaymaster(
PAYMASTER_OWNER.addr, ENTRYPOINT, address(ENTRYPOINT), PAYMASTER_FEE_COLLECTOR.addr, 7e3
PAYMASTER_OWNER.addr, ENTRYPOINT, address(ENTRYPOINT), PAYMASTER_FEE_COLLECTOR.addr, 27000
);
}


function test_RevertIf_DeployWithFeeCollectorSetToZero() external {
vm.expectRevert(abi.encodeWithSelector(FeeCollectorCanNotBeZero.selector));
new BiconomySponsorshipPaymaster(PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, address(0), 7e3);
new BiconomySponsorshipPaymaster(PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, address(0), 27000);
}

function test_RevertIf_DeployWithFeeCollectorAsContract() external {
vm.expectRevert(abi.encodeWithSelector(FeeCollectorCanNotBeContract.selector));
new BiconomySponsorshipPaymaster(PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, address(ENTRYPOINT), 7e3);
new BiconomySponsorshipPaymaster(PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, address(ENTRYPOINT), 27000);
}

function test_RevertIf_DeployWithUnaccountedGasCostTooHigh() external {
Expand All @@ -65,7 +65,7 @@ contract TestSponsorshipPaymasterWithPriceMarkup is TestBase {
assertEq(address(bicoPaymaster.entryPoint()), ENTRYPOINT_ADDRESS);
assertEq(bicoPaymaster.verifyingSigner(), PAYMASTER_SIGNER.addr);
assertEq(bicoPaymaster.feeCollector(), PAYMASTER_FEE_COLLECTOR.addr);
assertEq(bicoPaymaster.unaccountedGas(), 7e3);
assertEq(bicoPaymaster.unaccountedGas(), 27000);
}

function test_OwnershipTransfer() external prankModifier(PAYMASTER_OWNER.addr) {
Expand Down Expand Up @@ -204,10 +204,13 @@ contract TestSponsorshipPaymasterWithPriceMarkup is TestBase {
bicoPaymaster.withdrawTo(payable(BOB_ADDRESS), 1 ether);
}

function test_ValidatePaymasterAndPostOpWithoutPriceMarkup() external prankModifier(DAPP_ACCOUNT.addr) {
function test_ValidatePaymasterAndPostOpWithoutPriceMarkup() external {
startPrank(DAPP_ACCOUNT.addr);
bicoPaymaster.depositFor{ value: 10 ether }(DAPP_ACCOUNT.addr);
stopPrank();

PackedUserOperation[] memory ops = new PackedUserOperation[](1);
(PackedUserOperation memory userOp, bytes32 userOpHash) = createUserOp(ALICE, bicoPaymaster, 1e6);
(PackedUserOperation memory userOp, bytes32 userOpHash) = createUserOp(ALICE, "", bicoPaymaster, 1e6);
ops[0] = userOp;

uint256 initialBundlerBalance = BUNDLER.addr.balance;
Expand All @@ -218,7 +221,9 @@ contract TestSponsorshipPaymasterWithPriceMarkup is TestBase {
// submit userops
vm.expectEmit(true, false, true, true, address(bicoPaymaster));
emit IBiconomySponsorshipPaymaster.GasBalanceDeducted(DAPP_ACCOUNT.addr, 0, userOpHash);
startPrank(BUNDLER.addr);
ENTRYPOINT.handleOps(ops, payable(BUNDLER.addr));
stopPrank();

// Calculate and assert price markups and gas payments
calculateAndAssertAdjustments(
Expand All @@ -237,7 +242,7 @@ contract TestSponsorshipPaymasterWithPriceMarkup is TestBase {
uint32 priceMarkup = 1e6 + 1e5;

PackedUserOperation[] memory ops = new PackedUserOperation[](1);
(PackedUserOperation memory userOp, bytes32 userOpHash) = createUserOp(ALICE, bicoPaymaster, priceMarkup);
(PackedUserOperation memory userOp, bytes32 userOpHash) = createUserOp(ALICE, "", bicoPaymaster, priceMarkup);
ops[0] = userOp;

uint256 initialBundlerBalance = BUNDLER.addr.balance;
Expand Down
6 changes: 4 additions & 2 deletions test/unit/fuzz/TestFuzz_TestSponsorshipPaymaster.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ contract TestFuzz_SponsorshipPaymasterWithPriceMarkup is TestBase {
setupPaymasterTestEnvironment();
// Deploy Sponsorship Paymaster
bicoPaymaster = new BiconomySponsorshipPaymaster(
PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, PAYMASTER_FEE_COLLECTOR.addr, 7e3
PAYMASTER_OWNER.addr, ENTRYPOINT, PAYMASTER_SIGNER.addr, PAYMASTER_FEE_COLLECTOR.addr, 27000
);
}

Expand Down Expand Up @@ -97,7 +97,7 @@ contract TestFuzz_SponsorshipPaymasterWithPriceMarkup is TestBase {
bicoPaymaster.depositFor{ value: 10 ether }(DAPP_ACCOUNT.addr);

PackedUserOperation[] memory ops = new PackedUserOperation[](1);
(PackedUserOperation memory userOp, bytes32 userOpHash) = createUserOp(ALICE, bicoPaymaster, priceMarkup);
(PackedUserOperation memory userOp, bytes32 userOpHash) = createUserOp(ALICE, "", bicoPaymaster, priceMarkup);
ops[0] = userOp;

uint256 initialBundlerBalance = BUNDLER.addr.balance;
Expand All @@ -110,7 +110,9 @@ contract TestFuzz_SponsorshipPaymasterWithPriceMarkup is TestBase {
emit IBiconomySponsorshipPaymaster.PriceMarkupCollected(DAPP_ACCOUNT.addr, 0);
vm.expectEmit(true, false, true, true, address(bicoPaymaster));
emit IBiconomySponsorshipPaymaster.GasBalanceDeducted(DAPP_ACCOUNT.addr, 0, userOpHash);
vm.startPrank(BUNDLER.addr);
ENTRYPOINT.handleOps(ops, payable(BUNDLER.addr));
vm.stopPrank();

// Calculate and assert price markups and gas payments
calculateAndAssertAdjustments(
Expand Down

0 comments on commit 0be09ad

Please sign in to comment.