Solidity 101 course

Hi Everyone,

I am doing the solidity course, right now I am at the end of it and I do not understand the Multisig Project code’s modifier section, where Philip’s code looks the following( I have left he other parts of the code just in case too):

address[] public owners;
    uint limit;
    
    mapping(address => mapping(uint => bool)) approvals;
    // [address] > [transaction ID] > [yes/no]
    
    struct Transfer {
        address payable recipient;
        uint approval;
        bool approved;
        uint amount;
        uint transactionID;
        
    }
    
    Transfer[] transferRequests;
    
    constructor(address[] memory _owners, uint _limit){
        owners = _owners;
        limit = _limit;
    }

modifier onlyOwners() {
        bool owner = false;
        for (uint i = 0; i < owners.length; i++){
            if(owners[i] == msg.sender){
                owner = true;
                
            }
        }
        require(owner == true);
        _;
    }

So I understand the for loop part as have to loop through with the i integet to connect the numbers to the addresses in the address[] array. But why do we need the require(owner == true) at the end? Can someone please explain to me?

Thank you.

Right, hopefully I can help.

The entire point of this modifier declaration block is to allow us to use the onlyOwners modifier in our function declarations, such as withdrawal() or createTransferReqest(). Our goal is to allow our three Owner Addresses (the owners[] array specified in the constructor) to execute the function, and prevent any other addresses from doing so.

(Explanation of for loop—skip this paragraph if you don’t need)
Since we have the owners[] array of our approved addresses, we need each onlyOwner function to ensure that msg.sender (the address calling the function) is on that approved list. This is done here by looping through the entirety of owners[] (specified with i < owners.length in the for declaration) and comparing msg.sender with the approved addresses. Should msg.sender in fact be one of those approved addresses, then we set our approval boolean, bool owner to true.

Now we use the brilliant require() function to ensure that our for loop’s hard work pays off. Without the require(owner == true) the for loop would have set the owner boolean to true for no reason.

Then, of course, once require() says that it is indeed a member of the owners array who is trying to call the onlyOwners function, the _; executes the code of said function, e.g. our onlyOwners withdrawal function.

Okay. BAM. Let me know if you have any questions and I’ll try to clarify! Also, others, please point out any mistakes I have made. I’m learning as well!

1 Like

An excellent explanation! I would like to see some course with you :slight_smile: Can you teach how to build a frontend with react?

1 Like

Hahah very kind of you @Bhujanga, thank you! I am still quite new to the space & I find that trying to explain concepts to other is extremely helpful for my own understanding.

At the moment the only experience I have with React is the Google search I just opened in a new tab :joy: With the dApp project(s) I have in mind, I’ve a strong feeling I’ll be delving into the world of React sooner rather than later. Have you taken the Academy’s React course? I’ve just added it to my ever-growing to do list :slight_smile:

1 Like

Yes, I already went through the react course once, but will redo it again for sure. I am diving into using react for dapp frontend development right now. Quite exciting stuff. :slight_smile:

1 Like

Thanks for the explanation!

I am also very new ( programming since 2 months) and sometimes it takes a long time understanding a thing or two :).

2 Likes

@Riki I came into the Academy with a bit of programming experience, but I have certainly hit roadblocks by now! I am currently baffled by an assignment in Ethereum Smart Contract Programming 201, and I have to keep reminding myself to be patient & thorough.

So don’t feel like you’re alone or behind, we’re all right here with ya :slight_smile:

1 Like

That is very exciting! What project are you working on, if you don’t mind sharing a little bit? I’m interested in developing some of my own dApps & would love to hear where you’re at in the process.

1 Like

You can check out my Repos on Github. and also find some demonstration videos on my YouTube
So far I did develop a VotingDapp (Front- & Backend), A frontend for a DEX, and my latest project is called Ulysses Contract, a smart contract to help hodlers commit :sweat_smile: