Skip to content

Commit

Permalink
Merge pull request #12 from pie-dao/maint/audit-prep
Browse files Browse the repository at this point in the history
Maint/audit prep
  • Loading branch information
MickdeGraaf authored Nov 12, 2020
2 parents 66eb8d9 + 8613df8 commit ca388d8
Show file tree
Hide file tree
Showing 19 changed files with 751 additions and 192 deletions.
8 changes: 4 additions & 4 deletions contracts/callManagers/LendingManager/LendingLogicAave.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import "../../interfaces/IAaveLendingPool.sol";
contract LendingLogicAave is ILendingLogic {

IAaveLendingPool public lendingPool;
uint16 public referralCode;

constructor(address _lendingPool) {
constructor(address _lendingPool, uint16 _referralCode) {
lendingPool = IAaveLendingPool(_lendingPool);
referralCode = _referralCode;
}

function lend(address _underlying, uint256 _amount) external view override returns(address[] memory targets, bytes[] memory data) {
Expand All @@ -31,8 +33,7 @@ contract LendingLogicAave is ILendingLogic {

// Deposit into Aave
targets[2] = address(lendingPool);
// TODO set referral
data[2] = abi.encodeWithSelector(lendingPool.deposit.selector, _underlying, _amount, 0);
data[2] = abi.encodeWithSelector(lendingPool.deposit.selector, _underlying, _amount, referralCode);

return(targets, data);
}
Expand All @@ -46,5 +47,4 @@ contract LendingLogicAave is ILendingLogic {
return(targets, data);
}

// TODO add getter for interest rate
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,4 @@ contract LendingLogicCompound is ILendingLogic {
return(targets, data);
}

// TODO add getter for interest rate
}
28 changes: 28 additions & 0 deletions contracts/callManagers/LendingManager/LendingManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,24 @@ contract LendingManager is Ownable {
LendingRegistry public lendingRegistry;
IExperiPie public basket;

event Lend(address indexed underlying, uint256 amount, bytes32 indexed protocol);
event UnLend(address indexed wrapped, uint256 amount);
/**
@notice Constructor
@param _lendingRegistry Address of the lendingRegistry contract
@param _basket Address of the pool/pie/basket to manage
*/
constructor(address _lendingRegistry, address _basket) public {
lendingRegistry = LendingRegistry(_lendingRegistry);
basket = IExperiPie(_basket);
}

/**
@notice Move underlying to a lending protocol
@param _underlying Address of the underlying token
@param _amount Amount of underlying to lend
@param _protocol Bytes32 protocol key to lend to
*/
function lend(address _underlying, uint256 _amount, bytes32 _protocol) public onlyOwner {
// _amount or actual balance, whatever is less
uint256 amount = _amount.min(IERC20(_underlying).balanceOf(address(basket)));
Expand All @@ -35,8 +48,15 @@ contract LendingManager is Ownable {

// add wrapped token
addToken(lendingRegistry.underlyingToProtocolWrapped(_underlying, _protocol));

emit Lend(_underlying, _amount, _protocol);
}

/**
@notice Unlend wrapped token from its lending protocol
@param _wrapped Address of the wrapped token
@param _amount Amount of the wrapped token to unlend
*/
function unlend(address _wrapped, uint256 _amount) public onlyOwner {
// unlend token
// _amount or actual balance, whatever is less
Expand All @@ -54,8 +74,16 @@ contract LendingManager is Ownable {

// if needed remove wrapped
removeToken(_wrapped);

emit UnLend(_wrapped, _amount);
}

/**
@notice Unlend and immediately lend in a different protocol
@param _wrapped Address of the wrapped token to bounce to another protocol
@param _amount Amount of the wrapped token to bounce to the other protocol
@param _toProtocol Protocol to deposit bounced tokens in
*/
function bounce(address _wrapped, uint256 _amount, bytes32 _toProtocol) external {
unlend(_wrapped, _amount);
// Bounce all to new protocol
Expand Down
45 changes: 44 additions & 1 deletion contracts/callManagers/LendingManager/LendingRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,74 @@ contract LendingRegistry is Ownable {
// Maps protocol to addresses containing lend and unlend logic
mapping(bytes32 => address) public protocolToLogic;

// TODO events
event WrappedToProtocolSet(address indexed wrapped, bytes32 indexed protocol);
event WrappedToUnderlyingSet(address indexed wrapped, address indexed underlying);
event ProtocolToLogicSet(bytes32 indexed protocol, address indexed logic);
event UnderlyingToProtocolWrappedSet(address indexed underlying, bytes32 indexed protocol, address indexed wrapped);

/**
@notice Set which protocl a wrapped token belongs to
@param _wrapped Address of the wrapped token
@param _protocol Bytes32 key of the protocol
*/
function setWrappedToProtocol(address _wrapped, bytes32 _protocol) onlyOwner external {
wrappedToProtocol[_wrapped] = _protocol;
emit WrappedToProtocolSet(_wrapped, _protocol);
}

/**
@notice Set what is the underlying for a wrapped token
@param _wrapped Address of the wrapped token
@param _underlying Address of the underlying token
*/
function setWrappedToUnderlying(address _wrapped, address _underlying) onlyOwner external {
wrappedToUnderlying[_wrapped] = _underlying;
emit WrappedToUnderlyingSet(_wrapped, _underlying);
}

/**
@notice Set the logic contract for the protocol
@param _protocol Bytes32 key of the procol
@param _logic Address of the lending logic contract for that protocol
*/
function setProtocolToLogic(bytes32 _protocol, address _logic) onlyOwner external {
protocolToLogic[_protocol] = _logic;
emit ProtocolToLogicSet(_protocol, _logic);
}

/**
@notice Set the wrapped token for the underlying deposited in this protocol
@param _underlying Address of the unerlying token
@param _protocol Bytes32 key of the protocol
@param _wrapped Address of the wrapped token
*/
function setUnderlyingToProtocolWrapped(address _underlying, bytes32 _protocol, address _wrapped) onlyOwner external {
underlyingToProtocolWrapped[_underlying][_protocol] = _wrapped;
emit UnderlyingToProtocolWrappedSet(_underlying, _protocol, _wrapped);
}

/**
@notice Get tx data to lend the underlying amount in a specific protocol
@param _underlying Address of the underlying token
@param _amount Amount to lend
@param _protocol Bytes32 key of the protocol
@return targets Addresses of the contracts to call
@return data Calldata for the calls
*/
function getLendTXData(address _underlying, uint256 _amount, bytes32 _protocol) external view returns(address[] memory targets, bytes[] memory data) {
ILendingLogic lendingLogic = ILendingLogic(protocolToLogic[_protocol]);
require(address(lendingLogic) != address(0), "NO_LENDING_LOGIC_SET");

return lendingLogic.lend(_underlying, _amount);
}

/**
@notice Get the tx data to unlend the wrapped amount
@param _wrapped Address of the wrapped token
@param _amount Amount of wrapped token to unlend
@return targets Addresses of the contracts to call
@return data Calldata for the calls
*/
function getUnlendTXData(address _wrapped, uint256 _amount) external view returns(address[] memory targets, bytes[] memory data) {
ILendingLogic lendingLogic = ILendingLogic(protocolToLogic[wrappedToProtocol[_wrapped]]);
require(address(lendingLogic) != address(0), "NO_LENDING_LOGIC_SET");
Expand Down
Loading

0 comments on commit ca388d8

Please sign in to comment.