Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
First pass at updating contracts to modular format. Further testing r… (
Browse files Browse the repository at this point in the history
#287)

* First pass at updating contracts to modular format. Further testing required.

* Removed .only

* Finished test coverage.

* Remove .onlys

* Added modules mapping to core to track approved modules and limit access to module only functions in core.

* Formatting and removed deposit and withdraw from moduleInteration, with associated tests.

* Removed module address setting and querying on rebalancingSetTokenFactory and exchangeWrappers. Added redeemModule.

* Fixed some missing dependencies.

* Added redeemModule tests. Updated redeemInternal to specify a burn address and increment address to meet all the burning variations.

* Removed Ownable dependencies, small comment changes.
  • Loading branch information
bweick authored and felix2feng committed Nov 18, 2018
1 parent 9fe5475 commit 15af3fd
Show file tree
Hide file tree
Showing 34 changed files with 1,757 additions and 227 deletions.
6 changes: 2 additions & 4 deletions contracts/core/Core.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ import { CoreAccounting } from "./extensions/CoreAccounting.sol";
import { CoreFactory } from "./extensions/CoreFactory.sol";
import { CoreInternal } from "./extensions/CoreInternal.sol";
import { CoreIssuance } from "./extensions/CoreIssuance.sol";
import { CoreIssuanceOrder } from "./extensions/CoreIssuanceOrder.sol";
import { CoreRebalanceAuction } from "./extensions/CoreRebalanceAuction.sol";
import { CoreModuleInteraction } from "./extensions/CoreModuleInteraction.sol";
import { CoreState } from "./lib/CoreState.sol";


Expand All @@ -35,8 +34,7 @@ import { CoreState } from "./lib/CoreState.sol";
/* solium-disable-next-line no-empty-blocks */
contract Core is
CoreState,
CoreIssuanceOrder,
CoreRebalanceAuction,
CoreModuleInteraction,
CoreAccounting,
CoreInternal,
CoreFactory,
Expand Down
8 changes: 4 additions & 4 deletions contracts/core/RebalancingSetToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import { IVault } from "./interfaces/IVault.sol";


/**
* @title SetToken
* @title RebalancingSetToken
* @author Set Protocol
*
* Implementation of Rebalancing Set token.
Expand Down Expand Up @@ -411,10 +411,10 @@ contract RebalancingSetToken is
external
returns (address[], uint256[], uint256[])
{
// Make sure sender is Core
// Make sure sender is a module
require(
msg.sender == IRebalancingSetFactory(factory).core(),
"RebalancingSetToken.placeBid: Sender must be core"
ICore(IRebalancingSetFactory(factory).core()).validModules(msg.sender),
"RebalancingSetToken.placeBid: Sender must be approved module"
);

// Confirm in Rebalance State
Expand Down
1 change: 1 addition & 0 deletions contracts/core/RebalancingSetTokenFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

pragma solidity 0.4.25;

import { Ownable } from "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import { RebalancingSetToken } from "./RebalancingSetToken.sol";
import { ICore } from "./interfaces/ICore.sol";
import { LibBytes } from "../external/0x/LibBytes.sol";
Expand Down
7 changes: 4 additions & 3 deletions contracts/core/exchange-wrappers/KyberNetworkWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pragma experimental "ABIEncoderV2";

import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { ERC20Wrapper as ERC20 } from "../../lib/ERC20Wrapper.sol";
import { ICore } from "../interfaces/ICore.sol";
import { KyberNetworkProxyInterface } from "../../external/KyberNetwork/KyberNetworkProxyInterface.sol";
import { LibBytes } from "../../external/0x/LibBytes.sol";

Expand Down Expand Up @@ -53,7 +54,7 @@ contract KyberNetworkWrapper {
/**
* Initialize exchange wrapper with required addresses to facilitate Kyber trades
*
* @param _core Authorized Core contract that sends Kyber trades
* @param _core Deployed Core contract
* @param _kyberNetworkProxy KyberNetwork contract for filling orders
* @param _setTransferProxy Set Protocol transfer proxy contract
*/
Expand Down Expand Up @@ -131,8 +132,8 @@ contract KyberNetworkWrapper {
returns (address[], uint256[])
{
require(
msg.sender == core,
"KyberNetworkWrapper.exchange: Sender must be core"
ICore(core).validModules(msg.sender),
"KyberNetworkWrapper.exchange: Sender must be approved module"
);

// Ensure the issuance order maker token is allowed to be transferred by KyberNetworkProxy as the source token
Expand Down
11 changes: 6 additions & 5 deletions contracts/core/exchange-wrappers/TakerWalletWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ pragma experimental "ABIEncoderV2";

import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { ERC20Wrapper } from "../../lib/ERC20Wrapper.sol";
import { ICore } from "../interfaces/ICore.sol";
import { ITransferProxy } from "../interfaces/ITransferProxy.sol";
import { LibBytes } from "../../external/0x/LibBytes.sol";

Expand All @@ -41,10 +42,10 @@ contract TakerWalletWrapper {
/* ============ Constructor ============ */

/**
* Sets the transferProxy address for the contract
* Sets the transferProxy and Core address for the contract
*
* @param _core Authorized Core contract that sends Taker transfers orders
* @param _transferProxy Address of current transferProxy
* @param _core Deployed Core contract
* @param _transferProxy Address of current transferProxy
*/
constructor(
address _core,
Expand Down Expand Up @@ -83,8 +84,8 @@ contract TakerWalletWrapper {
returns(address[], uint256[])
{
require(
msg.sender == core,
"TakerWalletWrapper.exchange: Sender must be core"
ICore(core).validModules(msg.sender),
"TakerWalletWrapper.exchange: Sender must be approved module"
);

address[] memory takerTokens = new address[](_orderCount);
Expand Down
15 changes: 8 additions & 7 deletions contracts/core/exchange-wrappers/ZeroExExchangeWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pragma experimental "ABIEncoderV2";
import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";
import { CommonMath } from "../../lib/CommonMath.sol";
import { ERC20Wrapper as ERC20 } from "../../lib/ERC20Wrapper.sol";
import { ICore } from "../interfaces/ICore.sol";
import { IExchange as ZeroExExchange } from "../../external/0x/Exchange/interfaces/IExchange.sol";
import { LibBytes } from "../../external/0x/LibBytes.sol";
import { LibFillResults as ZeroExFillResults } from "../../external/0x/Exchange/libs/LibFillResults.sol";
Expand Down Expand Up @@ -51,11 +52,11 @@ contract ZeroExExchangeWrapper {
/**
* Initialize exchange wrapper with required addresses to facilitate 0x orders
*
* @param _core Authorized Core contract that sends 0x orders
* @param _zeroExExchange 0x Exchange contract for filling orders
* @param _zeroExProxy 0x Proxy contract for transferring
* @param _zeroExToken ZRX token contract addressed used for 0x relayer fees
* @param _setTransferProxy Set Protocol transfer proxy contract
* @param _core Deployed Core contract
* @param _zeroExExchange 0x Exchange contract for filling orders
* @param _zeroExProxy 0x Proxy contract for transferring
* @param _zeroExToken ZRX token contract addressed used for 0x relayer fees
* @param _setTransferProxy Set Protocol transfer proxy contract
*/
constructor(
address _core,
Expand Down Expand Up @@ -107,8 +108,8 @@ contract ZeroExExchangeWrapper {
returns (address[], uint256[])
{
require(
msg.sender == core,
"ZeroExExchangeWrapper.exchange: Sender must be core"
ICore(core).validModules(msg.sender),
"ZeroExExchangeWrapper.exchange: Sender must be approved module"
);

// Ensure the taker token is allowed to be transferred by ZeroEx Proxy
Expand Down
30 changes: 30 additions & 0 deletions contracts/core/extensions/CoreInternal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,36 @@ contract CoreInternal is
state.exchanges[_exchangeId] = address(0);
}

/**
* Add a module address with the mapping of tracked modules.
* Can only be called by owner of Core.
*
* @param _module Address of the module
*/
function addModule(
address _module
)
external
onlyOwner
{
state.validModules[_module] = true;
}

/**
* Remove a module address with the mapping of tracked modules.
* Can only be called by owner of Core.
*
* @param _module Enumeration of module within the mapping
*/
function removeModule(
address _module
)
external
onlyOwner
{
state.validModules[_module] = false;
}

/**
* Disables a Set from the mapping and array of tracked Sets.
* Can only be called by owner of Core.
Expand Down
14 changes: 9 additions & 5 deletions contracts/core/extensions/CoreIssuance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ contract CoreIssuance is
nonReentrant
{
redeemInternal(
msg.sender,
msg.sender,
_set,
_quantity
Expand Down Expand Up @@ -197,6 +198,7 @@ contract CoreIssuance is

redeemInternal(
state.vault,
msg.sender,
_set,
_quantity
);
Expand Down Expand Up @@ -308,16 +310,18 @@ contract CoreIssuance is
/**
* Exchange Set tokens for underlying components
*
* @param _burnAddress Address to redeem and burn tokens from
* @param _set Address of the Set to redeem
* @param _quantity Number of tokens to redeem
* @param _burnAddress Address to burn tokens from
* @param _incrementAddress Address to increment component tokens to
* @param _set Address of the Set to redeem
* @param _quantity Number of tokens to redeem
*/
function redeemInternal(
address _burnAddress,
address _incrementAddress,
address _set,
uint256 _quantity
)
private
internal
{
// Verify Set was created by Core and is enabled
require(
Expand Down Expand Up @@ -365,7 +369,7 @@ contract CoreIssuance is
// Increment the component amount
vault.incrementTokenOwner(
components[i],
msg.sender,
_incrementAddress,
tokenValue
);
}
Expand Down
159 changes: 159 additions & 0 deletions contracts/core/extensions/CoreModuleInteraction.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
Copyright 2018 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pragma solidity 0.4.25;

import { ReentrancyGuard } from "openzeppelin-solidity/contracts/utils/ReentrancyGuard.sol";
import { CoreState } from "../lib/CoreState.sol";
import { ICoreAccounting } from "../interfaces/ICoreAccounting.sol";
import { ICoreIssuance } from "../interfaces/ICoreIssuance.sol";

/**
* @title CoreModularInteraction
* @author Set Protocol
*
* Extension used to expose internal accounting functions to module
*/


contract CoreModuleInteraction is
ICoreAccounting,
ICoreIssuance,
CoreState,
ReentrancyGuard
{
/**
* Exposes internal function that deposits multiple tokens to the vault, to system
* modules. Quantities should be in the order of the addresses of the tokens being
* deposited.
*
* @param _from Address to transfer tokens from
* @param _to Address to credit for deposits
* @param _tokens Array of the addresses of the tokens being deposited
* @param _quantities Array of the amounts of tokens to deposit
*/
function batchDepositModule(
address _from,
address _to,
address[] _tokens,
uint256[] _quantities
)
external
{
// Require that only modules can call function
require(
state.validModules[msg.sender],
"Core.batchDepositModule: Sender not recognized module"
);

batchDepositInternal(
_from,
_to,
_tokens,
_quantities
);
}

/**
* Exposes internal function that withdraws multiple tokens from the vault, to system
* modules. Quantities should be in the order of the addresses of the tokens being withdrawn.
*
* @param _from Address to decredit for withdrawals
* @param _to Address to transfer tokens to
* @param _tokens Array of the addresses of the tokens being withdrawn
* @param _quantities Array of the amounts of tokens to withdraw
*/
function batchWithdrawModule(
address _from,
address _to,
address[] _tokens,
uint256[] _quantities
)
external
{
// Require that only modules can call function
require(
state.validModules[msg.sender],
"Core.batchWithdrawModule: Sender not recognized module"
);

batchWithdrawInternal(
_from,
_to,
_tokens,
_quantities
);
}

/**
* Expose internal function that exchanges components for Set tokens,
* accepting any owner, to system modules
*
* @param _owner Address to issue tokens to
* @param _set Address of the Set to issue
* @param _quantity Number of tokens to issue
*/
function issueModule(
address _owner,
address _set,
uint256 _quantity
)
external
{
// Require that only modules can call function
require(
state.validModules[msg.sender],
"Core.issueModule: Sender not recognized module"
);

issueInternal(
_owner,
_set,
_quantity
);
}

/**
* Expose internal function that exchanges Set tokens for components,
* accepting any owner, to system modules
*
* @param _burnAddress Address to burn token from
* @param _incrementAddress Address to increment component tokens to
* @param _set Address of the Set to redeem
* @param _quantity Number of tokens to redeem
*/
function redeemModule(
address _burnAddress,
address _incrementAddress,
address _set,
uint256 _quantity
)
external
{
// Require that only modules can call function
require(
state.validModules[msg.sender],
"Core.redeemModule: Sender not recognized module"
);

redeemInternal(
_burnAddress,
_incrementAddress,
_set,
_quantity
);
}
}
Loading

0 comments on commit 15af3fd

Please sign in to comment.