Advertisement
Prosperity-Author

GameMonitor.sol

Jan 8th, 2025 (edited)
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | Cryptocurrency | 0 0
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.22;
  3.  
  4. interface ISimpleGame {
  5.     function claim() external;
  6.     function claimFixed() external;
  7.     function deposit() external payable;
  8.     function totalDeposit() external view returns (uint256);
  9.     function isFinished() external view returns (bool);
  10. }
  11.  
  12. contract GameMonitor {
  13.     ISimpleGame public simpleGame;
  14.     bool public whenFixed;
  15.     address deployer;
  16.    
  17.     constructor(address _simpleGameAddress) {
  18.         simpleGame = ISimpleGame(_simpleGameAddress);
  19.         deployer = msg.sender;
  20.     }
  21.  
  22.     function setWhenFixedStatus(bool _whenFixed) external {
  23.         whenFixed = _whenFixed;
  24.     }
  25.  
  26.  
  27.     function monitorAndClaim() public payable{
  28.         require(!ISimpleGame(simpleGame).isFinished(), "!Finished");
  29.         uint256 balance = simpleGame.totalDeposit();
  30.         uint256 balanceBefore = address(this).balance;
  31.        
  32.         // in the case where ETH is directly sent to the contract
  33.         if (balance >= 0.9 ether && balance < 1 ether) {
  34.             require(balanceBefore >= 0.1 ether, "Not enough balance to deposit");
  35.             try simpleGame.deposit{value: 0.1 ether}() {
  36.                 // Deposit successful
  37.             } catch {
  38.                 revert("Deposit failed - same block");
  39.             }
  40.         }
  41.         balance = simpleGame.totalDeposit();
  42.         if (balance >= 1 ether) {
  43.             if (whenFixed) {
  44.                 simpleGame.claimFixed();
  45.             } else {
  46.                 simpleGame.claim();
  47.             }
  48.         }
  49.         uint256 balanceAfter = address(this).balance;
  50.         require(balanceAfter > balanceBefore, "Claim failed");
  51.     }
  52.  
  53.     function isGameReady() public view returns (bool, bool) {
  54.         return (simpleGame.totalDeposit() >= 1 ether, simpleGame.totalDeposit() >= 0.9 ether);
  55.     }
  56.  
  57.     function setSimpleGameAddress(address _newAddress) external {
  58.         simpleGame = ISimpleGame(_newAddress);
  59.     }
  60.  
  61.     function claim() public {
  62.         require(msg.sender == deployer, "Only deployer can claim");
  63.         payable(msg.sender).transfer(address(this).balance);
  64.     }
  65.  
  66.  
  67.     receive() external payable {}
  68. }
Tags: GameMonitor
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement