3

i have the following contract function

    function voteForAnswer(bytes32 questionKey, bytes32[] answerKeys)
     returns (bool success)
{
    // is the sender allowed to do this?
    if (voters[msg.sender].enabled == false) {
        throw;
    }
    // TODO: check for question existence;
    if (questions[questionKey].alreadyVoted[msg.sender] == true) {
        throw;
    }
    questions[questionKey].alreadyVoted[msg.sender] = true;
    for (uint i; i <= answerKeys.length; i++) {
        questions[questionKey].answers[answerKeys[i]].voteCount += 1;
        VoterVotedFor(msg.sender, questionKey, answerKeys[i]);
    }
}

How can i invoke the contract function?

enter image description here

currently i get the following Mist-Message

enter image description here

What do i wrong?

Thanks Thomas

UPDATE

i adapted my contract so that i do not use any more an array in my function VoteForAnswer; i still get an error:

pragma solidity ^0.4.0;

contract owned {
    address public owner;

    function owned() {
        owner = msg.sender;
    }

    modifier onlyOwner {
        if (msg.sender != owner) throw;
        _;
    }

    function transferOwnership(address newOwner) onlyOwner {
        owner = newOwner;
    }
}

contract Roadshow is owned {

    bytes32 public text;  // shortname (up to 32 bytes)
    uint public start; // datetime when roadshow starts
    uint public end;   // datetime when roadshow ends
    mapping(address => Voter) public voters;

    struct Voter {
        bool enabled;  // if true, that person is currently allowed to vote
    }

    struct Answer
    {
        bytes32 text;
        uint voteCount; // number of accumulated votes
        // add more non-key fields as needed
    }

    struct Question
    {
        bytes32 text;
        mapping(bytes32 => Answer) answers; // random access by question key and answer key
        bytes32[] answerList; // list of answer keys so we can look them up
        // add more non-key fields as needed
        mapping(address => bool) alreadyVoted;
      }

    mapping(bytes32 => Question) questions; // random access by question key
    bytes32[] questionList; // list of question keys so we can enumerate them

    function Roadshow(bytes32 _name) {
        text = _name;
        start = now;
        voters[msg.sender].enabled = true;
    }

    function addQuestion(bytes32 questionKey, bytes32 text)
        onlyOwner
        returns(bool success)
    {
        // not checking for duplicates
        questions[questionKey].text = text;
        questionList.push(questionKey);
        return true;
    }

    function getQuestion(bytes32 questionKey)
        public
        constant
        returns(bytes32 wording, uint answerCount)
    {
        return(
            questions[questionKey].text,
            questions[questionKey].answerList.length);
    }

    function addAnswer(bytes32 questionKey, bytes32 answerKey, bytes32 answerText)
        onlyOwner
        returns(bool success)
    {
        questions[questionKey].answerList.push(answerKey);
        questions[questionKey].answers[answerKey].text = answerText;
        // answer vote will init to 0 without our help
        // questionStructs[questionKey].answerStructs[answerKey].voteCount = 0;

        return true;
    }

    function getQuestionAnswer(bytes32 questionKey, bytes32 answerKey)
        public
        constant
        returns(bytes32 answerText, uint answerVoteCount)
    {
        return(
            questions[questionKey].answers[answerKey].text,
            questions[questionKey].answers[answerKey].voteCount);
    }

    function getQuestionAnswerText(bytes32 questionKey, bytes32 answerKey)
        public
        constant
        returns(bytes32 answerText)
    {
        answerText = questions[questionKey].answers[answerKey].text;

        return answerText;
    }

    function getQuestionAnswerCount(bytes32 questionKey, bytes32 answerKey)
        public
        constant
        returns(uint answerCount)
    {
        answerCount = questions[questionKey].answers[answerKey].voteCount;

        return answerCount;
    }

    function getQuestionCount()
        public
        constant
        returns(uint questionCount)
    {
        return questionList.length;
    }

    function getQuestionAtIndex(uint row)
        public
        constant
        returns(bytes32 questionkey)
    {
        return questionList[row];
    }

    function getQuestionAnswerCount(bytes32 questionKey)
        public
        constant
        returns(uint answerCount)
    {
        return(questions[questionKey].answerList.length);
    }

    function getQuestionAnswerAtIndex(bytes32 questionKey, uint answerRow)
        public
        constant
        returns(bytes32 answerKey)
    {
        return(questions[questionKey].answerList[answerRow]);
    }

    // in Ethereum we cannot pass dynamically sized arrays
    // function voteForAnswer(bytes32 questionKey, bytes32[] answerKeys)
    function voteForAnswer(bytes32 questionKey, bytes32 answerKey)
         returns (bool success)
    {
        // is the sender allowed to do this?
        if (voters[msg.sender].enabled == false) {
            throw;
        }
        // TODO: check for question existence;
        if (questions[questionKey].alreadyVoted[msg.sender] == true) {
            throw;
        }

        questions[questionKey].alreadyVoted[msg.sender] = true;
        questions[questionKey].answers[answerKey].voteCount += 1;
        VoterVotedFor(msg.sender, questionKey, answerKey);

        return true;
    }

    function addVoter(address _voter)
        onlyOwner
        returns (bool success)
    {
        voters[_voter] = Voter(true);
        VoterAdded(_voter, this.text());
        return true;
    }

    event VoterAdded(address _newVoter, bytes32 _questionKey);
    event VoterVotedFor(address _voter, bytes32 _questionKey, bytes32 _answerKey);
}

