Skip to content

Commit

Permalink
refactor: adjusted close fn with lpAsset
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNilesh committed May 18, 2024
1 parent 1595786 commit 91f6714
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
10 changes: 8 additions & 2 deletions src/contracts/QWManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,7 @@ contract QWManager is IQWManager {
}

IERC20 token = IERC20(_tokenAddress);
token.approve(_targetQwChild[i], _amount);
token.transferFrom(address(this), address(_targetQwChild[i]), _amount);
token.approve(address(_targetQwChild[i]), _amount);

(bool success) = IQWChild(_targetQwChild[i]).create(_callData[i], _tokenAddress, _amount);
if (!success) {
Expand All @@ -74,6 +73,13 @@ contract QWManager is IQWManager {
}

for (uint256 i = 0; i < _targetQwChild.length; i++) {
(, address lpAsset, uint256 amount) = abi.decode(
_callData[i],
(address, address, uint256)
);
IERC20 token = IERC20(lpAsset);
token.approve(address(_targetQwChild[i]), amount);

(bool success) = IQWChild(_targetQwChild[i]).close(_callData[i]);
if (!success) {
revert CallFailed();
Expand Down
9 changes: 7 additions & 2 deletions src/contracts/child/QWAave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ contract QWAave is IQWChild {
}

IERC20 token = IERC20(_tokenAddress);
token.transferFrom(QW_MANAGER, address(this), _amount);
token.approve(POOL, _amount);

IPool(POOL).supply(_tokenAddress, _amount, QW_MANAGER, 0);
Expand All @@ -65,11 +66,15 @@ contract QWAave is IQWChild {
revert InvalidCallData();
}

(address asset, uint256 amount) = abi.decode(
(address asset, address lpAsset, uint256 amount) = abi.decode(
_callData,
(address, uint256)
(address, address, uint256)
);

IERC20 token = IERC20(lpAsset);
token.transferFrom(QW_MANAGER, address(this), amount);
token.approve(POOL, amount);

IPool(POOL).withdraw(asset, amount, QW_MANAGER);
return true;
}
Expand Down
14 changes: 12 additions & 2 deletions test/integration/child/QWAave.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,11 @@ contract AaveIntegration is IntegrationBase {
test_Create_Aave();

vm.startPrank(_daiWhale);
uint256 amount = 1_000_000 ether;
bytes memory callData = abi.encode(_dai, amount);
uint256 amount = _aDai.balanceOf(address(_qwManager));
bytes memory callData = abi.encode(address(_dai), address(_aDai), amount);

uint256 aDaiBalanceBefore = _aDai.balanceOf(address(_qwManager));
uint256 daiBalanceBefore = _dai.balanceOf(address(_qwManager));

// Create dynamic arrays with one element each
address[] memory targetQWChild = new address[](1);
Expand All @@ -63,6 +66,13 @@ contract AaveIntegration is IntegrationBase {

// bug: qwAave contract need to have aDai tokens and approve them to aave pool contract to withdraw
_qwManager.close(targetQWChild, callDataArr);

uint256 aDaiBalanceAfter = _aDai.balanceOf(address(_qwManager));
uint256 daiBalanceAfter = _dai.balanceOf(address(_qwManager));

assertGe(daiBalanceAfter - daiBalanceBefore, amount);
assertEq(aDaiBalanceBefore - aDaiBalanceAfter, amount);
assertEq(aDaiBalanceAfter, 0);
vm.stopPrank();
}
}

0 comments on commit 91f6714

Please sign in to comment.