Skip to content

Commit

Permalink
Lottery contract
Browse files Browse the repository at this point in the history
  • Loading branch information
antico5 committed Sep 21, 2021
1 parent fa91ead commit cc3b5ee
Show file tree
Hide file tree
Showing 9 changed files with 28,507 additions and 0 deletions.
2 changes: 2 additions & 0 deletions lottery/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALCHEMY_API_KEY=
ROPSTEN_PRIVATE_KEY=
6 changes: 6 additions & 0 deletions lottery/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
node_modules
.env

#Hardhat files
cache
artifacts
41 changes: 41 additions & 0 deletions lottery/contracts/Lottery.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0;

import "@openzeppelin/contracts/access/Ownable.sol";
import "hardhat/console.sol";

contract Lottery is Ownable {
uint256 public constant minParticipants = 2;
uint256 public constant maxParticipants = 10;

address payable[] public participants;
address payable public lastWinner;

function participate() external payable {
require(msg.value == 0.1 ether, "ticket cost is 0.1 eth"); // 0.1 eth
require(
participants.length < maxParticipants,
"max participants reached for this round"
);

participants.push(payable(msg.sender));
}

function execute() external onlyOwner {
require(
participants.length >= minParticipants,
"need more participants"
);

uint256 randomNumber = uint256(keccak256(abi.encode(block.timestamp)));
uint256 winnerIndex = randomNumber % participants.length;
lastWinner = participants[winnerIndex];
lastWinner.transfer(address(this).balance);
delete participants;
}

function participantsCount() external view returns (uint256) {
return participants.length;
}
}
28 changes: 28 additions & 0 deletions lottery/hardhat.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import { task } from "hardhat/config";
import "@nomiclabs/hardhat-waffle";

import dotenv from 'dotenv'
dotenv.config()

// This is a sample Hardhat task. To learn how to create your own go to
// https://hardhat.org/guides/create-task.html
task("accounts", "Prints the list of accounts", async (args, hre) => {
const accounts = await hre.ethers.getSigners();

for (const account of accounts) {
console.log(await account.address);
}
});

// You need to export an object to set up your config
// Go to https://hardhat.org/config/ to learn more

export default {
solidity: "0.8.7",
networks: {
ropsten: {
url: `https://eth-ropsten.alchemyapi.io/v2/${process.env.ALCHEMY_API_KEY}`,
accounts: [`0x${process.env.ROPSTEN_PRIVATE_KEY}`],
},
},
};
Loading

0 comments on commit cc3b5ee

Please sign in to comment.