Skip to content

Commit

Permalink
feat: safeTransfers
Browse files Browse the repository at this point in the history
  • Loading branch information
sandybradley committed Aug 25, 2024
1 parent e60e93e commit 8949b55
Showing 1 changed file with 19 additions and 15 deletions.
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

0 comments on commit 8949b55

Please sign in to comment.