Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only wrap to WAVAX before creating liquidity pair #81

Merged
merged 3 commits into from
Jan 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 25 additions & 40 deletions contracts/LaunchEvent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ contract LaunchEvent is Ownable {
/// @dev The address of the JoePair, set after createLiquidityPool is called
IJoePair public pair;

/// @dev The total amount of wavax that was sent to the router to create the initial liquidity pair.
/// @dev The total amount of avax that was sent to the router to create the initial liquidity pair.
/// Used to calculate the amount of LP to send based on the user's participation in the launch event
uint256 private wavaxAllocated;
uint256 private avaxAllocated;

/// @dev The exact supply of LP minted when creating the initial liquidity pair.
uint256 private lpSupply;
Expand All @@ -115,10 +115,10 @@ contract LaunchEvent is Ownable {
/// will be an excess of tokens returned to the issuer if he calls `withdrawIncentives()`
uint256 private tokenIncentiveIssuerRefund;

/// @dev wavaxReserve is the exact amount of WAVAX that needs to be kept inside the contract in order to send everyone's
/// WAVAX. If there is some excess (because someone sent token directly to the contract), the
/// @dev avaxReserve is the exact amount of AVAX that needs to be kept inside the contract in order to send everyone's
/// AVAX. If there is some excess (because someone sent token directly to the contract), the
/// penaltyCollector can collect the excess using `skim()`
uint256 private wavaxReserve;
uint256 private avaxReserve;

event IssuingTokenDeposited(address indexed token, uint256 amount);

Expand Down Expand Up @@ -156,15 +156,6 @@ contract LaunchEvent is Ownable {

event TokenEmergencyWithdraw(address indexed user, uint256 amount);

/// @notice Receive AVAX from the WAVAX contract
/// @dev Needed for withdrawing from WAVAX contract
receive() external payable {
require(
msg.sender == address(WAVAX),
"LaunchEvent: you can't send AVAX directly to this contract"
);
}

/// @notice Modifier which ensures contract is in a defined phase
modifier atPhase(Phase _phase) {
_atPhase(_phase);
Expand Down Expand Up @@ -301,7 +292,6 @@ contract LaunchEvent is Ownable {
}

/// @notice Deposits AVAX and burns rJoe
/// @dev Checks are done in the `_depositWAVAX` function
function depositAVAX()
external
payable
Expand Down Expand Up @@ -332,14 +322,12 @@ contract LaunchEvent is Ownable {
}

user.balance = newAllocation;
wavaxReserve += msg.value;
avaxReserve += msg.value;

if (rJoeNeeded > 0) {
rJoe.burnFrom(msg.sender, rJoeNeeded);
}

WAVAX.deposit{value: msg.value}();

emit UserParticipated(msg.sender, msg.value, rJoeNeeded);
}

Expand All @@ -362,9 +350,8 @@ contract LaunchEvent is Ownable {
uint256 feeAmount = (_amount * getPenalty()) / 1e18;
uint256 amountMinusFee = _amount - feeAmount;

wavaxReserve -= _amount;
avaxReserve -= _amount;

WAVAX.withdraw(_amount);
_safeTransferAVAX(msg.sender, amountMinusFee);
if (feeAmount > 0) {
_safeTransferAVAX(rocketJoeFactory.penaltyCollector(), feeAmount);
Expand All @@ -386,15 +373,15 @@ contract LaunchEvent is Ownable {
0,
"LaunchEvent: liquid pair already exists"
);
require(wavaxReserve > 0, "LaunchEvent: no wavax balance");
require(avaxReserve > 0, "LaunchEvent: no avax balance");

uint256 tokenAllocated = tokenReserve;

// Adjust the amount of tokens sent to the pool if floor price not met
if (
floorPrice > (wavaxReserve * 10**token.decimals()) / tokenAllocated
floorPrice > (avaxReserve * 10**token.decimals()) / tokenAllocated
) {
tokenAllocated = (wavaxReserve * 10**token.decimals()) / floorPrice;
tokenAllocated = (avaxReserve * 10**token.decimals()) / floorPrice;
tokenIncentivesForUsers =
(tokenIncentivesForUsers * tokenAllocated) /
tokenReserve;
Expand All @@ -403,17 +390,18 @@ contract LaunchEvent is Ownable {
tokenIncentivesForUsers;
}

WAVAX.deposit{value: avaxReserve}();
if (factory.getPair(wavaxAddress, tokenAddress) == address(0)) {
pair = IJoePair(factory.createPair(wavaxAddress, tokenAddress));
} else {
pair = IJoePair(factory.getPair(wavaxAddress, tokenAddress));
}
WAVAX.transfer(address(pair), wavaxReserve);
WAVAX.transfer(address(pair), avaxReserve);
token.transfer(address(pair), tokenAllocated);
lpSupply = pair.mint(address(this));

wavaxAllocated = wavaxReserve;
wavaxReserve = 0;
avaxAllocated = avaxReserve;
avaxReserve = 0;

tokenReserve -= tokenAllocated;

Expand All @@ -422,7 +410,7 @@ contract LaunchEvent is Ownable {
tokenAddress,
wavaxAddress,
tokenAllocated,
wavaxAllocated
avaxAllocated
);
}

Expand Down Expand Up @@ -472,7 +460,7 @@ contract LaunchEvent is Ownable {
if (msg.sender == issuer) {
amount = tokenIncentiveIssuerRefund;
} else {
amount = (user.balance * tokenIncentivesForUsers) / wavaxAllocated;
amount = (user.balance * tokenIncentivesForUsers) / avaxAllocated;
}

require(amount > 0, "LaunchEvent: caller has no incentive to claim");
Expand All @@ -493,8 +481,7 @@ contract LaunchEvent is Ownable {

uint256 balance = user.balance;
user.balance = 0;
wavaxReserve -= balance;
WAVAX.withdraw(balance);
avaxReserve -= balance;

_safeTransferAVAX(msg.sender, balance);

Expand Down Expand Up @@ -530,13 +517,10 @@ contract LaunchEvent is Ownable {
token.transfer(penaltyCollector, excessToken);
}

uint256 excessWavax = WAVAX.balanceOf(address(this)) - wavaxReserve;
if (excessWavax > 0) {
WAVAX.transfer(penaltyCollector, excessWavax);
uint256 excessAvax = address(this).balance - avaxReserve;
if (excessAvax > 0) {
_safeTransferAVAX(penaltyCollector, excessAvax);
}

uint256 excessAvax = address(this).balance;
if (excessAvax > 0) _safeTransferAVAX(penaltyCollector, excessAvax);
}

/// @notice Returns the current penalty for early withdrawal
Expand All @@ -555,9 +539,9 @@ contract LaunchEvent is Ownable {
}

/// @notice Returns the current balance of the pool
/// @return The balances of WAVAX and issued token held by the launch contract
/// @return The balances of AVAX and issued token held by the launch contract
function getReserves() external view returns (uint256, uint256) {
return (wavaxReserve, tokenReserve + tokenIncentivesBalance);
return (avaxReserve, tokenReserve + tokenIncentivesBalance);
}

/// @notice Get the rJOE amount needed to deposit AVAX
Expand All @@ -569,12 +553,13 @@ contract LaunchEvent is Ownable {

/// @notice The total amount of liquidity pool tokens the user can withdraw
/// @param _user The address of the user to check
/// @return The user's balance of liquidity pool token
function pairBalance(address _user) public view returns (uint256) {
cryptofish7 marked this conversation as resolved.
Show resolved Hide resolved
UserInfo memory user = getUserInfo[_user];
if (wavaxAllocated == 0 || user.hasWithdrawnPair) {
if (avaxAllocated == 0 || user.hasWithdrawnPair) {
return 0;
}
return (user.balance * lpSupply) / wavaxAllocated / 2;
return (user.balance * lpSupply) / avaxAllocated / 2;
}

/// @dev Bytecode size optimization for the `atPhase` modifier.
Expand Down
2 changes: 1 addition & 1 deletion test/LaunchEventPhaseOne.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ describe("launch event contract phase one", function () {
value: ethers.utils.parseEther("1.0"),
})
).to.be.revertedWith(
"LaunchEvent: you can't send AVAX directly to this contract"
"Transaction reverted: function selector was not recognized and there's no fallback nor receive function"
);
});

Expand Down
8 changes: 4 additions & 4 deletions test/LaunchEventPhaseThree.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ describe("launch event contract phase three", function () {
);
});

it("should revert if try do withdraw WAVAX", async function () {
it("should revert if try do withdraw AVAX", async function () {
await expect(
this.LaunchEvent.connect(this.participant).withdrawAVAX(
ethers.utils.parseEther("1")
Expand Down Expand Up @@ -168,7 +168,7 @@ describe("launch event contract phase three", function () {
await this.LaunchEvent.connect(this.participant).createPair();
});

it.only("should add liquidity to pair where token0 balance > 0 and token1 balance > 0", async function () {
it("should add liquidity to pair where token0 balance > 0 and token1 balance > 0", async function () {
await this.factory.createPair(this.wavax.address, this.AUCTOK.address);
const pairAddress = await this.factory.getPair(
this.wavax.address,
Expand Down Expand Up @@ -235,7 +235,7 @@ describe("launch event contract phase three", function () {
await advanceTimeAndBlock(duration.days(4));
await expect(
this.LaunchEvent.connect(this.participant).createPair()
).to.be.revertedWith("LaunchEvent: no wavax balance");
).to.be.revertedWith("LaunchEvent: no avax balance");
});

it("should evenly distribute liquidity and incentives to issuer and participant", async function () {
Expand Down Expand Up @@ -442,7 +442,7 @@ describe("launch event contract phase three", function () {
await advanceTimeAndBlock(duration.days(4));
await expect(
this.LaunchEvent.connect(this.participant).createPair()
).to.be.revertedWith("LaunchEvent: no wavax balance");
).to.be.revertedWith("LaunchEvent: no avax balance");
});

it("should evenly distribute liquidity and incentives to issuer and participant", async function () {
Expand Down