@0x/contracts-staking: Rename Tuned event to ParamsChanged.

`@0x/contracts-staking`: Merge `exchange_fees` unit tests into `protocol_fees` unit tests.
`@0x/contracts-staking`: Remove `ProtocolFeeActor` and any use of it.
`@0x/contracts-staking`: Remove unused constants.
`@0x/contracts-staking`: Move WETH assertion constructor into `MixinDeploymentConstants`.
`@0x/contracts-staking`: Add more unit tests.
This commit is contained in:
Lawrence Forman
2019-09-10 00:25:10 -04:00
parent d8d791e4f0
commit 2ed39cd18d
24 changed files with 490 additions and 528 deletions

View File

@@ -1,48 +1,27 @@
import { blockchainTests, constants } from '@0x/contracts-test-utils';
import { blockchainTests, constants, expect, filterLogsToArguments, hexRandom } from '@0x/contracts-test-utils';
import { StakingRevertErrors } from '@0x/order-utils';
import { BigNumber } from '@0x/utils';
import { LogEntry } from 'ethereum-types';
import * as _ from 'lodash';
import { artifacts, TestProtocolFeesContract, TestProtocolFeesERC20ProxyContract } from '../src';
import {
artifacts,
TestProtocolFeesContract,
TestProtocolFeesERC20ProxyContract,
TestProtocolFeesERC20ProxyTransferFromCalledEventArgs,
} from '../src';
import { ProtocolFeeActor } from './actors/protocol_fee_actor';
import { getRandomPortion } from './utils/number_utils';
// tslint:disable:no-unnecessary-type-assertion
blockchainTests('Protocol Fee Unit Tests', env => {
// The accounts that will be used during testing.
let owner: string;
let exchange: string;
let nonExchange: string;
let makerAddress: string;
let payerAddress: string;
// The actor that will be used for testng `payProtocolFee` and `_unwrapETH`.
let protocolFeeActor: ProtocolFeeActor;
// The default protocol fee that will be paid -- a somewhat realistic value.
const DEFAULT_PROTOCOL_FEE_PAID = new BigNumber(150000).times(10000000);
// The default pool Id that will be used.
const DEFAULT_POOL_ID = '0x0000000000000000000000000000000000000000000000000000000000000001';
let ownerAddress: string;
let exchangeAddress: string;
let notExchangeAddress: string;
let testContract: TestProtocolFeesContract;
let wethAssetData: string;
before(async () => {
// Get accounts to represent the exchange and an address that is not a registered exchange.
[
owner,
exchange,
nonExchange,
makerAddress,
payerAddress,
] = (await env.web3Wrapper.getAvailableAddressesAsync()).slice(0, 6);
// Deploy the protocol fees contract.
const protocolFees = await TestProtocolFeesContract.deployFrom0xArtifactAsync(
artifacts.TestProtocolFees,
env.provider,
{
...env.txDefaults,
from: owner,
},
artifacts,
);
[ownerAddress, exchangeAddress, notExchangeAddress] = await env.web3Wrapper.getAvailableAddressesAsync();
// Deploy the erc20Proxy for testing.
const proxy = await TestProtocolFeesERC20ProxyContract.deployFrom0xArtifactAsync(
@@ -52,187 +31,349 @@ blockchainTests('Protocol Fee Unit Tests', env => {
{},
);
// Register the test ERC20Proxy in the exchange.
await protocolFees.setWethProxy.awaitTransactionSuccessAsync(proxy.address);
// Deploy the protocol fees contract.
testContract = await TestProtocolFeesContract.deployFrom0xArtifactAsync(
artifacts.TestProtocolFees,
env.provider,
{
...env.txDefaults,
from: ownerAddress,
},
artifacts,
exchangeAddress,
proxy.address,
);
// Register an exchange in the protocol fee contract.
await protocolFees.addExchangeAddress.awaitTransactionSuccessAsync(exchange, { from: owner });
// "Register" the makerAddress in the default pool.
await protocolFees.addMakerToPool.awaitTransactionSuccessAsync(DEFAULT_POOL_ID, makerAddress);
// Initialize the protocol fee actor.
protocolFeeActor = new ProtocolFeeActor([exchange], [makerAddress], protocolFees);
wethAssetData = await testContract.getWethAssetData.callAsync();
});
blockchainTests.resets('payProtocolFee', () => {
it('should revert if called by a non-exchange', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: nonExchange,
value: constants.ZERO_AMOUNT,
async function createTestPoolAsync(stake: BigNumber, makers: string[]): Promise<string> {
const poolId = hexRandom();
await testContract.createTestPool.awaitTransactionSuccessAsync(poolId, stake, makers);
return poolId;
}
blockchainTests.resets('payProtocolFee()', () => {
const randomAddress = () => hexRandom(constants.ADDRESS_LENGTH);
const DEFAULT_PROTOCOL_FEE_PAID = new BigNumber(150e3).times(1e9);
const { ZERO_AMOUNT } = constants;
const makerAddress = randomAddress();
const payerAddress = randomAddress();
let minimumStake: BigNumber;
before(async () => {
minimumStake = (await testContract.getParams.callAsync())[2];
});
describe('forbidden actions', () => {
it('should revert if called by a non-exchange', async () => {
const tx = testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: notExchangeAddress },
);
const expectedError = new StakingRevertErrors.OnlyCallableByExchangeError(notExchangeAddress);
return expect(tx).to.revertWith(expectedError);
});
it('should revert if `protocolFeePaid` is zero with zero value sent', async () => {
const tx = testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
ZERO_AMOUNT,
{ from: exchangeAddress, value: ZERO_AMOUNT },
);
const expectedError = new StakingRevertErrors.InvalidProtocolFeePaymentError(
StakingRevertErrors.ProtocolFeePaymentErrorCodes.ZeroProtocolFeePaid,
ZERO_AMOUNT,
ZERO_AMOUNT,
);
return expect(tx).to.revertWith(expectedError);
});
it('should revert if `protocolFeePaid` is zero with non-zero value sent', async () => {
const tx = testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
ZERO_AMOUNT,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID },
);
const expectedError = new StakingRevertErrors.InvalidProtocolFeePaymentError(
StakingRevertErrors.ProtocolFeePaymentErrorCodes.ZeroProtocolFeePaid,
ZERO_AMOUNT,
DEFAULT_PROTOCOL_FEE_PAID,
);
return expect(tx).to.revertWith(expectedError);
});
it('should revert if `protocolFeePaid` is < than the provided message value', async () => {
const tx = testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID.minus(1) },
);
const expectedError = new StakingRevertErrors.InvalidProtocolFeePaymentError(
StakingRevertErrors.ProtocolFeePaymentErrorCodes.MismatchedFeeAndPayment,
DEFAULT_PROTOCOL_FEE_PAID,
DEFAULT_PROTOCOL_FEE_PAID.minus(1),
);
return expect(tx).to.revertWith(expectedError);
});
it('should revert if `protocolFeePaid` is > than the provided message value', async () => {
const tx = testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID.plus(1) },
);
const expectedError = new StakingRevertErrors.InvalidProtocolFeePaymentError(
StakingRevertErrors.ProtocolFeePaymentErrorCodes.MismatchedFeeAndPayment,
DEFAULT_PROTOCOL_FEE_PAID,
DEFAULT_PROTOCOL_FEE_PAID.plus(1),
);
return expect(tx).to.revertWith(expectedError);
});
});
it('should revert if `protocolFeePaid` is zero with zero value sent', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: constants.ZERO_AMOUNT,
from: exchange,
value: constants.ZERO_AMOUNT,
describe('ETH fees', () => {
function assertNoWETHTransferLogs(logs: LogEntry[]): void {
const logsArgs = filterLogsToArguments<TestProtocolFeesERC20ProxyTransferFromCalledEventArgs>(
logs,
'TransferFromCalled',
);
expect(logsArgs).to.deep.eq([]);
}
it('should not transfer WETH if value is sent', async () => {
await createTestPoolAsync(minimumStake, []);
const receipt = await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID },
);
assertNoWETHTransferLogs(receipt.logs);
});
it('should update `protocolFeesThisEpochByPool` if the maker is in a pool', async () => {
const poolId = await createTestPoolAsync(minimumStake, [makerAddress]);
const receipt = await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID },
);
assertNoWETHTransferLogs(receipt.logs);
const poolFees = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(poolFees).to.bignumber.eq(DEFAULT_PROTOCOL_FEE_PAID);
});
it('should not update `protocolFeesThisEpochByPool` if maker is not in a pool', async () => {
const poolId = await createTestPoolAsync(minimumStake, []);
const receipt = await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID },
);
assertNoWETHTransferLogs(receipt.logs);
const poolFees = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(poolFees).to.bignumber.eq(ZERO_AMOUNT);
});
it('fees paid to the same maker should go to the same pool', async () => {
const poolId = await createTestPoolAsync(minimumStake, [makerAddress]);
const payAsync = async () => {
const receipt = await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID },
);
assertNoWETHTransferLogs(receipt.logs);
};
await payAsync();
await payAsync();
const expectedTotalFees = DEFAULT_PROTOCOL_FEE_PAID.times(2);
const poolFees = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(poolFees).to.bignumber.eq(expectedTotalFees);
});
});
it('should revert if `protocolFeePaid` is zero with non-zero value sent', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: constants.ZERO_AMOUNT,
from: exchange,
value: DEFAULT_PROTOCOL_FEE_PAID,
describe('WETH fees', () => {
function assertWETHTransferLogs(logs: LogEntry[], fromAddress: string, amount: BigNumber): void {
const logsArgs = filterLogsToArguments<TestProtocolFeesERC20ProxyTransferFromCalledEventArgs>(
logs,
'TransferFromCalled',
);
expect(logsArgs.length).to.eq(1);
for (const args of logsArgs) {
expect(args.assetData).to.eq(wethAssetData);
expect(args.from).to.eq(fromAddress);
expect(args.to).to.eq(testContract.address);
expect(args.amount).to.bignumber.eq(amount);
}
}
it('should transfer WETH if no value is sent and the maker is not in a pool', async () => {
await createTestPoolAsync(minimumStake, []);
const receipt = await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: ZERO_AMOUNT },
);
assertWETHTransferLogs(receipt.logs, payerAddress, DEFAULT_PROTOCOL_FEE_PAID);
});
it('should update `protocolFeesThisEpochByPool` if the maker is in a pool', async () => {
const poolId = await createTestPoolAsync(minimumStake, [makerAddress]);
const receipt = await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: ZERO_AMOUNT },
);
assertWETHTransferLogs(receipt.logs, payerAddress, DEFAULT_PROTOCOL_FEE_PAID);
const poolFees = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(poolFees).to.bignumber.eq(DEFAULT_PROTOCOL_FEE_PAID);
});
it('should not update `protocolFeesThisEpochByPool` if maker is not in a pool', async () => {
const poolId = await createTestPoolAsync(minimumStake, []);
const receipt = await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: ZERO_AMOUNT },
);
assertWETHTransferLogs(receipt.logs, payerAddress, DEFAULT_PROTOCOL_FEE_PAID);
const poolFees = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(poolFees).to.bignumber.eq(ZERO_AMOUNT);
});
it('fees paid to the same maker should go to the same pool', async () => {
const poolId = await createTestPoolAsync(minimumStake, [makerAddress]);
const payAsync = async () => {
const receipt = await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: ZERO_AMOUNT },
);
assertWETHTransferLogs(receipt.logs, payerAddress, DEFAULT_PROTOCOL_FEE_PAID);
};
await payAsync();
await payAsync();
const expectedTotalFees = DEFAULT_PROTOCOL_FEE_PAID.times(2);
const poolFees = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(poolFees).to.bignumber.eq(expectedTotalFees);
});
it('fees paid to the same maker in WETH then ETH should go to the same pool', async () => {
const poolId = await createTestPoolAsync(minimumStake, [makerAddress]);
const payAsync = async (inWETH: boolean) => {
await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{
from: exchangeAddress,
value: inWETH ? ZERO_AMOUNT : DEFAULT_PROTOCOL_FEE_PAID,
},
);
};
await payAsync(true);
await payAsync(false);
const expectedTotalFees = DEFAULT_PROTOCOL_FEE_PAID.times(2);
const poolFees = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(poolFees).to.bignumber.eq(expectedTotalFees);
});
});
it('should revert if `protocolFeePaid` is different than the provided message value', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID.minus(50),
from: exchange,
value: DEFAULT_PROTOCOL_FEE_PAID,
describe('Multiple makers', () => {
it('fees paid to different makers in the same pool go to that pool', async () => {
const otherMakerAddress = randomAddress();
const poolId = await createTestPoolAsync(minimumStake, [makerAddress, otherMakerAddress]);
const payAsync = async (_makerAddress: string) => {
await testContract.payProtocolFee.awaitTransactionSuccessAsync(
_makerAddress,
payerAddress,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID },
);
};
await payAsync(makerAddress);
await payAsync(otherMakerAddress);
const expectedTotalFees = DEFAULT_PROTOCOL_FEE_PAID.times(2);
const poolFees = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(poolFees).to.bignumber.eq(expectedTotalFees);
});
it('fees paid to makers in different pools go to their respective pools', async () => {
const [fee, otherFee] = _.times(2, () => getRandomPortion(DEFAULT_PROTOCOL_FEE_PAID));
const otherMakerAddress = randomAddress();
const poolId = await createTestPoolAsync(minimumStake, [makerAddress]);
const otherPoolId = await createTestPoolAsync(minimumStake, [otherMakerAddress]);
const payAsync = async (_poolId: string, _makerAddress: string, _fee: BigNumber) => {
// prettier-ignore
await testContract.payProtocolFee.awaitTransactionSuccessAsync(
_makerAddress,
payerAddress,
_fee,
{ from: exchangeAddress, value: _fee },
);
};
await payAsync(poolId, makerAddress, fee);
await payAsync(otherPoolId, otherMakerAddress, otherFee);
const [poolFees, otherPoolFees] = await Promise.all([
testContract.getProtocolFeesThisEpochByPool.callAsync(poolId),
testContract.getProtocolFeesThisEpochByPool.callAsync(otherPoolId),
]);
expect(poolFees).to.bignumber.eq(fee);
expect(otherPoolFees).to.bignumber.eq(otherFee);
});
});
it('should call `transferFrom` in the proxy if no value is sent and the maker is not in a pool', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress: payerAddress, // This is an unregistered maker address
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: constants.ZERO_AMOUNT,
});
});
it('should call `transferFrom` in the proxy and update `protocolFeesThisEpochByPool` if no value is sent and the maker is in a pool', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: constants.ZERO_AMOUNT,
});
});
it('should not call `transferFrom` in the proxy and should not update `protocolFeesThisEpochByPool` if value is sent and the maker is not in a pool', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress: payerAddress, // This is an unregistered maker address
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: DEFAULT_PROTOCOL_FEE_PAID,
});
});
it('should not call `transferFrom` in the proxy and should update `protocolFeesThisEpochByPool` if value is sent and the maker is in a pool', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: DEFAULT_PROTOCOL_FEE_PAID,
});
});
it('should only have one active pool if a fee is paid on behalf of one maker ETH twice', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: DEFAULT_PROTOCOL_FEE_PAID,
describe('Dust stake', () => {
it('credits pools with stake > minimum', async () => {
const poolId = await createTestPoolAsync(minimumStake.plus(1), [makerAddress]);
await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
constants.NULL_ADDRESS,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID },
);
const feesCredited = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(feesCredited).to.bignumber.eq(DEFAULT_PROTOCOL_FEE_PAID);
});
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: DEFAULT_PROTOCOL_FEE_PAID,
});
});
it('should only have one active pool if a fee is paid on behalf of one maker in WETH and then ETH', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: constants.ZERO_AMOUNT,
it('credits pools with stake == minimum', async () => {
const poolId = await createTestPoolAsync(minimumStake, [makerAddress]);
await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
constants.NULL_ADDRESS,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID },
);
const feesCredited = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(feesCredited).to.bignumber.eq(DEFAULT_PROTOCOL_FEE_PAID);
});
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: DEFAULT_PROTOCOL_FEE_PAID,
});
});
it('should only have one active pool if a fee is paid on behalf of one maker in ETH and then WETH', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: DEFAULT_PROTOCOL_FEE_PAID,
});
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: constants.ZERO_AMOUNT,
});
});
it('should only have one active pool if a fee is paid on behalf of one maker in WETH twice', async () => {
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: constants.ZERO_AMOUNT,
});
await protocolFeeActor.payProtocolFeeAsync({
poolId: DEFAULT_POOL_ID,
makerAddress,
payerAddress,
protocolFeePaid: DEFAULT_PROTOCOL_FEE_PAID,
from: exchange,
value: constants.ZERO_AMOUNT,
it('does not credit pools with stake < minimum', async () => {
const poolId = await createTestPoolAsync(minimumStake.minus(1), [makerAddress]);
await testContract.payProtocolFee.awaitTransactionSuccessAsync(
makerAddress,
constants.NULL_ADDRESS,
DEFAULT_PROTOCOL_FEE_PAID,
{ from: exchangeAddress, value: DEFAULT_PROTOCOL_FEE_PAID },
);
const feesCredited = await testContract.getProtocolFeesThisEpochByPool.callAsync(poolId);
expect(feesCredited).to.bignumber.eq(0);
});
});
});
});
// tslint:enable:no-unnecessary-type-assertion