Skip to content

Commit

Permalink
test: updating tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kupermind committed Oct 2, 2023
1 parent fbf1d09 commit 47b771b
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 28 deletions.
6 changes: 4 additions & 2 deletions contracts/staking/ServiceStakingBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ abstract contract ServiceStakingBase is IErrorsRegistries {
// Minimum service staking deposit value required for staking
uint256 public immutable minStakingDeposit;
// Liveness period
uint256 public immutable livenessPeriod = 1 days;
uint256 public immutable livenessPeriod;
// Liveness ratio in the format of 1e18
uint256 public immutable livenessRatio;
// Optional service multisig threshold requirement
Expand Down Expand Up @@ -158,7 +158,8 @@ abstract contract ServiceStakingBase is IErrorsRegistries {
constructor(StakingParams memory _stakingParams, address _serviceRegistry) {
// Initial checks
if (_stakingParams.maxNumServices == 0 || _stakingParams.rewardsPerSecond == 0 ||
_stakingParams.minStakingDeposit == 0 || _stakingParams.livenessRatio == 0) {
_stakingParams.minStakingDeposit == 0 || _stakingParams.livenessPeriod == 0 ||
_stakingParams.livenessRatio == 0) {
revert ZeroValue();
}
if (_serviceRegistry == address(0)) {
Expand All @@ -169,6 +170,7 @@ abstract contract ServiceStakingBase is IErrorsRegistries {
maxNumServices = _stakingParams.maxNumServices;
rewardsPerSecond = _stakingParams.rewardsPerSecond;
minStakingDeposit = _stakingParams.minStakingDeposit;
livenessPeriod = _stakingParams.livenessPeriod;
livenessRatio = _stakingParams.livenessRatio;
serviceRegistry = _serviceRegistry;

Expand Down
109 changes: 83 additions & 26 deletions test/ServiceStaking.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ describe.only("ServiceStakingNativeToken", function () {
let deployer;
let operator;
let agentInstances;
const maxNumServices = 10;
const rewardsPerSecond = "1" + "0".repeat(15);
const minStakingDeposit = 10;
const livenessRatio = "1" + "0".repeat(16); // 0.01 transaction per second (TPS)
const AddressZero = ethers.constants.AddressZero;
const defaultHash = "0x" + "5".repeat(64);
const bytes32Zero = "0x" + "0".repeat(64);
Expand All @@ -34,10 +30,20 @@ describe.only("ServiceStakingNativeToken", function () {
const agentIds = [1];
const agentParams = [[1, regBond]];
const threshold = 1;
const numAgentInstances = 1;
const livenessPeriod = 10; // Ten seconds
const initSupply = "5" + "0".repeat(26);
const payload = "0x";
const serviceParams = {
maxNumServices: 10,
rewardsPerSecond: "1" + "0".repeat(15),
minStakingDeposit: 10,
livenessPeriod: livenessPeriod, // Ten seconds
livenessRatio: "1" + "0".repeat(16), // 0.01 transaction per second (TPS)
agentIds: [],
threshold: 0,
numAgentInstances: 0,
configHash: bytes32Zero
}

Check failure on line 46 in test/ServiceStaking.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon

beforeEach(async function () {
signers = await ethers.getSigners();
Expand Down Expand Up @@ -82,13 +88,12 @@ describe.only("ServiceStakingNativeToken", function () {
await multiSend.deployed();

const ServiceStakingNativeToken = await ethers.getContractFactory("ServiceStakingNativeToken");
serviceStaking = await ServiceStakingNativeToken.deploy(maxNumServices, rewardsPerSecond, minStakingDeposit,
livenessRatio, [], 0, 0, bytes32Zero, serviceRegistry.address);
serviceStaking = await ServiceStakingNativeToken.deploy(serviceParams, serviceRegistry.address);
await serviceStaking.deployed();

const ServiceStakingToken = await ethers.getContractFactory("ServiceStakingToken");
serviceStakingToken = await ServiceStakingToken.deploy(maxNumServices, rewardsPerSecond, minStakingDeposit,
livenessRatio, [], 0, 0, bytes32Zero, [serviceRegistry.address, serviceRegistryTokenUtility.address], token.address);
serviceStakingToken = await ServiceStakingToken.deploy(serviceParams, serviceRegistry.address,
serviceRegistryTokenUtility.address, token.address);
await serviceStakingToken.deployed();

const ReentrancyAttacker = await ethers.getContractFactory("ReentrancyTokenAttacker");
Expand Down Expand Up @@ -134,19 +139,70 @@ describe.only("ServiceStakingNativeToken", function () {
const ServiceStakingNativeToken = await ethers.getContractFactory("ServiceStakingNativeToken");
const ServiceStakingToken = await ethers.getContractFactory("ServiceStakingToken");

await expect(ServiceStakingNativeToken.deploy(0, 0, 0, 0, [], 0, 0, bytes32Zero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroValue");
await expect(ServiceStakingNativeToken.deploy(maxNumServices, 0, 0, 0, [], 0, 0, bytes32Zero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroValue");
await expect(ServiceStakingNativeToken.deploy(maxNumServices, rewardsPerSecond, 0, 0, [], 0, 0, bytes32Zero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroValue");
await expect(ServiceStakingNativeToken.deploy(maxNumServices, rewardsPerSecond, minStakingDeposit, 0, [], 0, 0, bytes32Zero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroValue");
await expect(ServiceStakingNativeToken.deploy(maxNumServices, rewardsPerSecond, minStakingDeposit, livenessRatio, [], 0, 0, bytes32Zero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroAddress");

await expect(ServiceStakingToken.deploy(0, 0, 0, 0, [], 0, 0, bytes32Zero, [AddressZero, AddressZero], AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroValue");
await expect(ServiceStakingToken.deploy(maxNumServices, 0, 0, 0, [], 0, 0, bytes32Zero, [AddressZero, AddressZero], AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroValue");
await expect(ServiceStakingToken.deploy(maxNumServices, rewardsPerSecond, 0, 0, [], 0, 0, bytes32Zero, [AddressZero, AddressZero], AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroValue");
await expect(ServiceStakingToken.deploy(maxNumServices, rewardsPerSecond, minStakingDeposit, 0, [], 0, 0, bytes32Zero, [AddressZero, AddressZero], AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroValue");
await expect(ServiceStakingToken.deploy(maxNumServices, rewardsPerSecond, minStakingDeposit, livenessRatio, [], 0, 0, bytes32Zero, [AddressZero, AddressZero], AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroAddress");
await expect(ServiceStakingToken.deploy(maxNumServices, rewardsPerSecond, minStakingDeposit, livenessRatio, [], 0, 0, bytes32Zero, [serviceRegistry.address, AddressZero], AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroAddress");
await expect(ServiceStakingToken.deploy(maxNumServices, rewardsPerSecond, minStakingDeposit, livenessRatio, [], 0, 0, bytes32Zero, [serviceRegistry.address, serviceRegistryTokenUtility.address], AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroAddress");
let testServiceParams = {
maxNumServices: 0,
rewardsPerSecond: 0,
minStakingDeposit: 0,
livenessPeriod: 0,
livenessRatio: 0,
agentIds: [],
threshold: 0,
numAgentInstances: 0,
configHash: bytes32Zero
}

Check failure on line 152 in test/ServiceStaking.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon

// Service Staking Native Token
await expect(ServiceStakingNativeToken.deploy(testServiceParams, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroValue");

testServiceParams.maxNumServices = 1;
await expect(ServiceStakingNativeToken.deploy(testServiceParams, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroValue");

testServiceParams.rewardsPerSecond = 1;
await expect(ServiceStakingNativeToken.deploy(testServiceParams, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroValue");

testServiceParams.minStakingDeposit = 1;
await expect(ServiceStakingNativeToken.deploy(testServiceParams, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroValue");

testServiceParams.livenessPeriod = 1;
await expect(ServiceStakingNativeToken.deploy(testServiceParams, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroValue");

testServiceParams.livenessRatio = 1;
await expect(ServiceStakingNativeToken.deploy(testServiceParams, AddressZero)).to.be.revertedWithCustomError(ServiceStakingNativeToken, "ZeroAddress");


// Service Staking Token
testServiceParams = {
maxNumServices: 0,
rewardsPerSecond: 0,
minStakingDeposit: 0,
livenessPeriod: 0,
livenessRatio: 0,
agentIds: [],
threshold: 0,
numAgentInstances: 0,
configHash: bytes32Zero
}

Check failure on line 184 in test/ServiceStaking.js

View workflow job for this annotation

GitHub Actions / build

Missing semicolon

await expect(ServiceStakingToken.deploy(testServiceParams, AddressZero, AddressZero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroValue");

testServiceParams.maxNumServices = 1;
await expect(ServiceStakingToken.deploy(testServiceParams, AddressZero, AddressZero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroValue");

testServiceParams.rewardsPerSecond = 1;
await expect(ServiceStakingToken.deploy(testServiceParams, AddressZero, AddressZero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroValue");

testServiceParams.minStakingDeposit = 1;
await expect(ServiceStakingToken.deploy(testServiceParams, AddressZero, AddressZero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroValue");

testServiceParams.livenessPeriod = 1;
await expect(ServiceStakingToken.deploy(testServiceParams, AddressZero, AddressZero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroValue");

testServiceParams.livenessRatio = 1;
await expect(ServiceStakingToken.deploy(testServiceParams, AddressZero, AddressZero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroAddress");


await expect(ServiceStakingToken.deploy(testServiceParams, serviceRegistry.address, AddressZero, AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroAddress");
await expect(ServiceStakingToken.deploy(testServiceParams, serviceRegistry.address, serviceRegistryTokenUtility.address, AddressZero)).to.be.revertedWithCustomError(ServiceStakingToken, "ZeroAddress");
});
});

Expand All @@ -160,8 +216,9 @@ describe.only("ServiceStakingNativeToken", function () {
it("Should fail if the maximum number of staking services is reached", async function () {
// Deploy a contract with max number of services equal to one
const ServiceStakingNativeToken = await ethers.getContractFactory("ServiceStakingNativeToken");
const sStaking = await ServiceStakingNativeToken.deploy(1, rewardsPerSecond, minStakingDeposit, livenessRatio,
[], 0, 0, bytes32Zero, serviceRegistry.address);
const testServiceParams = serviceParams;
testServiceParams.maxNumServices = 1;
const sStaking = await ServiceStakingNativeToken.deploy(serviceParams, serviceRegistry.address);
await sStaking.deployed();

// Deposit to the contract
Expand Down Expand Up @@ -369,7 +426,7 @@ describe.only("ServiceStakingNativeToken", function () {
snapshot.restore();
});

it.only("Stake and unstake with the service activity", async function () {
it("Stake and unstake with the service activity", async function () {
// Take a snapshot of the current state of the blockchain
const snapshot = await helpers.takeSnapshot();

Expand Down Expand Up @@ -541,7 +598,7 @@ describe.only("ServiceStakingNativeToken", function () {
const snapshot = await helpers.takeSnapshot();

// Deposit to the contract
await deployer.sendTransaction({to: serviceStaking.address, value: rewardsPerSecond});
await deployer.sendTransaction({to: serviceStaking.address, value: serviceParams.rewardsPerSecond});

// Approve services
await serviceRegistry.approve(serviceStaking.address, serviceId);
Expand Down

0 comments on commit 47b771b

Please sign in to comment.