Solidity Basics

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.
1 Like

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 :slight_smile:

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 :slight_smile:

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.

1 Like

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 :slight_smile:

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

1 Like

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 :slight_smile:

Apologies for the confusion :pray:

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;
}

}

1 Like

Hi @atlwarrior,
… and welcome to the forum! I hope you’re enjoying the course so far :slightly_smiling_face:

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?

1 Like

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 ?

1 Like

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.

image

1 Like

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 :nerd_face:

Carlos Z

1 Like

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! :partying_face:

1 Like

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

1 Like

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 :slight_smile: