Skip to content

Commit

Permalink
Merge branch 'fix/access-control' of github.com:meTokens/meTokens-cor…
Browse files Browse the repository at this point in the history
…e into fix/access-control
  • Loading branch information
Carl Farterson committed Jan 14, 2022
2 parents fecd054 + 221c4b4 commit b9e011d
Show file tree
Hide file tree
Showing 35 changed files with 1,923 additions and 1,277 deletions.
51 changes: 16 additions & 35 deletions contracts/Foundry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,6 @@ contract Foundry is IFoundry, Ownable, Initializable {
if (block.timestamp > meToken_.endTime) {
hub_ = hub.getDetails(meToken_.targetHubId);
meToken_ = meTokenRegistry.finishResubscribe(_meToken);
hub_ = hub.getDetails(meToken_.hubId);
} else if (block.timestamp > meToken_.startTime) {
// Handle migration actions if needed
IMigration(meToken_.migration).poke(_meToken);
Expand All @@ -81,7 +80,7 @@ contract Foundry is IFoundry, Ownable, Initializable {
uint256 fee = (_assetsDeposited * fees.mintFee()) / PRECISION;
uint256 assetsDepositedAfterFees = _assetsDeposited - fee;

uint256 meTokensMinted = calculateMeTokensMinted(
uint256 meTokensMinted = _calculateMeTokensMinted(
_meToken,
assetsDepositedAfterFees
);
Expand All @@ -102,7 +101,7 @@ contract Foundry is IFoundry, Ownable, Initializable {
asset = targetHub_.asset;
}

vault.handleDeposit(asset, _assetsDeposited, fee, msg.sender);
vault.handleDeposit(msg.sender, asset, _assetsDeposited, fee);

meTokenRegistry.updateBalancePooled(
true,
Expand Down Expand Up @@ -169,14 +168,13 @@ contract Foundry is IFoundry, Ownable, Initializable {
) {
hub_ = hub.getDetails(meToken_.targetHubId);
meToken_ = meTokenRegistry.finishResubscribe(_meToken);
hub_ = hub.getDetails(meToken_.hubId);
}
// Calculate how many tokens are returned
uint256 rawAssetsReturned = calculateRawAssetsReturned(
uint256 rawAssetsReturned = _calculateRawAssetsReturned(
_meToken,
_meTokensBurned
);
uint256 assetsReturned = calculateActualAssetsReturned(
uint256 assetsReturned = _calculateActualAssetsReturned(
msg.sender,
_meToken,
_meTokensBurned,
Expand Down Expand Up @@ -215,7 +213,8 @@ contract Foundry is IFoundry, Ownable, Initializable {
);
}

uint256 fee = assetsReturned * feeRate;
uint256 fee = (assetsReturned * feeRate) / PRECISION;
assetsReturned = assetsReturned - fee;
IVault vault = IVault(hub_.vault);
address asset = hub_.asset;

Expand All @@ -230,41 +229,23 @@ contract Foundry is IFoundry, Ownable, Initializable {
asset = targetHub_.asset;
}

vault.handleWithdrawal(asset, assetsReturned, fee, _recipient);
vault.handleWithdrawal(_recipient, asset, assetsReturned, fee);

emit Burn(
_meToken,
asset,
msg.sender,
_recipient,
_meTokensBurned,
assetsReturned - fee
);
}

function calculateAssetsReturned(
address _sender,
address _meToken,
uint256 _meTokensBurned
) external view returns (uint256 assetsReturned) {
uint256 rawAssetsReturned = calculateRawAssetsReturned(
_meToken,
_meTokensBurned
);
assetsReturned = calculateActualAssetsReturned(
_sender,
_meToken,
_meTokensBurned,
rawAssetsReturned
assetsReturned
);
}

// NOTE: for now this does not include fees
function calculateMeTokensMinted(address _meToken, uint256 _assetsDeposited)
public
view
returns (uint256 meTokensMinted)
{
function _calculateMeTokensMinted(
address _meToken,
uint256 _assetsDeposited
) private view returns (uint256 meTokensMinted) {
Details.MeToken memory meToken_ = meTokenRegistry.getDetails(_meToken);
Details.Hub memory hub_ = hub.getDetails(meToken_.hubId);
// gas savings
Expand Down Expand Up @@ -326,10 +307,10 @@ contract Foundry is IFoundry, Ownable, Initializable {
}
}

function calculateRawAssetsReturned(
function _calculateRawAssetsReturned(
address _meToken,
uint256 _meTokensBurned
) public view returns (uint256 rawAssetsReturned) {
) private view returns (uint256 rawAssetsReturned) {
Details.MeToken memory meToken_ = meTokenRegistry.getDetails(_meToken);
Details.Hub memory hub_ = hub.getDetails(meToken_.hubId);

Expand Down Expand Up @@ -397,12 +378,12 @@ contract Foundry is IFoundry, Ownable, Initializable {
}

/// @dev applies refundRatio
function calculateActualAssetsReturned(
function _calculateActualAssetsReturned(
address _sender,
address _meToken,
uint256 _meTokensBurned,
uint256 rawAssetsReturned
) public view returns (uint256 actualAssetsReturned) {
) private view returns (uint256 actualAssetsReturned) {
Details.MeToken memory meToken_ = meTokenRegistry.getDetails(_meToken);
Details.Hub memory hub_ = hub.getDetails(meToken_.hubId);
// If msg.sender == owner, give owner the sell rate. - all of tokens returned plus a %
Expand Down
6 changes: 3 additions & 3 deletions contracts/Hub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -207,17 +207,17 @@ contract Hub is IHub, Ownable, Initializable {
}

/// @inheritdoc IHub
function getWarmup() external view override returns (uint256) {
function warmup() external view override returns (uint256) {
return _warmup;
}

/// @inheritdoc IHub
function getDuration() external view override returns (uint256) {
function duration() external view override returns (uint256) {
return _duration;
}

/// @inheritdoc IHub
function getCooldown() external view override returns (uint256) {
function cooldown() external view override returns (uint256) {
return _cooldown;
}

Expand Down
47 changes: 38 additions & 9 deletions contracts/interfaces/ICurve.sol
Original file line number Diff line number Diff line change
@@ -1,58 +1,87 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

/// @title Curve Interface
/// @title Generic Curve interface
/// @author Carl Farterson (@carlfarterson)
/// @dev Required for all Curves
interface ICurve {
event Updated(uint256 indexed hubId);
/// @notice Event when curveDetails are updated from target values to actual values
event Updated(uint256 indexed _hubId);

/// @notice Given a hub, baseX, baseY and connector weight, add the configuration to the
/// BancorZero Curve registry
/// @dev Curve need to be encoded as the Hub may register Curves for different curves
/// that may contain different Curve arguments
/// @param _hubId unique hub identifier
/// @param _encodedDetails encoded Curve arguments
/// @param _hubId unique hub identifier
/// @param _encodedDetails encoded Curve arguments
function register(uint256 _hubId, bytes calldata _encodedDetails) external;

/// @notice TODO
/// @param _hubId unique hub identifier
/// @param _encodedDetails encoded target Curve arguments
/// @notice Initialize reconfiguring curveDetails for a hub
/// @param _hubId unique hub identifier
/// @param _encodedDetails encoded target Curve arguments
function initReconfigure(uint256 _hubId, bytes calldata _encodedDetails)
external;

/// @notice Finish reconfiguring curveDetails for a hub
/// @param _hubId uinque hub identifier
function finishReconfigure(uint256 _hubId) external;

/// @notice Get curveDetails for a hub
/// @return curveDetails (TODO: curve w/ more than 4 curveDetails)
function getDetails(uint256 _hubId)
external
view
returns (uint256[4] memory);

/// @notice Calculate meTokens minted based on a curve's active details
/// @param _assetsDeposited Amount of assets deposited to the hub
/// @param _hubId unique hub identifier
/// @param _supply current meToken supply
/// @param _balancePooled area under curve
/// @return meTokensMinted amount of MeTokens minted
function viewMeTokensMinted(
uint256 _assetsDeposited,
uint256 _hubId,
uint256 _supply,
uint256 _balancePooled
) external view returns (uint256 meTokensMinted);

/// @notice Calculate assets returned based on a curve's active details
/// @param _meTokensBurned Amount of assets deposited to the hub
/// @param _hubId unique hub identifier
/// @param _supply current meToken supply
/// @param _balancePooled area under curve
/// @return assetsReturned amount of assets returned
function viewAssetsReturned(
uint256 _meTokensBurned,
uint256 _hubId,
uint256 _supply,
uint256 _balancePooled
) external view returns (uint256 assetsReturned);

/// @notice Calculate meTokens minted based on a curve's target details
/// @param _assetsDeposited Amount of assets deposited to the hub
/// @param _hubId unique hub identifier
/// @param _supply current meToken supply
/// @param _balancePooled area under curve
/// @return meTokensMinted amount of MeTokens minted
function viewTargetMeTokensMinted(
uint256 _assetsDeposited,
uint256 _hubId,
uint256 _supply,
uint256 _balancePooled
) external view returns (uint256 meTokensMinted);

/// @notice Calculate assets returned based on a curve's target details
/// @param _meTokensBurned Amount of assets deposited to the hub
/// @param _hubId unique hub identifier
/// @param _supply current meToken supply
/// @param _balancePooled area under curve
/// @return assetsReturned amount of assets returned
function viewTargetAssetsReturned(
uint256 _meTokensBurned,
uint256 _hubId,
uint256 _supply,
uint256 _balancePooled
) external view returns (uint256 assetsReturned);

function finishReconfigure(uint256 id) external;
}
36 changes: 29 additions & 7 deletions contracts/interfaces/IFees.sol
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

/// @title MeTokens protocol fee interface
/// @author Carl Farterson (@carlfarterson)
interface IFees {
function setBurnBuyerFee(uint256 amount) external;
/// @notice Set meToken protocol BurnBuyer fee
/// @param _fee new fee
function setBurnBuyerFee(uint256 _fee) external;

function setBurnOwnerFee(uint256 amount) external;
/// @notice Set meToken protocol BurnOwner fee
/// @param _fee new fee
function setBurnOwnerFee(uint256 _fee) external;

function setTransferFee(uint256 amount) external;
/// @notice Set meToken protocol Transfer fee
/// @param _fee new fee
function setTransferFee(uint256 _fee) external;

function setInterestFee(uint256 amount) external;
/// @notice Set meToken protocol Interest fee
/// @param _fee new fee
function setInterestFee(uint256 _fee) external;

function setYieldFee(uint256 amount) external;

function setOwner(address _owner) external;
/// @notice Set meToken protocol Yield fee
/// @param _fee new fee
function setYieldFee(uint256 _fee) external;

/// @notice Get mint fee
/// @return uint256 _mintFee
function mintFee() external view returns (uint256);

/// @notice Get burnBuyer fee
/// @return uint256 _burnBuyerFee
function burnBuyerFee() external view returns (uint256);

/// @notice Get burnOwner fee
/// @return uint256 _burnOwnerFee
function burnOwnerFee() external view returns (uint256);

/// @notice Get transfer fee
/// @return uint256 _transferFee
function transferFee() external view returns (uint256);

/// @notice Get interest fee
/// @return uint256 _interestFee
function interestFee() external view returns (uint256);

/// @notice Get yield fee
/// @return uint256 _yieldFee
function yieldFee() external view returns (uint256);
}
48 changes: 36 additions & 12 deletions contracts/interfaces/IFoundry.sol
Original file line number Diff line number Diff line change
@@ -1,31 +1,55 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

/// @title MeTokens foundry interface
/// @author Carl Farterson (@carlfarterson)
interface IFoundry {
/// @notice Event of minting a meToken
/// @param _meToken address of meToken minted
/// @param _asset address of asset deposited
/// @param _depositor address to deposit asset
/// @param _recipient address to receive minted meTokens
/// @param _assetsDeposited amount of assets deposited
/// @param _meTokensMinted amount of meTokens minted
event Mint(
address meToken,
address token,
address depositor,
address recipient,
uint256 assetsDeposited,
uint256 meTokensMinted
address _meToken,
address _asset,
address _depositor,
address _recipient,
uint256 _assetsDeposited,
uint256 _meTokensMinted
);

/// @notice Event of burning a meToken
/// @param _meToken address of meToken burned
/// @param _asset address of asset returned
/// @param _burner address to burn meTokens
/// @param _recipient address to receive underlying asset
/// @param _meTokensBurned amount of meTokens to burn
/// @param _assetsReturned amount of assets
event Burn(
address meToken,
address token,
address burner,
address recipient,
uint256 meTokensBurned,
uint256 assetsReturned
address _meToken,
address _asset,
address _burner,
address _recipient,
uint256 _meTokensBurned,
uint256 _assetsReturned
);

/// @notice Mint a meToken by depositing the underlying asset
/// @param _meToken address of meToken to mint
/// @param _assetsDeposited amount of assets to deposit
/// @param _recipient address to receive minted meTokens
function mint(
address _meToken,
uint256 _assetsDeposited,
address _recipient
) external;

/// @notice Burn a meToken to receive the underlying asset
/// @param _meToken address of meToken to burn
/// @param _meTokensBurned amount of meTokens to burn
/// @param _recipient address to receive the underlying assets
function burn(
address _meToken,
uint256 _meTokensBurned,
Expand Down
Loading

0 comments on commit b9e011d

Please sign in to comment.