consolidate MixinVaultCore and ZrxVault

This commit is contained in:
Michael Zhu
2019-09-23 18:22:02 -07:00
parent 1c42d0ab3c
commit dc06497cae
24 changed files with 573 additions and 959 deletions

View File

@@ -18,28 +18,41 @@
pragma solidity ^0.5.9;
import "@0x/contracts-utils/contracts/src/Authorizable.sol";
import "@0x/contracts-utils/contracts/src/LibRichErrors.sol";
import "@0x/contracts-utils/contracts/src/LibSafeMath.sol";
import "@0x/contracts-asset-proxy/contracts/src/interfaces/IAssetProxy.sol";
import "@0x/contracts-asset-proxy/contracts/src/interfaces/IAssetData.sol";
import "@0x/contracts-erc20/contracts/src/interfaces/IERC20Token.sol";
import "../interfaces/IZrxVault.sol";
import "./MixinVaultCore.sol";
import "./libs/LibStakingRichErrors.sol";
import "./interfaces/IZrxVault.sol";
/// @dev This vault manages Zrx Tokens.
/// When a user mints stake, their Zrx Tokens are deposited into this vault.
/// Similarly, when they burn stake, their Zrx Tokens are withdrawn from this vault.
/// There is a "Catastrophic Failure Mode" that, when invoked, only
/// allows withdrawals to be made. Once this vault is in catastrophic
/// failure mode, it cannot be returned to normal mode; this prevents
/// corruption of related state in the staking contract.
/// The contract also includes management of the staking contract
/// and setting the vault to "Catastrophic Failure Mode".
/// Catastrophic Failure Mode should only be set iff there is
/// non-recoverable corruption of the staking contracts. If there is a
/// recoverable flaw/bug/vulnerability, simply detach the staking contract
/// by setting its address to `address(0)`. In Catastrophic Failure Mode, only withdrawals
/// can be made (no deposits). Once Catastrophic Failure Mode is invoked,
/// it cannot be returned to normal mode; this prevents corruption of related
/// state in the staking contract.
contract ZrxVault is
IZrxVault,
MixinVaultCore
Authorizable,
IZrxVault
{
using LibSafeMath for uint256;
// mapping from Owner to ZRX balance
// Address of staking proxy contract
address payable public stakingProxyAddress;
// True iff vault has been set to Catastrophic Failure Mode
bool public isInCatastrophicFailure;
// Mapping from staker to ZRX balance
mapping (address => uint256) internal _balances;
// Zrx Asset Proxy
@@ -59,7 +72,10 @@ contract ZrxVault is
address _zrxTokenAddress
)
public
Authorizable()
{
_addAuthorizedAddress(owner);
zrxAssetProxy = IAssetProxy(_zrxProxyAddress);
_zrxToken = IERC20Token(_zrxTokenAddress);
_zrxAssetData = abi.encodeWithSelector(
@@ -68,6 +84,28 @@ contract ZrxVault is
);
}
/// @dev Sets the address of the StakingProxy contract.
/// Note that only the contract owner can call this function.
/// @param _stakingProxyAddress Address of Staking proxy contract.
function setStakingProxy(address payable _stakingProxyAddress)
external
onlyAuthorized
{
stakingProxyAddress = _stakingProxyAddress;
emit StakingProxySet(_stakingProxyAddress);
}
/// @dev Vault enters into Catastrophic Failure Mode.
/// *** WARNING - ONCE IN CATOSTROPHIC FAILURE MODE, YOU CAN NEVER GO BACK! ***
/// Note that only the contract owner can call this function.
function enterCatastrophicFailure()
external
onlyAuthorized
{
isInCatastrophicFailure = true;
emit InCatastrophicFailureMode(msg.sender);
}
/// @dev Sets the Zrx proxy.
/// Note that only an authorized address can call this function.
/// Note that this can only be called when *not* in Catastrophic Failure mode.
@@ -165,4 +203,48 @@ contract ZrxVault is
amount
);
}
modifier onlyStakingProxy() {
_assertSenderIsStakingProxy();
_;
}
modifier onlyInCatastrophicFailure() {
_assertInCatastrophicFailure();
_;
}
modifier onlyNotInCatastrophicFailure() {
_assertNotInCatastrophicFailure();
_;
}
function _assertSenderIsStakingProxy()
private
view
{
if (msg.sender != stakingProxyAddress) {
LibRichErrors.rrevert(LibStakingRichErrors.OnlyCallableByStakingContractError(
msg.sender
));
}
}
function _assertInCatastrophicFailure()
private
view
{
if (!isInCatastrophicFailure) {
LibRichErrors.rrevert(LibStakingRichErrors.OnlyCallableIfInCatastrophicFailureError());
}
}
function _assertNotInCatastrophicFailure()
private
view
{
if (isInCatastrophicFailure) {
LibRichErrors.rrevert(LibStakingRichErrors.OnlyCallableIfNotInCatastrophicFailureError());
}
}
}

