2

I have define update status event in my contract which basically print vote casted whenever transaction happen , however I am unable to see my event getting fired neither in my console or in testrpc console. Only thing I am getting is transaction id and timestamp. What's wrong I am doing here?

pragma solidity ^0.4.11;

contract Voting {

mapping (bytes32 => uint8) public votesReceived;

address sender;

bytes32[] public candidateList;
event Updatestatus (string _msg,address sender);
function Voting(bytes32[] candidateNames) {
candidateList = candidateNames;
}

function totalVotesFor(bytes32 candidate) returns (uint8) {
   if (validCandidate(candidate) == false) throw;
return votesReceived[candidate];
}

function voteForCandidate(bytes32 candidate) payable {
   if (validCandidate(candidate) == false) 
   throw;
   else{
   Updatestatus("First vote casted by",msg.sender);
   votesReceived[candidate] += 1;

 }
 }

   function validCandidate(bytes32 candidate) returns (bool) {
     for(uint i = 0; i < candidateList.length; i++) {
   if (candidateList[i] == candidate) {
    return true;
    }
   }
   return false;
   }
    }
2

Using a framework like Truffle makes your life easier as it abstracts most the complexity.

The result using the Truffle JS testing tools shows that your contract is correct and the event is triggered: enter image description here

I pushed the code into my Github if you want the whole project: here


Here is the details:

1. Initialisation

Init a Truffle project and create a sample contract (Metacoin)

truffle init 

2. Contracts

Copy your contract in the folder /contracts

3. Migration

The migration step corresponds to the deployment of the contracts on a blockchain (test-rpc, testnet, mainnet, ...). By default it deployed on an rpc-url localhost:8545 which points on test-rpc in my case

Then the file /migrations/2_deploy_contracts.js is used to code your deployment script

var Voting = artifacts.require("./Voting.sol");
module.exports = function(deployer) {
  deployer.deploy(Voting, ["greg", "bob", "tom"]);
};

To deploy on the blockchain, just run the following command

truffle migrate

4. Testing the contract

Truffle includes a Javascript testing tool. In /test/Voting.js

var Voting = artifacts.require("./Voting.sol");
contract('Voting', function(accounts) {

    it("test1", function() {
        var votingInstance;

        return Voting.deployed().then(function(instance) {
            votingInstance = instance;
            return votingInstance.voteForCandidate("greg", {from: accounts[0], value: web3.toWei(5, "ether")});

        }).then(function(tx) {
            console.log("Print events");
            console.log(tx.logs[0]);
        });
    });
});

To run the tests, simply execute:

truffle test

The callback of a transaction contains an object that includes logs (aka events):

{ tx: '0x76195a08487d0e1cc2dcd0518016165dbea87b3cb8aa935163d2b386597f0752',                              
  receipt:                                                                                               
   { transactionHash: '0x76195a08487d0e1cc2dcd0518016165dbea87b3cb8aa935163d2b386597f0752',              
     transactionIndex: 0,                                                                                
     blockHash: '0xf9fbed58551971facdfa504d5114725f0a78dcbbed78c76e03bf7b21b8a2a695',                    
     blockNumber: 32,                                                                                    
     gasUsed: 44355,                                                                                     
     cumulativeGasUsed: 44355,                                                                           
     contractAddress: null,                                                                              
     logs: [ [Object] ] },                                                                               
  logs:                                                                                                  
   [ { logIndex: 0,                                                                                      
       transactionIndex: 0,                                                                              
       transactionHash: '0x76195a08487d0e1cc2dcd0518016165dbea87b3cb8aa935163d2b386597f0752',            
       blockHash: '0xf9fbed58551971facdfa504d5114725f0a78dcbbed78c76e03bf7b21b8a2a695',                  
       blockNumber: 32,                                                                                  
       address: '0xe973ea06c0caa83807de42ab870b6102785c9a72',                                            
       type: 'mined',                                                                                    
       event: 'Updatestatus',                                                                            
       args: [Object] } ] }                                                                              
  • Hey @Greg Thanks for your great answer. It works for me now. Just one more question I want to create a webpage and connect this truffle with my webpage so that I can see these transaction logs on my browser console .Any good tutorial for such scenarios. Thanks again for helping me out. – mohammad obaid Jul 23 '17 at 17:58
  • You can find some ready-to-use boxes (Truffle + React) here. If you want to use other frontend technologies, you can try the JavaScript library truffle-contract – Greg Jeanmart Jul 23 '17 at 21:14

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.