Skip to content

Commit

Permalink
Reduce size of contract by inlining the modifiers only used once
Browse files Browse the repository at this point in the history
  • Loading branch information
jummy123 committed Jan 5, 2022
1 parent 5ec05a7 commit 65d323f
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 60 deletions.
81 changes: 21 additions & 60 deletions contracts/LaunchEvent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,7 @@ contract LaunchEvent is Ownable {

uint256 public tokenReserve;

//
// Constructor
//

constructor() {
rocketJoeFactory = IRocketJoeFactory(msg.sender);
Expand Down Expand Up @@ -177,59 +175,33 @@ contract LaunchEvent is Ownable {
issuerTimelock = _issuerTimelock;
}

//
// Modifiers
//

modifier notPaused() {
require(isPaused != true, "LaunchEvent: Contract is paused");
_;
}

modifier phaseOneOnly() {
// Public functions.

/// @notice Deposits AVAX and burns rJoe.
function depositAVAX() external payable notPaused {
require(
block.timestamp >= phaseOneStartTime &&
block.timestamp <= (phaseOneStartTime + phaseOneLengthSeconds),
"LaunchEvent: Not in phase one"
);
_;
}

modifier phaseThreeOrLater() {
require(
block.timestamp >= phaseThreeStartTime,
"LaunchEvent: Not in phase three"
);
_;
}

modifier pairNotCreated() {
require(
address(pair) == address(0),
"LaunchEvent: Pair is not 0 address"
);
_;
}

modifier pairCreated() {
require(address(pair) != address(0), "LaunchEvent: Pair is 0 address");
_;
}

//
// Public functions.
//

/// @notice Deposits AVAX and burns rJoe.
function depositAVAX() external payable phaseOneOnly notPaused {
WAVAX.deposit{value: msg.value}();
_depositWAVAX(msg.sender, msg.value); // checks are done here.
}

/// @dev withdraw AVAX only during phase 1 and 2.
function withdrawWAVAX(uint256 amount) public notPaused {
require(
isPhaseOne() || isPhaseTwo(),
block.timestamp >= phaseOneStartTime &&
block.timestamp <= (phaseOneStartTime + phaseOneLengthSeconds) ||
block.timestamp >= phaseTwoStartTime &&
block.timestamp <= (phaseTwoStartTime + phaseTwoLengthSeconds),
"LaunchEvent: Can't withdraw after phase 2."
);

Expand Down Expand Up @@ -276,7 +248,15 @@ contract LaunchEvent is Ownable {

/// @dev Create the uniswap pair, can be called by anyone but only once
/// @dev but only once after phase 3 has started.
function createPair() public notPaused phaseThreeOrLater pairNotCreated {
function createPair() public notPaused {
require(
block.timestamp >= phaseThreeStartTime,
"LaunchEvent: Not in phase three"
);
require(
address(pair) == address(0),
"LaunchEvent: Pair is not 0 address"
);
(address wavaxAddress, address tokenAddress) = (
address(WAVAX),
address(token)
Expand Down Expand Up @@ -311,7 +291,8 @@ contract LaunchEvent is Ownable {
}

/// @dev withdraw the liquidity pool tokens.
function withdrawLiquidity() public notPaused pairCreated {
function withdrawLiquidity() public notPaused {
require(address(pair) != address(0), "LaunchEvent: Pair is 0 address");
require(
block.timestamp > phaseThreeStartTime + userTimelock,
"LaunchEvent: Can't withdraw before user's timelock"
Expand All @@ -329,7 +310,8 @@ contract LaunchEvent is Ownable {
}

/// @dev withdraw the liquidity pool tokens, only for issuer.
function withdrawIssuerLiquidity() public notPaused pairCreated {
function withdrawIssuerLiquidity() public notPaused {
require(address(pair) != address(0), "LaunchEvent: Pair is 0 address");
require(msg.sender == issuer, "LaunchEvent: Caller is not Issuer");
require(
block.timestamp > phaseThreeStartTime + issuerTimelock,
Expand Down Expand Up @@ -374,9 +356,7 @@ contract LaunchEvent is Ownable {
isPaused = false;
}

//
// Internal functions.
//

/// @dev Transfers `value` AVAX to address.
function safeTransferAVAX(address to, uint256 value) internal {
Expand All @@ -394,7 +374,6 @@ contract LaunchEvent is Ownable {
/// @notice Use your allocation credits by sending WAVAX.
function _depositWAVAX(address from, uint256 amount)
internal
phaseOneOnly
notPaused
{
require(
Expand All @@ -412,22 +391,4 @@ contract LaunchEvent is Ownable {

user.allocation = user.allocation + amount;
}

function isPhaseOne() internal view returns (bool) {
require(
block.timestamp >= phaseOneStartTime &&
block.timestamp <= (phaseOneStartTime + phaseOneLengthSeconds),
"LaunchEvent: Not in phase one"
);
return true;
}

function isPhaseTwo() internal view returns (bool) {
require(
block.timestamp >= phaseTwoStartTime &&
block.timestamp <= (phaseTwoStartTime + phaseTwoLengthSeconds),
"LaunchEvent: Not in phase two"
);
return true;
}
}
1 change: 1 addition & 0 deletions hardhat.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
require("@nomiclabs/hardhat-ethers");
require("@openzeppelin/hardhat-upgrades");
require("@nomiclabs/hardhat-waffle");
require('hardhat-contract-sizer');

// The next line is part of the sample project, you don't need it in your
// project. It imports a Hardhat task definition, that can be used for
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"ethereum-waffle": "^3.1.1",
"ethers": "^5.0.0",
"hardhat": "^2.4.1",
"hardhat-contract-sizer": "^2.3.0",
"husky": "^4.2.5",
"prettier": "^2.5.1",
"prettier-plugin-solidity": "^1.0.0-beta.19"
Expand Down

0 comments on commit 65d323f

Please sign in to comment.