3 Answers 3

2

Not currently possible to pass a dynamic length array in/out of a function. See over here for a similar question with before/after code. Returning dyanamic array from function

The gist is we need to think in fixed sized chunks.

Hope it helps.

Re: Your updated code.

All in all, a pretty good interpretation of the approach I suggested.

Your code is compiling in Browser Solidity. There is a warning near line 177. You use this.text() where text would have the same meaning. Point is it does compile and it should deploy.

In the picture above, gas 0 suggests a failure to compile.

I think you might find Browser Solidity a better tool for working out the contract itself. Much faster feedback. If something goes haywire in deployment, that's a separate issue.

Mist isn't my tool of choice for deploying contracts. What I can say is there should be a gas estimate (not 0) if the contract compiles (should). Also, possibility something like this glitch is in the way: Unable to define greeterContract in the Greeter tutorial. Breaking change in Solidity 0.4.9!

4
  • Ok, i changed the code to the following function voteForAnswer(bytes32 questionKey, bytes32 answerKey) returns (bool success) { if (voters[msg.sender].enabled == false) { throw; } if (questions[questionKey].alreadyVoted[msg.sender] == true) { throw; } questions[questionKey].alreadyVoted[msg.sender] = true; questions[questionKey].answers[answerKey].voteCount += 1; VoterVotedFor(msg.sender, questionKey, answerKey); return true; } but i get still an error from Mist
    – tkdp
    Commented Apr 1, 2017 at 15:49
  • Maybe put the whole contract in the question so we see the problem? Commented Apr 2, 2017 at 2:24
  • Your code compiled for me in (with one warning) in Remix (a.k.a. browser Solidity) solc 0.4.8 and 0.4.10. So, in theory error is different now. Gas:0 suggests compiler error. Commented Apr 2, 2017 at 7:10
  • i'm so sorry!!! i changed simply the pragma from ^0.4.0 to 0.4.8 and i'm happy => sorry to bother you with so stupid questions
    – tkdp
    Commented Apr 2, 2017 at 10:02
1

Perhaps what you are looking for is bytes - a dynamically-sized array of byte that gets the same special treatment as string does.

For comparison, the code provided uses a dynamically-sized array of bytes32 - that is, chunks of 32 bytes.

0

lets consider i have function whose input parameter is

bytes32

bytes32[ ] // array of type bytes 32

uint8

which looks like:

 function abc(bytes32 id, bytes32[ ] name,uint8 version) returns(bool)
    {  
        //
     }

so now to provoke the function (using remix) you have to pass parameters which looks like:

"0x12",["0x1262","0x12","0x12"],8

Your Answer

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

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