Skip to content

Commit

Permalink
Merge pull request #122 from meTokens/refactor/cleanup
Browse files Browse the repository at this point in the history
Refactor/cleanup
  • Loading branch information
Carter Carlson authored Feb 26, 2022
2 parents 6018c93 + ea6b54a commit 9e98ea8
Show file tree
Hide file tree
Showing 64 changed files with 1,676 additions and 2,043 deletions.
2 changes: 1 addition & 1 deletion .solhintignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
contracts/interfaces
contracts/libs/Details.sol
contracts/libs/LibAppStorage.sol
Power.sol
ode_modules
ABDKMathQuad.sol
2 changes: 1 addition & 1 deletion contracts/Diamond.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pragma solidity ^0.8.0;

import {LibDiamond} from "./libs/LibDiamond.sol";
import {IDiamondCut} from "./interfaces/IDiamondCut.sol";
import {LibAppStorage} from "./libs/Details.sol";
import {LibAppStorage} from "./libs/LibAppStorage.sol";

contract Diamond {
constructor(address firstController, address diamondCutFacet) payable {
Expand Down
15 changes: 11 additions & 4 deletions contracts/DiamondInit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@
pragma solidity ^0.8.0;

import {IERC165} from "@openzeppelin/contracts/utils/introspection/IERC165.sol";
import {IERC173} from "./interfaces/IERC173.sol";
import {IRegistry} from "./interfaces/IRegistry.sol";
import {IMigrationRegistry} from "./interfaces/IMigrationRegistry.sol";
import {IDiamondCut} from "./interfaces/IDiamondCut.sol";
import {IDiamondLoupe} from "./interfaces/IDiamondLoupe.sol";
import {LibDiamond} from "./libs/LibDiamond.sol";
import {AppStorage} from "./libs/Details.sol";
import {AppStorage} from "./libs/LibAppStorage.sol";

/// @title Diamond Init
/// @author Carter Carlson (@cartercarlson), @zgorizzo69
/// @notice Contract to initialize state variables, similar to OZ's initilize()
contract DiamondInit {
struct Args {
uint256 mintFee;
Expand All @@ -25,10 +27,16 @@ contract DiamondInit {
address meTokenFactory;
}

address private immutable _owner;

constructor() {
_owner = msg.sender;
}

AppStorage internal s; // solhint-disable-line

// TODO: access control?
function init(Args memory _args) external {
require(msg.sender == _owner, "!owner");
s.diamond = _args.diamond;
s.vaultRegistry = _args.vaultRegistry;
s.curveRegistry = _args.curveRegistry;
Expand All @@ -50,6 +58,5 @@ contract DiamondInit {
ds.supportedInterfaces[type(IDiamondCut).interfaceId] = true;
ds.supportedInterfaces[type(IDiamondLoupe).interfaceId] = true;
ds.supportedInterfaces[type(IERC165).interfaceId] = true;
ds.supportedInterfaces[type(IERC173).interfaceId] = true;
}
}
4 changes: 2 additions & 2 deletions contracts/MeToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ pragma solidity ^0.8.0;
import {ERC20Burnable} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Burnable.sol";
import {ERC20} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";

/// @title meToken
/// @author Carl Farterson (@carlfarterson)
/// @title MeToken
/// @author Carter Carlson (@cartercarlson)
/// @notice Base erc20-like meToken contract used for all meTokens
contract MeToken is ERC20Burnable {
string public version;
Expand Down
10 changes: 3 additions & 7 deletions contracts/MeTokenFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,15 @@ pragma solidity ^0.8.0;
import {IMeTokenFactory} from "./interfaces/IMeTokenFactory.sol";
import {MeToken} from "./MeToken.sol";

/// @title meToken factory
/// @author Carl Farterson (@carlfarterson)
/// @notice This contract creates and deploys a users' meToken
/// @title MeToken factory
/// @author Carter Carlson (@cartercarlson)
/// @notice This contract creates and deploys a meToken, owned by an address
contract MeTokenFactory is IMeTokenFactory {
/// @notice create a meToken
/// @param name name of meToken
/// @param symbol symbol of meToken
function create(
string calldata name,
string calldata symbol,
address diamond
) external override returns (address) {
// Create our meToken
MeToken erc20 = new MeToken(name, symbol, diamond);
return address(erc20);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ import {ICurve} from "../interfaces/ICurve.sol";
import {ABDKMathQuad} from "../utils/ABDKMathQuad.sol";

/// @title Bancor curve registry and calculator
/// @author Carl Farterson (@carlfarterson), Chris Robison (@CBobRobison), @zgorizzo69
contract BancorABDK is ICurve {
/// @author Carter Carlson (@cartercarlson), Chris Robison (@CBobRobison), @zgorizzo69
contract BancorCurve is ICurve {
using ABDKMathQuad for uint256;
using ABDKMathQuad for bytes16;

struct Bancor {
struct CurveInfo {
uint256 baseY;
uint256 targetBaseY;
uint32 reserveWeight;
Expand All @@ -24,23 +24,23 @@ contract BancorABDK is ICurve {
address public hub;

// NOTE: keys are their respective hubId
mapping(uint256 => Bancor) private _bancors;
mapping(uint256 => CurveInfo) private _curves;

constructor(address _hub) {
require(_hub != address(0), "!hub");
hub = _hub;
}

/// @inheritdoc ICurve
function register(uint256 hubId, bytes calldata encodedDetails)
function register(uint256 hubId, bytes calldata encodedCurveInfo)
external
override
{
require(msg.sender == hub, "!hub");
require(encodedDetails.length > 0, "!encodedDetails");
require(encodedCurveInfo.length > 0, "!encodedCurveInfo");

(uint256 baseY, uint32 reserveWeight) = abi.decode(
encodedDetails,
encodedCurveInfo,
(uint256, uint32)
);
require(baseY > 0, "!baseY");
Expand All @@ -49,64 +49,64 @@ contract BancorABDK is ICurve {
"!reserveWeight"
);

Bancor storage bancor = _bancors[hubId];
bancor.baseY = baseY;
bancor.reserveWeight = reserveWeight;
CurveInfo storage curveInfo = _curves[hubId];
curveInfo.baseY = baseY;
curveInfo.reserveWeight = reserveWeight;
}

/// @inheritdoc ICurve
function initReconfigure(uint256 hubId, bytes calldata encodedDetails)
function initReconfigure(uint256 hubId, bytes calldata encodedCurveInfo)
external
override
{
require(msg.sender == hub, "!hub");

uint32 targetReserveWeight = abi.decode(encodedDetails, (uint32));
Bancor storage bancor = _bancors[hubId];
uint32 targetReserveWeight = abi.decode(encodedCurveInfo, (uint32));
CurveInfo storage curveInfo = _curves[hubId];

require(targetReserveWeight > 0, "!reserveWeight");
require(
targetReserveWeight != bancor.reserveWeight,
targetReserveWeight != curveInfo.reserveWeight,
"targetWeight!=Weight"
);

// targetBaseX = (old baseY * oldR) / newR
uint256 targetBaseY = (bancor.baseY * bancor.reserveWeight) /
uint256 targetBaseY = (curveInfo.baseY * curveInfo.reserveWeight) /
targetReserveWeight;
bancor.targetBaseY = targetBaseY;
bancor.targetReserveWeight = targetReserveWeight;
curveInfo.targetBaseY = targetBaseY;
curveInfo.targetReserveWeight = targetReserveWeight;
}

/// @inheritdoc ICurve
function finishReconfigure(uint256 hubId) external override {
require(msg.sender == hub, "!hub");
Bancor storage bancor = _bancors[hubId];
bancor.reserveWeight = bancor.targetReserveWeight;
bancor.baseY = bancor.targetBaseY;
bancor.targetReserveWeight = 0;
bancor.targetBaseY = 0;
CurveInfo storage curveInfo = _curves[hubId];
curveInfo.reserveWeight = curveInfo.targetReserveWeight;
curveInfo.baseY = curveInfo.targetBaseY;
curveInfo.targetReserveWeight = 0;
curveInfo.targetBaseY = 0;
}

function getBancorDetails(uint256 hubId)
function getCurveInfoBancor(uint256 hubId)
external
view
returns (Bancor memory)
returns (CurveInfo memory)
{
return _bancors[hubId];
return _curves[hubId];
}

/// @inheritdoc ICurve
function getCurveDetails(uint256 hubId)
function getCurveInfo(uint256 hubId)
external
view
override
returns (uint256[4] memory)
{
return [
_bancors[hubId].baseY,
uint256(_bancors[hubId].reserveWeight),
_bancors[hubId].targetBaseY,
uint256(_bancors[hubId].targetReserveWeight)
_curves[hubId].baseY,
uint256(_curves[hubId].reserveWeight),
_curves[hubId].targetBaseY,
uint256(_curves[hubId].targetReserveWeight)
];
}

Expand All @@ -117,20 +117,20 @@ contract BancorABDK is ICurve {
uint256 supply,
uint256 balancePooled
) external view override returns (uint256 meTokensMinted) {
Bancor memory bancor = _bancors[hubId];
CurveInfo memory curveInfo = _curves[hubId];

if (supply > 0) {
meTokensMinted = _viewMeTokensMinted(
assetsDeposited,
bancor.reserveWeight,
curveInfo.reserveWeight,
supply,
balancePooled
);
} else {
meTokensMinted = _viewMeTokensMintedFromZero(
assetsDeposited,
bancor.reserveWeight,
bancor.baseY
curveInfo.reserveWeight,
curveInfo.baseY
);
}
}
Expand All @@ -142,19 +142,19 @@ contract BancorABDK is ICurve {
uint256 supply,
uint256 balancePooled
) external view override returns (uint256 meTokensMinted) {
Bancor memory bancor = _bancors[hubId];
CurveInfo memory curveInfo = _curves[hubId];
if (supply > 0) {
meTokensMinted = _viewMeTokensMinted(
assetsDeposited,
bancor.targetReserveWeight,
curveInfo.targetReserveWeight,
supply,
balancePooled
);
} else {
meTokensMinted = _viewMeTokensMintedFromZero(
assetsDeposited,
bancor.targetReserveWeight,
bancor.targetBaseY
curveInfo.targetReserveWeight,
curveInfo.targetBaseY
);
}
}
Expand All @@ -166,10 +166,10 @@ contract BancorABDK is ICurve {
uint256 supply,
uint256 balancePooled
) external view override returns (uint256 assetsReturned) {
Bancor memory bancor = _bancors[hubId];
CurveInfo memory curveInfo = _curves[hubId];
assetsReturned = _viewAssetsReturned(
meTokensBurned,
bancor.reserveWeight,
curveInfo.reserveWeight,
supply,
balancePooled
);
Expand All @@ -182,10 +182,10 @@ contract BancorABDK is ICurve {
uint256 supply,
uint256 balancePooled
) external view override returns (uint256 assetsReturned) {
Bancor memory bancor = _bancors[hubId];
CurveInfo memory curveInfo = _curves[hubId];
assetsReturned = _viewAssetsReturned(
meTokensBurned,
bancor.targetReserveWeight,
curveInfo.targetReserveWeight,
supply,
balancePooled
);
Expand Down
Loading

0 comments on commit 9e98ea8

Please sign in to comment.