Share:
Notifications
Clear all

Earn While Learning Web3 Programming>> Start Here FREE!!!

[Solved] Intro To Smart Contract Event Listener In Solidity - Web3 Programming

1 Posts
1 Users
2 Reactions
554 Views
1
Topic starter

EXERCISE: Write Event and Emit for sendFund Function As Done In The Course Video:
i. See the the Smart Contract Code used in this course video in this forum below

ii. Allow anyone should be able to Send Fund (remove adminPriviledge restriction on the sendFund Function)

iii. Create Event and emit the event for senFund Function

iv. Post your code in this forum

(NOTE: please, don't go and copy paste from other students in the forum, try things on your own, so you can master it yourself)

 

 

NOTE:

This Is An Exercise From Our FREE Smart Contract Development With Solidity For Beginners Course (If you have not enrolled yet, you can join the course totally for FREE).

 

 

1 Answer
1
Topic starter

Event listener is essential in solidity to help index and log transactions for offchain use by dApps. 

Only max of 3 arguments can have the keyword indexed in the event but can have unlimited argument not indexed.

 

Event in solidity is written as follows:

event eventName(event to track here - see sample source code used in the course video below);

//We need to create an emiter that send the data needed to be logged to event above

emit eventName(event to track here - see sample source code used in the course video below);

 

Source code used in the video you can copy then do the exercise above to add event listener for addFund Function:

 

pragma solidity 0.8.7;

contract EventContract {

/*
SC SUMMARY: To learn about Event Listener in solidity
*/

//balance mapping
mapping (address => uint) Balance;

//EVENT
event trackWhaleTransfer(address indexed _sender, address indexed _recipient, uint _sendAmount);

//Admin Variable (Owner)
address admin;

//MODIFIER with Admin Priviledge Only or OnlyOwner
modifier adminPriviledge{

//REQUIRE ERROR CHECK
require(msg.sender == admin, "Can't Add Fund Because You Are Not An Admin");
_;
}

//CONSTRUCTOR
constructor() {

admin = msg.sender;

}

//add fund
function addFund(uint _newAmount) public adminPriviledge {

//REQUIRE ERROR CHECK
// require(msg.sender == admin, "Can't Add Fund Because You Are Not An Admin");

Balance[msg.sender] += _newAmount;

}

//check fund Balance
function checkFund() public view returns(uint) {

return Balance[msg.sender];

}

//transfer fund
function sendFund(address _recipient, uint _sendAmount) public {

//Initial Balance
uint initialBalance = Balance[msg.sender];

//REQUIRE ERROR handling
require(initialBalance >= _sendAmount, "You Don't Have Enough Balance For This Transfer - Try lower Amount");

//transfer fund by adding and substracting
Balance[msg.sender] -= _sendAmount;
Balance[_recipient] += _sendAmount;

//AfterTransfer Balance
uint afterTransferBalance = initialBalance - _sendAmount;

//ASSERT
assert(Balance[msg.sender] == afterTransferBalance);

//local variable - put msg.sender in a variable
address _sender = msg.sender;

//EVENT EMITTER
emit trackWhaleTransfer(_sender, _recipient, _sendAmount);

}

}

 

 

Share: