What it does
This smart contract code uses the Ethereum Blockchain to issue lottery tickets to paying customers, who then have a random chance of winning everyone else's bets for themselves.
How I built it
I used the Ethereum solidity Remix IDE.
Challenges I ran into
Integrating the contract into a browser, as I was not skilled with Javascript.
Accomplishments that I'm proud of
I managed to learn to read and use an Ethereum contract in a matter of days.
What I learned
There is more to a Decentralized app than just the contract, some javascript will be necessary to build a full UI.
The Code
pragma solidity ^0.4.11;
contract Lotto {
mapping(address => uint) internal players_wager;
mapping(uint => address) internal players;
uint internal num_of_users = 0;
uint public total_wagers = 0;
address internal owner;
string public game_name;
uint internal rand;
uint public winning_number;
function Lotto() {
owner = msg.sender;
game_name = "Ethereum Lotto";
}
function wager() public payable {
if (msg.value > 0) {
if (players_wager[msg.sender] == 0) {
players[num_of_users] = msg.sender;
num_of_users += 1;
}
players_wager[msg.sender] += msg.value;
total_wagers += 1;
//msg.value;
}
}
function wager_ether_value(address addr) returns(uint){
return players_wager[addr];
}
function user_address(uint nb) returns(address){
return players[nb];
}
function test_random() returns(uint) {
rand = uint(block.blockhash(block.number-1)) % total_wagers;
return rand;
}
function decide_winner() public returns(uint) {
if (msg.sender == owner) {
uint sum = 0;
winning_number = uint(block.blockhash(block.number-1)) % total_wagers;
for (uint i=0; i < num_of_users; i++) {
sum += players_wager[players[i]];
if (sum >= winning_number) {
selfdestruct(players[i]);
return;
}
}
}
}
}
Built With
- ethereum
- solidity
Log in or sign up for Devpost to join the conversation.