Solidity Basics

@filip hello filip!
I am excited to start learning the Ethereum Smart Contracts.
I am facing one problem right now, which is my payment plan on ivanontech academy.
I thought it is monthly payment plan., but it withdraw all of the 1400 dollars. I am very fine with paying monthly payment plan, but now i have a home monthly payment waiting for me, and it is very big amount for me to pay the academy at one.
I have also contacted the support team, if you could help me to reach out to them, because my home monthly mortgage payment is coming due soon. I am very sorry, I should have know before hand. I was planning on enrolling the academy with monthly payment plan. Can you help me to get my refund back sooner. I will be very appreciate you. thank you teacher.

Hi, @filip
How do you restrict that one address can add multiple key->values?
something like if (address = null) … ?

@Frederic_Vandenplas
Thanks for reaching out!

To add validations (restriction), the best way is to use require().

so in add multiple key->values function you only need to add

require(address == "the address you want to be create of maps")

check out: https://solidity.readthedocs.io/en/v0.6.0/control-structures.html#error-handling-assert-require-revert-and-exceptions

1 Like

@CryptoKhash
Support team will reach to you in less than 24 hours, please wait for them to help you solve that problem :slight_smile:

1 Like

Ok I see now.
Thanks Taha!

1 Like

Great! Thank you for your help Nielvosloo!

1 Like

I took the else if example and expanded it. How many generations are they now and when they begin and end???

pragma solidity 0.5.12;

contract ContactData{
    struct Person {
        uint id;
        string name;
        uint age;
        uint height;
        bool GenBoomer;
        bool GenXer;
        bool GenYer;
        bool GenZer;
      
    }
    mapping(address => Person) private people;

    // Person[] public people;
    function createPerson(string memory name, uint age, uint height) public {
       address creator = msg.sender;
       // creates a person fastest way
       
       // people.push(Person(name, age, height));
       
       // creates a person easier to read but longer
         Person memory newPerson;
         newPerson.name = name;
         newPerson.age = age;
         newPerson.height = height;
         
         if (age >= 58){
             newPerson.GenBoomer = true;
         }  
         else if(age >= 41){
             newPerson.GenXer = true;     
         }  
         else if(age >= 39){
             newPerson.GenYer = true;
         }  
         else if(age <= 24){
             newPerson.GenZer = true;
         }
         
         else{
             newPerson.GenBoomer = false;
         }
    
    
        people[creator] = newPerson;
    }
    function getPerson() public view returns(string memory name, uint age, uint height, bool 
    GenBoomer, bool GenXer, bool GenYer, bool GenZer){
        
        //people[msg.sender];
        address creator = msg.sender;
        return (people[creator].name, people[creator].age, people[creator].height, 
        people[creator].GenBoomer, people[creator].GenXer, people[creator].GenYer, 
        people[creator].GenZer);
    }
  
}
2 Likes

@Maximus

Check this out!

pragma solidity 0.5.12;

contract HelloWorld {
    
    struct Person {
        string name;
        uint age;
        uint height;
    }
    Person[] public people;
    
    function createPerson(string memory name, uint age, uint height) public {
        
        people.push(Person(name, age, height));
    }
}

In your code:

  1. Line 5: you have mentioned struct perso and not struct Person
  2. Line 16: you have not declared length of array as a parameter of struct. Hence you are getting the error.

Hope this helps :slight_smile:

3 Likes

Hi @Maximus,

Just to add to what @Taha has already explained, you also need to move the createPerson function’s closing curly bracket to the end of the function (just like Taha’s done in the code example he’s posted for you. At the moment you’re closing the function body as soon as you’ve opened it.

So, you can either omit people.length (as in Taha’s example) or include the additional struct property as in the video, and then on the creation of each new person, the value people.length evaluates to will be assigned to that property.

Just one final thing. Please don’t post screen shots. They are difficult to read, and we can’t copy and paste your code in order to execute and test it, if we need to. You should format your code before posting it here in the forum. Click on the </> icon in the menu at the top of this forum’s text editor. This will give you 2 sets of 3 back ticks.
```
input your code here
```
If you now input your code between these, you will end up with it nicely formatted. You should always format the code you post, as it is then much clearer and easier to read, and easier for someone else to copy and paste into a text editor for review and execution. It also makes it easier for you to organise your code with the correct spacing and indentation etc. and to spot any errors or copy-and-paste slips.

2 Likes

Hi @keithra1948,

Great idea, and you’ve made a good start. You’ve now got the data you need about the different generations and when they begin and end (provided by @Taha) . Also, consider the following:

  1. Instead of creating lots of separate properties with Boolean values (one for each generation) you can achieve the same functionality with just one property (e.g. generation) and use your conditional execution to assign a value to this property based on the generation of each new person e.g.
    if (age >= 120){
        newPerson.generation = "lost";
    }
    else if ...  // etc.
  1. Your final else statement should capture a new person whose age fails all of the preceding conditions, and either:
    (i) assigns the value of the most recent generation used in your categorisation to the generation property;
    *OR (if a previous condition has already accounted for the most recent generation used in your categorisation, and there are still some possible ages which would not be captured by one of the previous conditions)
    (ii) assigns a value to the generation property which represents the fact that no generation has been allocated to this person.
    At the moment your else statement captures a person between the ages of 25 and 38, but then doesn’t “assign” this person to any generation, which of course shouldn’t happen.

  2. Now that the creator address is used as the key in the mapping for each new person instance, the id property is no longer needed in the Person struct.

2 Likes

Enjoying this course!! very productive and excellent explained by Filip.

I want to share an screenshot of how I am adding notes for the code for every key considerations. They are attached a line below the code!

3 Likes

That’s great that you’re really enjoying the course, @josephg! :muscle:

That’s also a very good idea to add notes to your code. This will help you to process what the code and key concepts are doing. It should also help you to remember things more easily, and it will provide a useful reference for you as you progress further.

Just one observation:
You are right that we don’t use id as the key in our mapping in this contract. However, there is nothing stopping us from using uint id, it’s just that we choose to use an address as the key, instead.

4 Likes

Thanks for the feedback!

1 Like

I finally made it over to the Ethereum Smart Contract Programming 101 course today! I’m super excited to jump into all the great material the Academy has provided. Have a great day!

1 Like

Great to have you here @scornkrypto! :smiley:

And great that you’re so pumped and ready to dive into Solidity :muscle:

Do check out other posts here in the forum if you’re not sure about something during the course and are looking for further clarification — you’ll find lots of further explanations here. But if you can’t find the answer you’re looking for then feel free to post your question and we’ll do our best to help you out! :slight_smile:

2 Likes

Thank you… Will do that i the future :smile: :

1 Like

Hello… I’m building off of the HelloWorld contract. I’m building a Web3.js interface. Now, I’m trying to call the withdrawAll() function back into my wallet. any tips or suggestions?

Solidity:

    function withdrawAll() public onlyOwner returns(uint) {
        uint toTransfer = balance;
        balance = 0;
        msg.sender.transfer(toTransfer);
        return toTransfer;
    }

Javascript:

document.getElementById('withdrawButton').addEventListener('click', () => {
     console.log('withdraw the funds !');
     myContract.methods.withdrawAll().call()
         .then(function (result) {
               console.log(result);
         });
});

Since you create a new thread for the same, answered to your question here.

Thanks

2 Likes