Additional Solidity Concepts - Discussion

As I understand it, the two are compatible: it gets executed every time the function is called but at the very beginning of it.

1 Like

What do we exactly mean by persistent data storage vs temporary data storage?

In non-blockchain programming these terms normally mean that the data is stored in the disk vs in RAM. In EVM however, this is different, hence my question.

1 Like

Yes, good point, @cesc :ok_hand:

The modifier is invoked in the function header, so in that sense it’s called when the function is called, but before the code in the function body is executed.

The “before” in the actual quiz answer means the modifier’s code is executed before the function body. But a correct answer could equally be what you’ve said …

Hi @cesc,

Persistent data storage means data that is stored in the contract state i.e. in the smart contract’s state variables (including any storage arrays and mappings). We say that this data is stored persistently, because after a function which modifies the contract state has been successfully executed, a transaction will have been broadcast to the network, added to a block, mined (or validated), and added to the blockchain. The data could remain in the contract state for a long time, or it could be changed in the very next function call. Either way, however, the data has still been added to the blockchain at some point, and has persisted in the contract state after at least one function execution.

In contrast, temporary data storage refers to data that the EVM only needs to store whilst a function is executing the code in its function body. For example, it may be needed as part of a computation, or held in a local variable until being assigned to a state variable later on in the function’s execution. In either case, while data is being stored locally and hasn’t been assigned to the contract state, it is only needed temporarily until the function has finished executing. It can then be discarded by the EVM and will not be added to the blockchain.

Let me know if you have any further questions :slight_smile:

1 Like

Need Help
As new remix ide is released i am not able to compile using older version its only showing
latest local version 0.8.7. what should i do to use older version to compile

Hey @imran82,

I’ve just replied to you about this in the other discussion topic.

You should have all of the compiler versions available in the Compiler-field dropdown at the top of the Solidity Compiler panel. If you don’t have this panel available to you in the menu on the left, you can install it by clicking the Plugin Manager icon (at the bottom of the menu, just above the Settings cog). You need to scroll down and find the Solidity Compiler in the Inactive Modules list.

If the only compiler version available to you in the Compiler field is the latest local version 0.8.7, then it’s possible that your browser-version of Remix has an issue at the moment. I had this happen to me a few weeks ago, and it took 24 hours to be resolved. If this is the case for you, then in the meantime you can change your contract’s pragma statement to this same version, and just use that instead of v0.7.5.

Also …

(What I mean by this is that the compiler version declared in the pragma statement of the contract displayed in the text editor will be automatically selected from the dropdown)

Auto Compile is really helpful, especially when you’re learning, because it will highlight any errors or issues as you code. It’s much easier to address and resolve issues one, or a few, at a time, rather than having a lot to correct, all together, at the end.

Let me know how you get on, and if you still have any problems with this after trying my suggestions.

So storage is non-volatile and memory is volatile to put it simply. Are once saved are they stored on the blockchain ledger?

In the photo below, with “depositTo” in “event”, and “msg.sender” in “emit balanceAdded”, does it mean money is deposited into the sender of the message? If not, what does it mean? Why is it “depositTo” and “msg.sender”?

The event decalartion needs a name for its arguments, the type and name, then, in the function, you want to trigger the event with the variables _toAdd (which is a uint) and msg.sender.

Carlos Z

So are “storage” variables declared within a function actually stored on permanent storage?

  1. If they are, how is that possible if the entire function is stored in memory and only has a lifetime for the function’s execution and then erased from memory?

  2. If they are not stored on permanent storage, then where are they stored?

1 Like

no the only way you can create global variables is to declare your var globally outside of functions. any vairable that you create inside a function is limited to the scope of that function,

Yes thank you. In the interim I tested it and the Solidity compiler doesn’t even let me specify storage within a function unless I assign it (as a reference) to an actual storage var declared in the contract’s scope…

1 Like

hi is event kind of the same as console log in JS?

Hi @Khudeja, Yes you can compare it with the console log-in js. Event is a way to log immutable data on the blockchain which can be monitored or accessed at a later time.

1 Like

function addUser(uint id, uint balance) public {
users[id] = User(id, balance);
}

function updateBalance(uint id, uint balance) public {
     users[id].balance = balance;
}

function getBalance(uint id) view public returns (uint) {
    return users[id].balance;

Hi @Filip

I tried to implement the event of the transfer function, but when I deploy the contract it doesn’t work… I don’t know where’s the mistake??

pragma solidity 0.8.18;

contract Bank{

    mapping (address => uint) balance;
    
    address owner;
    
    event balanceAdded(uint amount, address indexed depositedTo);

    event transferHistory(uint amount, address indexed transferredTo);

    modifier onlyOwner{
        require(msg.sender == owner);
        _;
    }
    
    constructor(){
      owner = msg.sender;
    }

    function addBlance(uint _toAdd) public onlyOwner returns (uint){
        emit balanceAdded(_toAdd, msg.sender);
        balance[msg.sender] += _toAdd;
        return balance[msg. sender];
    }

   function getBalance() public view returns (uint){
              return balance[msg.sender];
   }

   function transfer(address recipient, uint amount) public {
       require(balance[msg.sender] >= amount, "Balance not sufficient");
       require(msg.sender != recipient, "Don't transfer money to yourself");
       emit transferHistory( amount, recipient);
       uint previousSenderBalance = balance[msg.sender];

       _transfer(msg.sender, recipient, amount);

       assert(balance[msg.sender]== previousSenderBalance - amount);
    }

    function _transfer(address from, address to, uint amount) private {
        balance[from] -= amount;
        balance[to] += amount;
    }
}

Hi @Ahmad_Abd_Allah

The code looks correct. How did you find out it is not working? Did you receive any errors?

no, but it doesn’t show the in the console the logs transferHistory.

Can you share the data which you are seeing in the console and the code realted to this?
Are you testing this in remix or truffle?

Hi John,
I’m using Remix, but I’ll try it again, maybe I didn’t deploy it correctly.

Sure I’ll update you.

Thank you my friend for all your support.

Have a nice one!

1 Like