moveStake assertion; use SimulationEnvironment to track global stake and staking pools

This commit is contained in:
Michael Zhu
2019-11-06 19:03:51 -08:00
parent c2919bcdb0
commit f33a9d162a
13 changed files with 373 additions and 88 deletions

View File

@@ -1,10 +1,23 @@
import { BlockchainBalanceStore, LocalBalanceStore } from '@0x/contracts-exchange';
import { GlobalStakeByStatus, OwnerStakeByStatus, StakeStatus, StoredBalance } from '@0x/contracts-staking';
import { expect } from '@0x/contracts-test-utils';
import { assetDataUtils } from '@0x/order-utils';
import { BigNumber } from '@0x/utils';
import { BigNumber, logUtils } from '@0x/utils';
import { TxData } from 'ethereum-types';
import { DeploymentManager } from '../utils/deployment_manager';
import { FunctionAssertion } from '../utils/function_assertions';
import { FunctionAssertion, FunctionResult } from '../utils/function_assertions';
function expectedUndelegatedStake(
initStake: OwnerStakeByStatus | GlobalStakeByStatus,
amount: BigNumber,
): StoredBalance {
return {
currentEpoch: initStake[StakeStatus.Undelegated].currentEpoch,
currentEpochBalance: initStake[StakeStatus.Undelegated].currentEpochBalance.minus(amount),
nextEpochBalance: initStake[StakeStatus.Undelegated].nextEpochBalance.minus(amount),
};
}
/**
* Returns a FunctionAssertion for `unstake` which assumes valid input is provided. The
@@ -14,6 +27,8 @@ import { FunctionAssertion } from '../utils/function_assertions';
export function validUnstakeAssertion(
deployment: DeploymentManager,
balanceStore: BlockchainBalanceStore,
globalStake: GlobalStakeByStatus,
ownerStake: OwnerStakeByStatus,
): FunctionAssertion<LocalBalanceStore> {
const { stakingWrapper, zrxVault } = deployment.staking;
const { zrx } = deployment.tokens;
@@ -29,9 +44,31 @@ export function validUnstakeAssertion(
);
return expectedBalances;
},
after: async (expectedBalances: LocalBalanceStore) => {
after: async (
expectedBalances: LocalBalanceStore,
_result: FunctionResult,
amount: BigNumber,
txData: Partial<TxData>,
) => {
logUtils.log(`unstake(${amount})`);
await balanceStore.updateErc20BalancesAsync();
balanceStore.assertEquals(expectedBalances);
const ownerUndelegatedStake = await stakingWrapper.getOwnerStakeByStatus.callAsync(
txData.from as string,
StakeStatus.Undelegated,
);
const expectedOwnerUndelegatedStake = expectedUndelegatedStake(ownerStake, amount);
expect(ownerUndelegatedStake, 'Owner undelegated stake').to.deep.equal(expectedOwnerUndelegatedStake);
ownerStake[StakeStatus.Undelegated] = expectedOwnerUndelegatedStake;
const globalUndelegatedStake = await stakingWrapper.getGlobalStakeByStatus.callAsync(
StakeStatus.Undelegated,
);
const expectedGlobalUndelegatedStake = expectedUndelegatedStake(globalStake, amount);
expect(globalUndelegatedStake, 'Global undelegated stake').to.deep.equal(expectedGlobalUndelegatedStake);
globalStake[StakeStatus.Undelegated] = expectedGlobalUndelegatedStake;
},
});
}