2

I am getting the follow error with the contract below when I compile in remix and javascript. Also how do I fix it.

UnimplementedFeatureError: Only in-memory reference type can be stored.

pragma solidity ^0.4.0;

contract TwoupFactory {
    address[] public deployedTwoUpGames;

    function createTwoUpGame(uint minimum) public {
        address newTwoup = new Twoup(minimum, msg.sender);
        deployedTwoUpGames.push(newTwoup);
    }

    function getDeployedCampaigns() public view returns (address[]) {
        return deployedTwoUpGames;
    }
}

contract Twoup {
    struct Player {
        uint BetAmount;
        address Address;
        bool PickedHeads;
    }

    struct TossResult {
        bool HeadsCoinOne;
        bool HeadsCoinTwo;
    }

    TossResult[] public tosses; 
    Player[] public players;
    address public spinner;    
    uint public kitty; 
    uint public totalBet;
    bool isGameCompleted;

    function Twoup(uint housekitty, address newSpinner) public payable
    {
        require(msg.value > 10 ether);
        spinner = newSpinner;
        kitty = housekitty;
        isGameCompleted = false; 
        totalBet = 0;
    }    

    function tossCoin() private view returns (bool) {
        return ((uint(keccak256(block.difficulty, now, players)) % 2) == 0);
    }

    function pickWinner() public 
    {
        require(msg.sender == spinner);
        require(isGameCompleted == false);

        tosses.push(TossResult({ HeadsCoinOne: tossCoin(), HeadsCoinTwo: tossCoin() }));
    }   
}
2

keccak256 will not support for struct datatype. Struct is complex datatype. Replace your tossCoin with length of an array. Or you need to calculate hash for struct and pass that hash to keccak256() method.

Here is two example.

  1. Replace players with players.length. I guess you dont want to get the length and replace it. If that not the case replace with 2nd method.
function tossCoin() private view returns (bool) {
    return ((uint(keccak256(block.difficulty, now, players.length)) % 2) == 0);
}
  1. Calculate hash for struct, might be useful for you.
pragma solidity ^0.4.0;

contract TwoupFactory {
    address[] public deployedTwoUpGames;

    function createTwoUpGame(uint minimum) payable public {
        Twoup  newTwoup = new Twoup(minimum, msg.sender);
        deployedTwoUpGames.push(newTwoup);
    }

    function getDeployedCampaigns() public view returns (address[]) {
        return deployedTwoUpGames;
    }
}

contract Twoup {
    struct Player {
        uint BetAmount;
        address Address;
        bool PickedHeads;
    }

    struct TossResult {
        bool HeadsCoinOne;
        bool HeadsCoinTwo;
    }

    TossResult[] public tosses; 
    Player[] public players;
    address public spinner;    
    uint public kitty; 
    uint public totalBet;
    bool isGameCompleted;

    function Twoup(uint housekitty, address newSpinner) public payable
    {
        require(msg.value > 10 ether);
        spinner = newSpinner;
        kitty = housekitty;
        isGameCompleted = false; 
        totalBet = 0;
    }    

    function tossCoin() private view returns (bool) {
        return ((uint(keccak256(block.difficulty, now, getStructHash())) % 2) == 0);
    }

    function getStructHash() public view returns(bytes32){
        bytes32 hash;
        for(uint index=0;index<players.length;index++){
            hash = keccak256(players[index].BetAmount, players[index].Address, players[index].PickedHeads, hash);
        }
        return hash;
    }

    function pickWinner() public 
    {
        require(msg.sender == spinner);
        require(isGameCompleted == false);
        TossResult memory result= TossResult({ HeadsCoinOne: tossCoin(), HeadsCoinTwo: tossCoin() });
        tosses.push(result);
    }   
}
0

Passing array of structs is not supported in solidity. Error is in line:

return ((uint(keccak256(block.difficulty, now, players)) % 2) == 0);

players is an array of struct. You can use array of bytes32, uint etc to pass this to your function. Like following code will work well.

return ((uint(keccak256(block.difficulty, now, players.length)) % 2) == 0);
  • God to hear this helped. Mark the answer accepted if this solved your problem. – Prashant Prabhakar Singh Feb 27 '18 at 4:21

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.