Skip to content

Commit

Permalink
feat:add test for swapping on base
Browse files Browse the repository at this point in the history
  • Loading branch information
livingrockrises committed Oct 28, 2024
1 parent 5b28b10 commit f38aa22
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
2 changes: 1 addition & 1 deletion contracts/token/swaps/Uniswapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ abstract contract Uniswapper {
tokenOut: wrappedNative,
fee: tokenToPools[tokenIn],
recipient: address(this),
deadline: block.timestamp,
deadline: block.timestamp + 3600,
amountIn: amountIn,
amountOutMinimum: minAmountOut,
sqrtPriceLimitX96: 0
Expand Down
14 changes: 9 additions & 5 deletions test/base/TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
using UserOperationLib for PackedUserOperation;

address constant ENTRYPOINT_ADDRESS = address(0x0000000071727De22E5E9d8BAf0edAc6f37da032);
address constant WRAPPED_NATIVE_ADDRESS = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);
address constant SWAP_ROUTER_ADDRESS = address(0xE592427A0AEce92De3Edee1F18E0157C05861564);
// NotE: updating below to WETH on Base.
address constant WRAPPED_NATIVE_ADDRESS = address(0x4200000000000000000000000000000000000006);
// Review address kept
address constant SWAP_ROUTER_ADDRESS = address(0x2626664c2603336E57B271c5C0b26F421741e481);

Vm.Wallet internal PAYMASTER_OWNER;
Vm.Wallet internal PAYMASTER_SIGNER;
Expand Down Expand Up @@ -400,7 +402,7 @@ abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
actualPriceMarkup = resultingFeeCollectorPaymasterBalance - initialFeeCollectorBalance;
}

function getMaxPenalty(PackedUserOperation calldata userOp) public view returns (uint256) {
function getMaxPenalty(PackedUserOperation calldata userOp) public pure returns (uint256) {
return (
uint128(uint256(userOp.accountGasLimits))
+ uint128(bytes16(userOp.paymasterAndData[_PAYMASTER_POSTOP_GAS_OFFSET:_PAYMASTER_DATA_OFFSET]))
Expand Down Expand Up @@ -465,8 +467,10 @@ 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("gasPaidBySAInERC20", gasPaidBySAInERC20);
console2.log("gasCollectedInERC20ByPaymaster", gasCollectedInERC20ByPaymaster);
console2.log("maxPenalty", maxPenalty);
console2.log("totalGasFeePaid", totalGasFeePaid);

// 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
Expand Down
27 changes: 25 additions & 2 deletions test/unit/concrete/TestTokenPaymaster.Base.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ contract TestTokenPaymasterBase is TestBase {
vm.selectFork(forkId);
setupPaymasterTestEnvironment();

console2.log("current block timestamp ", block.timestamp);

swapRouter = ISwapRouter(0x2626664c2603336E57B271c5C0b26F421741e481); // uniswap swap router v2 on base
// Deploy the token paymaster
tokenPaymaster = new BiconomyTokenPaymaster(
PAYMASTER_OWNER.addr,
PAYMASTER_SIGNER.addr,
ENTRYPOINT,
50000, // unaccounted gas
1e6, // price markup
1e6, // price markup (for independent mode)
1 days, // price expiry duration
nativeOracle,
swapRouter,
Expand Down Expand Up @@ -126,5 +128,26 @@ contract TestTokenPaymasterBase is TestBase {
this.getMaxPenalty(ops[0]));
}

// Todo: write a test to make a swap.
// test to make a swap.
function test_BaseFork_Success_TokenPaymaster_SwapToNativeAndDeposit() external {
deal(address(usdc), address(tokenPaymaster), 100e6);
uint256 initialTokenBalance = usdc.balanceOf(address(tokenPaymaster));
uint256 initialDepositOnEntryPoint = tokenPaymaster.getDeposit();

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

// Review reson for failure
// startPrank(PAYMASTER_OWNER.addr);
// tokenPaymaster.swapTokenAndDeposit(address(usdc), initialTokenBalance, 1);
// stopPrank();

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

// uint256 newDepositOnEntryPoint = tokenPaymaster.getDeposit();
// assertGt(newDepositOnEntryPoint, initialDepositOnEntryPoint);
}
}

0 comments on commit f38aa22

Please sign in to comment.