Skip to content

Commit

Permalink
add a test for independent miode
Browse files Browse the repository at this point in the history
  • Loading branch information
livingrockrises committed Oct 27, 2024
1 parent ef204df commit 8279e9b
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 3 deletions.
10 changes: 9 additions & 1 deletion test/base/TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { Nexus } from "@nexus/contracts/Nexus.sol";
import { CheatCodes } from "@nexus/test/foundry/utils/CheatCodes.sol";
import { BaseEventsAndErrors } from "./BaseEventsAndErrors.sol";
import { MockToken } from "@nexus/contracts/mocks/MockToken.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol";

import { BiconomySponsorshipPaymaster } from "../../contracts/sponsorship/BiconomySponsorshipPaymaster.sol";

Expand Down Expand Up @@ -439,7 +440,7 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {

function calculateAndAssertAdjustmentsForTokenPaymaster(
BiconomyTokenPaymaster tokenPaymaster,
MockToken token,
IERC20 token,
uint256 initialBundlerBalance,
uint256 initialPaymasterEpBalance,
uint256 initialUserTokenBalance,
Expand Down Expand Up @@ -467,6 +468,7 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
// console2.log("gasPaidBySAInERC20", gasPaidBySAInERC20);
// console2.log("gasCollectedInERC20ByPaymaster", gasCollectedInERC20ByPaymaster);

// Note: yet to figure out why we're charging too low in tokens vs bundler is paying high gas fees!
// Review we will also need to update premium numbers in below if there is premium: multiply by 1e6 / premium
// assertGt(gasPaidBySAInERC20 * 1e18 / tokenPrice, BUNDLER.addr.balance - initialBundlerBalance);

Expand All @@ -481,6 +483,12 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
return array;
}

function _toSingletonArray(uint24 element) internal pure returns (uint24[] memory) {
uint24[] memory array = new uint24[](1);
array[0] = element;
return array;
}

function _toSingletonArray(IOracle oracle) internal pure returns (IOracle[] memory) {
IOracle[] memory array = new IOracle[](1);
array[0] = oracle;
Expand Down
59 changes: 57 additions & 2 deletions test/unit/concrete/TestTokenPaymaster.Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ contract TestTokenPaymasterBase is TestBase {
WRAPPED_NATIVE_ADDRESS,
_toSingletonArray(address(usdc)),
_toSingletonArray(IOracle(address(tokenOracle))),
new address[](0),
new uint24[](0)
_toSingletonArray(address(usdc)),
_toSingletonArray(uint24(500))
);
}

Expand Down Expand Up @@ -70,4 +70,59 @@ contract TestTokenPaymasterBase is TestBase {
assertEq(testArtifact.unaccountedGas(), 50000);
assertEq(testArtifact.independentPriceMarkup(), 1e6);
}

function test_BaseFork_Success_TokenPaymaster_IndependentMode_WithoutPremium() external {
tokenPaymaster.deposit{ value: 10 ether }();
deal(address(usdc), address(ALICE_ACCOUNT), 100e6);
vm.startPrank(address(ALICE_ACCOUNT));
usdc.approve(address(tokenPaymaster), usdc.balanceOf(address(ALICE_ACCOUNT)));
vm.stopPrank();

vm.startPrank(PAYMASTER_OWNER.addr);
tokenPaymaster.setUnaccountedGas(200_000);
vm.stopPrank();

uint256 initialBundlerBalance = BUNDLER.addr.balance;
uint256 initialPaymasterEpBalance = tokenPaymaster.getDeposit();
uint256 initialUserTokenBalance = usdc.balanceOf(address(ALICE_ACCOUNT));
uint256 initialPaymasterTokenBalance = usdc.balanceOf(address(tokenPaymaster));

PackedUserOperation memory userOp = buildUserOpWithCalldata(ALICE, "", address(VALIDATOR_MODULE));

// Encode paymasterAndData for independent mode
bytes memory paymasterAndData = abi.encodePacked(
address(tokenPaymaster),
uint128(3e6), // assumed gas limit for test
uint128(3e6), // assumed verification gas for test
uint8(IBiconomyTokenPaymaster.PaymasterMode.INDEPENDENT),
address(usdc)
);

userOp.paymasterAndData = paymasterAndData;
userOp.signature = signUserOp(ALICE, userOp);

PackedUserOperation[] memory ops = new PackedUserOperation[](1);
ops[0] = userOp;

vm.expectEmit(true, true, false, false, address(tokenPaymaster));
emit IBiconomyTokenPaymaster.TokensRefunded(address(ALICE_ACCOUNT), address(usdc), 0, bytes32(0));

vm.expectEmit(true, true, false, false, address(tokenPaymaster));
emit IBiconomyTokenPaymaster.PaidGasInTokens(address(ALICE_ACCOUNT), address(usdc), 0, 0, 1e6, bytes32(0));

startPrank(BUNDLER.addr);
ENTRYPOINT.handleOps(ops, payable(BUNDLER.addr));
stopPrank();

calculateAndAssertAdjustmentsForTokenPaymaster(
tokenPaymaster,
usdc,
initialBundlerBalance,
initialPaymasterEpBalance,
initialUserTokenBalance,
initialPaymasterTokenBalance,
401606430000000, // tokenPrice
100000,
this.getMaxPenalty(ops[0]));
}
}

0 comments on commit 8279e9b

Please sign in to comment.