Skip to content

Commit

Permalink
fix: some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jakekidd committed Jul 4, 2024
1 parent 570aef0 commit 3d3c0f4
Show file tree
Hide file tree
Showing 11 changed files with 800 additions and 712 deletions.
9 changes: 8 additions & 1 deletion script/child/DeployQWAaveV2.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import {Script} from 'forge-std/Script.sol';
contract DeployQWAaveV2 is Script, DeployBase {
struct ConfigParams {
address aaveLendingPool;
address investmentToken;
address assetToken;
}

QWAaveV2 public qwAaveV2;
Expand All @@ -36,7 +38,12 @@ contract DeployQWAaveV2 is Script, DeployBase {
qwRegistry = QWRegistry(registryAddr);

// Deploy QwChild
qwAaveV2 = new QWAaveV2(baseParams.qwManager, configParams.aaveLendingPool);
qwAaveV2 = new QWAaveV2(
baseParams.qwManager,
configParams.aaveLendingPool,
configParams.investmentToken,
configParams.assetToken
);

// Register Child in registry
qwRegistry.registerChild(address(qwAaveV2));
Expand Down
115 changes: 56 additions & 59 deletions src/contracts/child/QWAaveV3.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,72 @@ import {IQWChild} from 'interfaces/IQWChild.sol';
* @notice This contract integrates with AaveV3 protocol for Quant Wealth management.
*/
contract QWAaveV3 is IQWChild {
// Variables
address public immutable QW_MANAGER;
address public immutable POOL;
// Variables
address public immutable QW_MANAGER;
address public immutable POOL;
address public immutable INVESTMENT_TOKEN;
address public immutable ASSET_TOKEN;

// Custom errors
error InvalidCallData(); // Error for invalid call data
error UnauthorizedAccess(); // Error for unauthoruzed caller
// Custom errors
error InvalidCallData(); // Error for invalid call data
error UnauthorizedAccess(); // Error for unauthorized caller

modifier onlyQwManager() {
if (msg.sender != QW_MANAGER) {
revert UnauthorizedAccess();
modifier onlyQwManager() {
if (msg.sender != QW_MANAGER) {
revert UnauthorizedAccess();
}
_;
}
_;
}

/**
* @dev Constructor to initialize the contract with required addresses.
* @param _qwManager The address of the Quant Wealth Manager contract.
* @param _pool The address of the AaveV3 pool contract.
*/
constructor(address _qwManager, address _pool) {
QW_MANAGER = _qwManager;
POOL = _pool;
}

// Functions
/**
* @notice Executes a transaction on AaveV3 pool to deposit tokens.
* @dev This function is called by the parent contract to deposit tokens into the AaveV3 pool.
* @param _callData Encoded function call data (not used in this implementation).
* @param _tokenAddress Address of the token to be deposited.
* @param _amount Amount of tokens to be deposited.
* @return success boolean indicating the success of the transaction.
*/
function create(
bytes memory _callData,
address _tokenAddress,
uint256 _amount
) external override onlyQwManager returns (bool success) {
if (_callData.length != 0) {
revert InvalidCallData();
/**
* @dev Constructor to initialize the contract with required addresses.
* @param _qwManager The address of the Quant Wealth Manager contract.
* @param _pool The address of the AaveV3 pool contract.
* @param _investmentToken The address of the token to be invested.
* @param _assetToken The address of the asset token received after investment.
*/
constructor(address _qwManager, address _pool, address _investmentToken, address _assetToken) {
QW_MANAGER = _qwManager;
POOL = _pool;
INVESTMENT_TOKEN = _investmentToken;
ASSET_TOKEN = _assetToken;
}

IERC20 token = IERC20(_tokenAddress);
token.transferFrom(QW_MANAGER, address(this), _amount);
token.approve(POOL, _amount);
// Functions
/**
* @notice Executes a transaction on AaveV3 pool to deposit tokens.
* @dev This function is called by the parent contract to deposit tokens into the AaveV3 pool.
* @param _amount Amount of tokens to be deposited.
* @return success boolean indicating the success of the transaction.
* @return assetAmountReceived Amount of assets received from the deposit.
*/
function open(uint256 _amount) external override onlyQwManager returns (bool success, uint256 assetAmountReceived) {
IERC20 token = IERC20(INVESTMENT_TOKEN);
token.transferFrom(QW_MANAGER, address(this), _amount);
token.approve(POOL, _amount);

IPool(POOL).supply(INVESTMENT_TOKEN, _amount, address(this), 0);
assetAmountReceived = IERC20(ASSET_TOKEN).balanceOf(address(this));

IPool(POOL).supply(_tokenAddress, _amount, QW_MANAGER, 0);
return true;
}
// TODO: Transfer tokens to QWManager

/**
* @notice Executes a transaction on AaveV3 pool to withdraw tokens.
* @dev This function is called by the parent contract to withdraw tokens from the AaveV3 pool.
* @param _callData Encoded function call data containing the asset and amount to be withdrawn.
* @return success boolean indicating the success of the transaction.
*/
function close(bytes memory _callData) external override onlyQwManager returns (bool success) {
if (_callData.length == 0) {
revert InvalidCallData();
success = true;
}

(address asset, address lpAsset, uint256 amount) = abi.decode(_callData, (address, address, uint256));
/**
* @notice Executes a transaction on AaveV3 pool to withdraw tokens.
* @dev This function is called by the parent contract to withdraw tokens from the AaveV3 pool.
* @param _ratio Percentage of holdings to be withdrawn, with 8 decimal places for precision.
* @return success boolean indicating the success of the transaction.
* @return tokenAmountReceived Amount of tokens received from the withdrawal.
*/
function close(uint256 _ratio) external override onlyQwManager returns (bool success, uint256 tokenAmountReceived) {
uint256 totalHoldings = IERC20(ASSET_TOKEN).balanceOf(address(this));
uint256 amountToWithdraw = (totalHoldings * _ratio) / 1e8;

IERC20 token = IERC20(lpAsset);
token.transferFrom(QW_MANAGER, address(this), amount);
token.approve(POOL, amount);
IPool(POOL).withdraw(INVESTMENT_TOKEN, amountToWithdraw, QW_MANAGER);
tokenAmountReceived = amountToWithdraw;

IPool(POOL).withdraw(asset, amount, QW_MANAGER);
return true;
}
success = true;
}
}
136 changes: 77 additions & 59 deletions src/contracts/child/QWCompound.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,75 +10,93 @@ import {IQWChild} from 'interfaces/IQWChild.sol';
* @notice This contract integrates with Compound protocol for Quant Wealth management.
*/
contract QWCompound is IQWChild {
// Variables
address public immutable QW_MANAGER;
address public immutable COMET;
// Variables
address public immutable QW_MANAGER;
address public immutable COMET;
address public immutable INVESTMENT_TOKEN;
address public immutable ASSET_TOKEN;

// Custom errors
error InvalidCallData(); // Error for invalid call data
error UnauthorizedAccess(); // Error for unauthoruzed caller
// Custom errors
error InvalidCallData(); // Error for invalid call data
error UnauthorizedAccess(); // Error for unauthorized caller

modifier onlyQwManager() {
if (msg.sender != QW_MANAGER) {
revert UnauthorizedAccess();
modifier onlyQwManager() {
if (msg.sender != QW_MANAGER) {
revert UnauthorizedAccess();
}
_;
}
_;
}

/**
* @dev Constructor to initialize the contract with required addresses.
* @param _qwManager The address of the Quant Wealth Manager contract.
* @param _comet The address of the Compound comet contract.
*/
constructor(address _qwManager, address _comet) {
QW_MANAGER = _qwManager;
COMET = _comet;
}

// Functions
/**
* @notice Executes a transaction on Compound comet to deposit tokens.
* @dev This function is called by the parent contract to deposit tokens into the Compound comet.
* @param _callData Encoded function call data (not used in this implementation).
* @param _tokenAddress Address of the token to be deposited.
* @param _amount Amount of tokens to be deposited.
* @return success boolean indicating the success of the transaction.
*/
function create(
bytes memory _callData,
address _tokenAddress,
uint256 _amount
) external override onlyQwManager returns (bool success) {
if (_callData.length != 0) {
revert InvalidCallData();
/**
* @dev Constructor to initialize the contract with required addresses.
* @param _qwManager The address of the Quant Wealth Manager contract.
* @param _comet The address of the Compound comet contract.
* @param _investmentToken The address of the investment token (e.g., USDC).
* @param _assetToken The address of the asset token received from Compound (e.g., cUSDC).
*/
constructor(
address _qwManager,
address _comet,
address _investmentToken,
address _assetToken
) {
QW_MANAGER = _qwManager;
COMET = _comet;
INVESTMENT_TOKEN = _investmentToken;
ASSET_TOKEN = _assetToken;
}

IERC20 token = IERC20(_tokenAddress);
token.transferFrom(QW_MANAGER, address(this), _amount);
token.approve(COMET, _amount);
// Functions
/**
* @notice Executes a transaction on Compound comet to deposit tokens.
* @dev This function is called by the parent contract to deposit tokens into the Compound comet.
* @param _amount Amount of tokens to be deposited.
* @return success boolean indicating the success of the transaction.
* @return assetAmountReceived The amount of asset tokens received from the deposit.
*/
function open(
uint256 _amount
) external override onlyQwManager returns (bool success, uint256 assetAmountReceived) {
IERC20 token = IERC20(INVESTMENT_TOKEN);
token.transferFrom(QW_MANAGER, address(this), _amount);
token.approve(COMET, _amount);

IComet(COMET).supplyTo(QW_MANAGER, _tokenAddress, _amount);
return true;
}
// Perform the supply to Compound and get the current balance before and after to calculate the received amount
uint256 balanceBefore = IERC20(ASSET_TOKEN).balanceOf(address(this));
IComet(COMET).supplyTo(address(this), INVESTMENT_TOKEN, _amount);
uint256 balanceAfter = IERC20(ASSET_TOKEN).balanceOf(address(this));

/**
* @notice Executes a transaction on Compound comet to withdraw tokens.
* @dev This function is called by the parent contract to withdraw tokens from the Compound comet.
* @param _callData Encoded function call data containing the asset and amount to be withdrawn.
* @return success boolean indicating the success of the transaction.
*/
function close(bytes memory _callData) external override onlyQwManager returns (bool success) {
if (_callData.length == 0) {
revert InvalidCallData();
assetAmountReceived = balanceAfter - balanceBefore;
success = true;
}

(address asset, address lpAsset, uint256 amount) = abi.decode(_callData, (address, address, uint256));
/**
* @notice Executes a transaction on Compound comet to withdraw tokens.
* @dev This function is called by the parent contract to withdraw tokens from the Compound comet.
* @param _ratio Percentage of holdings to be withdrawn, with 8 decimal places for precision.
* @return success boolean indicating the success of the transaction.
* @return tokenAmountReceived The amount of tokens received from the withdrawal.
*/
function close(
uint256 _ratio
) external override onlyQwManager returns (bool success, uint256 tokenAmountReceived) {
uint256 totalHoldings = IERC20(ASSET_TOKEN).balanceOf(address(this));
uint256 amountToWithdraw = (totalHoldings * _ratio) / 1e8;

// Perform the withdraw from Compound and get the current balance before and after to calculate the received amount
uint256 balanceBefore = IERC20(INVESTMENT_TOKEN).balanceOf(address(this));
IComet(COMET).withdrawTo(address(this), INVESTMENT_TOKEN, amountToWithdraw);
uint256 balanceAfter = IERC20(INVESTMENT_TOKEN).balanceOf(address(this));

IERC20 token = IERC20(lpAsset);
token.transferFrom(QW_MANAGER, address(this), amount);
token.approve(COMET, amount);
tokenAmountReceived = balanceAfter - balanceBefore;
success = true;
}

IComet(COMET).withdrawTo(QW_MANAGER, asset, amount);
return true;
}
/**
* @notice Gets the address of the Quant Wealth Manager contract.
* @dev Returns the address of the Quant Wealth Manager contract.
*/
function QW_MANAGER() external view override returns (address) {
return QW_MANAGER;
}
}
Loading

0 comments on commit 3d3c0f4

Please sign in to comment.