1

can some please explain what is event in solidium...what we can do with event in our contract...it will be nice if you kindly explain with some code....u can use the following code to explain with adding one or two events...

contract CarRentalServiceCustomer is GeneralAccount{
    string public customerName;
    string public postalAddress;
    string public phoneNumber;
    string public emailAddress;

    mapping(address=>CarRentService) public rentCar;

    struct CarRentService{ 
        bool active;
        uint lastUpdate;
        uint registrationFee;   // e.g. To register DriveNow or Car2GO, you have to pay approx 25 Euro
        uint256 rent;              // youtube video suggest it to uint256, then have to provide 18 digit number
    }

    function CarRentalServiceCustomer(string _name,string _postalAddress,
                                    string _phoneNumber,string _emailAddress) payable {
        customerName  = _name;
        postalAddress = _postalAddress;
        phoneNumber   = _phoneNumber;
        emailAddress  = _emailAddress;
    }

    function registerToCarRentalServiceProvider(address _providerAddress) onlyOwner {

        rentCar[_providerAddress] = CarRentService({
                                    active: true, 
                                    lastUpdate: now,
                                    registrationFee: 0,
                                    rent: 0
                                    });
    }

    function setRegistrationFee(uint256 _registrationFee) {
        if(rentCar[msg.sender].active){
            rentCar[msg.sender].lastUpdate      = now;
            rentCar[msg.sender].registrationFee = _registrationFee;
        }
        else{
            throw;
        }
    }

    function setCarRent(uint256 _rent) {
        if(rentCar[msg.sender].active){
            rentCar[msg.sender].lastUpdate = now;
            rentCar[msg.sender].rent       = _rent;
        }
        else{
            throw;
        }
    }

    function payRegistrationFeeToCarRentalServiceProvider(address _providerAddress) returns (bool){
        if(_providerAddress.send(rentCar[_providerAddress].registrationFee))
        {
            rentCar[_providerAddress].registrationFee = 0;
            return true;
        }
        else{
            return false;
        }
    }

    function payRentToCarRentalServiceProvider(address _providerAddress) returns (bool){
        if(_providerAddress.send(rentCar[_providerAddress].rent))
        {
            rentCar[_providerAddress].rent = 0;
            return true;
        }
        else{
            return false;
        }
    }

    function unsubscribeFromCarRentalService(address _providerAddress){
        if(rentCar[_providerAddress].active = false && rentCar[_providerAddress].rent == 0 
        && rentCar[_providerAddress].registrationFee == 0 ){
            rentCar[_providerAddress].active = false;
        } else {
            throw;
        }
    }

    function payTestFunds() payable{
        //This is a do nothing function
    }
}
0

1 Answer 1

1

Take a look here: http://solidity.readthedocs.io/en/develop/contracts.html#events

Basically events create log entries in the blockchain. I find them especially useful for functions that modify the state of the blockchain (transactions). Functions that modify the state of the blockchain cannot have return values, because their execution is not completed immediately, they are only finished once the block that contains the transaction has been mined.

To your example: Lets say you want to rent a car. How expensive that is may depend on certain parameters and conditions. So in order to let the user know how much he actually paid you might fire an event containing that information. The user can later(after the transaction has been mined) access the event's data by retrieving a transaction receipt for this transaction.

1
  • Hi Max. I think it's good answer and I see what you're getting at with mining. Maybe there's another way to say "Functions that modify the state of the blockchain cannot have return values" It strikes me as misleading this way. Commented Feb 10, 2017 at 1:14

Not the answer you're looking for? Browse other questions tagged or ask your own question.