Skip to content

Commit

Permalink
fix encoding context
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipp Makarov authored and Filipp Makarov committed Nov 21, 2024
1 parent 852d219 commit 0597761
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 17 deletions.
20 changes: 17 additions & 3 deletions contracts/token/BiconomyTokenPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,14 @@ contract BiconomyTokenPaymaster is

// deduct max penalty from the token amount we pass to the postOp
// so we don't refund it at postOp
context = abi.encode(userOp.sender, tokenAddress, tokenAmount-((maxPenalty*tokenPrice*externalPriceMarkup)/(_NATIVE_TOKEN_DECIMALS*_MARKUP_DENOMINATOR)), tokenPrice, externalPriceMarkup, userOpHash);
context = abi.encode(
userOp.sender,
tokenAddress,
tokenAmount-((maxPenalty*tokenPrice*externalPriceMarkup)/(_NATIVE_TOKEN_DECIMALS*_MARKUP_DENOMINATOR)),
tokenPrice,
externalPriceMarkup,
userOpHash
);
validationData = _packValidationData(false, validUntil, validAfter);
} else if (mode == PaymasterMode.INDEPENDENT) {
// Use only oracles for the token specified in modeSpecificData
Expand All @@ -577,7 +584,14 @@ contract BiconomyTokenPaymaster is
SafeTransferLib.safeTransferFrom(tokenAddress, userOp.sender, address(this), tokenAmount);

context =
abi.encode(userOp.sender, tokenAddress, tokenAmount-((maxPenalty*tokenPrice*independentPriceMarkup)/(_NATIVE_TOKEN_DECIMALS*_MARKUP_DENOMINATOR)), independentPriceMarkup, userOpHash);
abi.encode(
userOp.sender,
tokenAddress,
tokenAmount-((maxPenalty*tokenPrice*independentPriceMarkup)/(_NATIVE_TOKEN_DECIMALS*_MARKUP_DENOMINATOR)),
tokenPrice,
independentPriceMarkup,
userOpHash
);
validationData = 0; // Validation success and price is valid indefinetly
}
}
Expand All @@ -597,7 +611,7 @@ contract BiconomyTokenPaymaster is
)
internal
override
{
{
// Decode context data
(
address userOpSender,
Expand Down
11 changes: 1 addition & 10 deletions test/base/TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,6 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
}

// Note: Token paymaster could also get into stack deep issues.
// TODO: Refactor to reduce stack depth
/// @notice Generates and signs the paymaster data for a user operation.
/// @dev This function prepares the `paymasterAndData` field for a `PackedUserOperation` with the correct signature.
/// @param userOp The user operation to be signed.
Expand Down Expand Up @@ -440,7 +439,7 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
assertApproxEqRel(totalGasFeePaid + actualPriceMarkup + maxPenalty, gasPaidByDapp, 0.02e18);
}

function calculateAndAssertAdjustmentsForTokenPaymaster(
function calculateAndAssertAdjustmentsForTokenPaymaster(
BiconomyTokenPaymaster tokenPaymaster,
IERC20 token,
uint256 initialBundlerBalance,
Expand All @@ -455,10 +454,8 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
view
{
uint256 totalGasFeePaid = BUNDLER.addr.balance - initialBundlerBalance;

// Assert that what paymaster paid is the same as what the bundler received
assertEq(totalGasFeePaid, initialPaymasterEpBalance - tokenPaymaster.getDeposit());

uint256 gasPaidBySAInERC20 = initialUserTokenBalance - token.balanceOf(address(ALICE_ACCOUNT));

uint256 gasCollectedInERC20ByPaymaster = token.balanceOf(address(tokenPaymaster)) - initialPaymasterTokenBalance;
Expand All @@ -467,12 +464,6 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
// unless ofcourse there is same token transfer in calldata
assertEq(gasPaidBySAInERC20, gasCollectedInERC20ByPaymaster);

console2.log("gasPaidBySAInERC20", gasPaidBySAInERC20);
console2.log("gasCollectedInERC20ByPaymaster", gasCollectedInERC20ByPaymaster);
console2.log("maxPenalty", maxPenalty);
console2.log("totalGasFeePaid", totalGasFeePaid);
console2.log(uint256(1226028000000) + uint256(1794876000000));

// 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 Down
6 changes: 3 additions & 3 deletions test/unit/concrete/TestTokenPaymaster.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -520,12 +520,12 @@ contract TestTokenPaymaster is TestBase {

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

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

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

startPrank(BUNDLER.addr);
ENTRYPOINT.handleOps(ops, payable(BUNDLER.addr));
Expand Down
1 change: 0 additions & 1 deletion test/unit/concrete/TestTokenPaymasterParserLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ contract TestTokenPaymasterParserLib is Test {
assertEq(parsedModeSpecificData, modeSpecificData, "Mode specific data should match");
}

// TODO: review prices added inline with MockOracle
function test_ParseExternalModeSpecificData() public view {
// Simulate valid external mode specific data
uint48 expectedValidUntil = uint48(block.timestamp + 1 days);
Expand Down

0 comments on commit 0597761

Please sign in to comment.