Skip to content

Commit

Permalink
added custom test
Browse files Browse the repository at this point in the history
  • Loading branch information
mtabasco committed Jun 24, 2024
1 parent 9c3b11a commit 9ddafc0
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 2 deletions.
4 changes: 3 additions & 1 deletion test/helpers/contract-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ export const registerOperators = async function (
fee: BigInt,
gasGroups: GasGroup[] = [GasGroup.REGISTER_OPERATOR],
) {
const newOperatorIds = [];
const targetOperatorId = lastOperatorId + numberOfOperators;
for (let i = lastOperatorId; i < lastOperatorId + numberOfOperators && i < mockedOperators.length; i++) {
const operator = mockedOperators[i];
Expand All @@ -185,9 +186,10 @@ export const registerOperators = async function (
const event = eventsByName.OperatorAdded[0];
operator.id = Number(event.args.operatorId);
mockedOperators[i] = operator;
newOperatorIds.push(operator.id);
}
lastOperatorId = targetOperatorId;
return lastOperatorId;
return newOperatorIds;
};

export const coldRegisterValidator = async function () {
Expand Down
84 changes: 83 additions & 1 deletion test/operators/whitelist.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@ import { trackGas, GasGroup } from '../helpers/gas-usage';
import { ethers } from 'hardhat';
import { expect } from 'chai';

import { mine } from '@nomicfoundation/hardhat-network-helpers';

// Declare globals
let ssvNetwork: any, ssvViews: any, mockWhitelistingContract: any, mockWhitelistingContractAddress: any;
let ssvNetwork: any, ssvViews: any, ssvToken: any, mockWhitelistingContract: any, mockWhitelistingContractAddress: any;
const OPERATOR_IDS_10 = Array.from({ length: 10 }, (_, i) => i + 1);

describe('Whitelisting Operator Tests', () => {
Expand All @@ -18,6 +20,7 @@ describe('Whitelisting Operator Tests', () => {
const metadata = await initializeContract();
ssvNetwork = metadata.ssvNetwork;
ssvViews = metadata.ssvNetworkViews;
ssvToken = metadata.ssvToken;

mockWhitelistingContract = await hre.viem.deployContract('MockWhitelistingContract', [[]], {
client: owners[0].client,
Expand Down Expand Up @@ -967,4 +970,83 @@ describe('Whitelisting Operator Tests', () => {
[],
);
});

it('Custom test: Operators balances sync', async () => {
// owners[2] -> operators' owner
// owners[3] -> whitelisted address for all 4 operators

// create 4 operators with a fee
const operatorIds = await registerOperators(2, 4, CONFIG.minimalOperatorFee);

// set operators private
ssvNetwork.write.setOperatorsPrivateUnchecked([operatorIds], {
account: owners[2].account,
});

// whitelist owners[3] address for all operators
await ssvNetwork.write.setOperatorsWhitelists([operatorIds, [owners[3].account.address]], {
account: owners[2].account,
});

// owners[3] registers a validator
const minDepositAmount = (BigInt(CONFIG.minimalBlocksBeforeLiquidation) + 2n) * CONFIG.minimalOperatorFee * 4n;
await ssvToken.write.approve([ssvNetwork.address, minDepositAmount], { account: owners[3].account });
const { eventsByName } = await trackGas(
ssvNetwork.write.registerValidator(
[
DataGenerator.publicKey(1),
operatorIds,
await DataGenerator.shares(2, 1, operatorIds),
minDepositAmount,
{
validatorCount: 0,
networkFeeIndex: 0,
index: 0,
balance: 0n,
active: true,
},
],
{ account: owners[3].account },
),
);

const firstCluster = eventsByName.ValidatorAdded[0].args;

// liquidate the cluster
await mine(CONFIG.minimalBlocksBeforeLiquidation);
const liquidatedCluster = await trackGas(
ssvNetwork.write.liquidate([firstCluster.owner, firstCluster.operatorIds, firstCluster.cluster]),
);
const updatedCluster = liquidatedCluster.eventsByName.ClusterLiquidated[0].args;

// withdraw all operators' earnings
for (let i = 0; i < operatorIds.length; i++) {
await ssvNetwork.write.withdrawAllOperatorEarnings([operatorIds[i]], {
account: owners[2].account,
});
}

// de-whitelist owners[3] address for all operators
await ssvNetwork.write.removeOperatorsWhitelists([operatorIds, [owners[3].account.address]], {
account: owners[2].account,
});

// check operators' balance is 0 after few blocks
await mine(1000);
for (let i = 0; i < operatorIds.length; i++) {
expect(await ssvViews.read.getOperatorEarnings([operatorIds[i]])).to.equal(0);
}

// reactivate the cluster
await ssvToken.write.approve([ssvNetwork.address, minDepositAmount], { account: owners[3].account });
await ssvNetwork.write.reactivate([updatedCluster.operatorIds, minDepositAmount, updatedCluster.cluster], {
account: owners[3].account,
});

// all operators have have the right balance
await mine(100);
for (let i = 0; i < operatorIds.length; i++) {
expect(await ssvViews.read.getOperatorEarnings([operatorIds[i]])).to.equal(CONFIG.minimalOperatorFee * 100n);
}
});
});

0 comments on commit 9ddafc0

Please sign in to comment.