Skip to content

Commit

Permalink
public => internal for lib methods
Browse files Browse the repository at this point in the history
  • Loading branch information
Filipp Makarov authored and Filipp Makarov committed Nov 22, 2024
1 parent 4341bed commit 950b583
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 36 deletions.
12 changes: 6 additions & 6 deletions contracts/libraries/TokenPaymasterParserLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ library TokenPaymasterParserLib {
function parsePaymasterAndData(
bytes calldata paymasterAndData
)
external
internal
pure
returns (IBiconomyTokenPaymaster.PaymasterMode mode, bytes memory modeSpecificData)
returns (IBiconomyTokenPaymaster.PaymasterMode mode, bytes calldata modeSpecificData)
{
unchecked {
mode = IBiconomyTokenPaymaster.PaymasterMode(uint8(bytes1(paymasterAndData[PAYMASTER_MODE_OFFSET])));
Expand All @@ -25,15 +25,15 @@ library TokenPaymasterParserLib {
function parseExternalModeSpecificData(
bytes calldata modeSpecificData
)
external
internal
pure
returns (
uint48 validUntil,
uint48 validAfter,
address tokenAddress,
uint256 tokenPrice, // Review: why uint128 and not uint256. in independent mode it is uint256
uint256 tokenPrice,
uint32 externalPriceMarkup,
bytes memory signature
bytes calldata signature
)
{
validUntil = uint48(bytes6(modeSpecificData[:6]));
Expand All @@ -47,7 +47,7 @@ library TokenPaymasterParserLib {
function parseIndependentModeSpecificData(
bytes calldata modeSpecificData
)
external
internal
pure
returns (address tokenAddress)
{
Expand Down
2 changes: 1 addition & 1 deletion contracts/token/BiconomyTokenPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,7 @@ contract BiconomyTokenPaymaster is
override
returns (bytes memory context, uint256 validationData)
{
(PaymasterMode mode, bytes memory modeSpecificData) = userOp.paymasterAndData.parsePaymasterAndData();
(PaymasterMode mode, bytes calldata modeSpecificData) = userOp.paymasterAndData.parsePaymasterAndData();

if (uint8(mode) > 1) {
revert InvalidPaymasterMode();
Expand Down
7 changes: 4 additions & 3 deletions contracts/token/swaps/Uniswapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import "@uniswap/v3-periphery/contracts/interfaces/IPeripheryPayments.sol";
* @notice Based on Infinitism's Uniswap Helper contract
*/
abstract contract Uniswapper {

event SwappingReverted(address tokenIn, uint256 amountIn, bytes reason);
/// @notice The Uniswap V3 SwapRouter contract
ISwapRouter public immutable uniswapRouter;

Check failure on line 19 in contracts/token/swaps/Uniswapper.sol

View workflow job for this annotation

GitHub Actions / Lint sources

Function order is incorrect, state variable declaration can not go after event definition (line 17)

Expand Down Expand Up @@ -65,9 +67,8 @@ abstract contract Uniswapper {

try uniswapRouter.exactInputSingle(params) returns (uint256 _amountOut) {
amountOut = _amountOut;
} catch {
// Review could emit an event here
// Uniswap Reverted
} catch (bytes memory reason) {
emit SwappingReverted(tokenIn, amountIn, reason);
amountOut = 0;
}
}
Expand Down
14 changes: 2 additions & 12 deletions test/base/TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -485,7 +485,7 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
uint256 gasPaidBySAInNativeTokens = gasPaidBySAInERC20 * 1e18 / tokenPrice;

// Assert we never undercharge
assertGte(gasPaidBySAInNativeTokens, BUNDLER.addr.balance - initialBundlerBalance);
assertGe(gasPaidBySAInNativeTokens, BUNDLER.addr.balance - initialBundlerBalance);

// Ensure that max 2% difference between what is should have been charged and what was charged
// this difference comes from difference of postop gas and estimated postop gas (paymaster.unaccountedGas)
Expand All @@ -510,14 +510,4 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
array[0] = oracle;
return array;
}
}
/*
1226028000000
1794876000000
2921664000000
3020904000000
1800000000000 max penalty
*/
}
28 changes: 28 additions & 0 deletions test/mocks/PaymasterParserLibExposed.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.27;

import { TokenPaymasterParserLib } from "../../contracts/libraries/TokenPaymasterParserLib.sol";
import { IBiconomyTokenPaymaster } from "../../contracts/interfaces/IBiconomyTokenPaymaster.sol";

library PaymasterParserLibExposed {
using TokenPaymasterParserLib for bytes;

function parsePaymasterAndData(bytes calldata paymasterAndData) public pure returns (IBiconomyTokenPaymaster.PaymasterMode mode, bytes calldata modeSpecificData) {
return paymasterAndData.parsePaymasterAndData();
}

function parseExternalModeSpecificData(bytes calldata modeSpecificData) public pure returns (
uint48 validUntil,
uint48 validAfter,
address tokenAddress,
uint256 tokenPrice,
uint32 externalPriceMarkup,
bytes calldata signature
) {
return modeSpecificData.parseExternalModeSpecificData();
}

function parseIndependentModeSpecificData(bytes calldata modeSpecificData) public pure returns (address tokenAddress) {
return modeSpecificData.parseIndependentModeSpecificData();
}
}
28 changes: 28 additions & 0 deletions test/mocks/PaymasterParserLibWrapper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: Unlicensed
pragma solidity ^0.8.27;

import { PaymasterParserLibExposed } from "./PaymasterParserLibExposed.sol";
import { IBiconomyTokenPaymaster } from "../../contracts/interfaces/IBiconomyTokenPaymaster.sol";

contract PaymasterParserLibWrapper {
using PaymasterParserLibExposed for bytes;

function parsePaymasterAndData(bytes calldata paymasterAndData) external pure returns (IBiconomyTokenPaymaster.PaymasterMode mode, bytes memory modeSpecificData) {
(mode, modeSpecificData) = paymasterAndData.parsePaymasterAndData();
}

function parseExternalModeSpecificData(bytes calldata modeSpecificData) external pure returns (
uint48 validUntil,
uint48 validAfter,
address tokenAddress,
uint256 tokenPrice,
uint32 externalPriceMarkup,
bytes memory signature
) {
(validUntil, validAfter, tokenAddress, tokenPrice, externalPriceMarkup, signature) = modeSpecificData.parseExternalModeSpecificData();
}

function parseIndependentModeSpecificData(bytes calldata modeSpecificData) external pure returns (address tokenAddress) {
tokenAddress = modeSpecificData.parseIndependentModeSpecificData();
}
}
12 changes: 6 additions & 6 deletions test/unit/concrete/TestTokenPaymaster.Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,17 +148,17 @@ contract TestTokenPaymasterBase is TestBase {
console2.log("initialTokenBalance", initialTokenBalance);
uint256 initialDepositOnEntryPoint = tokenPaymaster.getDeposit();

// vm.startPrank(address(tokenPaymaster));
// usdc.approve(address(SWAP_ROUTER_ADDRESS), usdc.balanceOf(address(tokenPaymaster)));
// vm.stopPrank();
vm.startPrank(address(tokenPaymaster));
//usdc.approve(address(SWAP_ROUTER_ADDRESS), usdc.balanceOf(address(tokenPaymaster)));
vm.stopPrank();

// Review reason for failure
startPrank(PAYMASTER_OWNER.addr);
tokenPaymaster.swapTokenAndDeposit(address(usdc), 1e6, 0);
tokenPaymaster.swapTokenAndDeposit(address(usdc), 99e6, 0);
stopPrank();

// uint256 newTokenBalance = usdc.balanceOf(address(tokenPaymaster));
// assertEq(newTokenBalance, 0);
uint256 newTokenBalance = usdc.balanceOf(address(tokenPaymaster));
assertEq(newTokenBalance, 0);

// uint256 newDepositOnEntryPoint = tokenPaymaster.getDeposit();
// assertGt(newDepositOnEntryPoint, initialDepositOnEntryPoint);
Expand Down
22 changes: 14 additions & 8 deletions test/unit/concrete/TestTokenPaymasterParserLib.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@ pragma solidity ^0.8.27;
import "forge-std/Test.sol";
import "../../../contracts/libraries/TokenPaymasterParserLib.sol";
import { IBiconomyTokenPaymaster } from "../../../contracts/interfaces/IBiconomyTokenPaymaster.sol";
import { PaymasterParserLibWrapper } from "../../mocks/PaymasterParserLibWrapper.sol";

// Mock contract to test the TokenPaymasterParserLib
contract TestTokenPaymasterParserLib is Test {
using TokenPaymasterParserLib for bytes;

PaymasterParserLibWrapper public parser;

function setUp() public {
parser = new PaymasterParserLibWrapper();
}

function test_ParsePaymasterAndData_ExternalMode() public {
// Simulate an example paymasterAndData for External Mode
Expand All @@ -27,7 +33,7 @@ contract TestTokenPaymasterParserLib is Test {

// Parse the paymasterAndData
(IBiconomyTokenPaymaster.PaymasterMode parsedMode, bytes memory parsedModeSpecificData) =
paymasterAndData.parsePaymasterAndData();
parser.parsePaymasterAndData(paymasterAndData);

// Validate the mode and modeSpecificData
assertEq(uint8(parsedMode), uint8(expectedMode), "Mode should match External");
Expand All @@ -52,7 +58,7 @@ contract TestTokenPaymasterParserLib is Test {

// Parse the paymasterAndData
(IBiconomyTokenPaymaster.PaymasterMode parsedMode, bytes memory parsedModeSpecificData) =
paymasterAndData.parsePaymasterAndData();
parser.parsePaymasterAndData(paymasterAndData);

// Validate the mode and modeSpecificData
assertEq(uint8(parsedMode), uint8(expectedMode), "Mode should match Independent");
Expand Down Expand Up @@ -87,7 +93,7 @@ contract TestTokenPaymasterParserLib is Test {
uint256 parsedTokenPrice,
uint32 parsedExternalPriceMarkup,
bytes memory parsedSignature
) = externalModeSpecificData.parseExternalModeSpecificData();
) = parser.parseExternalModeSpecificData(externalModeSpecificData);

// Validate the parsed values
assertEq(parsedValidUntil, expectedValidUntil, "ValidUntil should match");
Expand All @@ -98,13 +104,13 @@ contract TestTokenPaymasterParserLib is Test {
assertEq(parsedSignature, expectedSignature, "Signature should match");
}

function test_ParseIndependentModeSpecificData() public pure {
function test_ParseIndependentModeSpecificData() public view {
// Simulate valid independent mode specific data
address expectedTokenAddress = address(0x9876543210AbCDef9876543210ABCdEf98765432);
bytes memory independentModeSpecificData = abi.encodePacked(bytes20(expectedTokenAddress));

// Parse the mode specific data
address parsedTokenAddress = independentModeSpecificData.parseIndependentModeSpecificData();
address parsedTokenAddress = parser.parseIndependentModeSpecificData(independentModeSpecificData);

// Validate the parsed token address
assertEq(parsedTokenAddress, expectedTokenAddress, "Token address should match");
Expand All @@ -116,7 +122,7 @@ contract TestTokenPaymasterParserLib is Test {

// Expect the test to revert due to invalid data length
vm.expectRevert();
invalidExternalModeSpecificData.parseExternalModeSpecificData();
parser.parseExternalModeSpecificData(invalidExternalModeSpecificData);
}

function test_RevertIf_InvalidIndependentModeSpecificDataLength() public {
Expand All @@ -125,6 +131,6 @@ contract TestTokenPaymasterParserLib is Test {

// Expect the test to revert due to invalid data length
vm.expectRevert();
invalidIndependentModeSpecificData.parseIndependentModeSpecificData();
parser.parseIndependentModeSpecificData(invalidIndependentModeSpecificData);
}
}

0 comments on commit 950b583

Please sign in to comment.