Skip to content

Commit

Permalink
fix: use safe erc (#37)
Browse files Browse the repository at this point in the history
* fix: use safe erc

* test: adjust for usdt
  • Loading branch information
Schlagonia authored Feb 5, 2024
1 parent b445607 commit 82c2751
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 28 deletions.
7 changes: 5 additions & 2 deletions src/swappers/SolidlySwapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.18;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {ISolidly} from "../interfaces/Solidly/ISolidly.sol";

Expand All @@ -20,6 +21,8 @@ import {ISolidly} from "../interfaces/Solidly/ISolidly.sol";
* will need to be set for any token pairs that should use a stable pool.
*/
contract SolidlySwapper {
using SafeERC20 for ERC20;

// Optional Variable to be set to not sell dust.
uint256 public minAmountToSell;
// Defaults to WETH on mainnet.
Expand Down Expand Up @@ -145,8 +148,8 @@ contract SolidlySwapper {
uint256 _amount
) internal virtual {
if (ERC20(_token).allowance(address(this), _contract) < _amount) {
ERC20(_token).approve(_contract, 0);
ERC20(_token).approve(_contract, _amount);
ERC20(_token).safeApprove(_contract, 0);
ERC20(_token).safeApprove(_contract, _amount);
}
}
}
7 changes: 5 additions & 2 deletions src/swappers/UniswapV2Swapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.18;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {IUniswapV2Router02} from "../interfaces/Uniswap/V2/IUniswapV2Router02.sol";

Expand All @@ -17,6 +18,8 @@ import {IUniswapV2Router02} from "../interfaces/Uniswap/V2/IUniswapV2Router02.so
* based on needs or chain its used on.
*/
contract UniswapV2Swapper {
using SafeERC20 for ERC20;

// Optional Variable to be set to not sell dust.
uint256 public minAmountToSell;
// Defaults to WETH on mainnet.
Expand Down Expand Up @@ -119,8 +122,8 @@ contract UniswapV2Swapper {
uint256 _amount
) internal virtual {
if (ERC20(_token).allowance(address(this), _contract) < _amount) {
ERC20(_token).approve(_contract, 0);
ERC20(_token).approve(_contract, _amount);
ERC20(_token).safeApprove(_contract, 0);
ERC20(_token).safeApprove(_contract, _amount);
}
}
}
7 changes: 5 additions & 2 deletions src/swappers/UniswapV3Swapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
pragma solidity 0.8.18;

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {ISwapRouter} from "../interfaces/Uniswap/V3/ISwapRouter.sol";

Expand All @@ -21,6 +22,8 @@ import {ISwapRouter} from "../interfaces/Uniswap/V3/ISwapRouter.sol";
* function to easily set this for any token pairs needed.
*/
contract UniswapV3Swapper {
using SafeERC20 for ERC20;

// Optional Variable to be set to not sell dust.
uint256 public minAmountToSell;
// Defaults to WETH on mainnet.
Expand Down Expand Up @@ -187,8 +190,8 @@ contract UniswapV3Swapper {
uint256 _amount
) internal virtual {
if (ERC20(_token).allowance(address(this), _contract) < _amount) {
ERC20(_token).approve(_contract, 0);
ERC20(_token).approve(_contract, _amount);
ERC20(_token).safeApprove(_contract, 0);
ERC20(_token).safeApprove(_contract, _amount);
}
}
}
12 changes: 7 additions & 5 deletions src/test/HealthCheck.t.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.18;

import {Setup, IStrategy} from "./utils/Setup.sol";
import {Setup, IStrategy, SafeERC20, ERC20} from "./utils/Setup.sol";

import {MockHealthCheck, IMockHealthCheck} from "./mocks/MockHealthCheck.sol";

contract HealthCheckTest is Setup {
using SafeERC20 for ERC20;

IMockHealthCheck public healthCheck;

function setUp() public override {
Expand Down Expand Up @@ -294,11 +296,11 @@ contract HealthCheckTest is Setup {
);

// Loose .01%
uint256 loss = _amount / 10000;
uint256 loss = _amount / MAX_BPS;

// simulate loss
vm.prank(address(healthCheck));
asset.transfer(management, loss);
asset.safeTransfer(management, loss);

assertEq(
healthCheck.doHealthCheck(),
Expand Down Expand Up @@ -391,11 +393,11 @@ contract HealthCheckTest is Setup {
);

// Loose .01%
uint256 loss = _amount / 10_000;
uint256 loss = _amount / MAX_BPS;

// simulate loss
vm.prank(address(healthCheck));
asset.transfer(management, loss);
asset.safeTransfer(management, loss);

assertEq(
healthCheck.doHealthCheck(),
Expand Down
6 changes: 3 additions & 3 deletions src/test/UniV2Swapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ contract UniswapV2SwapperTest is Setup {

ERC20 public base;

uint256 public minBaseAmount = 1e8;
uint256 public minBaseAmount = 1e10;
uint256 public maxBaseAmount = 1e20;

function setUp() public override {
Expand Down Expand Up @@ -74,7 +74,7 @@ contract UniswapV2SwapperTest is Setup {

function test_swapFrom_multiHop(uint256 amount) public {
// Need to make sure we are getting enough DAI to be non 0 USDC.
vm.assume(amount >= 1e15 && amount <= maxFuzzAmount);
vm.assume(amount >= minFuzzAmount && amount <= maxFuzzAmount);
ERC20 swapTo = ERC20(tokenAddrs["USDC"]);

// Send some asset to the contract
Expand Down Expand Up @@ -110,7 +110,7 @@ contract UniswapV2SwapperTest is Setup {
assertEq(base.balanceOf(address(uniV2Swapper)), 0);

// Define the minimum amount of Base to receive
uint256 minOut = amount;
uint256 minOut = amount * 1e12;

// Perform swap from asset to Base with minimum output requirement
vm.expectRevert();
Expand Down
15 changes: 8 additions & 7 deletions src/test/UniV3Swapper.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ contract UniswapV3SwapperTest is Setup {

ERC20 public weth;

uint256 public minWethAmount = 1e8;
uint256 public minWethAmount = 1e10;
uint256 public maxWethAmount = 1e20;

function setUp() public override {
Expand Down Expand Up @@ -105,7 +105,7 @@ contract UniswapV3SwapperTest is Setup {
// Assert WETH balance in the contract is 0
assertEq(weth.balanceOf(address(uniV3Swapper)), 0);

uint256 toGet = amount / 4_000;
uint256 toGet = (amount * 1e12) / 4_000;

// Perform swap from asset to WETH
uint256 amountIn = uniV3Swapper.swapTo(
Expand Down Expand Up @@ -136,7 +136,7 @@ contract UniswapV3SwapperTest is Setup {
// Assert asset balance in the contract is 0
assertEq(asset.balanceOf(address(uniV3Swapper)), 0);

uint256 toGet = amount * 1_000;
uint256 toGet = (amount * 1_000) / 1e12;

// Perform swap from WETH to asset
uint256 amountIn = uniV3Swapper.swapTo(
Expand All @@ -156,7 +156,7 @@ contract UniswapV3SwapperTest is Setup {

function test_swapFrom_multiHop(uint256 amount) public {
// Need to make sure we are getting enough DAI to be non 0 USDC.
vm.assume(amount >= 1e15 && amount <= maxFuzzAmount);
vm.assume(amount >= minFuzzAmount && amount <= maxFuzzAmount);
ERC20 swapTo = ERC20(tokenAddrs["USDC"]);
// Set fees for weth and asset
uniV3Swapper.setUniFees(address(weth), address(asset), 500);
Expand Down Expand Up @@ -192,7 +192,8 @@ contract UniswapV3SwapperTest is Setup {

function test_swapTo_multiHop(uint256 amount) public {
// Need to make sure we are getting enough DAI to be non 0 USDC.
vm.assume(amount >= 1e15 && amount <= maxFuzzAmount);
vm.assume(amount >= minFuzzAmount && amount <= maxFuzzAmount);

ERC20 swapTo = ERC20(tokenAddrs["USDC"]);
// Set fees for weth and asset
uniV3Swapper.setUniFees(address(weth), address(asset), 500);
Expand All @@ -209,7 +210,7 @@ contract UniswapV3SwapperTest is Setup {
assertEq(swapTo.balanceOf(address(uniV3Swapper)), 0);

// Define the desired amount to receive
uint256 toGet = amount / 2e12;
uint256 toGet = amount / 10;

// Perform swap from asset to swap_to
uint256 amountIn = uniV3Swapper.swapTo(
Expand Down Expand Up @@ -243,7 +244,7 @@ contract UniswapV3SwapperTest is Setup {
assertEq(weth.balanceOf(address(uniV3Swapper)), 0);

// Define the minimum amount of WETH to receive
uint256 minOut = amount;
uint256 minOut = amount * 1e12;

// Perform swap from asset to WETH with minimum output requirement
vm.expectRevert();
Expand Down
17 changes: 10 additions & 7 deletions src/test/utils/Setup.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "forge-std/console.sol";
import {ExtendedTest} from "./ExtendedTest.sol";

import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import {SafeERC20} from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";

import {VyperDeployer} from "./VyperDeployer.sol";
import {IStrategy} from "@tokenized-strategy/interfaces/IStrategy.sol";
Expand All @@ -15,6 +16,8 @@ import {IVaultFactory} from "@yearn-vaults/interfaces/IVaultFactory.sol";
import {MockStrategy} from "../mocks/MockStrategy.sol";

contract Setup is ExtendedTest {
using SafeERC20 for ERC20;

VyperDeployer public vyperDeployer = new VyperDeployer();

// Contract instances that we will use repeatedly.
Expand All @@ -41,17 +44,17 @@ contract Setup is ExtendedTest {
uint256 public MAX_BPS = 10_000;

// Fuzz amount
uint256 public maxFuzzAmount = 1e24;
uint256 public minFuzzAmount = 100_000;
uint256 public maxFuzzAmount = 1e12;
uint256 public minFuzzAmount = MAX_BPS;

// Default profit max unlock time is set for 10 days
uint256 public profitMaxUnlockTime = 10 days;

function setUp() public virtual {
_setTokenAddrs();

// Set asset
asset = ERC20(tokenAddrs["DAI"]);
// Make sure everything works with USDT
asset = ERC20(tokenAddrs["USDT"]);

// Set decimals
decimals = asset.decimals();
Expand Down Expand Up @@ -122,11 +125,11 @@ contract Setup is ExtendedTest {
address _user,
uint256 _amount
) public {
vm.prank(_user);
asset.approve(address(_strategy), _amount);
vm.startPrank(_user);
asset.safeApprove(address(_strategy), _amount);

vm.prank(_user);
_strategy.deposit(_amount, _user);
vm.stopPrank();
}

function mintAndDepositIntoStrategy(
Expand Down

0 comments on commit 82c2751

Please sign in to comment.