1

I am trying to compile a contract using truffle and I am not able to find what this error is related to.

EggFactory.sol

pragma solidity ^0.4.19;

contract EggFactory{

    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits; //10000000000000000

    event NewEgg(uint eggId, uint dna);

    struct Egg{
        uint dna;
        uint createdAt;
        bool hatched;
    }

    Egg[] public eggs;

    mapping (uint => address) public eggToOwner;
    mapping (address => uint) ownerEggCount;

    function createEgg(uint _dna) public{
        uint id = eggs.push(Egg(_dna,now,false)) - 1;
        eggToOwner[id] = msg.sender;
        ownerEggCount[msg.sender]++;
        emit NewEgg(id, _dna);
    }

    function generateRandomDna() private view returns (uint){
        // Get the now time, concatenate it with the last blockhash and hash it
        uint rand = uint256(keccak256(now, block.blockhash));
        return rand % dnaModulus;
    }

    function createRandomEgg() public{
        require(ownerEggCount[msg.sender] == 0);
        uint randDna = generateRandomDna();
        createEgg(randDna);
    }
}

EggHatching.sol

pragma solidity ^0.4.19;

import "./EggFactory.sol";

contract EggHatching is EggFactory{

    uint hatchTime = 1 days;

    function hatchEgg(uint eggId) public {
        require(msg.sender == eggToOwner[eggId]);
        require(hasHatched(eggId) == true);
        eggs[eggId].hatched = true;
    }

    function hasHatched(uint eggId) internal view returns (bool){
        return ((now - eggs[eggId].createdAt) >= hatchTime);
    }

    function getEggType(uint eggId) internal view returns (uint){

    }
}

I am aware that the logic of the contract in itself might be flawed in some aspects.

2
  • Can you share the exact error message you're seeing? And when do you see it? (What command did you run?)
    – user19510
    Apr 25 '18 at 13:50
  • $ truffle compile I obtain the following: Compiling ./contracts/EggFactory.sol... Compiling ./contracts/EggHatching.sol... Compiling ./contracts/Migrations.sol... InternalCompilerError: Unknown magic member. Compilation failed. See above. Apr 25 '18 at 14:28
0

block.blockhash takes a block number as parameter. Replace

uint rand = uint256(keccak256(now, block.blockhash));

with

uint rand = uint256(keccak256(now, block.blockhash(block.number)));

and it should compile.

Also, note that a miner can influence the value of now, and thus the value of rand.

3
  • Can the downvoter please give their reason?
    – Henk
    Apr 25 '18 at 14:24
  • Thank you. Is this "unknown magic member" something you came across by yourself in the past, or did find any documentation explaining it? Also, regarding the RNG, I'm completely aware of it. I'm currently in the process of experimentation with solidity and smart contracts and just wanted to verify some concepts I learned. Apr 25 '18 at 14:35
  • I found a compiler error using Remix on that line, and checked this page to find the usage of blockhash.
    – Henk
    Apr 26 '18 at 8:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.