Solidity -041 GeneralStructure

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

// Contract GeneralStructure demonstrates various Solidity features including state variables,
// structs, modifiers, events, and enums.
contract GeneralStructure {
    // State variables are stored on the blockchain and represent the contract's storage.
    int public stateIntVariable; // Public integer variable, automatically creates a getter function.
    string stateStringVariable; // Private string variable, not accessible outside the contract.
    address personIdentifier; // Address variable, typically used to store Ethereum addresses.
    MyStruct human; // Custom struct variable, used to represent complex data structures.
    bool constant hasIncome = true; // Constant boolean variable, its value is set at compile time and cannot be changed.

    // Struct definition for MyStruct.
    // Structs allow the creation of custom types to represent grouped data.
    struct MyStruct {
        string name; // Name of the person.
        uint myAge; // Age of the person.
        bool isMarried; // Marital status of the person.
        uint[] bankAccountsNumbers; // List of bank account numbers.
    }

    // Modifier onlyBy.
    // Modifiers are used to change the behavior of functions in a declarative way.
    modifier onlyBy() {
        require(msg.sender == personIdentifier, "Caller is not authorized");
        _; // Continues execution of the modified function.
    }

    // Event declaration for ageRead.
    // Events allow logging to the Ethereum blockchain. Useful for notifying clients about contract activities.
    event ageRead(address indexed _personIdentifier, int age);

    // Enumeration declaration for gender.
    // Enums are used to create custom types with a limited set of 'constant values'.
    enum gender { male, female }

    // Function getAge.
    // Demonstrates use of a modifier, struct instantiation, enum usage, and event emission.
    function getAge(address _personIdentifier) external onlyBy() payable returns (uint) {
        // Instantiating struct with initial values.
        human = MyStruct("Ritesh", 10, true, new uint[](3));
        
        // Using enum to set a gender.
        gender _gender = gender.male;
        
        // Emitting an event with the personIdentifier and stateIntVariable.
        emit ageRead(personIdentifier, stateIntVariable);
        
        // Function must return a value, example provided below is arbitrary and should be replaced with actual logic.
        return human.myAge;
    }
}
 

//Deploy:

Solidity -041 GeneralStructure_第1张图片

你可能感兴趣的:(Solidity,智能合约,区块链,信任链,去中心化,分布式账本)