Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // SPDX-License-Identifier: MIT
- pragma solidity ^0.8.22;
- interface ISimpleGame {
- function claim() external;
- function claimFixed() external;
- function deposit() external payable;
- function totalDeposit() external view returns (uint256);
- function isFinished() external view returns (bool);
- }
- contract GameMonitor {
- ISimpleGame public simpleGame;
- bool public whenFixed;
- address deployer;
- constructor(address _simpleGameAddress) {
- simpleGame = ISimpleGame(_simpleGameAddress);
- deployer = msg.sender;
- }
- function setWhenFixedStatus(bool _whenFixed) external {
- whenFixed = _whenFixed;
- }
- function monitorAndClaim() public payable{
- require(!ISimpleGame(simpleGame).isFinished(), "!Finished");
- uint256 balance = simpleGame.totalDeposit();
- uint256 balanceBefore = address(this).balance;
- // in the case where ETH is directly sent to the contract
- if (balance >= 0.9 ether && balance < 1 ether) {
- require(balanceBefore >= 0.1 ether, "Not enough balance to deposit");
- try simpleGame.deposit{value: 0.1 ether}() {
- // Deposit successful
- } catch {
- revert("Deposit failed - same block");
- }
- }
- balance = simpleGame.totalDeposit();
- if (balance >= 1 ether) {
- if (whenFixed) {
- simpleGame.claimFixed();
- } else {
- simpleGame.claim();
- }
- }
- uint256 balanceAfter = address(this).balance;
- require(balanceAfter > balanceBefore, "Claim failed");
- }
- function isGameReady() public view returns (bool, bool) {
- return (simpleGame.totalDeposit() >= 1 ether, simpleGame.totalDeposit() >= 0.9 ether);
- }
- function setSimpleGameAddress(address _newAddress) external {
- simpleGame = ISimpleGame(_newAddress);
- }
- function claim() public {
- require(msg.sender == deployer, "Only deployer can claim");
- payable(msg.sender).transfer(address(this).balance);
- }
- receive() external payable {}
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement