Skip to content

Commit

Permalink
Withdraw collateral asset from vault (#109)
Browse files Browse the repository at this point in the history
* Controller skeleton code created

* clean

* fix syntax & clean

* clean

* fix

* init tests

* comment function

* clean & add tests

* getter/view functions

* add simple tests

* improve tests

* improve tests

* fix

* ignore mock

* fix isAuthorized

* Update contracts/Controller.sol

Co-authored-by: Anton Cheng <antonassocareer@gmail.com>

* lint

* open vault action

* update solcover

* add tests

* update solcover

* clean code

* improve tests

* fix Otoken call

* Update contracts/Controller.sol

Co-authored-by: aparnakr <aparnalocked@gmail.com>

* Update contracts/Controller.sol

Co-authored-by: aparnakr <aparnalocked@gmail.com>

* Update contracts/Controller.sol

Co-authored-by: aparnakr <aparnalocked@gmail.com>

* Update contracts/Controller.sol

Co-authored-by: aparnakr <aparnalocked@gmail.com>

* add if

* fix typo

Co-authored-by: aparnakr <aparnalocked@gmail.com>

* clean code

Co-authored-by: aparnakr <aparnalocked@gmail.com>

* test expired otoken

* fix typo

* improve price tests

* add more tests

* refactor

* clean

* init

* update open vault

* clean

* refactor

* refactor

* add require statement

* deposit long

* modify expected result

* update all expected result to be an input rather just a query from contract

* modify open vault to start vault id from 0

* update open vault

* update _runActions

* update branch

* tests

* [ ] should execute depositing long otoken into vault in multiple actions

* init withdraw

* withdraw long

* fix test

* withdraw long otoken tests

* fix code

* test depositing two long otokens

* improve tests

* update mock

* deposit collateral

* tests

* final unit tests

* add open vault test

* fix typo

Co-authored-by: aparnakr <aparnalocked@gmail.com>

* withdraw collateral asset

* unit tests

* add withdraw zero amount test

* replace modifier by internal function

* test to check withdrawing more than vault balance

* fix typo

* fix conflicts & typo

* add tests

* refactor

Co-authored-by: Zubin Koticha <zkoticha@gmail.com>
Co-authored-by: Anton Cheng <antonassocareer@gmail.com>
Co-authored-by: aparnakr <aparnalocked@gmail.com>
  • Loading branch information
4 people authored Sep 3, 2020
1 parent c991626 commit 8082042
Show file tree
Hide file tree
Showing 2 changed files with 539 additions and 233 deletions.
35 changes: 34 additions & 1 deletion contracts/Controller.sol
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ contract Controller is ReentrancyGuard, Ownable {
vault = _withdrawLong(Actions._parseWithdrawArgs(action));
} else if (actionType == Actions.ActionType.DepositCollateral) {
vault = _depositCollateral(Actions._parseDepositArgs(action));
} else if (actionType == Actions.ActionType.WithdrawCollateral) {
vault = _withdrawCollateral(Actions._parseWithdrawArgs(action));
}
}

Expand Down Expand Up @@ -418,7 +420,34 @@ contract Controller is ReentrancyGuard, Ownable {
* @dev only account owner or operator can withdraw long option from vault
* @param _args WithdrawArgs structure
*/
// function _withdrawCollateral(Actions.WithdrawArgs memory _args) internal isAuthorized(_args.owner) {}
function _withdrawCollateral(Actions.WithdrawArgs memory _args)
internal
isAuthorized(msg.sender, _args.owner)
returns (MarginAccount.Vault memory)
{
require(checkVaultId(_args.owner, _args.vaultId), "Controller: invalid vault id");

MarginAccount.Vault memory vault = vaults[_args.owner][_args.vaultId];
if (isNotEmpty(vault.shortOtokens)) {
OtokenInterface otoken = OtokenInterface(vault.shortOtokens[0]);

require(
now <= otoken.expiryTimestamp(),
"Controller: can not withdraw collateral from a vault with an expired short otoken"
);
}

vaults[_args.owner][_args.vaultId]._removeCollateral(_args.asset, _args.amount, _args.index);

address marginPoolModule = AddressBookInterface(addressBook).getMarginPool();
MarginPoolInterface marginPool = MarginPoolInterface(marginPoolModule);

marginPool.transferToUser(_args.asset, _args.to, _args.amount);

emit CollateralAssetWithdrawed(_args.asset, _args.owner, _args.to, _args.vaultId, _args.amount);

return vaults[_args.owner][_args.vaultId];
}

/**
* @notice mint option into vault
Expand Down Expand Up @@ -460,4 +489,8 @@ contract Controller is ReentrancyGuard, Ownable {
function checkVaultId(address _accountOwner, uint256 _vaultId) internal view returns (bool) {
return ((_vaultId > 0) && (_vaultId <= accountVaultCounter[_accountOwner]));
}

function isNotEmpty(address[] memory _array) internal view returns (bool) {
return (_array.length > 0) && (_array[0] != address(0));
}
}
Loading

0 comments on commit 8082042

Please sign in to comment.