NicolaBernini / BlockchainAnalysis

Analysis of Blockchain related Stuff
3 stars 2 forks source link

Solidity Exercise - User Data 20210623 #16

Open NicolaBernini opened 3 years ago

NicolaBernini commented 3 years ago

Overview

Solidity Exercise - User Data 20210623

Solidity Exercise Description

https://github.com/kevaundray/SmartContractChallenges/blob/master/Ethereum/1/Info.md

Brief Description


- Create a smart contract that stores the name and age of a given person. 
- Accounts are not allowed to change the name of other accounts name and age.

Solution 1

Code

pragma solidity ^0.5.0; 
pragma experimental ABIEncoderV2;

contract Users{
    struct user_data{
        string name; 
        uint8 age;

    }

    struct _user_data{
        user_data data;
        bool is_present;
    }

    mapping(address => _user_data) data; 

    function set(string memory name, uint8 age) public {
        data[msg.sender].data.name = name; 
        data[msg.sender].data.age = age; 
        data[msg.sender].is_present = true; 
    }

    function get() public view returns (user_data memory) {
        require(data[msg.sender].is_present, "User not registered");
        return data[msg.sender].data; 
    }

}

Comments

Data Structures

  1. struct user_data
  1. struct _user_data
  1. mapping data

Functions

  1. get()
  1. set()
    • It encodes the passed data into a struct user_data that is associated to address of the user via the mapping data and it also sets the is_present flag

Appunto