Skip to content

Commit

Permalink
Testing coverage improved
Browse files Browse the repository at this point in the history
  • Loading branch information
ramijames committed Dec 15, 2023
1 parent 283dc1a commit d5894b2
Show file tree
Hide file tree
Showing 8 changed files with 64 additions and 9 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,19 @@ This is a simple lottery system in Solidity which can be deployed to EVMs.

## How it works

Each lottery has a maximum number of participants. The contract owner must set this on deployment.
Each lottery has a maximum number of participants. The contract owner must set this on deployment. Default is 10.

Each new lottery will run until the max number of participants is reached. Upon completion the contract will pay out to the randomly selected winner.

There is a contract fee which is subtracted from the total contract payout. This contract fee is set on deployment.

At the end of each round:

1. The winner is paid out
2. The manager gets his fee
3. Players are reset
4. The maximum number of players is increased

## Testing

Testing is using Hardhat.
Expand All @@ -22,4 +29,7 @@ npx hardhat test

Coverage includes:

- Ensuring that the deployment
- Should return the list of players
- Should call the enter function nine times which confirms that up to the maxPlayers it allows more players
- Should call the enter function one more time and have a winner which also resets the lottery to a zero state
- Should confirm that the maxPlayers is doubled each round
1 change: 1 addition & 0 deletions artifacts/build-info/4dc042bab0d530a483ccf98123e616cc.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion artifacts/build-info/9659705d15341296c1eaec44af821c13.json

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"_format": "hh-sol-dbg-1",
"buildInfo": "../../build-info/9659705d15341296c1eaec44af821c13.json"
"buildInfo": "../../build-info/4dc042bab0d530a483ccf98123e616cc.json"
}
17 changes: 15 additions & 2 deletions artifacts/contracts/simple-lottery.sol/SimpleLottery.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions cache/solidity-files-cache.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"_format": "hh-sol-cache-2",
"files": {
"/Users/ramijames/_CODE/simple-lottery/contracts/simple-lottery.sol": {
"lastModificationDate": 1702680471512,
"contentHash": "56f6def233975d36775984323a25f505",
"lastModificationDate": 1702682956016,
"contentHash": "6403b133d637105989b732edb77bd295",
"sourceName": "contracts/simple-lottery.sol",
"solcConfig": {
"version": "0.8.19",
Expand Down
7 changes: 6 additions & 1 deletion contracts/simple-lottery.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ contract SimpleLottery {
players = new address payable[](0);
winner = payable(address(0));
prize = 0;
maxPlayers = maxPlayers * 2;
}

function finishRound() internal {
Expand All @@ -40,7 +41,6 @@ contract SimpleLottery {

prize = (prize * 9) / 10;
players = new address payable[](0);
maxPlayers = maxPlayers * 2;

winner.transfer(prize);
earningsWallet.transfer(address(this).balance);
Expand Down Expand Up @@ -69,6 +69,11 @@ contract SimpleLottery {
return players;
}

// function to return maxPlayers
function getMaxPlayers() public view returns (uint256) {
return maxPlayers;
}

// This is dangerous! Recommended to use a public RNG oracle like Chainlink VRF
function random() private view returns (uint256) {
return uint256(keccak256(abi.encodePacked(block.prevrandao, block.timestamp, players.length)));
Expand Down
27 changes: 27 additions & 0 deletions test/simple-lottery.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,31 @@ describe("SimpleLottery", function () {
expect(players.length).to.equal(0);
console.log("players: ", players);
});

// should confirm that the maxPlayers is doubled each round
it("Should confirm that the maxPlayers is doubled each round", async function () {
// Deploy the SimpleLottery contract
const SimpleLottery = await ethers.getContractFactory("SimpleLottery");
const simpleLottery = await SimpleLottery.deploy();

// Wait for the contract to be mined
await simpleLottery.deployed();

// Call the enter function and set the amount to send
const amount = ethers.utils.parseEther("1");

// call the enter function nine times
for (let i = 0; i < 10; i++) {
await simpleLottery.enter({ value: amount });
}

// Call the getPlayers function
const maxPlayers = await simpleLottery.maxPlayers();
const max = await maxPlayers.toNumber();

// Perform assertions
// maxPlayers should be greater than the initial value of 10
console.log("maxPlayers: ", max);
expect(max).to.be.greaterThan(10);
});
});

0 comments on commit d5894b2

Please sign in to comment.