首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >以太:用bytes32数组调用契约函数

以太:用bytes32数组调用契约函数
EN

Ethereum用户
提问于 2017-03-30 09:57:04
回答 2查看 5.1K关注 0票数 3

我有以下合同职能

代码语言:javascript
复制
    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]);
    }
}

如何调用契约函数?

目前,我收到以下的迷雾信息

我做错什么了?

谢谢托马斯

更新

我调整了我的契约,使我不再在函数VoteForAnswer中使用数组;我仍然得到一个错误:

代码语言:javascript
复制
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);
}
EN

回答 2

Ethereum用户

发布于 2017-03-30 11:28:43

也许您要寻找的是bytes -- 获得与string所做的一个动态大小的byte数组。

作为比较,提供的代码使用了一个动态大小的bytes32数组,即32个字节的块。

票数 1
EN

Ethereum用户

发布于 2017-11-10 12:50:21

假设我有一个函数,其输入参数是

代码语言:javascript
复制
bytes32

bytes32[ ] // array of type bytes 32

uint8

看上去像是:

代码语言:javascript
复制
 function abc(bytes32 id, bytes32[ ] name,uint8 version) returns(bool)
    {  
        //
     }

因此,现在要激发函数(使用混合),您必须传递参数,如下所示:

代码语言:javascript
复制
"0x12",["0x1262","0x12","0x12"],8
票数 0
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/13729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档