Skip to content

Commit

Permalink
more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipp Makarov authored and Filipp Makarov committed Oct 22, 2024
1 parent 07d908d commit 146929a
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 39 deletions.
5 changes: 5 additions & 0 deletions contracts/common/BiconomySponsorshipPaymasterErrors.sol
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ contract BiconomySponsorshipPaymasterErrors {
*/
error CanNotWithdrawZeroAmount();

/**
* @notice Throws when no request has been submitted
*/
error NoRequestSubmitted();

/**
* @notice Throws when trying unaccountedGas is too high
*/
Expand Down
1 change: 1 addition & 0 deletions contracts/sponsorship/BiconomySponsorshipPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ contract BiconomySponsorshipPaymaster is
*/
function executeWithdrawalRequest(address paymasterId) external nonReentrant {
WithdrawalRequest memory req = requests[paymasterId];
if(req.requestSubmittedTimestamp == 0) revert NoRequestSubmitted();
uint256 clearanceTimestamp = req.requestSubmittedTimestamp + getDelay(paymasterId);
if (block.timestamp < clearanceTimestamp)
revert RequestNotClearedYet(clearanceTimestamp);
Expand Down
7 changes: 0 additions & 7 deletions test/base/TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -356,20 +356,13 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
uint256 totalGasFeePaid = BUNDLER.addr.balance - initialBundlerBalance;
uint256 gasPaidByDapp = initialDappPaymasterBalance - bicoPaymaster.getBalance(DAPP_ACCOUNT.addr);

console2.log("1");
// Assert that what paymaster paid is the same as what the bundler received
assertEq(totalGasFeePaid, initialPaymasterEpBalance - bicoPaymaster.getDeposit());

console2.log("2");
// Assert that adjustment collected (if any) is correct
assertEq(expectedPriceMarkup, actualPriceMarkup);

console2.log("3");
// Gas paid by dapp is higher than paymaster
// Guarantees that EP always has sufficient deposit to pay back dapps
assertGt(gasPaidByDapp, BUNDLER.addr.balance - initialBundlerBalance);

console2.log("4");
// Ensure that max 2% difference between total gas paid + the adjustment premium and gas paid by dapp (from
// paymaster)
assertApproxEqRel(totalGasFeePaid + actualPriceMarkup + maxPenalty, gasPaidByDapp, 0.02e18);
Expand Down
62 changes: 49 additions & 13 deletions test/unit/concrete/TestSponsorshipPaymaster.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import { MockToken } from "@nexus/contracts/mocks/MockToken.sol";
contract TestSponsorshipPaymasterWithPriceMarkup is TestBase {
BiconomySponsorshipPaymaster public bicoPaymaster;

uint256 public constant WITHDRAWAL_DELAY = 3600;
uint256 public constant MIN_DEPOSIT = 1e15;

function setUp() public {
setupPaymasterTestEnvironment();
// Deploy Sponsorship Paymaster
Expand All @@ -19,8 +22,8 @@ contract TestSponsorshipPaymasterWithPriceMarkup is TestBase {
verifyingSignerArg: PAYMASTER_SIGNER.addr,
feeCollectorArg: PAYMASTER_FEE_COLLECTOR.addr,
unaccountedGasArg: 7e3,
_paymasterIdWithdrawalDelay: 3600,
_minDeposit: 1e15
_paymasterIdWithdrawalDelay: WITHDRAWAL_DELAY,
_minDeposit: MIN_DEPOSIT
}
);
}
Expand Down Expand Up @@ -191,16 +194,6 @@ contract TestSponsorshipPaymasterWithPriceMarkup is TestBase {

/*
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_WithdrawZeroAmount() external prankModifier(DAPP_ACCOUNT.addr) {
vm.expectRevert(abi.encodeWithSelector(CanNotWithdrawZeroAmount.selector));
bicoPaymaster.withdrawTo(payable(BOB_ADDRESS), 0 ether);
}
function test_WithdrawTo() external prankModifier(DAPP_ACCOUNT.addr) {
uint256 depositAmount = 10 ether;
bicoPaymaster.depositFor{ value: depositAmount }(DAPP_ACCOUNT.addr);
Expand All @@ -220,10 +213,53 @@ contract TestSponsorshipPaymasterWithPriceMarkup is TestBase {

// test canceling the request

function test_submitWithdrawalRequest_Fails_with_ZeroAmount() external prankModifier(DAPP_ACCOUNT.addr) {
vm.expectRevert(abi.encodeWithSelector(CanNotWithdrawZeroAmount.selector));
bicoPaymaster.submitWithdrawalRequest(BOB_ADDRESS, 0 ether);
}

function test_submitWithdrawalRequest_Fails_with_ZeroAddress() external prankModifier(DAPP_ACCOUNT.addr) {
vm.expectRevert(abi.encodeWithSelector(CanNotWithdrawToZeroAddress.selector));
bicoPaymaster.submitWithdrawalRequest(address(0), 1 ether);
}

function test_submitWithdrawalRequest_Fails_If_not_enough_balance() external prankModifier(DAPP_ACCOUNT.addr) {
uint256 depositAmount = 1 ether;
bicoPaymaster.depositFor{ value: depositAmount }(DAPP_ACCOUNT.addr);
vm.expectRevert(abi.encodeWithSelector(InsufficientFundsInGasTank.selector));
bicoPaymaster.submitWithdrawalRequest(BOB_ADDRESS, depositAmount + 1);
}

function test_executeWithdrawalRequest_Fails_with_NoRequestSubmitted() external prankModifier(DAPP_ACCOUNT.addr) {
vm.expectRevert(abi.encodeWithSelector(NoRequestSubmitted.selector));
bicoPaymaster.executeWithdrawalRequest(DAPP_ACCOUNT.addr);
}

function test_submitWithdrawalRequest_Happy_Scenario() external prankModifier(DAPP_ACCOUNT.addr) {
uint256 depositAmount = 1 ether;
bicoPaymaster.depositFor{ value: depositAmount }(DAPP_ACCOUNT.addr);
bicoPaymaster.submitWithdrawalRequest(BOB_ADDRESS, depositAmount);
vm.warp(block.timestamp + WITHDRAWAL_DELAY + 1);
uint256 dappPaymasterBalanceBefore = bicoPaymaster.getBalance(DAPP_ACCOUNT.addr);
uint256 bobBalanceBefore = BOB_ADDRESS.balance;
bicoPaymaster.executeWithdrawalRequest(DAPP_ACCOUNT.addr);
uint256 dappPaymasterBalanceAfter = bicoPaymaster.getBalance(DAPP_ACCOUNT.addr);
uint256 bobBalanceAfter = BOB_ADDRESS.balance;
assertEq(dappPaymasterBalanceAfter, dappPaymasterBalanceBefore - depositAmount);
assertEq(bobBalanceAfter, bobBalanceBefore + depositAmount);
// can not withdraw again
vm.expectRevert(abi.encodeWithSelector(NoRequestSubmitted.selector));
bicoPaymaster.executeWithdrawalRequest(DAPP_ACCOUNT.addr);
}

// try to use balance while request is cleared



// test minimal deposit
function test_depositFor_RevertsIf_DepositIsLessThanMinDeposit() external {
vm.expectRevert(abi.encodeWithSelector(LowDeposit.selector));
bicoPaymaster.depositFor{ value: 1e15 - 1 }(DAPP_ACCOUNT.addr);
bicoPaymaster.depositFor{ value: MIN_DEPOSIT - 1 }(DAPP_ACCOUNT.addr);
}

function test_ValidatePaymasterAndPostOpWithoutPriceMarkup() external {
Expand Down
36 changes: 17 additions & 19 deletions test/unit/fuzz/TestFuzz_TestSponsorshipPaymaster.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { MockToken } from "@nexus/contracts/mocks/MockToken.sol";

contract TestFuzz_SponsorshipPaymasterWithPriceMarkup is TestBase {
BiconomySponsorshipPaymaster public bicoPaymaster;

uint256 public constant WITHDRAWAL_DELAY = 3600;
uint256 public constant MIN_DEPOSIT = 1e15;
function setUp() public {
setupPaymasterTestEnvironment();
// Deploy Sponsorship Paymaster
Expand Down Expand Up @@ -41,25 +42,22 @@ contract TestFuzz_SponsorshipPaymasterWithPriceMarkup is TestBase {
}

// Rebuild submitting and exeuting withdraw request fuzz

/*
function testFuzz_WithdrawTo(uint256 withdrawAmount) external prankModifier(DAPP_ACCOUNT.addr) {
vm.assume(withdrawAmount <= 1000 ether && withdrawAmount > 0 ether);
vm.deal(DAPP_ACCOUNT.addr, withdrawAmount);
bicoPaymaster.depositFor{ value: withdrawAmount }(DAPP_ACCOUNT.addr);
uint256 danInitialBalance = BOB_ADDRESS.balance;
vm.expectEmit(true, true, true, true, address(bicoPaymaster));
emit IBiconomySponsorshipPaymaster.GasWithdrawn(DAPP_ACCOUNT.addr, BOB_ADDRESS, withdrawAmount);
bicoPaymaster.withdrawTo(payable(BOB_ADDRESS), withdrawAmount);
uint256 dappPaymasterBalance = bicoPaymaster.getBalance(DAPP_ACCOUNT.addr);
assertEq(dappPaymasterBalance, 0 ether);
uint256 expectedDanBalance = danInitialBalance + withdrawAmount;
assertEq(BOB_ADDRESS.balance, expectedDanBalance);
function test_submitWithdrawalRequest_Happy_Scenario(uint256 depositAmount) external prankModifier(DAPP_ACCOUNT.addr) {
vm.assume(depositAmount <= 1000 ether && depositAmount > MIN_DEPOSIT);
bicoPaymaster.depositFor{ value: depositAmount }(DAPP_ACCOUNT.addr);
bicoPaymaster.submitWithdrawalRequest(BOB_ADDRESS, depositAmount);
vm.warp(block.timestamp + WITHDRAWAL_DELAY + 1);
uint256 dappPaymasterBalanceBefore = bicoPaymaster.getBalance(DAPP_ACCOUNT.addr);
uint256 bobBalanceBefore = BOB_ADDRESS.balance;
bicoPaymaster.executeWithdrawalRequest(DAPP_ACCOUNT.addr);
uint256 dappPaymasterBalanceAfter = bicoPaymaster.getBalance(DAPP_ACCOUNT.addr);
uint256 bobBalanceAfter = BOB_ADDRESS.balance;
assertEq(dappPaymasterBalanceAfter, dappPaymasterBalanceBefore - depositAmount);
assertEq(bobBalanceAfter, bobBalanceBefore + depositAmount);
// can not withdraw again
vm.expectRevert(abi.encodeWithSelector(NoRequestSubmitted.selector));
bicoPaymaster.executeWithdrawalRequest(DAPP_ACCOUNT.addr);
}
*/

function testFuzz_Receive(uint256 ethAmount) external prankModifier(ALICE_ADDRESS) {
vm.assume(ethAmount <= 1000 ether && ethAmount > 0 ether);
Expand Down

0 comments on commit 146929a

Please sign in to comment.