Description:
Smart contract deployed on Ethereum.
Blockchain: Ethereum
Source Code: View Code On The Blockchain
Solidity Source Code:
// SPDX-License-Identifier: MIT
// Access to play: https://husbando-bet.pages.dev/
pragma solidity ^0.8.30;
struct Commit {
uint256 blockNumber;
uint256 count;
}
contract HusbandoBet {
mapping(address => Commit) private revealBlock;
uint256 private count;
bool private active;
address private immutable owner;
uint256 private constant BET_AMOUNT = 0.002 ether;
uint256 private constant BET_PREMIUM = (BET_AMOUNT * 2);
uint256 private constant ODDS = 4; // 1 in 4 chance to win
error SendFailed();
modifier onlyOwner() {
require(msg.sender == owner, "Only owner");
_;
}
constructor() {
owner = msg.sender;
count = 0;
active = true;
}
function isActive() public view returns (bool) {
return (address(this).balance + BET_AMOUNT) >= BET_PREMIUM && active;
}
function betResult() private returns (bool) {
Commit memory commit = revealBlock[msg.sender];
require(commit.blockNumber < block.number, "Wait for next block");
require(commit.blockNumber + 250 > block.number, "Bet not found or expired");
bytes32 blockHash = blockhash(commit.blockNumber);
uint256 luckyNumber = uint256(blockHash) + commit.count;
delete revealBlock[msg.sender];
return (luckyNumber % ODDS) == 0;
}
receive() external payable {
require(isActive(), "Betting is not active");
require(msg.value == BET_AMOUNT, "Only accepting 0.002 ETH");
count += 1;
revealBlock[msg.sender] = Commit(block.number, count);
}
function withdraw() public {
require(betResult(), "You did not win");
(bool sent, ) = payable(msg.sender).call{value: BET_PREMIUM}("");
if (!sent) revert SendFailed();
}
function adminWithdraw() public onlyOwner {
require(!active, "Contract must be deactivated");
(bool sent, ) = payable(owner).call{value: address(this).balance}("");
if (!sent) revert SendFailed();
}
function activate() public onlyOwner {
active = true;
}
function deactivate() public onlyOwner {
active = false;
}
function fundContract() external payable onlyOwner {}
}
Submitted on: 2025-10-06 13:08:48
Comments
Log in to comment.
No comments yet.