Skip to content

Instantly share code, notes, and snippets.

@andrewgcodes
Created February 16, 2025 03:38
Show Gist options
  • Save andrewgcodes/eda4b614c0cf01cb07687e021824c5c1 to your computer and use it in GitHub Desktop.
Save andrewgcodes/eda4b614c0cf01cb07687e021824c5c1 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.26+commit.8a97fa7a.js&optimize=false&runs=200&gist=
// SPDX-License-Identifier: MIT
// USE AT OWN RISK, MADE FOR HACKATHON BY BEGINNER!!!!
pragma solidity ^0.8.0;
contract LocationBasedWildfirePredictionMarket {
uint256 public marketCounter;
struct Market {
uint256 id;
int256 lat1;
int256 long1;
int256 lat2;
int256 long2;
uint256 deadline;
bool finalized;
bool outcome;
uint256 totalYes;
uint256 totalNo;
}
mapping(uint256 => Market) public markets;
mapping(uint256 => mapping(address => uint256)) public betsYes;
mapping(uint256 => mapping(address => uint256)) public betsNo;
mapping(uint256 => mapping(address => bool)) public claimed;
event MarketCreated(
uint256 indexed marketId,
int256 lat1,
int256 long1,
int256 lat2,
int256 long2,
uint256 deadline
);
event BetPlaced(uint256 indexed marketId, address indexed bettor, bool prediction, uint256 amount);
event MarketFinalized(uint256 indexed marketId, bool outcome);
event RewardClaimed(uint256 indexed marketId, address indexed bettor, uint256 reward);
function createMarket(
int256 lat1,
int256 long1,
int256 lat2,
int256 long2,
uint256 durationSeconds
) external {
require(lat1 < lat2, "lat1 must be less than lat2");
require(long1 < long2, "long1 must be less than long2");
marketCounter++;
uint256 deadline = block.timestamp + durationSeconds;
markets[marketCounter] = Market({
id: marketCounter,
lat1: lat1,
long1: long1,
lat2: lat2,
long2: long2,
deadline: deadline,
finalized: false,
outcome: false,
totalYes: 0,
totalNo: 0
});
emit MarketCreated(marketCounter, lat1, long1, lat2, long2, deadline);
}
function placeBet(uint256 marketId, bool prediction) external payable {
Market storage market = markets[marketId];
require(market.id != 0, "Market does not exist");
require(block.timestamp < market.deadline, "Betting period is over");
require(msg.value > 0, "Bet must be > 0");
if (prediction) {
betsYes[marketId][msg.sender] += msg.value;
market.totalYes += msg.value;
} else {
betsNo[marketId][msg.sender] += msg.value;
market.totalNo += msg.value;
}
emit BetPlaced(marketId, msg.sender, prediction, msg.value);
}
function finalizeMarket(uint256 marketId) external {
Market storage market = markets[marketId];
require(market.id != 0, "Market does not exist");
require(block.timestamp >= market.deadline, "Betting period not over yet");
require(!market.finalized, "Market already finalized");
uint256 random = uint256(
keccak256(abi.encodePacked(blockhash(block.number - 1), block.timestamp, marketId))
);
market.outcome = (random % 2 == 0);
market.finalized = true;
emit MarketFinalized(marketId, market.outcome);
}
function claimReward(uint256 marketId) external {
Market storage market = markets[marketId];
require(market.id != 0, "Market does not exist");
require(market.finalized, "Market not finalized yet");
require(!claimed[marketId][msg.sender], "Reward already claimed");
uint256 reward = 0;
uint256 totalPool = market.totalYes + market.totalNo;
if (market.outcome) {
require(betsYes[marketId][msg.sender] > 0, "No winning bet");
reward = (betsYes[marketId][msg.sender] * totalPool) / market.totalYes;
} else {
require(betsNo[marketId][msg.sender] > 0, "No winning bet");
reward = (betsNo[marketId][msg.sender] * totalPool) / market.totalNo;
}
claimed[marketId][msg.sender] = true;
payable(msg.sender).transfer(reward);
emit RewardClaimed(marketId, msg.sender, reward);
}
receive() external payable {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment