Skip to content

Commit

Permalink
postop cost added back and more tests added
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivaanshK committed Jul 9, 2024
1 parent 40aed7e commit 81abba5
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 3 deletions.
5 changes: 5 additions & 0 deletions contracts/common/Errors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,9 @@ contract BiconomySponsorshipPaymasterErrors {
* @notice Throws when trying to withdraw to address(0)
*/
error CanNotWithdrawToZeroAddress();

/**
* @notice Throws when trying postOpCost is too high
*/
error PostOpCostTooHigh();
}
28 changes: 26 additions & 2 deletions contracts/sponsorship/SponsorshipPaymasterWithPremium.sol
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ contract BiconomySponsorshipPaymaster is

address public verifyingSigner;
address public feeCollector;
uint48 public postOpCost;
uint32 private constant PRICE_DENOMINATOR = 1e6;

// note: could rename to PAYMASTER_ID_OFFSET
Expand Down Expand Up @@ -119,6 +120,20 @@ contract BiconomySponsorshipPaymaster is
emit FeeCollectorChanged(oldFeeCollector, _newFeeCollector, msg.sender);
}

/**
* @dev Set a new unaccountedEPGasOverhead value.
* @param value The new value to be set as the unaccountedEPGasOverhead.
* @notice only to be called by the owner of the contract.
*/
function setPostopCost(uint48 value) external payable onlyOwner {
if (value > 200_000) {
revert PostOpCostTooHigh();
}
uint256 oldValue = postOpCost;
postOpCost = value;
emit PostopCostChanged(oldValue, value);
}

/**
* @dev Override the default implementation.
*/
Expand Down Expand Up @@ -232,12 +247,21 @@ contract BiconomySponsorshipPaymaster is
/// @dev This function is called after a user operation has been executed or reverted.
/// @param context The context containing the token amount and user sender address.
/// @param actualGasCost The actual gas cost of the transaction.
function _postOp(PostOpMode, bytes calldata context, uint256 actualGasCost, uint256) internal override {
function _postOp(
PostOpMode,
bytes calldata context,
uint256 actualGasCost,
uint256 actualUserOpFeePerGas
)
internal
override
{
unchecked {
(address paymasterId, uint32 dynamicAdjustment, bytes32 userOpHash) =
abi.decode(context, (address, uint32, bytes32));

uint256 adjustedGasCost = (actualGasCost * dynamicAdjustment) / PRICE_DENOMINATOR;
uint256 totalGasCost = actualGasCost + (postOpCost * actualUserOpFeePerGas);
uint256 adjustedGasCost = (totalGasCost * dynamicAdjustment) / PRICE_DENOMINATOR;

// Deduct the adjusted cost
paymasterIdBalances[paymasterId] -= adjustedGasCost;
Expand Down
4 changes: 3 additions & 1 deletion test/foundry/base/NexusTestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,8 @@ abstract contract NexusTestBase is CheatCodes, BaseEventsAndErrors {
//premium
expectedPremium = totalGasFeesCharged - ((totalGasFeesCharged * 1e6) / premium);
actualPremium = resultingFeeCollectorPaymasterBalance - initialFeeCollectorBalance;
} else revert("Premium must be more than 1e6");
} else {
revert("Premium must be more than 1e6");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,24 @@ contract TestSponsorshipPaymasterWithPremium is NexusTestBase {
bicoPaymaster.setFeeCollector(DAN_ADDRESS);
}

function test_SetPostopCost() external prankModifier(PAYMASTER_OWNER.addr) {
uint48 initialPostopCost = bicoPaymaster.postOpCost();
uint48 newPostopCost = 5_000;

vm.expectEmit(true, true, false, true, address(bicoPaymaster));
emit IBiconomySponsorshipPaymaster.PostopCostChanged(initialPostopCost, newPostopCost);
bicoPaymaster.setPostopCost(newPostopCost);

uint48 resultingPostopCost = bicoPaymaster.postOpCost();
assertEq(resultingPostopCost, newPostopCost);
}

function test_RevertIf_SetPostopCostToHigh() external prankModifier(PAYMASTER_OWNER.addr) {
uint48 newPostopCost = 200_001;
vm.expectRevert(abi.encodeWithSelector(PostOpCostTooHigh.selector));
bicoPaymaster.setPostopCost(newPostopCost);
}

function test_DepositFor() external {
uint256 dappPaymasterBalance = bicoPaymaster.getBalance(DAPP_ACCOUNT.addr);
uint256 depositAmount = 10 ether;
Expand Down Expand Up @@ -134,6 +152,31 @@ contract TestSponsorshipPaymasterWithPremium is NexusTestBase {
bicoPaymaster.deposit{ value: 1 ether }();
}

function test_WithdrawTo() external prankModifier(DAPP_ACCOUNT.addr) {
uint256 depositAmount = 10 ether;
bicoPaymaster.depositFor{ value: depositAmount }(DAPP_ACCOUNT.addr);
uint256 danInitialBalance = DAN_ADDRESS.balance;

vm.expectEmit(true, true, true, true, address(bicoPaymaster));
emit IBiconomySponsorshipPaymaster.GasWithdrawn(DAPP_ACCOUNT.addr, DAN_ADDRESS, depositAmount);
bicoPaymaster.withdrawTo(payable(DAN_ADDRESS), depositAmount);

uint256 dappPaymasterBalance = bicoPaymaster.getBalance(DAPP_ACCOUNT.addr);
assertEq(dappPaymasterBalance, 0 ether);
uint256 expectedDanBalance = danInitialBalance + depositAmount;
assertEq(DAN_ADDRESS.balance, expectedDanBalance);
}

function test_RevertIf_WithdrawToZeroAddress() external prankModifier(DAPP_ACCOUNT.addr) {
vm.expectRevert(abi.encodeWithSelector(CanNotWithdrawToZeroAddress.selector));
bicoPaymaster.withdrawTo(payable(address(0)), 0 ether);
}

function test_RevertIf_WithdrawToExceedsBalance() external prankModifier(DAPP_ACCOUNT.addr) {
vm.expectRevert(abi.encodeWithSelector(InsufficientFundsInGasTank.selector));
bicoPaymaster.withdrawTo(payable(DAN_ADDRESS), 1 ether);
}

function test_ValidatePaymasterAndPostOpWithoutPremium() external prankModifier(DAPP_ACCOUNT.addr) {
bicoPaymaster.depositFor{ value: 10 ether }(DAPP_ACCOUNT.addr);
// No premoium
Expand Down

0 comments on commit 81abba5

Please sign in to comment.