In this post, we will learn how to declare & use events for logging in Solidity Language.
Advertisements
Notice: This is one of the multi-post series of Learn Solidity - Build Decentralized Application in Ethereum. This is an attempt to teach you all about Solidity - A Programming Language for Ethereum Based Smart Contracts. If you want to take this as a video course please signup using below button.
pragma solidity 0.4.8;
// Events are the way to inform external monitoring entities about the
// cenrtain activities in the smart contracts
// they can also be used as a log in the system.
// lets checkout out this coin example where we will jump deeper
// in the next lecture.
contract Coin {
/*
* @title A Simple Subcurrency Example
* @author Toshendra Sharma
* @notice Example for the Solidity Course
* @dev This is only for demo the simple Coin example
*
*/
address public minter;
uint public totalCoins;
event LogCoinsMinted(address deliveredTo, uint amount);
event LogCoinsSent(address sentTo, uint amount);
// Please note the caps lock. Your mist walled will you capital letters
// to seperate th event name into a meaningful name.
// As I said Events allow the convenient usage of the EVM logging facilities.
// Events are inheritable members of contracts. When they are called,
// they cause the arguments to be stored in the transaction's log.
// Up to three parameters can receive the attribute indexed which
// will cause the respective arguments to be treated as log topics
// instead of data. The hash of the signature of the event is one of
// the topics except you declared the event with anonymous specifier.
// All non-indexed arguments will be stored in the data part of the log.
mapping (address => uint) balances;
function Coin(uint initialCoins) {
minter = msg.sender;
totalCoins = initialCoins;
balances[minter] = initialCoins;
}
/// @notice Mint the coins
/// @dev This does not return any value
/// @param owner address of the coin owner, amount amount of coins to be delivered to owner
/// @return Nothing
function mint(address owner, uint amount) {
if (msg.sender != minter) return;
balances[owner] += amount;
totalCoins += amount;
LogCoinsMinted(owner, amount);
}
function send(address receiver, uint amount) {
if (balances[msg.sender] < amount) return;
balances[msg.sender] -= amount;
balances[receiver] += amount;
LogCoinsSent(receiver, amount);
}
function queryBalance(address addr) constant returns (uint balance) {
return balances[addr];
}
function killCoin() returns (bool status) {
if (msg.sender != minter) throw;
selfdestruct(minter);
}
}