Skip to content

Commit

Permalink
Merge pull request #10 from meTokens/9-01
Browse files Browse the repository at this point in the history
9-01
  • Loading branch information
Carl Farterson authored Sep 1, 2021
2 parents 714f5d8 + 93e0258 commit 7546061
Show file tree
Hide file tree
Showing 10 changed files with 74 additions and 676 deletions.
36 changes: 14 additions & 22 deletions contracts/Foundry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ import "./interfaces/ICurveValueSet.sol";
import "./interfaces/IVault.sol";
import "./interfaces/IHub.sol";
import "./interfaces/IFoundry.sol";

import "./libs/WeightedAverage.sol";
import "./libs/Details.sol";


contract Foundry is IFoundry, Ownable, Initializable {

Expand All @@ -38,34 +41,23 @@ contract Foundry is IFoundry, Ownable, Initializable {
/// @inheritdoc IFoundry
function mint(address _meToken, uint256 _collateralDeposited, address _recipient) external override {

IMeTokenRegistry.Details memory details = meTokenRegistry.getDetails(_meToken);

require(hub.getStatus(details.id) != 0, "Hub inactive");

IERC20 meToken = IERC20(_meToken);
ICurveValueSet curve = ICurveValueSet(hub.getCurve(details.id));
IVault vault = IVault(hub.getVault(details.id));
IMeTokenRegistry.Details memory meTokenDetails = meTokenRegistry.getDetails(_meToken);
HubDetails memory hubDetails = hub.getDetails(meTokenDetails.id);
require(hubDetails.active, "Hub inactive");

uint256 fee = _collateralDeposited * fees.mintFee() / PRECISION;
uint256 collateralDepositedAfterFees = _collateralDeposited - fee;

// Calculate how much meToken is minted
// NOTE: this is what i want
// uint256 meTokensMinted = curve.calculateMintReturn(
// meToken (address)
// collateralDepositedAfterFees
// )

uint256 meTokensMinted = curve.calculateMintReturn(
uint256 meTokensMinted = ICurveValueSet(hubDetails.curve).calculateMintReturn(
collateralDepositedAfterFees,
details.id,
meToken.totalSupply(),
details.balancePooled
meTokenDetails.id,
IERC20(meToken).totalSupply(),
meTokenDetails.balancePooled
);

// Send collateral to vault and update balance pooled
IERC20 collateralToken = IERC20(vault.getCollateralAsset());
collateralToken.transferFrom(msg.sender, address(this), _collateralDeposited);
address collateralToken = IVault(hubDetails.vault).getCollateralAsset());
IERC20(collateralToken).transferFrom(msg.sender, address(this), _collateralDeposited);

meTokenRegistry.incrementBalancePooled(
true,
Expand All @@ -74,7 +66,7 @@ contract Foundry is IFoundry, Ownable, Initializable {
);

// Transfer fees
if (fee > 0) {vault.addFee(fee);}
if (fee > 0) {IVault(hubDetails.vault).addFee(fee);}

// Mint meToken to user
meToken.mint(_recipient, meTokensMinted);
Expand All @@ -86,7 +78,7 @@ contract Foundry is IFoundry, Ownable, Initializable {

IMeTokenRegistry.Details memory details = meTokenRegistry.getDetails(_meToken);

require(hub.getStatus(details.id) != 0, "Hub inactive");
require(hub.isActive(details.id), "Hub inactive");

ICurveValueSet curve = ICurveValueSet(hub.getCurve(details.id));
IVault vault = IVault(hub.getVault(details.id));
Expand Down
71 changes: 17 additions & 54 deletions contracts/Hub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ import "./interfaces/ICurveRegistry.sol";
import "./interfaces/ICurveValueSet.sol";
// import "./interfaces/IUpdater.sol";

import "./libs/Status.sol";
import "./libs/Details.sol";


/// @title meToken hub
/// @author Carl Farterson (@carlfarterson)
Expand All @@ -38,13 +39,10 @@ contract Hub is Ownable, Initializable {
address vault;
address curve;
uint refundRatio;
Status status;
bool active;
}

mapping(uint => Details) private hubs;

// TODO: ensure this is properly checked
enum Status { INACTIVE, ACTIVE, QUEUED, UPDATING}
mapping(uint => HubDetails) private hubs;

constructor() {}

Expand Down Expand Up @@ -89,23 +87,20 @@ contract Hub is Ownable, Initializable {
address vault = IVaultFactory(_vaultFactory).create(_vaultName, _collateralAsset, _encodedVaultAdditionalArgs);

// Save the hub to the registry
hubs[count++] = Details(
_name,
_owner,
address(vault),
_curve,
_refundRatio,
Status.ACTIVE
);
HubDetails storage hubDetails = HubDetails({
name:_name,
owner:_owner,
active: true,
vault:address(vault),
curve:_curve,
refundRatio:_refundRatio
});
hubs[count++] = hubDetails;
}


function deactivate(uint id) external exists(id) {
// TODO: access control
Details storage details = hubs[id];

require(details.status == Status.ACTIVE, "Hub not active");
details.status = Status.INACTIVE;
// emit Deactivate(id);
}

Expand Down Expand Up @@ -139,27 +134,6 @@ contract Hub is Ownable, Initializable {
if (shifting != 0) {
details.refundRatio = shifting;
}
details.status = Status.ACTIVE;
}


function setStatus(
uint id,
uint status
) public returns (bool) {
// TODO: access control
// Can only be from Foundry when setting to QUEUED > UPDATING,
// Or from Updater when setting a curve to QUEUED
// require(
// msg.sender == address(updater) || msg.sender == foundry,
// "!updater && !foundry"
// );

Details storage details = hubs[id];
require(uint(details.status) != status, "Cannot set to same status");
details.status = Status(status);
// emit SetStatus(id, status);
return true;
}

function getCount() external view returns (uint) {return count;}
Expand All @@ -170,9 +144,9 @@ contract Hub is Ownable, Initializable {
}


function getStatus(uint id) public view returns (uint) {
function isActive(uint id) public view returns (bool) {
Details memory details = hubs[id];
return uint(details.status);
return details.active;
}


Expand All @@ -185,20 +159,9 @@ contract Hub is Ownable, Initializable {
function getDetails(
uint id
) external view exists(id) returns (
string memory name,
address owner,
address vault,
address curve_,
uint refundRatio,
uint status
HubDetails memory hubDetails
) {
Details memory details = hubs[id];
name = details.name;
owner = details.owner;
vault = details.vault;
curve_ = details.curve;
refundRatio = details.refundRatio;
status = uint(details.status);
hubDetails = hubs[id];
}

function getCurve(uint id) external view returns (address) {
Expand Down
12 changes: 2 additions & 10 deletions contracts/interfaces/IHub.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ pragma solidity ^0.8.0;
interface IHub {

event Register(string name, address indexed vault); // TODO: decide on arguments
event SetStatus(uint id, uint status);
event Deactivate(uint id);

/*
Expand Down Expand Up @@ -68,7 +67,6 @@ interface IHub {
address vault,
address curve,
uint refundRatio,
uint status
);

/// @notice Helper to fetch only owner of hubDetails
Expand All @@ -94,12 +92,6 @@ interface IHub {

/// @notice TODO
/// @param id Unique hub identifier
/// @return uint Indexed position of hub status
function getStatus(uint id) external view returns (uint);

/// @notice Function to modify a hubs' Status
/// @param id Unique hub identifier
/// @param status Target Status index position.
/// @return true/false that function was successful
function setStatus(uint id, uint status) external returns (bool);
/// @return bool is the hub active?
function isActive(uint id) external view returns (bool);
}
21 changes: 21 additions & 0 deletions contracts/libs/Details.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;

struct HubDetails {
string name;
address owner;
bool active;

address vault;
address curve;
uint refundRatio;

bool updating;
uint startTime;
uint endTime;

address migrationVault;
address targetVault;
address targetCurve;
uint targetRefundRatio;
}
12 changes: 0 additions & 12 deletions contracts/libs/Status.sol

This file was deleted.

7 changes: 6 additions & 1 deletion contracts/registries/MeTokenRegistry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ import "../interfaces/IHub.sol";
import "../interfaces/IVault.sol";
import "../interfaces/ICurveValueSet.sol";

import "../libs/Details.sol";


/// @title meToken registry
/// @author Carl Farterson (@carlfarterson)
/// @notice This contract tracks basic information about all meTokens
Expand Down Expand Up @@ -36,7 +39,9 @@ contract MeTokenRegistry is IMeTokenRegistry, Roles {
) external override {
// TODO: access control
require(!isOwner(msg.sender), "msg.sender already owns a meToken");
require(hub.getStatus(_hubId) != 0, "Hub inactive"); // TODO: validate
HubDetails memory hubDetails = hub.getDetails[_hubId];

require(hubDetails[(_hubId) != 0, "Hub inactive"); // TODO: validate

// Initial collateral deposit from owner by finding the vault,
// and then the collateral asset tied to that vault
Expand Down
Loading

0 comments on commit 7546061

Please sign in to comment.