Created
June 19, 2025 12:56
-
-
Save coderwithsense/914f3d01a401eb975dc75014a1f1ec6b 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.30+commit.73712a01.js&optimize=false&runs=200&gist=
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.20; | |
import "./PredictionGame.sol"; | |
contract PredictionFactory { | |
address public immutable owner; | |
address[] public allGames; | |
mapping(address => address[]) public gamesByCreator; | |
mapping(string => address) public priceFeeds; // tokenPair => Chainlink Aggregator | |
mapping(address => bool) public isGame; // Track deployed game addresses | |
event GameCreated( | |
address indexed creator, | |
address indexed game, | |
string tokenPair, | |
uint256 targetPrice, | |
uint256 expiry | |
); | |
constructor() { | |
owner = msg.sender; | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner, "Not owner"); | |
_; | |
} | |
function setPriceFeed(string calldata tokenPair, address feed) external onlyOwner { | |
require(feed != address(0), "Invalid feed"); | |
priceFeeds[tokenPair] = feed; | |
} | |
function createPredictionGame( | |
string calldata tokenPair, // e.g., "ETH/USD" | |
uint256 targetPrice, // e.g., 3000 | |
uint256 expiry, // e.g., block.timestamp + 2 days | |
uint256 creatorFee, // e.g., 100 = 1% | |
uint256 minBetAmount // e.g., 0.01 ether | |
) external returns (address) { | |
address feed = priceFeeds[tokenPair]; | |
require(feed != address(0), "Feed not set"); | |
uint256 nowTs = block.timestamp; | |
require(expiry > nowTs, "Expiry must be in the future"); | |
require(expiry <= nowTs + 30 * 365 days, "Max expiry: 30 years"); | |
PredictionGame game = new PredictionGame( | |
msg.sender, | |
tokenPair, | |
targetPrice, | |
expiry, | |
creatorFee, | |
minBetAmount, | |
feed | |
); | |
address gameAddr = address(game); | |
allGames.push(gameAddr); | |
gamesByCreator[msg.sender].push(gameAddr); | |
isGame[gameAddr] = true; | |
emit GameCreated(msg.sender, gameAddr, tokenPair, targetPrice, expiry); | |
return gameAddr; | |
} | |
function getAllGames() external view returns (address[] memory) { | |
return allGames; | |
} | |
function getGamesByCreator(address creator) external view returns (address[] memory) { | |
return gamesByCreator[creator]; | |
} | |
function getPriceFeed(string calldata tokenPair) external view returns (address) { | |
return priceFeeds[tokenPair]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment