Skip to content

Commit

Permalink
feat/fix - exit leveraged position
Browse files Browse the repository at this point in the history
  • Loading branch information
Dvisacker committed Oct 2, 2024
1 parent e6f3868 commit 99bb31d
Show file tree
Hide file tree
Showing 7 changed files with 216 additions and 93 deletions.
41 changes: 32 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,35 +82,58 @@ leverage-local-fork:

leverage-arbitrum:
@echo "Leveraging on Arbitrum..."
@cd $(CONTRACTS_DIR) && forge script script/Leverage.s.sol:Leverage --rpc-url $(ARBITRUM_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast -vvvv
@cd $(CONTRACTS_DIR) && forge script script/Leverage.s.sol:Leverage --rpc-url $(ARBITRUM_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast -vv

leverage-mainnet:
@echo "Leveraging on Mainnet..."
@cd $(CONTRACTS_DIR) && forge script script/Leverage.s.sol:Leverage --rpc-url $(MAINNET_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vvvv
@cd $(CONTRACTS_DIR) && forge script script/Leverage.s.sol:Leverage --rpc-url $(MAINNET_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vv

exit-with-flashloan-local-fork:
flashloan-exit-local-fork:
@echo "Exiting with flashloan on Local..."
@cd $(CONTRACTS_DIR) && forge script script/ExitWithFlashloan.s.sol:ExitWithFlashloan --rpc-url http://localhost:8545 --private-key $(PRIVATE_KEY) --broadcast -vv

exit-with-flashloan-arbitrum:
flashloan-exit-arbitrum:
@echo "Exiting with flashloan on Arbitrum..."
@cd $(CONTRACTS_DIR) && forge script script/ExitWithFlashloan.s.sol:ExitWithFlashloan --rpc-url $(ARBITRUM_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast -vvvv
@cd $(CONTRACTS_DIR) && forge script script/ExitWithFlashloan.s.sol:ExitWithFlashloan --rpc-url $(ARBITRUM_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast -vv

exit-with-flashloan-mainnet:
flashloan-exit-mainnet:
@echo "Exiting with flashloan on Mainnet..."
@cd $(CONTRACTS_DIR) && forge script script/ExitWithFlashloan.s.sol:ExitWithFlashloan --rpc-url $(MAINNET_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vvvv
@cd $(CONTRACTS_DIR) && forge script script/ExitWithFlashloan.s.sol:ExitWithFlashloan --rpc-url $(MAINNET_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vv

exit-local-fork:
@echo "Exiting on Local..."
@cd $(CONTRACTS_DIR) && forge script script/Exit.s.sol:Exit --rpc-url http://localhost:8545 --private-key $(PRIVATE_KEY) --broadcast -vv

exit-arbitrum:
@echo "Exiting on Arbitrum..."
@cd $(CONTRACTS_DIR) && forge script script/Exit.s.sol:Exit --rpc-url $(ARBITRUM_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast -vv

exit-mainnet:
@echo "Exiting on Mainnet..."
@cd $(CONTRACTS_DIR) && forge script script/Exit.s.sol:Exit --rpc-url $(MAINNET_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vv

repay-local-fork:
@echo "Repaying on Local..."
@cd $(CONTRACTS_DIR) && forge script script/Repay.s.sol:Repay --rpc-url http://localhost:8545 --private-key $(PRIVATE_KEY) --broadcast -vv

repay-arbitrum:
@echo "Repaying on Arbitrum..."
@cd $(CONTRACTS_DIR) && forge script script/Repay.s.sol:Repay --rpc-url $(ARBITRUM_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast -vv


get-position-local-fork:
@echo "Getting position on Local..."
@cd $(CONTRACTS_DIR) && forge script script/GetPosition.s.sol:GetPosition --rpc-url http://localhost:8545 --private-key $(PRIVATE_KEY) --broadcast -vv

get-position-arbitrum:
@echo "Getting position on Arbitrum..."
@cd $(CONTRACTS_DIR) && forge script script/GetPosition.s.sol:GetPosition --rpc-url $(ARBITRUM_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast -vvvv
@cd $(CONTRACTS_DIR) && forge script script/GetPosition.s.sol:GetPosition --rpc-url $(ARBITRUM_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast -vv

get-position-mainnet:
@echo "Getting position on Mainnet..."
@cd $(CONTRACTS_DIR) && forge script script/GetPosition.s.sol:GetPosition --rpc-url $(MAINNET_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vvvv
@cd $(CONTRACTS_DIR) && forge script script/GetPosition.s.sol:GetPosition --rpc-url $(MAINNET_RPC_URL) --private-key $(PRIVATE_KEY) --broadcast --verify --etherscan-api-key $(ETHERSCAN_API_KEY) -vv



# Combined commands
build: rust-build sol-build
Expand Down
35 changes: 35 additions & 0 deletions contracts/script/Exit.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import {Script} from "forge-std/Script.sol";
import {AaveLooper} from "../src/AaveLooper.sol";
import {ERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {console2} from "forge-std/console2.sol";
import {DevOpsTools} from "../lib/foundry-devops/src/DevOpsTools.sol";
import {HelperConfig} from "./HelperConfig.s.sol";

contract Exit is Script {
HelperConfig helperConfig = new HelperConfig();
address aaveLooper = DevOpsTools.get_most_recent_deployment("AaveLooper", block.chainid);
address supplyAsset;
address borrowAsset;
uint256 initialAmount;
uint256 iterations;

function run() external {
HelperConfig.NetworkConfig memory networkConfig = helperConfig.getActiveNetworkConfig();

supplyAsset = vm.envOr("SUPPLY_ASSET", networkConfig.usdc);
borrowAsset = vm.envOr("BORROW_ASSET", networkConfig.weth);
initialAmount = vm.envOr("INITIAL_AMOUNT", uint256(1000000));

AaveLooper looper = AaveLooper(aaveLooper);

console2.log("Supply asset:", supplyAsset);
console2.log("Borrow asset:", borrowAsset);

vm.startBroadcast();
looper.exitPosition(supplyAsset, borrowAsset, 5, 500);
vm.stopBroadcast();
}
}
45 changes: 41 additions & 4 deletions contracts/script/GetPosition.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ import {Script} from "forge-std/Script.sol";
import {AaveLooper} from "../src/AaveLooper.sol";
import {ERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {console2} from "forge-std/console2.sol";
import {console} from "forge-std/console.sol";
import {DevOpsTools} from "../lib/foundry-devops/src/DevOpsTools.sol";
import {HelperConfig} from "./HelperConfig.s.sol";
import {Strings} from "../lib/openzeppelin-contracts/contracts/utils/Strings.sol";

contract GetPosition is Script {
HelperConfig helperConfig = new HelperConfig();
Expand All @@ -16,9 +18,25 @@ contract GetPosition is Script {
uint256 initialAmount;
uint256 iterations;

function formatFloat(uint256 value, uint256 decimals) internal pure returns (string memory) {
uint256 factor = 10 ** decimals;
uint256 integerPart = value / factor;
uint256 fractionalPart = value % factor;

string memory integerStr = Strings.toString(integerPart);
string memory fractionalStr = Strings.toString(fractionalPart);

// Pad the fractional part with leading zeros
while (bytes(fractionalStr).length < decimals) {
fractionalStr = string(abi.encodePacked("0", fractionalStr));
}

return string(abi.encodePacked(integerStr, ".", fractionalStr));
}

function run() external {
HelperConfig.NetworkConfig memory networkConfig = helperConfig.getActiveNetworkConfig();
AaveLooper looper = AaveLooper(aaveLooper);
HelperConfig.NetworkConfig memory networkConfig = helperConfig.getActiveNetworkConfig();

(
uint256 totalCollateral,
Expand All @@ -29,10 +47,29 @@ contract GetPosition is Script {
uint256 healthFactor
) = looper.getPositionData();

supplyAsset = vm.envOr("SUPPLY_ASSET", networkConfig.usdc);
borrowAsset = vm.envOr("BORROW_ASSET", networkConfig.weth);

uint256 borrowAssetBalance = ERC20(borrowAsset).balanceOf(address(looper));
uint256 supplyAssetBalance = ERC20(supplyAsset).balanceOf(address(looper));
string memory borrowAssetBalanceFormatted = formatFloat(borrowAssetBalance, 18);
string memory supplyAssetBalanceFormatted = formatFloat(supplyAssetBalance, 6);
uint256 userBorrowAssetBalance = ERC20(borrowAsset).balanceOf(address(msg.sender));
uint256 userSupplyAssetBalance = ERC20(supplyAsset).balanceOf(address(msg.sender));
string memory userBorrowAssetBalanceFormatted = formatFloat(userBorrowAssetBalance, 18);
string memory userSupplyAssetBalanceFormatted = formatFloat(userSupplyAssetBalance, 6);
string memory totalCollateralUSD = formatFloat(totalCollateral, 8);
string memory totalDebtUSD = formatFloat(totalDebt, 8);
string memory availableBorrowsUSD = formatFloat(availableBorrows, 8);

console2.log("Contract address:", aaveLooper);
console2.log("Total collateral:", totalCollateral);
console2.log("Total debt:", totalDebt);
console2.log("Available borrows:", availableBorrows);
console2.log("Borrow asset contract balance (WETH):", borrowAssetBalanceFormatted);
console2.log("Supply asset contract balance (USDC):", supplyAssetBalanceFormatted);
console2.log("Borrow asset user balance (WETH):", userBorrowAssetBalanceFormatted);
console2.log("Supply asset user balance (USDC):", userSupplyAssetBalanceFormatted);
console2.log("Total collateral:", totalCollateralUSD);
console2.log("Total debt:", totalDebtUSD);
console2.log("Available borrows:", availableBorrowsUSD);
console2.log("Current liquidation threshold:", currentLiquidationThreshold);
console2.log("LTV:", ltv);
console2.log("Health factor:", healthFactor);
Expand Down
2 changes: 1 addition & 1 deletion contracts/script/Leverage.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ contract Leverage is Script {
initialAmount = vm.envOr("INITIAL_AMOUNT", uint256(1000000));
iterations = vm.envOr("ITERATIONS", uint256(3));

address uniswapV3Factory = networkConfig.uniswapV3Factory;
// address uniswapV3Factory = networkConfig.uniswapV3Factory;

AaveLooper looper = AaveLooper(aaveLooper);

Expand Down
36 changes: 36 additions & 0 deletions contracts/script/Repay.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import {Script} from "forge-std/Script.sol";
import {AaveLooper} from "../src/AaveLooper.sol";
import {ERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {console2} from "forge-std/console2.sol";
import {DevOpsTools} from "../lib/foundry-devops/src/DevOpsTools.sol";
import {HelperConfig} from "./HelperConfig.s.sol";

contract Repay is Script {
HelperConfig helperConfig = new HelperConfig();
address aaveLooper = DevOpsTools.get_most_recent_deployment("AaveLooper", block.chainid);
address supplyAsset;
address borrowAsset;
uint256 initialAmount;
uint256 iterations;

function run() external {
console2.log("Aave looper address:", aaveLooper);
HelperConfig.NetworkConfig memory networkConfig = helperConfig.getActiveNetworkConfig();

supplyAsset = vm.envOr("SUPPLY_ASSET", networkConfig.usdc);
borrowAsset = vm.envOr("BORROW_ASSET", networkConfig.weth);
initialAmount = vm.envOr("INITIAL_AMOUNT", uint256(1000000));

AaveLooper looper = AaveLooper(aaveLooper);

console2.log("Supply asset:", supplyAsset);
console2.log("Borrow asset:", borrowAsset);

vm.startBroadcast();
looper._repayBorrow(borrowAsset, 1);
vm.stopBroadcast();
}
}
35 changes: 35 additions & 0 deletions contracts/script/Withdraw.s.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import {Script} from "forge-std/Script.sol";
import {AaveLooper} from "../src/AaveLooper.sol";
import {ERC20} from "../lib/openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import {console2} from "forge-std/console2.sol";
import {DevOpsTools} from "../lib/foundry-devops/src/DevOpsTools.sol";
import {HelperConfig} from "./HelperConfig.s.sol";

contract ExitWithFlashloan is Script {
HelperConfig helperConfig = new HelperConfig();
address aaveLooper = DevOpsTools.get_most_recent_deployment("AaveLooper", block.chainid);
address supplyAsset;
address borrowAsset;
uint256 initialAmount;
uint256 iterations;

function run() external {
HelperConfig.NetworkConfig memory networkConfig = helperConfig.getActiveNetworkConfig();

supplyAsset = vm.envOr("SUPPLY_ASSET", networkConfig.usdc);
borrowAsset = vm.envOr("BORROW_ASSET", networkConfig.weth);
initialAmount = vm.envOr("INITIAL_AMOUNT", uint256(1000000));

AaveLooper looper = AaveLooper(aaveLooper);

console2.log("Supply asset:", supplyAsset);
console2.log("Borrow asset:", borrowAsset);

vm.startBroadcast();
looper.exitPositionWithFlashLoan(supplyAsset, borrowAsset);
vm.stopBroadcast();
}
}
Loading

0 comments on commit 99bb31d

Please sign in to comment.