View File

@@ -1,53 +0,0 @@
/*
Copyright 2019 ZeroEx Intl.
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.5.9;
/// @dev This mixin contains core logic for vaults.
/// This includes management of the staking contract
/// and setting the vault to "Catastrophic Failure Mode".
/// It's up to the vault how they handle this failure mode; however,
/// all vaults should disable all functionality aside from withdrawals.
/// Vaults should only be set to Catastrophic Failure Mode iff there is
/// non-recoverable corruption of the staking contracts. If there is a
/// recoverable flaw/bug/vulnerability, simply detach the staking contract
/// by setting its address to `address(0)`. Once in Catastrophic Failure Mode,
/// a vault cannot be reset to normal mode; this prevents corruption of related
/// state in the staking contract.
interface IVaultCore {
/// @dev Emmitted whenever a StakingProxy is set in a vault.
event StakingProxySet(address stakingProxyAddress);
/// @dev Emitted when the Staking contract is put into Catastrophic Failure Mode
/// @param sender Address of sender (`msg.sender`)
event InCatastrophicFailureMode(address sender);
/// @dev Sets the address of the StakingProxy contract.
/// Note that this is callable only by an authorized address.
/// @param _stakingProxyAddress Address of Staking proxy contract.
function setStakingProxy(address payable _stakingProxyAddress)
external;
/// @dev Vault enters into Catastrophic Failure Mode.
/// *** WARNING - ONCE IN CATOSTROPHIC FAILURE MODE, YOU CAN NEVER GO BACK! ***
/// Note that this is callable only by an authorized address.
function enterCatastrophicFailure()
external;
}

View File

@@ -22,12 +22,24 @@ pragma solidity ^0.5.9;
/// @dev This vault manages Zrx Tokens.
/// When a user mints stake, their Zrx Tokens are deposited into this vault.
/// Similarly, when they burn stake, their Zrx Tokens are withdrawn from this vault.
/// There is a "Catastrophic Failure Mode" that, when invoked, only
/// allows withdrawals to be made. Once this vault is in catastrophic
/// failure mode, it cannot be returned to normal mode; this prevents
/// corruption of related state in the staking contract.
/// The contract also includes management of the staking contract
/// and setting the vault to "Catastrophic Failure Mode".
/// Catastrophic Failure Mode should only be set iff there is
/// non-recoverable corruption of the staking contracts. If there is a
/// recoverable flaw/bug/vulnerability, simply detach the staking contract
/// by setting its address to `address(0)`. In Catastrophic Failure Mode, only withdrawals
/// can be made (no deposits). Once Catastrophic Failure Mode is invoked,
/// it cannot be returned to normal mode; this prevents corruption of related
/// state in the staking contract.
interface IZrxVault {
/// @dev Emmitted whenever a StakingProxy is set in a vault.
event StakingProxySet(address stakingProxyAddress);
/// @dev Emitted when the Staking contract is put into Catastrophic Failure Mode
/// @param sender Address of sender (`msg.sender`)
event InCatastrophicFailureMode(address sender);
/// @dev Emitted when Zrx Tokens are deposited into the vault.
/// @param staker of Zrx Tokens.
/// @param amount of Zrx Tokens deposited.
@@ -47,6 +59,18 @@ interface IZrxVault {
/// @dev Emitted whenever the ZRX AssetProxy is set.
event ZrxProxySet(address zrxProxyAddress);
/// @dev Sets the address of the StakingProxy contract.
/// Note that only the contract staker can call this function.
/// @param _stakingProxyAddress Address of Staking proxy contract.
function setStakingProxy(address payable _stakingProxyAddress)
external;
/// @dev Vault enters into Catastrophic Failure Mode.
/// *** WARNING - ONCE IN CATOSTROPHIC FAILURE MODE, YOU CAN NEVER GO BACK! ***
/// Note that only the contract staker can call this function.
function enterCatastrophicFailure()
external;
/// @dev Sets the Zrx proxy.
/// Note that only the contract staker can call this.
/// Note that this can only be called when *not* in Catastrophic Failure mode.

View File

@@ -1,103 +0,0 @@
/*
Copyright 2019 ZeroEx Intl.
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.5.9;
import "@0x/contracts-utils/contracts/src/Authorizable.sol";
import "@0x/contracts-utils/contracts/src/LibRichErrors.sol";
import "../libs/LibStakingRichErrors.sol";
import "../interfaces/IVaultCore.sol";
/// @dev This mixin contains core logic for vaults.
/// This includes management of the staking contract
/// and setting the vault to "Catastrophic Failure Mode".
/// It's up to the vault how they handle this failure mode; however,
/// all vaults should disable all functionality aside from withdrawals.
/// Vaults should only be set to Catastrophic Failure Mode iff there is
/// non-recoverable corruption of the staking contracts. If there is a
/// recoverable flaw/bug/vulnerability, simply detach the staking contract
/// by setting its address to `address(0)`. Once in Catastrophic Failure Mode,
/// a vault cannot be reset to normal mode; this prevents corruption of related
/// status in the staking contract.
contract MixinVaultCore is
Authorizable,
IVaultCore
{
// Address of staking contract
address payable public stakingProxyAddress;
// True iff vault has been set to Catastrophic Failure Mode
bool public isInCatastrophicFailure;
/// @dev Asserts that the sender (`msg.sender`) is the staking contract.
modifier onlyStakingProxy {
if (msg.sender != stakingProxyAddress) {
LibRichErrors.rrevert(LibStakingRichErrors.OnlyCallableByStakingContractError(
msg.sender
));
}
_;
}
/// @dev Asserts that this contract *is in* Catastrophic Failure Mode.
modifier onlyInCatastrophicFailure {
if (!isInCatastrophicFailure) {
LibRichErrors.rrevert(LibStakingRichErrors.OnlyCallableIfInCatastrophicFailureError());
}
_;
}
/// @dev Asserts that this contract *is not in* Catastrophic Failure Mode.
modifier onlyNotInCatastrophicFailure {
if (isInCatastrophicFailure) {
LibRichErrors.rrevert(LibStakingRichErrors.OnlyCallableIfNotInCatastrophicFailureError());
}
_;
}
/// @dev Sets the vault owner and adds owner as an authorized address.
constructor()
public
Authorizable()
{
_addAuthorizedAddress(owner);
}
/// @dev Sets the address of the StakingProxy contract.
/// Note that only an authorized address can call this function.
/// @param _stakingProxyAddress Address of Staking proxy contract.
function setStakingProxy(address payable _stakingProxyAddress)
external
onlyAuthorized
{
stakingProxyAddress = _stakingProxyAddress;
emit StakingProxySet(_stakingProxyAddress);
}
/// @dev Vault enters into Catastrophic Failure Mode.
/// *** WARNING - ONCE IN CATOSTROPHIC FAILURE MODE, YOU CAN NEVER GO BACK! ***
/// Note that only an authorized address can call this function.
function enterCatastrophicFailure()
external
onlyAuthorized
{
isInCatastrophicFailure = true;
emit InCatastrophicFailureMode(msg.sender);
}
}

View File

@@ -1,44 +0,0 @@
/*
Copyright 2019 ZeroEx Intl.
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.5.9;
import "../src/vaults/MixinVaultCore.sol";
// solhint-disable no-empty-blocks
contract TestMixinVaultCore is
MixinVaultCore
{
function assertStakingProxy()
external
view
onlyStakingProxy
{}
function assertInCatastrophicFailure()
external
view
onlyInCatastrophicFailure
{}
function assertNotInCatastrophicFailure()
external
view
onlyNotInCatastrophicFailure
{}
}