Created
December 29, 2022 12:57
-
-
Save BellaBe/93fab8cb1d8c8b15107ed9fbd7dfda21 to your computer and use it in GitHub Desktop.
Smart contract general structure
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.13; | |
contract GeneralStructure{ | |
// state variables | |
uint public stateUintVariable; | |
string stateStringVariable; | |
address owner; | |
Person person; | |
// enumeration | |
enum Gender{male, female} | |
// struct | |
struct Person{ | |
string name; | |
uint age; | |
Gender gender; | |
bool isEmployed; | |
uint[] bankAccountNumbers; | |
} | |
// mapping | |
mapping(address => Person) people; | |
// constructor | |
constructor(uint _stateUintVariable, string memory _stateStringVariable){ | |
owner = msg.sender; | |
stateUintVariable = _stateUintVariable; | |
stateStringVariable = _stateStringVariable; | |
} | |
// modifier | |
modifier onlyBy(){ | |
require(msg.sender == owner, "Only owner"); | |
_; | |
} | |
// event | |
event ageRead(address, uint); | |
// function | |
function addPerson(address _addressIdentifier, string memory _name, uint _age, bool _isEmployed) public { | |
Person memory newHuman = Person(_name, _age, Gender.male, _isEmployed, new uint[](3)); | |
people[_addressIdentifier] = newHuman; | |
} | |
// function | |
function getAge(address _addressIdentifier) onlyBy() external returns (uint age){ | |
person = people[_addressIdentifier]; | |
emit ageRead(_addressIdentifier, person.age); | |
return person.age; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment