5

I'm having trouble getting a history of events. I'm unsure if I am misunderstanding how events work, if I'm coding wrong, or if it has anything to do with the recent Ethereum attacks slowing down the network. My workflow is that I have a single contract deployed called "KnowledgeExchangeManager" and it in turn creates "KnowledgeExchange" contracts when you submit a question. When a KnowledgeExchange contract is created it also creates a KnowledgeExchangeEvent so that I can later look up the contracts that have been generated.

Contract:

pragma solidity ^0.4.2;

contract KnowledgeExchangeManager {

    address owner;

    event KnowledgeExchangeEvent(
        address indexed _from,
        address _exchange
    );

    function KnowledgeExchangeManager() {

        owner = msg.sender;

    }

    function submitQuestion(string _title, string _body) payable {

        KnowledgeExchange exchangeContract = new KnowledgeExchange(msg.sender, _title, _body);
        KnowledgeExchangeEvent(msg.sender, exchangeContract);

    }

    function kill() { 
        if (msg.sender == owner) selfdestruct(owner); 
    }

}

contract KnowledgeExchange {

    event BountyClaimed(
        address indexed _owner
    );

    struct Question {
        address owner;
        string title;
        string body;
    }

    struct Answer {
        address owner;
        string body;
    }

    Question question;
    Answer answer;
    bool public answered;

    function KnowledgeExchange(address questioner, string _title, string _body) payable {

        question = Question(questioner, _title, _body);
        answered = false;

    }

    function getQuestionDetails() returns (address, string, string, uint) {

        return (question.owner, question.title, question.body, this.balance);
    } 

    function submitAnswer(string _body) {

        bool success = msg.sender.send(this.balance);
        if (!success) {
            throw;
        }
        answered = true;
        answer = Answer(msg.sender, _body);
        BountyClaimed(msg.sender);

    }

    function getAnswerDetails() returns (address, string) {

        return (answer.owner, answer.body);
    }

    function getBounty() returns (uint256) {
        return this.balance;
    } 

    function kill() { 
        if (msg.sender == question.owner) selfdestruct(question.owner); 
    }

}

Javascript UI:

var accounts;
var account;
var knowledgeManager;

function refreshQuestions() {

 console.log("Refreshing Questions...");
   var exchangeEvent = knowledgeManager.KnowledgeExchangeEvent({fromBlock: 0, toBlock: 'latest'});
   exchangeEvent.watch(function(error, events) {
     console.log("Got Questions."); 
     if (!error) {
       console.log(events);
     } else {
       console.log(error);
     }

     exchangeEvent.stopWatching();
   });

};

function submitQuestion() {

  knowledgeManager.submitQuestion("Test Question", "Lorem ipsum dolor amet", {from: account, gas: 900000}, function(error, txId) {
    if (error) {
      console.log(error);
    } else {
      console.log(txId);
    }
  });

};

window.onload = function() {

  console.log("Loading App");

  web3.eth.getAccounts(function(err, accs) {
    if (err != null) {
      alert("There was an error fetching your accounts.");
      return;
    }

    if (accs.length == 0) {
      alert("Couldn't get any accounts! Make sure your Ethereum client is configured correctly.");
      return;
    }

    accounts = accs;
    account = accounts[0];
    knowledgeManager = web3.eth.contract(JSON.parse('[ { "constant": false, "inputs": [ { "name": "_title", "type": "string" }, { "name": "_body", "type": "string" } ], "name": "submitQuestion", "outputs": [], "payable": true, "type": "function" }, { "constant": false, "inputs": [], "name": "kill", "outputs": [], "payable": false, "type": "function" }, { "inputs": [], "type": "constructor" }, { "anonymous": false, "inputs": [ { "indexed": true, "name": "_from", "type": "address" }, { "indexed": false, "name": "_exchange", "type": "address" } ], "name": "KnowledgeExchangeEvent", "type": "event" } ]'))
            .at("0xb52e407F7Feb3C2D14b1721584dd3ea34166C716");

    refreshQuestions();
  });


}

I had assumed the javascript should be fetching all events when the page loads, but it rarely ever does. If it does work it will be after quite a while (5 min). I have tested my application in both Chrome with Metamask running and in Mist Browser. I'm using the morden test network.

1 Answers1

3

Try adding {} before {fromBlock...}:

var exchangeEvent = knowledgeManager.KnowledgeExchangeEvent({}, {fromBlock: 0, toBlock: 'latest'});

See How do I retrieve the Voted events from The DAO for further details, including adding further filters in this {} parameter.

BokkyPooBah
  • 40,274
  • 14
  • 123
  • 193