Skip to content
This repository has been archived by the owner on Jul 9, 2021. It is now read-only.

Staking params upgradability #2166

Merged
merged 11 commits into from
Sep 17, 2019
4 changes: 2 additions & 2 deletions contracts/coordinator/test/libs.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addressUtils, chaiSetup, constants, provider, txDefaults, web3Wrapper } from '@0x/contracts-test-utils';
import { chaiSetup, constants, provider, randomAddress, txDefaults, web3Wrapper } from '@0x/contracts-test-utils';
import { BlockchainLifecycle } from '@0x/dev-utils';
import { transactionHashUtils } from '@0x/order-utils';
import { BigNumber, providerUtils } from '@0x/utils';
Expand All @@ -13,7 +13,7 @@ const blockchainLifecycle = new BlockchainLifecycle(web3Wrapper);
describe('Libs tests', () => {
let coordinatorContract: CoordinatorContract;
let chainId: number;
const exchangeAddress = addressUtils.generatePseudoRandomAddress();
const exchangeAddress = randomAddress();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏


before(async () => {
await blockchainLifecycle.startAsync();
Expand Down
4 changes: 2 additions & 2 deletions contracts/coordinator/test/mixins.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { constants as exchangeConstants, exchangeDataEncoder, ExchangeFunctionName } from '@0x/contracts-exchange';
import {
addressUtils,
chaiSetup,
constants,
expectContractCallFailedAsync,
getLatestBlockTimestampAsync,
provider,
randomAddress,
TransactionFactory,
txDefaults,
web3Wrapper,
Expand Down Expand Up @@ -33,7 +33,7 @@ describe('Mixins tests', () => {
let approvalFactory1: ApprovalFactory;
let approvalFactory2: ApprovalFactory;
let defaultOrder: SignedOrder;
const exchangeAddress = addressUtils.generatePseudoRandomAddress();
const exchangeAddress = randomAddress();
let exchangeDomain: EIP712DomainWithDefaultSchema;

before(async () => {
Expand Down
4 changes: 2 additions & 2 deletions contracts/exchange-libs/test/lib_eip712_exchange_domain.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { addressUtils, blockchainTests, constants, expect } from '@0x/contracts-test-utils';
import { blockchainTests, constants, expect, randomAddress } from '@0x/contracts-test-utils';
import { BigNumber, signTypedDataUtils } from '@0x/utils';
import * as ethUtil from 'ethereumjs-util';

Expand Down Expand Up @@ -28,7 +28,7 @@ blockchainTests('LibEIP712ExchangeDomain', env => {
});
it('should calculate the correct domain hash when verifyingContractAddressIfExists is set to a non-null address', async () => {
const chainId = 1;
const verifyingContractAddress = addressUtils.generatePseudoRandomAddress();
const verifyingContractAddress = randomAddress();
const libEIP712ExchangeDomainContract = await TestLibEIP712ExchangeDomainContract.deployFrom0xArtifactAsync(
artifacts.TestLibEIP712ExchangeDomain,
env.provider,
Expand Down
14 changes: 7 additions & 7 deletions contracts/exchange/test/lib_exchange_rich_error_decoder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import {
addressUtils,
blockchainTests,
constants,
expect,
hexRandom,
OrderStatus,
orderUtils,
randomAddress,
} from '@0x/contracts-test-utils';
import { ExchangeRevertErrors, generatePseudoRandomSalt } from '@0x/order-utils';
import { BigNumber, RevertError } from '@0x/utils';
Expand Down Expand Up @@ -59,8 +59,8 @@ blockchainTests.resets('LibExchangeRichErrorDecoder', ({ provider, txDefaults })
(() => {
const errorCode = ExchangeRevertErrors.SignatureErrorCode.Illegal;
const orderHash = orderUtils.generatePseudoRandomOrderHash();
const signer = addressUtils.generatePseudoRandomAddress();
const validator = addressUtils.generatePseudoRandomAddress();
const signer = randomAddress();
const validator = randomAddress();
const data = hexRandom(ERROR_DATA_LENGTH);
const signature = hexRandom(SIGNATURE_LENGTH);
const errorData = hexRandom(ERROR_DATA_LENGTH);
Expand All @@ -78,7 +78,7 @@ blockchainTests.resets('LibExchangeRichErrorDecoder', ({ provider, txDefaults })

(() => {
const orderHash = orderUtils.generatePseudoRandomOrderHash();
const address = addressUtils.generatePseudoRandomAddress();
const address = randomAddress();
createDecodeTest(ExchangeRevertErrors.ExchangeInvalidContextError, [
ExchangeRevertErrors.ExchangeContextErrorCodes.InvalidMaker,
orderHash,
Expand All @@ -103,14 +103,14 @@ blockchainTests.resets('LibExchangeRichErrorDecoder', ({ provider, txDefaults })
})();

(() => {
const maker = addressUtils.generatePseudoRandomAddress();
const sender = addressUtils.generatePseudoRandomAddress();
const maker = randomAddress();
const sender = randomAddress();
const currentEpoch = generatePseudoRandomSalt();
createDecodeTest(ExchangeRevertErrors.OrderEpochError, [maker, sender, currentEpoch]);
})();

(() => {
const assetProxyAddress = addressUtils.generatePseudoRandomAddress();
const assetProxyAddress = randomAddress();
createDecodeTest(ExchangeRevertErrors.AssetProxyExistsError, [
hexRandom(ASSET_PROXY_ID_LENGTH),
assetProxyAddress,
Expand Down
12 changes: 6 additions & 6 deletions contracts/exchange/test/signature_validator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import {
addressUtils,
blockchainTests,
constants,
expect,
Expand All @@ -8,6 +7,7 @@ import {
LogDecoder,
OrderFactory,
orderUtils,
randomAddress,
TransactionFactory,
} from '@0x/contracts-test-utils';
import {
Expand Down Expand Up @@ -428,11 +428,11 @@ blockchainTests.resets('MixinSignatureValidator', env => {
const defaultOrderParams = {
...constants.STATIC_ORDER_PARAMS,
makerAddress,
feeRecipientAddress: addressUtils.generatePseudoRandomAddress(),
makerAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()),
takerAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()),
makerFeeAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()),
takerFeeAssetData: assetDataUtils.encodeERC20AssetData(addressUtils.generatePseudoRandomAddress()),
feeRecipientAddress: randomAddress(),
makerAssetData: assetDataUtils.encodeERC20AssetData(randomAddress()),
takerAssetData: assetDataUtils.encodeERC20AssetData(randomAddress()),
makerFeeAssetData: assetDataUtils.encodeERC20AssetData(randomAddress()),
takerFeeAssetData: assetDataUtils.encodeERC20AssetData(randomAddress()),
makerFee: constants.ZERO_AMOUNT,
takerFee: constants.ZERO_AMOUNT,
domain: {
Expand Down
19 changes: 16 additions & 3 deletions contracts/staking/contracts/src/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ contract Staking is
MixinStakingPool,
MixinExchangeFees
{

// this contract can receive ETH
// solhint-disable no-empty-blocks
function ()
Expand All @@ -44,13 +43,27 @@ contract Staking is
/// @dev Initialize storage owned by this contract.
/// This function should not be called directly.
/// The StakingProxy contract will call it in `attachStakingContract()`.
function init()
/// @param _wethProxyAddress The address that can transfer WETH for fees.
/// @param _ethVaultAddress Address of the EthVault contract.
/// @param _rewardVaultAddress Address of the StakingPoolRewardVault contract.
/// @param _zrxVaultAddress Address of the ZrxVault contract.
function init(
address _wethProxyAddress,
address _ethVaultAddress,
address payable _rewardVaultAddress,
address _zrxVaultAddress
)
external
onlyOwner
{
// DANGER! When performing upgrades, take care to modify this logic
// to prevent accidentally clearing prior state.
_initMixinScheduler();
_initMixinParams();
_initMixinParams(
_wethProxyAddress,
_ethVaultAddress,
_rewardVaultAddress,
_zrxVaultAddress
);
}
}
79 changes: 67 additions & 12 deletions contracts/staking/contracts/src/StakingProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,28 @@ contract StakingProxy is
/// @param _stakingContract Staking contract to delegate calls to.
/// @param _readOnlyProxy The address of the read only proxy.
/// @param _wethProxyAddress The address that can transfer WETH for fees.
constructor(address _stakingContract, address _readOnlyProxy, address _wethProxyAddress)
/// @param _ethVaultAddress Address of the EthVault contract.
/// @param _rewardVaultAddress Address of the StakingPoolRewardVault contract.
/// @param _zrxVaultAddress Address of the ZrxVault contract.
constructor(
address _stakingContract,
address _readOnlyProxy,
address _wethProxyAddress,
address _ethVaultAddress,
address _rewardVaultAddress,
address _zrxVaultAddress
)
public
MixinStorage()
{
readOnlyProxy = _readOnlyProxy;
wethAssetProxy = IAssetProxy(_wethProxyAddress);
_attachStakingContract(_stakingContract);
_attachStakingContract(
_stakingContract,
_wethProxyAddress,
_ethVaultAddress,
_rewardVaultAddress,
_zrxVaultAddress
);
}

/// @dev Delegates calls to the staking contract, if it is set.
Expand All @@ -58,12 +73,32 @@ contract StakingProxy is

/// @dev Attach a staking contract; future calls will be delegated to the staking contract.
/// Note that this is callable only by this contract's owner.
/// @param _stakingContract Address of staking contract.
function attachStakingContract(address _stakingContract)
/// @param _stakingContract Address of staking contract.
/// @param _wethProxyAddress The address that can transfer WETH for fees.
/// Use address in storage if NIL_ADDRESS is passed in.
/// @param _ethVaultAddress Address of the EthVault contract.
/// Use address in storage if NIL_ADDRESS is passed in.
/// @param _rewardVaultAddress Address of the StakingPoolRewardVault contract.
/// Use address in storage if NIL_ADDRESS is passed in.
/// @param _zrxVaultAddress Address of the ZrxVault contract.
/// Use address in storage if NIL_ADDRESS is passed in.
function attachStakingContract(
address _stakingContract,
address _wethProxyAddress,
address _ethVaultAddress,
address _rewardVaultAddress,
address _zrxVaultAddress
)
external
onlyOwner
{
_attachStakingContract(_stakingContract);
_attachStakingContract(
_stakingContract,
_wethProxyAddress == NIL_ADDRESS ? address(wethAssetProxy) : _wethProxyAddress,
_ethVaultAddress == NIL_ADDRESS ? address(ethVault) : _ethVaultAddress,
_rewardVaultAddress == NIL_ADDRESS ? address(rewardVault) : _rewardVaultAddress,
_zrxVaultAddress == NIL_ADDRESS ? address(zrxVault) : _zrxVaultAddress
);
}

/// @dev Detach the current staking contract.
Expand All @@ -86,7 +121,6 @@ contract StakingProxy is
} else {
stakingContract = readOnlyProxyCallee;
}

emit ReadOnlyModeSet(readOnlyMode);
}

Expand Down Expand Up @@ -132,16 +166,37 @@ contract StakingProxy is

/// @dev Attach a staking contract; future calls will be delegated to the staking contract.
/// @param _stakingContract Address of staking contract.
function _attachStakingContract(address _stakingContract)
/// @param _wethProxyAddress The address that can transfer WETH for fees.
/// @param _ethVaultAddress Address of the EthVault contract.
/// @param _rewardVaultAddress Address of the StakingPoolRewardVault contract.
/// @param _zrxVaultAddress Address of the ZrxVault contract.
function _attachStakingContract(
address _stakingContract,
address _wethProxyAddress,
address _ethVaultAddress,
address _rewardVaultAddress,
address _zrxVaultAddress
)
private
{
// Attach the staking contract
stakingContract = readOnlyProxyCallee = _stakingContract;
emit StakingContractAttachedToProxy(_stakingContract);

// Call `init()` on the staking contract to initialize storage.
(bool didInitSucceed, bytes memory initReturnData) =
_stakingContract.delegatecall(abi.encode(IStorageInit(0).init.selector));
(bool didInitSucceed, bytes memory initReturnData) = stakingContract.delegatecall(
abi.encodeWithSelector(
IStorageInit(0).init.selector,
_wethProxyAddress,
_ethVaultAddress,
_rewardVaultAddress,
_zrxVaultAddress
)
);
if (!didInitSucceed) {
assembly { revert(add(initReturnData, 0x20), mload(initReturnData)) }
assembly {
revert(add(initReturnData, 0x20), mload(initReturnData))
}
}
emit StakingContractAttachedToProxy(_stakingContract);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ contract MixinExchangeFees is
totalWeightedStake != 0 ? activePools[i].weightedStake : 1, // only rewards are accounted for if no one has staked
totalWeightedStake != 0 ? totalWeightedStake : 1, // this is to avoid divide-by-zero in cobb douglas
cobbDouglasAlphaNumerator,
cobbDouglasAlphaDenomintor
cobbDouglasAlphaDenominator
);

// record reward in vault
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,5 +129,5 @@ contract MixinStorage is
uint32 public cobbDouglasAlphaNumerator;

// Denominator for cobb douglas alpha factor.
uint32 public cobbDouglasAlphaDenomintor;
uint32 public cobbDouglasAlphaDenominator;
}
20 changes: 11 additions & 9 deletions contracts/staking/contracts/src/interfaces/IStakingEvents.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,22 @@ interface IStakingEvents {
/// @param minimumPoolStake Minimum amount of stake required in a pool to collect rewards.
/// @param maximumMakersInPool Maximum number of maker addresses allowed to be registered to a pool.
/// @param cobbDouglasAlphaNumerator Numerator for cobb douglas alpha factor.
/// @param cobbDouglasAlphaDenomintor Denominator for cobb douglas alpha factor.
event ParamsChanged(
/// @param cobbDouglasAlphaDenominator Denominator for cobb douglas alpha factor.
/// @param wethProxyAddress The address that can transfer WETH for fees.
/// @param ethVaultAddress Address of the EthVault contract.
/// @param rewardVaultAddress Address of the StakingPoolRewardVault contract.
/// @param zrxVaultAddress Address of the ZrxVault contract.
event ParamsSet(
uint256 epochDurationInSeconds,
uint32 rewardDelegatedStakeWeight,
uint256 minimumPoolStake,
uint256 maximumMakersInPool,
uint256 cobbDouglasAlphaNumerator,
uint256 cobbDouglasAlphaDenomintor
uint256 cobbDouglasAlphaDenominator,
address wethProxyAddress,
address ethVaultAddress,
address rewardVaultAddress,
address zrxVaultAddress
);

/// @dev Emitted by MixinScheduler when the timeLock period is changed.
Expand Down Expand Up @@ -128,10 +136,4 @@ interface IStakingEvents {
bytes32 poolId,
address makerAddress
);

/// @dev Emitted by MixinStakingPoolRewardVault when the vault's address is changed.
/// @param rewardVaultAddress Address of new reward vault.
event StakingPoolRewardVaultChanged(
address rewardVaultAddress
);
}
16 changes: 15 additions & 1 deletion contracts/staking/contracts/src/interfaces/IStakingProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,21 @@ interface IStakingProxy /* is IStaking */
/// @dev Attach a staking contract; future calls will be delegated to the staking contract.
/// Note that this is callable only by this contract's owner.
/// @param _stakingContract Address of staking contract.
function attachStakingContract(address _stakingContract)
/// @param _wethProxyAddress The address that can transfer WETH for fees.
/// Use address in storage if NIL_ADDRESS is passed in.
/// @param _ethVaultAddress Address of the EthVault contract.
/// Use address in storage if NIL_ADDRESS is passed in.
/// @param _rewardVaultAddress Address of the StakingPoolRewardVault contract.
/// Use address in storage if NIL_ADDRESS is passed in.
/// @param _zrxVaultAddress Address of the ZrxVault contract.
/// Use address in storage if NIL_ADDRESS is passed in.
function attachStakingContract(
address _stakingContract,
address _wethProxyAddress,
address _ethVaultAddress,
address _rewardVaultAddress,
address _zrxVaultAddress
)
external;

/// @dev Detach the current staking contract.
Expand Down
2 changes: 1 addition & 1 deletion contracts/staking/contracts/src/interfaces/IStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ interface IStorage {
view
returns (uint32);

function cobbDouglasAlphaDenomintor()
function cobbDouglasAlphaDenominator()
external
view
returns (uint32);
Expand Down
12 changes: 11 additions & 1 deletion contracts/staking/contracts/src/interfaces/IStorageInit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,15 @@ pragma solidity ^0.5.9;
interface IStorageInit {

/// @dev Initialize storage owned by this contract.
function init() external;
/// @param _wethProxyAddress The address that can transfer WETH for fees.
/// @param _ethVaultAddress Address of the EthVault contract.
/// @param _rewardVaultAddress Address of the StakingPoolRewardVault contract.
/// @param _zrxVaultAddress Address of the ZrxVault contract.
function init(
address _wethProxyAddress,
address _ethVaultAddress,
address _rewardVaultAddress,
address _zrxVaultAddress
)
external;
}
Loading