Ich habe die folgende Vertragsfunktion
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]);
}
}
Wie kann ich die Vertragsfunktion aufrufen?
aktuell bekomme ich folgende Mist-Meldung
Was mache ich falsch?
Danke Thomas
AKTUALISIEREN
Ich habe meinen Vertrag so angepasst, dass ich in meiner Funktion VoteForAnswer kein Array mehr verwende; Ich bekomme immer noch einen Fehler:
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);
}
Derzeit ist es nicht möglich, ein Array mit dynamischer Länge in/aus einer Funktion zu übergeben. Siehe hier für eine ähnliche Frage mit Vorher/Nachher-Code. Rückgabe eines dynamischen Arrays von der Funktion
Das Wesentliche ist, dass wir in Stücken fester Größe denken müssen.
Ich hoffe es hilft.
Betreff: Ihr aktualisierter Code.
Alles in allem eine ziemlich gute Interpretation des von mir vorgeschlagenen Ansatzes.
Ihr Code wird in Browser Solidity kompiliert. Es gibt eine Warnung in der Nähe von Zeile 177. Die Verwendung von this.text()
where text
hätte dieselbe Bedeutung. Der Punkt ist, dass es kompiliert und bereitgestellt werden sollte .
Im Bild oben gas 0
deutet auf einen Fehler beim Kompilieren hin.
Ich denke, Sie finden Browser Solidity vielleicht ein besseres Werkzeug, um den Vertrag selbst auszuarbeiten. Viel schnelleres Feedback. Wenn bei der Bereitstellung etwas schief geht, ist das ein separates Problem.
Mist ist nicht mein bevorzugtes Werkzeug für die Bereitstellung von Verträgen. Was ich sagen kann ist, dass es eine Gasschätzung (nicht 0) geben sollte, wenn der Vertrag erstellt wird (sollte). Außerdem besteht die Möglichkeit, dass so etwas wie dieser Fehler im Weg ist: GreeterContract kann im Greeter-Tutorial nicht definiert werden. Breaking Change in Solidity 0.4.9!
Vielleicht suchen Sie nach bytes
einem Array mit dynamischer Größe byte
, das die gleiche Sonderbehandlung erhält wiestring
dies der Fall ist.
Zum Vergleich verwendet der bereitgestellte Code ein Array mit dynamischer Größe bytes32
, dh Blöcke von 32 Bytes.
Nehmen wir an, ich habe eine Funktion, deren Eingabeparameter ist
bytes32
bytes32[ ] // array of type bytes 32
uint8
das sieht aus wie:
function abc(bytes32 id, bytes32[ ] name,uint8 version) returns(bool)
{
//
}
Um nun die Funktion (mit Remix) zu provozieren, müssen Sie Parameter übergeben, die wie folgt aussehen:
"0x12",["0x1262","0x12","0x12"],8
tkdp
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; }
aber ich bekomme immer noch eine Fehlermeldung von MistRob Hitchens
Rob Hitchens
tkdp