1

I have a simple code with 2 events in 2 different functions. However, one event is generated and the second one not.

pragma solidity ^0.4.9;

/* Contract accepting ethers during 10 minutes */
contract ReceiveEther {

    address receivingAccount;
    uint deadline;
    event EtherReceival(address sender, uint amount);
    event ContractDisposed(string contractName, uint time);

    /*  at initialization, setup the owner */
    function ReceiveEther(address _account) {
        // set the receiving account
        receivingAccount = _account;
        // set the deadline as 10 minutes
        deadline = now + 10 * 1 minutes;
    }   

    /* The function without name is the default function that is called whenever 
       anyone sends funds to a contract */
    function () public payable {
        uint amount = msg.value;
        receivingAccount.send(amount);
        EtherReceival(msg.sender, amount);
    }

    modifier afterDeadline() { if (now >= deadline) _; }

    /* checks if the time limit has been reached and ends the contract */
    function dispose() afterDeadline {
        ContractDisposed("ReceiveEther", now);
        suicide(receivingAccount);
    }
}

I deploy it as:

// must unlock the account we are creating the contract from so we can use it
personal.unlockAccount(eth.accounts[1],"Node01Account01")
// must set the _account parameter
_account = eth.accounts[1]
// contract code
var receiveether_sol_receiveetherContract = web3.eth.contract([{"constant":false,"inputs":[],"name":"dispose","outputs":[],"payable":false,"type":"function"},{"inputs":[{"name":"_account","type":"address"}],"payable":false,"type":"constructor"},{"payable":true,"type":"fallback"},{"anonymous":false,"inputs":[{"indexed":false,"name":"sender","type":"address"},{"indexed":false,"name":"amount","type":"uint256"}],"name":"EtherReceival","type":"event"},{"anonymous":false,"inputs":[{"indexed":false,"name":"contractName","type":"string"},{"indexed":false,"name":"time","type":"uint256"}],"name":"ContractDisposed","type":"event"}]);
var receiveether_sol_receiveether = receiveether_sol_receiveetherContract.new(
   _account,
   {
     from: web3.eth.accounts[0], 
     data: '0x6060604052341561000c57fe5b6040516020806101af83398101604052515b60008054600160a060020a031916600160a060020a03831617905542610258016001555b505b61015c806100536000396000f300606060405236156100255763ffffffff60e060020a6000350416634c86659e811461009e575b61009c5b600080546040513492600160a060020a039092169183156108fc02918491818181858888f1505060408051600160a060020a03331681526020810186905281517f75f33ed68675112c77094e7c5b073890598be1d23e27cd7f6907b4a7d98ac61995509081900390910192509050a15b50565b005b34156100a657fe5b61009c6100b0565b005b600154421061012c5760408051426020820152818152600c818301527f5265636569766545746865720000000000000000000000000000000000000000606082015290517f223c10abd9be53f20e899d6a493799dadfb2534befd381ad380ce06a14b1d41f9181900360800190a1600054600160a060020a0316ff5b5b5b5600a165627a7a72305820391aec2c5f68f456a73c07b6a1e5e8fe465c9de6a4820cb5606e0191dd09a2de0029', 
     gas: '4700000'
   }, function (e, contract){
    console.log(e, contract);
    if (typeof contract.address !== 'undefined') {
         console.log('Contract mined! address: ' + contract.address + ' transactionHash: ' + contract.transactionHash);
     // configure the event to watch for changes
     receiveether_sol_receiveether.EtherReceival({}, function(error, result){
       if (!error)
         console.log("[Ether received: Sender: " + result.args.sender + ", Amount: " + web3.fromWei(result.args.amount, "ether")+" ether(s)]");
     });
    receiveether_sol_receiveether.ContractDisposed({}, function(error, result){
       if (!error)
         console.log("[Contract " + result.args.contractName + " has been disposed.]");
     });
    }
 })

After I deploy and I interact with it:

> loadScript("contracts/ReceiveEtherWithLog.js")
null [object Object]
true
> null [object Object]
Contract mined! address: 0xb57df20fad55c6e91d6c525292e075c39bb47855 transactionHash: 0x2abb38d0b6754a1631c1cf6bcfad3bacc869af6cb6a0e23166fb9434cab00624
> var tx = {from:  personal.listAccounts[2], to: "0xb57df20fad55c6e91d6c525292e075c39bb47855", value: web3.toWei(1.0, "ether")}
undefined
> personal.sendTransaction(tx, "Node01Account02")
"0x928be3639e82345b3720841f11d6ac8d06f6bfcc0be34600f85d2a39364d771b"
> [Ether received: Sender: 0x7d6af3000bb220d013a3b02e3dd964f9fac7fa87, Amount: 1 ether(s)]
> receiveether_sol_receiveether.dispose({from: eth.accounts[2]})
"0x6000b524f2a5f03a645c2b7b4b0fda6b9358219004a75ba5b2a1c1105b304766"
>

Here I would expect that the ContractDisposed event is created and then a log is generated in the console. However, nothing happens. I don't know the origin of the problem. I don't know if the event is created or not. Any ideas?

2

Not possible to emit an event from a transaction that does:

suicide()

Just not compatible with event emission or return values. A calling contract would be able to emit an event for "I killed 0x123" but the victim can't speak.

Just as an FYI, suicide() was renamed to selfdestruct() in an effort to be more sensitive to personal tragedies individuals have experienced. Just mentioning it to save you the time of wading through docs in search of a definitive clarification. They do the same thing right now but we can expect end-of-support for the deprecated form at some point.

Hope it helps.

  • I was thinking that since I create the event before suicide(), it should work. According to your explanation, events are created after the function finishes, right? – Önder Gürcan Mar 17 '17 at 15:49
  • Possibly someone can chime in with a protocol-level explanation. Not so much about the sequence of events. Similar to the way throw reverts things that came before. As a developer take-away, selfdestruct() zeros out the contract's current state and the destroyed contract can't talk. Return values and event emissions don't fly. – Rob Hitchens - B9lab Mar 17 '17 at 16:43

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.