tests for pools - create pool and increment id

This commit is contained in:
Greg Hysen
2019-06-01 16:24:29 -07:00
parent e645aa1ee5
commit 561fe9c3ea
8 changed files with 91 additions and 23 deletions

View File

@@ -30,9 +30,6 @@ contract Staking is
MixinStake,
MixinPools
{
constructor()
public
{}
///// STAKING /////
@@ -119,14 +116,6 @@ contract Staking is
}
///// POOLS /////
function createPool(uint8 operatorShare)
external
returns (bytes32 poolId)
{
poolId = _createPool(msg.sender, operatorShare);
return poolId;
}
modifier onlyPoolOperator(bytes32 poolId) {
require(
msg.sender == _getPoolOperator(poolId),
@@ -135,6 +124,22 @@ contract Staking is
_;
}
function getNextPoolId()
external
returns (bytes32 nextPoolId)
{
nextPoolId = _getNextPoolId();
return nextPoolId;
}
function createPool(uint8 operatorShare)
external
returns (bytes32 poolId)
{
poolId = _createPool(msg.sender, operatorShare);
return poolId;
}
function addMakerToPool(
bytes32 poolId,

View File

@@ -23,7 +23,7 @@ contract MixinConstants {
uint256 constant TOKEN_MULTIPLIER = 10**18;
bytes32 constant INITIAL_POOL_ID = 0x0000000000000000000000000000000080000000000000000000000000000000;
bytes32 constant INITIAL_POOL_ID = 0x0000000000000000000000000000000100000000000000000000000000000000;
bytes32 constant public NIL_MAKER_ID = 0x0000000000000000000000000000000000000000000000000000000000000000;

View File

@@ -21,35 +21,45 @@ pragma solidity ^0.5.5;
import "./MixinStorage.sol";
import "@0x/contracts-utils/contracts/src/SafeMath.sol";
import "./MixinConstants.sol";
import "../interfaces/IStakingEvents.sol";
contract MixinPools is
SafeMath,
IStakingEvents,
MixinConstants,
MixinStorage
{
constructor()
function _getNextPoolId()
internal
returns (bytes32)
{
// the upper 128 bits are used for pool id
nextPoolId = INITIAL_POOL_ID;
return nextPoolId;
}
function _createPool(address operatorAddress, uint8 operatorShare)
internal
returns (bytes32 poolId)
{
// validate input
require(
operatorShare <= 100,
"OPERATOR_SHARE_MUST_BE_A_PERCENTAGE_BETWEEN_0_AND_100"
);
//
poolId = nextPoolId;
nextPoolId = bytes32(_safeAdd(uint256(nextPoolId), 1));
nextPoolId = bytes32(_safeAdd(uint256(nextPoolId >> 128), 1) << 128);
//
Pool memory pool = Pool({
operatorAddress: operatorAddress,
operatorShare: operatorShare
});
poolById[poolId] = pool;
//
emit PoolCreated(poolId, operatorAddress, operatorShare);
return poolId;
}

View File

@@ -19,9 +19,12 @@
pragma solidity ^0.5.5;
import "../interfaces/IVault.sol";
import "./MixinConstants.sol";
contract MixinStorage {
contract MixinStorage is
MixinConstants
{
// address of staking contract
address stakingContract;
@@ -36,7 +39,7 @@ contract MixinStorage {
mapping (bytes32 => uint256) totalDelegatedStake;
// tracking Maker Id
bytes32 nextPoolId;
bytes32 nextPoolId = INITIAL_POOL_ID;
struct Pool {
address operatorAddress;

View File

@@ -11,4 +11,10 @@ interface IStakingEvents {
address owner,
uint256 amount
);
event PoolCreated(
bytes32 poolId,
address operatorAddress,
uint8 operatorShare
);
}

View File

@@ -14,6 +14,8 @@ import * as chai from 'chai';
import { LogWithDecodedArgs } from 'ethereum-types';
import * as _ from 'lodash';
import { constants as stakingConstants } from './utils/constants';
import { StakingWrapper } from './utils/staking_wrapper';
import { ERC20Wrapper, ERC20ProxyContract } from '@0x/contracts-asset-proxy';
@@ -219,6 +221,19 @@ describe('Staking Core', () => {
// validation
expect(ownerRewardFloatingPoint).to.be.bignumber.equal(expectedOwnerReward);
});
it.only('pools', async() => {
// create first pool
const operatorAddress = stakers[0];
const operatorShare = 39;
const poolId = await stakingWrapper.createPoolAsync(operatorAddress, operatorShare);
expect(poolId).to.be.equal(stakingConstants.INITIAL_POOL_ID);
// check that the next pool id was incremented
const expectedNextPoolId = "0x0000000000000000000000000000000200000000000000000000000000000000";
const nextPoolId = await stakingWrapper.getNextPoolIdAsync();
expect(nextPoolId).to.be.equal(expectedNextPoolId);
//
});
});
});
// tslint:enable:no-unnecessary-type-assertion

View File

@@ -0,0 +1,3 @@
export const constants = {
INITIAL_POOL_ID: "0x0000000000000000000000000000000100000000000000000000000000000000",
};

View File

@@ -93,9 +93,9 @@ export class StakingWrapper {
txDefaults,
);
}
private async _executeTransactionAsync(calldata: string, from: string): Promise<TransactionReceiptWithDecodedLogs> {
private async _executeTransactionAsync(calldata: string, from?: string): Promise<TransactionReceiptWithDecodedLogs> {
const txData = {
from,
from: (from ? from : this._ownerAddres),
to: this.getStakingProxyContract().address,
data: calldata,
gas: 3000000
@@ -105,9 +105,9 @@ export class StakingWrapper {
);
return txReceipt;
}
private async _callAsync(calldata: string, from: string): Promise<any> {
private async _callAsync(calldata: string, from?: string): Promise<any> {
const txData = {
from,
from: (from ? from : this._ownerAddres),
to: this.getStakingProxyContract().address,
data: calldata,
gas: 3000000
@@ -134,6 +134,32 @@ export class StakingWrapper {
const balance = await this._callAsync(calldata, holder);
return balance;
}
public async getNextPoolIdAsync(): Promise<string> {
const calldata = this.getStakingContract().getNextPoolId.getABIEncodedTransactionData();
const nextPoolId = await this._callAsync(calldata);
return nextPoolId;
}
public async createPoolAsync(operatorAddress: string, operatorShare: number): Promise<string> {
const calldata = this.getStakingContract().createPool.getABIEncodedTransactionData(operatorShare);
const txReceipt = await this._executeTransactionAsync(calldata, operatorAddress);
const createPoolLog = this._logDecoder.decodeLogOrThrow(txReceipt.logs[0]);
const poolId = (createPoolLog as any).args.poolId;
return poolId;
}
/*
public async addMakerToPoolAsync(poolId: string, makerAddress: string, makerSignature: string): Promise<void> {
}
public async removeMakerFromPoolAsync(poolId: string, makerAddress: string): Promise<void> {
}
public async getMakerPoolId(makerAddress: string): Promise<string> {
}
public async getMakersForPool(poolId: string): Promise<string[]> {
}
*/
public async getZrxVaultBalance(holder: string): Promise<BigNumber> {
const balance = await this.getZrxVaultContract().balanceOf.callAsync(holder);
return balance;