Skip to content

Commit

Permalink
Merge branch 'master' into test-9-upgrade-pragma
Browse files Browse the repository at this point in the history
  • Loading branch information
ControlCplusControlV authored Aug 27, 2024
2 parents 39d54af + c5a7964 commit 972bfd3
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 15 deletions.
26 changes: 26 additions & 0 deletions script/FoldCaptiveStaking.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.19;

import "forge-std/Script.sol";
import "src/FoldCaptiveStaking.sol";

contract FoldCaptiveStakingScript is Script {
INonfungiblePositionManager public positionManager =
INonfungiblePositionManager(0xC36442b4a4522E871399CD717aBDD847Ab11FE88);
IUniswapV3Pool public pool = IUniswapV3Pool(0x5eCEf3b72Cb00DBD8396EBAEC66E0f87E9596e97);
WETH public weth = WETH(payable(0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2));
ERC20 public fold = ERC20(0xd084944d3c05CD115C09d072B9F44bA3E0E45921);


function run() public {
vm.startBroadcast();
FoldCaptiveStaking foldCaptiveStaking =
new FoldCaptiveStaking(address(positionManager), address(pool), address(weth), address(fold));

fold.transfer(address(foldCaptiveStaking), 1_000_000);
weth.transfer(address(foldCaptiveStaking), 1_000_000);

foldCaptiveStaking.initialize();
vm.stopBroadcast();
}
}
10 changes: 10 additions & 0 deletions script/deploy-fork.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/usr/bin/env bash

source .env

forge script script/FoldCaptiveStaking.s.sol:FoldCaptiveStakingScript \
--chain-id 1 \
--fork-url $RPC_MAINNET \
--broadcast \
--private-key $PRIVATE_KEY \
-vvv
12 changes: 12 additions & 0 deletions script/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/usr/bin/env bash

source .env

forge script script/FoldCaptiveStaking.s.sol:FoldCaptiveStakingScript \
--chain-id 1 \
--rpc-url $RPC_MAINNET \
--broadcast \
--private-key $PRIVATE_KEY \
--verify \
--etherscan-api-key $ETHERSCAN_API_KEY \
-vvv
34 changes: 19 additions & 15 deletions src/FoldCaptiveStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {IUniswapV3Pool} from "./interfaces/IUniswapV3Pool.sol";

/// Libraries
import {TickMath} from "./libraries/TickMath.sol";
import {SafeTransferLib} from "lib/solmate/src/utils/SafeTransferLib.sol";

/// contracts
import {ERC20} from "lib/solmate/src/tokens/ERC20.sol";
Expand All @@ -19,6 +20,9 @@ import {WETH} from "lib/solmate/src/tokens/WETH.sol";
/// @title FoldCaptiveStaking
/// @notice Staking contract for managing FOLD token liquidity on Uniswap V3
contract FoldCaptiveStaking is Owned(msg.sender) {
using SafeTransferLib for ERC20;
using SafeTransferLib for WETH;

/*//////////////////////////////////////////////////////////////
INITIALIZATION
//////////////////////////////////////////////////////////////*/
Expand Down Expand Up @@ -87,8 +91,8 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
deadline: block.timestamp + 1 minutes
});

token0.approve(address(positionManager), type(uint256).max);
token1.approve(address(positionManager), type(uint256).max);
token0.safeApprove(address(positionManager), type(uint256).max);
token1.safeApprove(address(positionManager), type(uint256).max);

uint128 liquidity;
(TOKEN_ID, liquidity,,) = positionManager.mint(params);
Expand Down Expand Up @@ -198,16 +202,16 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
deadline: block.timestamp + 1 minutes
});

token0.transferFrom(msg.sender, address(this), amount0);
token1.transferFrom(msg.sender, address(this), amount1);
token0.safeTransferFrom(msg.sender, address(this), amount0);
token1.safeTransferFrom(msg.sender, address(this), amount1);

(uint128 liquidity, uint256 actualAmount0, uint256 actualAmount1) = positionManager.increaseLiquidity(params);

