Skip to content

Commit

Permalink
tests compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
ShivaanshK committed Sep 15, 2024
1 parent 02537c2 commit f20dba4
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 35 deletions.
7 changes: 5 additions & 2 deletions contracts/token/BiconomyTokenPaymaster.sol
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ contract BiconomyTokenPaymaster is
revert UnaccountedGasTooHigh();
}
if (_independentPriceMarkup > MAX_PRICE_MARKUP || _independentPriceMarkup < PRICE_DENOMINATOR) {
// Not between 0% and 100% markup
revert InvalidPriceMarkup();
}
if (_independentTokens.length != _oracles.length) {
Expand Down Expand Up @@ -248,7 +249,8 @@ contract BiconomyTokenPaymaster is
* @notice only to be called by the owner of the contract.
*/
function setPriceMarkup(uint256 _newIndependentPriceMarkup) external payable override onlyOwner {
if (_newPriceMarkup > MAX_PRICE_MARKUP || _newIndependentPriceMarkup < PRICE_DENOMINATOR) {
if (_newIndependentPriceMarkup > MAX_PRICE_MARKUP || _newIndependentPriceMarkup < PRICE_DENOMINATOR) {
// Not between 0% and 100% markup
revert InvalidPriceMarkup();
}
uint256 oldIndependentPriceMarkup = independentPriceMarkup;
Expand Down Expand Up @@ -485,7 +487,8 @@ contract BiconomyTokenPaymaster is
// Transfer full amount to this address. Unused amount will be refunded in postOP
SafeTransferLib.safeTransferFrom(tokenAddress, userOp.sender, address(this), tokenAmount);

context = abi.encode(userOp.sender, tokenAddress, tokenAmount, tokenPrice, independentPriceMarkup, userOpHash);
context =
abi.encode(userOp.sender, tokenAddress, tokenAmount, tokenPrice, independentPriceMarkup, userOpHash);
validationData = 0; // Validation success and price is valid indefinetly
}
}
Expand Down
4 changes: 4 additions & 0 deletions test/base/TestBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ import {
abstract contract TestBase is CheatCodes, TestHelper, BaseEventsAndErrors {
address constant ENTRYPOINT_ADDRESS = address(0x0000000071727De22E5E9d8BAf0edAc6f37da032);

address constant WRAPPED_NATIVE_ADDRESS = address(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2);

address constant SWAP_ROUTER_ADDRESS = address(0xE592427A0AEce92De3Edee1F18E0157C05861564);

Vm.Wallet internal PAYMASTER_OWNER;
Vm.Wallet internal PAYMASTER_SIGNER;
Vm.Wallet internal PAYMASTER_FEE_COLLECTOR;
Expand Down
117 changes: 84 additions & 33 deletions test/unit/concrete/TestTokenPaymaster.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ import {
import { MockOracle } from "../../mocks/MockOracle.sol";
import { MockToken } from "@nexus/contracts/mocks/MockToken.sol";
import { IERC20 } from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

import "../../../contracts/token/swaps/Uniswapper.sol";

contract TestTokenPaymaster is TestBase {
BiconomyTokenPaymaster public tokenPaymaster;
ISwapRouter swapRouter;
MockOracle public nativeAssetToUsdOracle;
MockToken public testToken;
MockToken public testToken2;
Expand All @@ -24,74 +25,93 @@ contract TestTokenPaymaster is TestBase {
setupPaymasterTestEnvironment();

// Deploy mock oracles and tokens
swapRouter = ISwapRouter(address(SWAP_ROUTER_ADDRESS));
nativeAssetToUsdOracle = new MockOracle(100_000_000, 8); // Oracle with 8 decimals for ETH
tokenOracle = new MockOracle(100_000_000, 8); // Oracle with 8 decimals for ERC20 token
testToken = new MockToken("Test Token", "TKN");
testToken2 = new MockToken("Test Token 2", "TKN2");


// Deploy the token paymaster
tokenPaymaster = new BiconomyTokenPaymaster(
PAYMASTER_OWNER.addr,
PAYMASTER_SIGNER.addr,
ENTRYPOINT,
5000, // unaccounted gas
1e6, // price markup
nativeAssetToUsdOracle,
1 days, // price expiry duration
nativeAssetToUsdOracle,
swapRouter,
WRAPPED_NATIVE_ADDRESS,
_toSingletonArray(address(testToken)),
_toSingletonArray(IOracle(address(tokenOracle)))
_toSingletonArray(IOracle(address(tokenOracle))),
new address[](0),
new uint24[](0)
);
}

function test_Deploy() external {
// Deploy the token paymaster
BiconomyTokenPaymaster testArtifact = new BiconomyTokenPaymaster(
PAYMASTER_OWNER.addr,
PAYMASTER_SIGNER.addr,
ENTRYPOINT,
5000,
1e6,
5000, // unaccounted gas
1e6, // price markup
1 days, // price expiry duration
nativeAssetToUsdOracle,
1 days,
swapRouter,
WRAPPED_NATIVE_ADDRESS,
_toSingletonArray(address(testToken)),
_toSingletonArray(IOracle(address(tokenOracle)))
_toSingletonArray(IOracle(address(tokenOracle))),
new address[](0),
new uint24[](0)
);

assertEq(testArtifact.owner(), PAYMASTER_OWNER.addr);
assertEq(address(testArtifact.entryPoint()), ENTRYPOINT_ADDRESS);
assertEq(testArtifact.verifyingSigner(), PAYMASTER_SIGNER.addr);
assertEq(address(testArtifact.nativeAssetToUsdOracle()), address(nativeAssetToUsdOracle));
assertEq(testArtifact.unaccountedGas(), 5000);
assertEq(testArtifact.priceMarkup(), 1e6);
assertEq(testArtifact.independentPriceMarkup(), 1e6);
}

function test_RevertIf_DeployWithSignerSetToZero() external {
vm.expectRevert(BiconomyTokenPaymasterErrors.VerifyingSignerCanNotBeZero.selector);
// Deploy the token paymaster
new BiconomyTokenPaymaster(
PAYMASTER_OWNER.addr,
address(0),
ENTRYPOINT,
5000,
1e6,
5000, // unaccounted gas
1e6, // price markup
1 days, // price expiry duration
nativeAssetToUsdOracle,
1 days,
swapRouter,
WRAPPED_NATIVE_ADDRESS,
_toSingletonArray(address(testToken)),
_toSingletonArray(IOracle(address(tokenOracle)))
_toSingletonArray(IOracle(address(tokenOracle))),
new address[](0),
new uint24[](0)
);
}

function test_RevertIf_DeployWithSignerAsContract() external {
vm.expectRevert(BiconomyTokenPaymasterErrors.VerifyingSignerCanNotBeContract.selector);
// Deploy the token paymaster
new BiconomyTokenPaymaster(
PAYMASTER_OWNER.addr,
address(ENTRYPOINT),
ENTRYPOINT_ADDRESS,
ENTRYPOINT,
5000,
1e6,
5000, // unaccounted gas
1e6, // price markup
1 days, // price expiry duration
nativeAssetToUsdOracle,
1 days,
swapRouter,
WRAPPED_NATIVE_ADDRESS,
_toSingletonArray(address(testToken)),
_toSingletonArray(IOracle(address(tokenOracle)))
_toSingletonArray(IOracle(address(tokenOracle))),
new address[](0),
new uint24[](0)
);
}

Expand All @@ -101,12 +121,16 @@ contract TestTokenPaymaster is TestBase {
PAYMASTER_OWNER.addr,
PAYMASTER_SIGNER.addr,
ENTRYPOINT,
50_001, // too high unaccounted gas
1e6,
500_001, // unaccounted gas
1e6, // price markup
1 days, // price expiry duration
nativeAssetToUsdOracle,
1 days,
swapRouter,
WRAPPED_NATIVE_ADDRESS,
_toSingletonArray(address(testToken)),
_toSingletonArray(IOracle(address(tokenOracle)))
_toSingletonArray(IOracle(address(tokenOracle))),
new address[](0),
new uint24[](0)
);
}

Expand All @@ -116,12 +140,16 @@ contract TestTokenPaymaster is TestBase {
PAYMASTER_OWNER.addr,
PAYMASTER_SIGNER.addr,
ENTRYPOINT,
5000,
2e6 + 1, // too high price markup
5000, // unaccounted gas
2e6 + 1, // price markup
1 days, // price expiry duration
nativeAssetToUsdOracle,
1 days,
swapRouter,
WRAPPED_NATIVE_ADDRESS,
_toSingletonArray(address(testToken)),
_toSingletonArray(IOracle(address(tokenOracle)))
_toSingletonArray(IOracle(address(tokenOracle))),
new address[](0),
new uint24[](0)
);
}

Expand Down Expand Up @@ -178,19 +206,43 @@ contract TestTokenPaymaster is TestBase {
assertEq(testToken.balanceOf(ALICE_ADDRESS), mintAmount);
}

function test_RevertIf_InvalidOracleDecimals() external {
function test_RevertIf_InvalidNativeOracleDecimals() external {
MockOracle invalidOracle = new MockOracle(100_000_000, 18); // invalid oracle with 18 decimals
vm.expectRevert(BiconomyTokenPaymasterErrors.InvalidOracleDecimals.selector);
new BiconomyTokenPaymaster(
PAYMASTER_OWNER.addr,
PAYMASTER_SIGNER.addr,
ENTRYPOINT,
5000, // unaccounted gas
1e6, // price markup
1 days, // price expiry duration
invalidOracle,
swapRouter,
WRAPPED_NATIVE_ADDRESS,
_toSingletonArray(address(testToken)),
_toSingletonArray(IOracle(address(tokenOracle))),
new address[](0),
new uint24[](0)
);
}

function test_RevertIf_InvalidTokenOracleDecimals() external {
MockOracle invalidOracle = new MockOracle(100_000_000, 18); // invalid oracle with 18 decimals
vm.expectRevert(BiconomyTokenPaymasterErrors.InvalidOracleDecimals.selector);
new BiconomyTokenPaymaster(
PAYMASTER_OWNER.addr,
PAYMASTER_SIGNER.addr,
ENTRYPOINT,
5000,
1e6,
invalidOracle, // incorrect oracle decimals
1 days,
50000, // unaccounted gas
1e6, // price markup
1 days, // price expiry duration
nativeAssetToUsdOracle,
swapRouter,
WRAPPED_NATIVE_ADDRESS,
_toSingletonArray(address(testToken)),
_toSingletonArray(IOracle(address(tokenOracle)))
_toSingletonArray(IOracle(address(invalidOracle))),
new address[](0),
new uint24[](0)
);
}

Expand Down Expand Up @@ -395,5 +447,4 @@ contract TestTokenPaymaster is TestBase {
vm.expectRevert();
ENTRYPOINT.handleOps(ops, payable(BUNDLER.addr));
}

}

0 comments on commit f20dba4

Please sign in to comment.