Skip to content

Commit

Permalink
feat: use Swapper from utils
Browse files Browse the repository at this point in the history
  • Loading branch information
0xtekgrinder committed Aug 23, 2024
1 parent a37f003 commit aa1fea0
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 69 deletions.
49 changes: 18 additions & 31 deletions contracts/helpers/RebalancerFlashloanSwap.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,20 @@ pragma solidity ^0.8.19;
import "./Rebalancer.sol";
import { IERC3156FlashBorrower } from "oz/interfaces/IERC3156FlashBorrower.sol";
import { IERC3156FlashLender } from "oz/interfaces/IERC3156FlashLender.sol";
import { Swapper } from "utils/src/Swapper.sol";

/// @title RebalancerFlashloan
/// @author Angle Labs, Inc.
/// @dev Rebalancer contract for a Transmuter with as collaterals a liquid stablecoin and an yield bearing asset
/// using this liquid stablecoin as an asset
contract RebalancerFlashloanSwap is Rebalancer, IERC3156FlashBorrower {
contract RebalancerFlashloanSwap is Rebalancer, IERC3156FlashBorrower, Swapper {
using SafeERC20 for IERC20;
using SafeCast for uint256;
bytes32 public constant CALLBACK_SUCCESS = keccak256("ERC3156FlashBorrower.onFlashLoan");

/// @notice Angle stablecoin flashloan contract
IERC3156FlashLender public immutable FLASHLOAN;

address public swapRouter;
address public tokenTransferAddress;
uint32 public maxSlippage;

constructor(
Expand All @@ -29,13 +28,11 @@ contract RebalancerFlashloanSwap is Rebalancer, IERC3156FlashBorrower {
address _swapRouter,
address _tokenTransferAddress,
uint32 _maxSlippage
) Rebalancer(_accessControlManager, _transmuter) {
) Rebalancer(_accessControlManager, _transmuter) Swapper(_swapRouter, _tokenTransferAddress) {
if (address(_flashloan) == address(0)) revert ZeroAddress();
FLASHLOAN = _flashloan;
IERC20(AGTOKEN).safeApprove(address(_flashloan), type(uint256).max);

swapRouter = _swapRouter;
tokenTransferAddress = _tokenTransferAddress;
maxSlippage = _maxSlippage;
}

Expand Down Expand Up @@ -64,18 +61,18 @@ contract RebalancerFlashloanSwap is Rebalancer, IERC3156FlashBorrower {

/**
* @notice Set the token transfer address
* @param _tokenTransferAddress address of the token transfer contract
* @param newTokenTransferAddress address of the token transfer contract
*/
function setTokenTransferAddress(address _tokenTransferAddress) external onlyGuardian {
tokenTransferAddress = _tokenTransferAddress;
function setTokenTransferAddress(address newTokenTransferAddress) public override onlyGuardian {
super.setTokenTransferAddress(newTokenTransferAddress);
}

/**
* @notice Set the swap router
* @param _swapRouter address of the swap router
* @param newSwapRouter address of the swap router
*/
function setSwapRouter(address _swapRouter) external onlyGuardian {
swapRouter = _swapRouter;
function setSwapRouter(address newSwapRouter) public override onlyGuardian {
super.setSwapRouter(newSwapRouter);
}

/**
Expand All @@ -86,23 +83,6 @@ contract RebalancerFlashloanSwap is Rebalancer, IERC3156FlashBorrower {
maxSlippage = _maxSlippage;
}

/**
* @notice Perform the swap using the router/aggregator
* @param callData bytes to call the router/aggregator
*/
function _performRouterSwap(bytes memory callData) internal {
(bool success, bytes memory retData) = swapRouter.call(callData);

if (!success) {
if (retData.length != 0) {
assembly {
revert(add(32, retData), mload(retData))
}
}
revert SwapError();
}
}

/**
* @notice Swap token using the router/aggregator
* @param tokenIn address of the token to swap
Expand All @@ -117,8 +97,15 @@ contract RebalancerFlashloanSwap is Rebalancer, IERC3156FlashBorrower {
uint256 amount
) internal returns (uint256) {
uint256 balance = IERC20(tokenOut).balanceOf(address(this));
_adjustAllowance(tokenIn, tokenTransferAddress, amount);
_performRouterSwap(callData);

address[] memory tokens = new address[](1);
tokens[0] = tokenIn;
bytes[] memory callDatas = new bytes[](1);
callDatas[0] = callData;
uint256[] memory amounts = new uint256[](1);
amounts[0] = amount;
_swap(tokens, callDatas, amounts);

uint256 amountOut = IERC20(tokenOut).balanceOf(address(this)) - balance;
if (amountOut < (amount * (BPS - maxSlippage)) / BPS) {
revert SlippageTooHigh();
Expand Down
2 changes: 1 addition & 1 deletion lib/utils
3 changes: 3 additions & 0 deletions scripts/Constants.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ address constant USDC = 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48;
address constant BERNX = 0x3f95AA88dDbB7D9D484aa3D482bf0a80009c52c9;
address constant STEAK_USDC = 0xBEEF01735c132Ada46AA9aA4c54623cAA92A64CB;
address constant BIB01 = 0xCA30c93B02514f86d5C86a6e375E3A330B435Fb5;
address constant USDM = 0x59D9356E565Ab3A36dD77763Fc0d87fEaf85508C;

address constant ONEINCH_ROUTER = 0x111111125421cA6dc452d289314280a0f8842A65;

// EUROC
uint128 constant FIREWALL_BURN_RATIO_EUROC = uint128(0);
Expand Down
36 changes: 36 additions & 0 deletions scripts/DeployHarvesterSwap.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.19;

import "./utils/Utils.s.sol";
import { console } from "forge-std/console.sol";
import { HarvesterSwap } from "contracts/helpers/HarvesterSwap.sol";
import "./Constants.s.sol";

contract DeployHarvester is Utils {
function run() external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

address deployer = vm.addr(deployerPrivateKey);
console.log("Deployer address: ", deployer);
address rebalancer = 0x22604C0E5633A9810E01c9cb469B23Eee17AC411;
address asset = USDM;
address collateral = USDC;
uint64 targetExposure = (13 * 1e9) / 100;
uint64 overrideExposures = 0;
uint96 maxSlippage = 1e9 / 100;
HarvesterSwap HarvesterSwap = new HarvesterSwap(
rebalancer,
collateral,
asset,
targetExposure,
overrideExposures,
0,
0,
maxSlippage
);
console.log("HarvesterSwap deployed at: ", address(HarvesterSwap));

vm.stopBroadcast();
}
}
36 changes: 36 additions & 0 deletions scripts/DeployRebalancerFlashloanSwap.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.19;

import "./utils/Utils.s.sol";
import { console } from "forge-std/console.sol";
import { RebalancerFlashloanSwap } from "contracts/helpers/RebalancerFlashloanSwap.sol";
import { IAccessControlManager } from "contracts/utils/AccessControl.sol";
import { ITransmuter } from "contracts/interfaces/ITransmuter.sol";
import { IERC3156FlashLender } from "oz/interfaces/IERC3156FlashLender.sol";
import "./Constants.s.sol";
import "oz/interfaces/IERC20.sol";
import "oz-upgradeable/token/ERC20/extensions/IERC20MetadataUpgradeable.sol";

contract DeployRebalancerFlashloanSwapSwap is Utils {
function run() external {
uint256 deployerPrivateKey = vm.envUint("DEPLOYER_PRIVATE_KEY");
vm.startBroadcast(deployerPrivateKey);

address deployer = vm.addr(deployerPrivateKey);
console.log("Deployer address: ", deployer);
console.log(address(IAccessControlManager(_chainToContract(CHAIN_SOURCE, ContractType.CoreBorrow))));
console.log(address(ITransmuter(_chainToContract(CHAIN_SOURCE, ContractType.TransmuterAgUSD))));
RebalancerFlashloanSwap rebalancer = new RebalancerFlashloanSwap(
IAccessControlManager(_chainToContract(CHAIN_SOURCE, ContractType.CoreBorrow)),
ITransmuter(_chainToContract(CHAIN_SOURCE, ContractType.TransmuterAgUSD)),
IERC3156FlashLender(_chainToContract(CHAIN_SOURCE, ContractType.FlashLoan)),
ONEINCH_ROUTER,
ONEINCH_ROUTER,
50 // 0.5%
);

console.log("Rebalancer deployed at: ", address(rebalancer));

vm.stopBroadcast();
}
}
37 changes: 1 addition & 36 deletions scripts/Helpers.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,49 +7,14 @@ import "./utils/Utils.s.sol";
/// @title Utils
/// @author Angle Labs, Inc.
contract Helpers is Utils {
mapping(uint256 => uint256) internal forkIdentifier;
uint256 public arbitrumFork;
uint256 public avalancheFork;
uint256 public ethereumFork;
uint256 public optimismFork;
uint256 public polygonFork;
uint256 public gnosisFork;
uint256 public bnbFork;
uint256 public celoFork;
uint256 public polygonZkEVMFork;
uint256 public baseFork;
uint256 public lineaFork;

address public alice;
address public bob;
address public charlie;
address public dylan;
address public sweeper;

function setUp() public virtual {
arbitrumFork = vm.createFork(vm.envString("ETH_NODE_URI_ARBITRUM"));
avalancheFork = vm.createFork(vm.envString("ETH_NODE_URI_AVALANCHE"));
ethereumFork = vm.createFork(vm.envString("ETH_NODE_URI_MAINNET"));
optimismFork = vm.createFork(vm.envString("ETH_NODE_URI_OPTIMISM"));
polygonFork = vm.createFork(vm.envString("ETH_NODE_URI_POLYGON"));
gnosisFork = vm.createFork(vm.envString("ETH_NODE_URI_GNOSIS"));
bnbFork = vm.createFork(vm.envString("ETH_NODE_URI_BSC"));
celoFork = vm.createFork(vm.envString("ETH_NODE_URI_CELO"));
polygonZkEVMFork = vm.createFork(vm.envString("ETH_NODE_URI_POLYGON_ZKEVM"));
baseFork = vm.createFork(vm.envString("ETH_NODE_URI_BASE"));
lineaFork = vm.createFork(vm.envString("ETH_NODE_URI_LINEA"));

forkIdentifier[CHAIN_ARBITRUM] = arbitrumFork;
forkIdentifier[CHAIN_AVALANCHE] = avalancheFork;
forkIdentifier[CHAIN_ETHEREUM] = ethereumFork;
forkIdentifier[CHAIN_OPTIMISM] = optimismFork;
forkIdentifier[CHAIN_POLYGON] = polygonFork;
forkIdentifier[CHAIN_GNOSIS] = gnosisFork;
forkIdentifier[CHAIN_BNB] = bnbFork;
forkIdentifier[CHAIN_CELO] = celoFork;
forkIdentifier[CHAIN_POLYGONZKEVM] = polygonZkEVMFork;
forkIdentifier[CHAIN_BASE] = baseFork;
forkIdentifier[CHAIN_LINEA] = lineaFork;
super.setUpForks();

alice = vm.addr(1);
bob = vm.addr(2);
Expand Down

0 comments on commit aa1fea0

Please sign in to comment.