0

I am trying to solve the CoinFlip puzzle from Ethernaut

Every time I run my flipIt function through remix it goes through fine and runs, but it never hits the official CoinFlip Contract. as you can see from etherscan.

I even ran the flip function from the CoinFlip contract manually through remix in my previous instance and it works fine. I attempted a new instance because I'm honestly not sure what's going on.

I also checked out Smart Contract Programmers Video and I don't think I'm missing anything in terms of global variables or imports for interacting with an already deployed contract using another contract.

As a last attempt I tried this solution and it still didn't work.

My only guess is that perhaps the CoinFlip flip function needs to be external? But people have solved it so I don't know.

Every thing compiles fine and I'm using the 0.8.0 compiler. Here is my code below in Remix:

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

contract CoinFlipCaller {

  uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;

  function flipIt(address _coinFlipAddress) external {
    uint256 blockValue = uint256(blockhash(block.number - 1));
    uint256 coinflip = blockValue / FACTOR;
    bool side = coinflip == 1 ? true : false;
    CoinFlip coinFlipContract = CoinFlip(_coinFlipAddress);
    coinFlipContract.flip(side);
  }

}

contract CoinFlip {

  uint256 public consecutiveWins;
  uint256 lastHash;
  uint256 FACTOR = 57896044618658097711785492504343953926634992332820282019728792003956564819968;

  constructor() {
    consecutiveWins = 0;
  }

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

    if (lastHash == blockValue) {
      revert();
    }

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

    if (side == _guess) {
      consecutiveWins++;
      return true;
    } else {
      consecutiveWins = 0;
      return false;
    }
  }
}
3
  • In the link you sent. It did 'hit' the 'CoinFlip' contract. you need to check internal transaction: goerli.etherscan.io/address/…
    – minhhn2910
    Dec 31, 2022 at 3:49
  • Well that answers that, of course it's something silly like that haha. I ran the flipIt function again and theres like a 10 second delay after the flipIt transaction finishes and the CoinFlip contract to show an internal transaction, so future viewers be patient with etherscan updates. Thanks for pointing that out @minhhn2910 Dec 31, 2022 at 13:49
  • another thing for future viewers make sure to raise your gas limit as it kept failing every now and then due to an out of gas error. Dec 31, 2022 at 14:01

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.