if (actualAmount0 < amount0) {
token0.transfer(msg.sender, amount0 - actualAmount0);
token0.safeTransfer(msg.sender, amount0 - actualAmount0);
}
if (actualAmount1 < amount1) {
token1.transfer(msg.sender, amount1 - actualAmount1);
token1.safeTransfer(msg.sender, amount1 - actualAmount1);
}

balances[msg.sender].amount += liquidity;
Expand Down Expand Up @@ -243,8 +247,8 @@ contract FoldCaptiveStaking is Owned(msg.sender) {

(uint128 liquidity, uint256 actualAmount0, uint256 actualAmount1) = positionManager.increaseLiquidity(params);

token0.transfer(msg.sender, fee0Owed - actualAmount0);
token1.transfer(msg.sender, fee1Owed - actualAmount1);
token0.safeTransfer(msg.sender, fee0Owed - actualAmount0);
token1.safeTransfer(msg.sender, fee1Owed - actualAmount1);

balances[msg.sender].token0FeeDebt = uint128(token0FeesPerLiquidity);
balances[msg.sender].token1FeeDebt = uint128(token1FeesPerLiquidity);
Expand All @@ -264,8 +268,8 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
uint256 fee1Owed = (token1FeesPerLiquidity - balances[msg.sender].token1FeeDebt) * balances[msg.sender].amount
/ liquidityUnderManagement;

token0.transfer(msg.sender, fee0Owed);
token1.transfer(msg.sender, fee1Owed);
token0.safeTransfer(msg.sender, fee0Owed);
token1.safeTransfer(msg.sender, fee1Owed);

balances[msg.sender].token0FeeDebt = uint128(token0FeesPerLiquidity);
balances[msg.sender].token1FeeDebt = uint128(token1FeesPerLiquidity);
Expand All @@ -278,7 +282,7 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
uint256 rewardsOwed = (rewardsPerLiquidity - balances[msg.sender].rewardDebt) * balances[msg.sender].amount
/ liquidityUnderManagement;

WETH9.transfer(msg.sender, rewardsOwed);
WETH9.safeTransfer(msg.sender, rewardsOwed);

balances[msg.sender].rewardDebt = uint128(rewardsPerLiquidity);

Expand Down Expand Up @@ -320,8 +324,8 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
revert WithdrawFailed();
}

token0.transfer(msg.sender, amount0);
token1.transfer(msg.sender, amount1);
token0.safeTransfer(msg.sender, amount0);
token1.safeTransfer(msg.sender, amount1);

emit Withdraw(msg.sender, liquidity);
}
Expand Down Expand Up @@ -385,8 +389,8 @@ contract FoldCaptiveStaking is Owned(msg.sender) {
revert WithdrawFailed();
}

token0.transfer(owner, amount0);
token1.transfer(owner, amount1);
token0.safeTransfer(owner, amount0);
token1.safeTransfer(owner, amount1);

emit InsuranceClaimed(owner, amount0, amount1);
}
Expand Down
17 changes: 17 additions & 0 deletions test/UnitTests.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,23 @@ contract UnitTests is BaseCaptiveTest {
// Expect revert due to minimum deposit requirement
vm.expectRevert(DepositAmountBelowMinimum.selector);
foldCaptiveStaking.deposit(0.5 ether, 0.5 ether, 0);
}

/// @dev Deposit Cap Enforcement: Test to ensure the deposit cap is respected.
function testDepositCap() public {
uint256 cap = 100 ether;
foldCaptiveStaking.setDepositCap(cap);

vm.deal(User01, 0.5 ether);
vm.startPrank(User01);

weth.deposit{value: 0.5 ether}();
weth.approve(address(foldCaptiveStaking), type(uint256).max);
fold.approve(address(foldCaptiveStaking), type(uint256).max);

// Expect revert due to minimum deposit requirement
vm.expectRevert(DepositAmountBelowMinimum.selector);
foldCaptiveStaking.deposit(0.5 ether, 0.5 ether, 0);

vm.stopPrank();
}
Expand Down

0 comments on commit 972bfd3

Please sign in to comment.