Skip to content

Instantly share code, notes, and snippets.

@hrkrshnn

hrkrshnn/bad.sol Secret

Created May 24, 2022 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hrkrshnn/1025ec2b7672b1a1e2e40b2ab9508a75 to your computer and use it in GitHub Desktop.
Save hrkrshnn/1025ec2b7672b1a1e2e40b2ab9508a75 to your computer and use it in GitHub Desktop.
A contract with a bad design
struct Bet {
address user;
uint bet;
bool won;
}
contract Lottery {
address immutable owner = msg.sender;
Bet[] bets;
// store how much each user
uint winnings;
function deposit(uint bet) payable external {
require(msg.value == 1 ether);
bets.push(Bet(msg.sender, bet, false));
}
function pickWinner() external {
require(owner == msg.sender);
uint winning_number = 0;
uint winners = 0;
// Imagine some magic code for generating randomness
// and assigning to the variable winning_number
for (uint i = 0; i < bets.length; i++) {
if (bets[i].bet == winning_number) {
bets[i].won = true;
winners++;
}
}
winnings = address(this).balance / winners;
}
function claim() external {
// function that allows people to claim
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment