Skip to content

Commit

Permalink
feat/Add multiple collateral transfer to user and pool.
Browse files Browse the repository at this point in the history
  • Loading branch information
aleone committed Aug 14, 2020
1 parent d42af0b commit 262b74c
Show file tree
Hide file tree
Showing 4 changed files with 350 additions and 407 deletions.
57 changes: 52 additions & 5 deletions contracts/MarginPool.sol
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ contract MarginPool {
* @notice transfers asset from user to pool
* @dev all tokens are scaled to have 1e18 precision in contracts,
* so amounts are scaled down to native token decimals using _calcTransferAmount().
* If _asset equal to WETH address, transfer WETH from Controller address to pool
* @param _asset address of asset to transfer
* @param _user address of user to transfer assets from
* @param _amount amount of token to transfer from _user, scaled to 1e18 of precision
Expand All @@ -57,8 +56,8 @@ contract MarginPool {
address _asset,
address _user,
uint256 _amount
) external onlyController returns (bool) {
require(_amount > 0, "MarginPool: transferToPool amount is below 0");
) public onlyController returns (bool) {
require(_amount > 0, "MarginPool: transferToPool amount is below or equal to 0");

// transfer val from _user to pool
return ERC20Interface(_asset).transferFrom(_user, address(this), _amount);
Expand All @@ -77,10 +76,58 @@ contract MarginPool {
address _asset,
address payable _user,
uint256 _amount
) external onlyController returns (bool) {
require(_amount > 0, "MarginPool: transferToUser amount is below 0");
) public onlyController returns (bool) {
require(_amount > 0, "MarginPool: transferToUser amount is below or equal to 0");

// transfer asset val from Pool to _user
return ERC20Interface(_asset).transfer(_user, _amount);
}

/**
* @notice transfers multiple assets from users to pool
* @dev all tokens are scaled to have 1e18 precision in contracts,
* so amounts are scaled down to native token decimals using _calcTransferAmount().
* @param _asset addresses of assets to transfer
* @param _user addresses of users to transfer assets to
* @param _amount amount of each token to transfer to _user, scaled to 1e18 of precision
*/
function batchTransferToPool(
address[] memory _asset,
address[] memory _user,
uint256[] memory _amount
) public onlyController {
require(
_asset.length == _user.length && _user.length == _amount.length,
"MarginPool: batchTransferToPool array lengths are not equal"
);

for (uint256 i = 0; i < _asset.length; i++) {
// transfer val from _user to pool
transferToPool(_asset[i], _user[i], _amount[i]);
}
}

/**
* @notice transfers multiple assets from pool to users
* @dev all tokens are scaled to have 1e18 precision in contracts,
* so amounts are scaled down to native token decimals using _calcTransferAmount()
* @param _asset addresses of assets to transfer
* @param _user addresses of users to transfer assets to
* @param _amount amount of each token to transfer to _user, scaled to 1e18 of precision
*/
function batchTransferToUser(
address[] memory _asset,
address payable[] memory _user,
uint256[] memory _amount
) public onlyController {
require(
_asset.length == _user.length && _user.length == _amount.length,
"MarginPool: batchTransferToUser array lengths are not equal"
);

for (uint256 i = 0; i < _asset.length; i++) {
// transfer val from Pool to _pool
transferToUser(_asset[i], _user[i], _amount[i]);
}
}
}

0 comments on commit 262b74c

Please sign in to comment.