Solidity Flashcards
How to create a mapping
Solidity
mapping(_KeyType => _ValueType) myMapping;
What are modifiers
Modifiers can automatically check a condition prior to executing the function.
function close() onlyOwner { selfdestruct(owner); }
Types of functions
View or Constant Functions
- Functions can be declared view or constant in which case they promise not to modify the state, but can read from them.
Pure Functions
- Functions can be declared pure in which case they promise not to read from or modify the state.
- Operate on argument only.
Payable Functions
- Functions that receive Ether are marked as payable function.
Variable storage options
storage
- variable is a state variable (store on blockchain).
memory
- variable is in memory and it exists while a function is being called.
- Allocated by the callee and their value can be modified inside the function (they’re mutable).
- You can declare a variable inside a function memory located as well as it’s parameters.
calldata
- special data location that contains function arguments, only available for external functions.
- Value is allocated by the caller.
- No variables declared inside the function and no parameters of a function which is not external.
What are events
- EVM logging facilities
- Can be used to “call” JavaScript callbacks in the user interface of a dapp, which listen for these events.
Describe error handling
- assert(bool condition): throws if the condition is not met - to be used for internal errors.
- require(bool condition): throws if the condition is not met - to be used for errors in inputs or external components.
- revert(): abort execution and revert state changes