Skip to content

Commit

Permalink
renaming contract combinedActionsGuard
Browse files Browse the repository at this point in the history
  • Loading branch information
Robsonsjre committed Aug 3, 2021
1 parent ee35855 commit f1c8696
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 30 deletions.
16 changes: 8 additions & 8 deletions contracts/amm/OptionAMMPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/Address.sol";
import "./AMM.sol";
import "../lib/CappedPool.sol";
import "../lib/FlashloanProtection.sol";
import "../lib/CombinedActionsGuard.sol";
import "../interfaces/IPriceProvider.sol";
import "../interfaces/IIVProvider.sol";
import "../interfaces/IBlackScholes.sol";
Expand Down Expand Up @@ -34,7 +34,7 @@ import "../options/rewards/AaveIncentives.sol";
* - feePoolA and feePoolB: responsible for handling Liquidity providers fees.
*/

contract OptionAMMPool is AMM, IOptionAMMPool, CappedPool, FlashloanProtection, AaveIncentives {
contract OptionAMMPool is AMM, IOptionAMMPool, CappedPool, CombinedActionsGuard, AaveIncentives {
using SafeMath for uint256;
uint256 public constant PRICING_DECIMALS = 18;
uint256 private constant _SECONDS_IN_A_YEAR = 31536000;
Expand Down Expand Up @@ -124,7 +124,7 @@ contract OptionAMMPool is AMM, IOptionAMMPool, CappedPool, FlashloanProtection,
uint256 amountOfB,
address owner
) external override capped(tokenB(), amountOfB) {
_nonReentrant();
_nonCombinedActions();
_beforeStartOfExerciseWindow();
_emergencyStopCheck();
_addLiquidity(amountOfA, amountOfB, owner);
Expand All @@ -138,7 +138,7 @@ contract OptionAMMPool is AMM, IOptionAMMPool, CappedPool, FlashloanProtection,
* @param amountOfB amount of TokenB to add
*/
function removeLiquidity(uint256 amountOfA, uint256 amountOfB) external override {
_nonReentrant();
_nonCombinedActions();
_emergencyStopCheck();
_removeLiquidity(amountOfA, amountOfB);
_getTradeInfo();
Expand Down Expand Up @@ -182,7 +182,7 @@ contract OptionAMMPool is AMM, IOptionAMMPool, CappedPool, FlashloanProtection,
address owner,
uint256 initialIVGuess
) external override returns (uint256) {
_nonReentrant();
_nonCombinedActions();
_beforeStartOfExerciseWindow();
_emergencyStopCheck();
priceProperties.initialIVGuess = initialIVGuess;
Expand Down Expand Up @@ -213,7 +213,7 @@ contract OptionAMMPool is AMM, IOptionAMMPool, CappedPool, FlashloanProtection,
address owner,
uint256 initialIVGuess
) external override returns (uint256) {
_nonReentrant();
_nonCombinedActions();
_beforeStartOfExerciseWindow();
_emergencyStopCheck();
priceProperties.initialIVGuess = initialIVGuess;
Expand Down Expand Up @@ -243,7 +243,7 @@ contract OptionAMMPool is AMM, IOptionAMMPool, CappedPool, FlashloanProtection,
address owner,
uint256 initialIVGuess
) external override returns (uint256) {
_nonReentrant();
_nonCombinedActions();
_beforeStartOfExerciseWindow();
_emergencyStopCheck();
priceProperties.initialIVGuess = initialIVGuess;
Expand Down Expand Up @@ -274,7 +274,7 @@ contract OptionAMMPool is AMM, IOptionAMMPool, CappedPool, FlashloanProtection,
address owner,
uint256 initialIVGuess
) external override returns (uint256) {
_nonReentrant();
_nonCombinedActions();
_beforeStartOfExerciseWindow();
_emergencyStopCheck();
priceProperties.initialIVGuess = initialIVGuess;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

pragma solidity 0.6.12;

contract FlashloanProtection {
contract CombinedActionsGuard {
mapping(address => uint256) sessions;

/**
* @dev Prevents an address from calling more than one function that contains this
* function in the same block
*/
function _nonReentrant() internal {
require(sessions[tx.origin] != block.number, "FlashloanProtection: reentrant call");
function _nonCombinedActions() internal {
require(sessions[tx.origin] != block.number, "CombinedActionsGuard: reentrant call");
sessions[tx.origin] = block.number;
}
}
8 changes: 4 additions & 4 deletions contracts/mocks/FlashloanSample.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

pragma solidity 0.6.12;

import "../lib/FlashloanProtection.sol";
import "../lib/CombinedActionsGuard.sol";

contract FlashloanSample is FlashloanProtection {
contract FlashloanSample is CombinedActionsGuard {
uint256 public interactions = 0;

function one() public {
_nonReentrant();
_nonCombinedActions();
interactions += 1;
}

function two() public {
_nonReentrant();
_nonCombinedActions();
interactions += 1;
}
}
4 changes: 2 additions & 2 deletions test/amm/OptionAMMPool.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ scenarios.forEach(scenario => {
await mockStrikeAsset.connect(buyer).approve(attackerContract.address, ethers.constants.MaxUint256)
const tradeDetails = await optionAMMPool.getOptionTradeDetailsExactAOutput(optionLiquidityToBuy)

await expect(attackerContract.connect(buyer).addLiquidityAndBuy(optionAMMPool.address, optionLiquidityToAdd, stableLiquidityToAdd, optionLiquidityToBuy, tradeDetails.newIV, buyerAddress)).to.be.revertedWith('FlashloanProtection: reentrant call')
await expect(attackerContract.connect(buyer).addLiquidityAndBuy(optionAMMPool.address, optionLiquidityToAdd, stableLiquidityToAdd, optionLiquidityToBuy, tradeDetails.newIV, buyerAddress)).to.be.revertedWith('CombinedActionsGuard: reentrant call')
})
it('Should revert if an origin address tries to perform -add liquidity- and -remove liquidity- in the same block', async () => {
const AttackerContract = await ethers.getContractFactory('AttackerOptionPool')
Expand All @@ -1147,7 +1147,7 @@ scenarios.forEach(scenario => {
await mockStrikeAsset.connect(buyer).mint(stableLiquidityToAdd.mul(200))
await mockStrikeAsset.connect(buyer).approve(attackerContract.address, ethers.constants.MaxUint256)

await expect(attackerContract.connect(buyer).addLiquidityAndRemove(optionAMMPool.address, stableLiquidityToAdd, optionLiquidityToAdd, buyerAddress)).to.be.revertedWith('FlashloanProtection: reentrant call')
await expect(attackerContract.connect(buyer).addLiquidityAndRemove(optionAMMPool.address, stableLiquidityToAdd, optionLiquidityToAdd, buyerAddress)).to.be.revertedWith('CombinedActionsGuard: reentrant call')
})
})

Expand Down
26 changes: 13 additions & 13 deletions test/lib/FlashloanProtection.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,28 @@ const { ethers } = require('hardhat')
const { expect } = require('chai')
const { toBigNumber } = require('../../utils/utils')

describe('FlashloanProtection', () => {
let reentrancySample, reentrancyAttacker
describe('CombinedActions', () => {
let combinedSample, combinedAttacker

before(async () => {
const ReentrancySample = await ethers.getContractFactory('FlashloanSample')
const ReentrancyAttacker = await ethers.getContractFactory('CombinedAttacker')
const CombinedSample = await ethers.getContractFactory('FlashloanSample')
const CombinedAttacker = await ethers.getContractFactory('CombinedAttacker')

reentrancySample = await ReentrancySample.deploy()
reentrancyAttacker = await ReentrancyAttacker.deploy()
combinedSample = await CombinedSample.deploy()
combinedAttacker = await CombinedAttacker.deploy()

await reentrancySample.deployed()
await reentrancyAttacker.deployed()
await combinedSample.deployed()
await combinedAttacker.deployed()
})

it('Should revert if an origin try to call function one and two at same block.number from the same origin', async () => {
await expect(reentrancyAttacker.zapper(reentrancySample.address)).to.be.revertedWith('FlashloanProtection: reentrant call')
await expect(combinedAttacker.zapper(combinedSample.address)).to.be.revertedWith('CombinedActionsGuard: reentrant call')
})

it('Should allow if the user tries to execute the same transaction, but in a sequence of blocks', async () => {
await reentrancyAttacker.oneProxy(reentrancySample.address)
await reentrancyAttacker.oneProxy(reentrancySample.address)
await reentrancyAttacker.twoProxy(reentrancySample.address)
expect(await reentrancySample.interactions()).to.be.equal(3)
await combinedAttacker.oneProxy(combinedSample.address)
await combinedAttacker.oneProxy(combinedSample.address)
await combinedAttacker.twoProxy(combinedSample.address)
expect(await combinedSample.interactions()).to.be.equal(3)
})
})

0 comments on commit f1c8696

Please sign in to comment.