kindly need some help, where do i find the java script course?
Can anybody explain why option B is the correct answer for this one?
Why is it useful to be able to listen for Solidity events being fired?
- Because we can create a log of events in our contract
- Because frontends in dapps can react to changes in our contracts.
- Because external contracts can react to changes in our contracts.
Hi @Aida1
Itâs the course called JavaScript Programming for Blockchain Developers. Can you still not find it on the Academy website?
Here in the forum, all of the discussion topics for each course can be found in separate categories, which you can see displayed on the home page, on the left. The Category for the JavaScript course is called JavaScript Programming (marked olive-green). But the links to the relevant discussion topics for each part of the course and each course assignment are in the course itself on the Academy website.
Let me know if you are still experiencing difficulties, with some more specific details about where youâre trying to find it
You can also contact the support team with this type of query, especially if itâs a problem associated with the migration to the new Academy 2.0 website:
[email protected]
Hey @tanu_g,
The key to this question is the word listen. Event listeners can be written in JavaScript in our dappâs frontend. Event listeners listen out for events like a click on a specific element (e.g. a button) and then execute a function whenever that specific click occurs.
We can also write event listeners in JavaScript which listen out for, and capture, specific events emitted by our deployed smart contracts in the backend. This allows the frontend to capture key event data as soon as it is emitted. This event data can then be evaluated within the event listenerâs function, in order to trigger dynamic changes to the user interface to enhance the overall userâs experience e.g. notifications, alerts etc.
Let me know if anything is still unclear, or if you have any further questions
Thanks, @jon_m for making it clear.
Also, like you said emitted events can be captured, just wanted to know a little more about the bridge between an emit statement in solidity and event listener function in javascript.
Is this something like the event listener will receive the event log to browse or something else is there.
You need to use a library like web3.js (links below) to create an instance of your smart contract in your JavaScript file. You can then use that instance, together with the syntax specified in the libraryâs documentation (links below), to interact with the smart contract, and call specific functions (by their name), and set up event listeners that listen for specific events being emitted (also by name)
https://web3js.readthedocs.io/en/v1.3.4/
https://github.com/ChainSafe/web3.js
This obviously requires learning about the web3.js library and how to use it, and this will be covered in more advanced smart contract programming courses. There is a lot of set syntax to be learnt which can seem complicated at first, but once you get the hang of it, itâs quite repetitive and so you soon get the hang of it.
Once the smart contract function calls and event listeners are set up in your JavaScript file, and the smart contract deployed, whenever an event is âcapturedâ by a listener, an event
object is returned. One of the properties of this event object is another object called returnValues
. It is returnValues
that contains the actual values emitted by the event, each one assigned to a property with the same name as the corresponding event argument. You can then handle this data using dot notation to access the different property values e.g.
// In your smart contract
mapping(address => uint) private balances;
event balanceAdded(uint depositAmount, address accountHolder, uint newBalance);
function addBalance(uint _amount) public {
// other code here
emit balanceAdded(_amount, msg.sender, balances[msg.sender]);
}
/* In the event listener in your JavaScript file, you can access
each value emitted in the event, as follows: */
event.returnValues.depositAmount
event.returnValues.accountHolder
event.returnValues.newBalance
This is a very simplified explanation and example, but hopefully it gives you an idea of how it works in practice
If youâre feeling brave here is the section of the web3.js documentation about events:
https://web3js.readthedocs.io/en/v1.3.4/web3-eth-contract.html#contract-events
Hi Jonathan,
Thank you so much for your response, havenât checked the JavaScript programming course yet. But the thing is I have been going through all the topics under beginner courses and everything was okay, till I reached the first topic under Intermediate course. it feels like am off topic, I donât understand anything thatâs why am looking for the JavaScript course. Do you think it will make me understand these topics. Thanks
Hey @Aida1,
Yes, you need to do the JavaScript Programming course before attempting this Ethereum Smart Contract Programming (Solidity) 101 course â especially if you are a beginner programmer. The JavaScript Programming course is designed for complete beginners, and introduces a lot of fundamental programming concepts, which are assumed knowledge for this Solidity course, and the pace of this course is also much quicker. So Iâm not surprised you suddenly felt out of your depth here! The JavaScript course should actually have been included under the Beginner courses, and this is being corrected. Youâll find the JavaScript course amongst the full course library
Apologies for the confusion
Hi Filip, Iâm just starting this course and I have a question. On your video âVariables & Scopeâ, at the end, you have the code below where you add âmemoryâ to the string variable within the function scope only. Then, wouldnât you need to also add âpureâ to the function itself? Thanks!
pragma solidity 0.8.4;
contract HelloWorld {
function hello() public returns(string memory){
String memory message = âHello Worldâ;
return message;
}
}
Hi @atlwarrior,
⌠and welcome to the forum! I hope youâre enjoying the course so far
Correct â well observed!
This function should be marked as pure
, because it doesnât need to modify or read the contract state (i.e. state variables). We should therefore restrict the access that it has by defining itâs function type as pure
.
If the value assigned to the local variable message
was a value referenced from a state variable or a mapping, then the function would need to be able to read the contract state, but not modify it, so we would define itâs function type as view
.
The functionâs code will still compile if you leave the function type as undefined, but if you do this the compiler should generate a warning (in yellow/orange) because best practice, in terms of reducing security risk, is to restrict the access a function has to the contract state to only what is strictly necessary.
Also, just to confirm⌠having to explicity define a local variableâs data location (e.g. memory) has no bearing whatsoever on the functionâs type (pure
, view
, undefined or payable
). These are two separate concepts which are determined by separate criteria.
Iâm sure this was just a copy-and-paste error, but a variableâs data type should be written starting with a lower case letter (e.g. string
). The exceptions to this are when the data type is the name of a struct, a contract or an interface (provided you have started these names with a capital letter, which is the convention).
Let me know if anything is unclear, or if you have any further questions.
HI Fillip, I have a question regarding the private and public function lesson. You made the _transfer function private. Why would that specific function be private and not public? what woud the security risk be if the other contracts could see this function?
I have a function in the Bank contract and every time the getBalance() function is returning 0. Any help appreciated.
mapping(address => uint) balance;
function addBalance(uint _toAdd) public returns(uint){
require(_toAdd >0,"Balance should be greater than 0");
balance[msg.sender] += _toAdd;
return balance[msg.sender];
}
function getBalance() public view returns(uint){
balance[msg.sender];
}
Hi , is the msg.sender the address of the contract creator as said in the lecture or address of the entity calling the contract ?
Hi I was on the loops video and tried to do number+=2 to get 20 plus the initial input integer but it didnât work. What am I missing? Thank you.
Can you please share the error you are receiving?
no, msg.sender
refers to the function caller, lets say both of us have DAI, to know how many DAI have each of us, both of us will call the balanceOf
function, and this function will return us our proper balance for each of us, so msg.sender
will be the account address that invoke the function.
Hope it made myself clear. Let me know
Carlos Z
the screenshot shows number++
, if you change that to number = number + 2
for example, it should do the trick.
Carlos Z
Do you think itâs useful/important to complete the React Development course before doing the Ethereum Smart Contract 101 course?
I feel like it could be useful but Iâm absolutely chomping at the bit to rip through the smart contract course!
Hi peeps,
Got a question about the Struct from the getPerson code. Can someone plz explain to me the âuint _indexâ and why it is added? Please se below to know exactly what I mean.
âfunction getPerson(uint _index) public view returns(uint, string memory) { <â Function for getting persons age â_indexâ and string ânameâ
Person memory personToReturn = people[_index];
return(personToReturn.age, personToReturn.name);
}â
Many Thanks
Hi @DavidV,
There is less of a security risk if we restrict access to a function to only what is necessary. In our example, _transfer() is what we can call a helper function. Only the transfer() function, which is located in the same contract, needs to access it, and so we should restrict its visibility to private. Helper functions often provide functionality to more than one function. If a helper function is only called by other functions in the same contract, then it should be marked private
. However, if a helper function needs to be inherited by a derived contract, then it would need to be marked internal
. It would only need to be marked public
if it also needed to be called from an external contract.
Just let me know if you have any further questions