{"id":20040003,"url":"https://github.com/carmhack/solidity-lottery-dapp","last_synced_at":"2026-06-05T13:31:17.375Z","repository":{"id":113278607,"uuid":"503281393","full_name":"carmhack/solidity-lottery-dapp","owner":"carmhack","description":null,"archived":false,"fork":false,"pushed_at":"2022-06-20T12:41:13.000Z","size":1141,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-02T06:43:07.575Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/carmhack.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-06-14T08:55:48.000Z","updated_at":"2022-06-16T15:25:02.000Z","dependencies_parsed_at":null,"dependency_job_id":"8ecab9cf-4681-4b6f-a235-b437350a6879","html_url":"https://github.com/carmhack/solidity-lottery-dapp","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/carmhack/solidity-lottery-dapp","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carmhack%2Fsolidity-lottery-dapp","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carmhack%2Fsolidity-lottery-dapp/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carmhack%2Fsolidity-lottery-dapp/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carmhack%2Fsolidity-lottery-dapp/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/carmhack","download_url":"https://codeload.github.com/carmhack/solidity-lottery-dapp/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/carmhack%2Fsolidity-lottery-dapp/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":33944671,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-05-26T15:22:16.424Z","status":"online","status_checked_at":"2026-06-05T02:00:06.157Z","response_time":120,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-13T10:40:02.396Z","updated_at":"2026-06-05T13:31:17.357Z","avatar_url":"https://github.com/carmhack.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Lottery Game\nThis repo contains an example project of a **lottery dApp**. The project was developed for Cadena's Ethereum 101 course and is for educational purposes only.\n\n#### Frontend Stack\n- React\n- Milligram CSS\n- ethers.js\n#### Backend Stack\n- Solidity\n- hardhat\n\n\n\n## Smart contract\nThe smart contract store different data\n```js\nuint256 private currentId;\nmapping(uint256 =\u003e Game) private games;\nmapping(uint256 =\u003e mapping(address =\u003e bool)) private uniquePlayers;\naddress private owner;\n```\n\n- **currentId**: id of the latest created games\n- **games**: list of all games (ref. to Game struct, below)\n- **uniquePlayers**: address list of unique player for a single game\n- **owner**: address of the owner\n\nThere is one struct called **Game** that describe a single lottery data.\n```js\nstruct Game {\n    uint256 id;\n    address[] players;\n    uint256 price;\n    uint256 total;\n    address winner;\n    bool hasWinner;\n    uint256 endDate;\n}\n```\n\n#### Create Game\n```js\nfunction createGame(uint256 _price, uint256 _seconds) payable public onlyOwner {\n    require(_price \u003e 0, \"Ticket price must be greater than zero\");\n    require(_seconds \u003e 0, \"Game time must be greater than zero\");\n    Game memory newGame = Game({\n        id: currentId,\n        players: new address[](0),\n        total: 0,\n        price: _price,\n        winner: address(0),\n        hasWinner: false,\n        endDate: block.timestamp + _seconds * 1 seconds\n    });\n\n    games[currentId] = newGame;\n    currentId++;\n}\n```\n\nThe function receives the ticket price and duration (in seconds) as input and creates the new game. Function is marked as `payable` and has the modifier `onlyOwner`.\n\n#### Take Part\n```js\nfunction takePart(uint256 _gameId) public payable {\n    Game storage game = games[_gameId];\n    require(block.timestamp \u003c game.endDate, \"Game is already complete\");\n    require(game.price == msg.value, \"Value must be equal to ticket price\");\n    \n    game.players.push(msg.sender);\n    game.total += msg.value;\n    bool alreadyTakePart = uniquePlayers[_gameId][msg.sender];\n    // Player can only take part 1 time\n    if (alreadyTakePart == false) {\n        uniquePlayers[_gameId][msg.sender] = true;\n    }\n}\n```\n\nThe function receives the id of the game you want to participate in. Check that the game has not already finished and that the `msg.value` is equal to the ticket price. If this is the first time the address has participated, it is set to `true` in the `uniquePlayers` structure. Function is marked as `payable`.\n\n#### Pick Winner\n```js\nfunction pickWinner(uint256 _gameId) public onlyOwner {\n    Game storage game = games[_gameId];\n    require(block.timestamp \u003c game.endDate, \"Game is already complete\");\n    require(!game.hasWinner, \"Game has already a winner\");\n    if (game.players.length == 1) {\n        require(game.players[0] != address(0), \"There are no players in this game\");\n        game.winner = game.players[0];\n        game.hasWinner = true;\n        (bool success, ) = game.winner.call{value: game.total}(\"\");\n        require(success, \"Transfer failed\");\n    } else {\n        uint256 winner = random(game.players) % game.players.length;\n        game.winner = game.players[winner];\n        game.hasWinner = true;\n        (bool success, ) = game.winner.call{value: game.total }(\"\");\n        require(success, \"Transfer failed\");\n    }\n}\n```\n\nThe function receives the id of the game whose winner you want to choose. If there was only one participant, the lottery `total` is turned over to his address. If there are more than 1 participants, a winner is chosen randomly. Function has the modifier `onlyOwner`.\n#### About randomness\nThe generated number is pseudo-random and, in a real context, it should be done using an oracle (e.g. chainlink VRF).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarmhack%2Fsolidity-lottery-dapp","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcarmhack%2Fsolidity-lottery-dapp","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcarmhack%2Fsolidity-lottery-dapp/lists"}