Solidity Notes

김현학·2024년 6월 25일
0

ETH

목록 보기
1/1

state variables

contract MultisigTutorial {
  	address[] public owners;
	mapping(address => bool) public isOwner;
	uint public numConfirmationsRequired;
}
  1. owners: An array containing the addresses of all owners of the multi-signature wallet.
  2. isOwner: A mapping indicating whether an address is an owner.
  3. numConfirmationsRequired: The number of confirmations required for a transaction.

modifier

special keyword to amend the behavior of functions

mapping(address => bool) public isOwner;

modifier onlyOwner() {
  require(isOwner[msg.sender], "not owner");
  _;
}

onlyOwner modifier ensures that only owners can execute a function. It does this by checking whether the address of the caller is an owner.


constructor

defines a function that executed only once during the deployment of the contract.

uint public numConfirmationsRequired;

constructor(address[] memory _owners, uint _numConfirmationsRequired) {
	require(_owners.length > 0, "owners required");
  	require(
    	_numConfirmationsRequired > 0 &&
      	_numConfirmationsRequired <= _owners.length,
      	"invalid number of required confirmations"
    );
  
  for (uint i = 0; i < _owners.length; i++) {
  	address owner = _owners[i];
    
    require(owner != address(0), "invalid owner");
    require(!isOwner[owner], "owner not unique");
    
    isOwner[owner] = true;
    owners.push(owner);
  }

  numConfirmationsRequired = _numConfirmationsRequired;
}

It initializes essential parameters, in this case, the list of owners and the required number of confirmations. (temporarily assign to isOwner for assure uniqueness)


view

Functions can be declared view in which case they promise not to modify the state.


Order of functions


Contract Elements Order

  1. pragma
  2. import
  3. event
  4. error
  5. interface
  6. library
  7. contract

Orders in each [ contract / library / interface ]

  1. Type declarations
  2. State variables
  3. event
  4. error
  5. modifier
  6. function

Orders of functions

  1. constructor
  2. receive function (if exists)
  3. fallback function (if exists)
  4. external
  5. public
  6. internal
  7. private

Function Types

function (<parameter types>) {internal | external} [pure | view | payable] [returns (<return types>)]

0개의 댓글