Skip to content

Commit

Permalink
Add updateLockPeriod and update test cases for dividend contract
Browse files Browse the repository at this point in the history
  • Loading branch information
lurenpluto committed Aug 2, 2024
1 parent 54d59e9 commit 8380071
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
9 changes: 9 additions & 0 deletions contracts/dividend.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ contract DividendContract is Initializable, UUPSUpgradeable, ReentrancyGuardUpgr
cycles[0].startBlocktime = block.timestamp;
}

/**
* Update the user stake lock period, only owner can call this function
* @param _lockPeriod the new lock period in seconds
*/
function updateLockPeriod(uint256 _lockPeriod) public onlyOwner {
lockPeriod = _lockPeriod;
}

/**
* Update the related proposal contract address, only owner can call this function
* @param _proposalContract the new staking token address
Expand Down Expand Up @@ -251,6 +259,7 @@ contract DividendContract is Initializable, UUPSUpgradeable, ReentrancyGuardUpgr
* @return true if the user unstake is locked, otherwise false
*/
function isUserUnstakeLocked(address user) public view returns (bool) {
// console.log("lockState[user] %d - block.timestamp %d < lockPeriod %d", lockState[user], block.timestamp, lockPeriod);
return block.timestamp - lockState[user] <= lockPeriod;
}

Expand Down
28 changes: 28 additions & 0 deletions test/test_dividend2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -700,4 +700,32 @@ describe("Devidend", function () {
);
}
});

it("test stake lock", async () => {
// Update the lock period to 5 seconds
dividend.connect(signers[0]).updateLockPeriod(5);

// signers[1] stake 1 DMC
await (await dividend.connect(signers[1]).stake(1)).wait();

// try unstake but will be reverted
await expect(dividend.connect(signers[1]).unstake(1)).to.be.revertedWith(
"Unstake is locked"
);

// sleep 3 seconds
mine(3, { interval: 1 });

// try unstake but will be reverted
await expect(dividend.connect(signers[1]).unstake(1)).to.be.revertedWith(
"Unstake is locked"
);

// sleep 1 seconds and try unstake
mine(1, { interval: 1 });
await (await dividend.connect(signers[1]).unstake(1)).wait();

dividend.connect(signers[0]).updateLockPeriod(0);

});
});

0 comments on commit 8380071

Please sign in to comment.