L7 - Ethereum Smart Contracts Flashcards
Is the source code of a contract stored on the blockchain?
No only the byte code.
Which state does a contract on the blockchain have?
A contract has its own, persistent state which is defined by state variables.
Steps from a Solidity Code to a Deployed Smart Contract
- Contract development
- Compilation
- Transaction creation
- Transaction mined
Which steps take place on chain?
- Transaction creation
4. Transaction mined
- Contract development
Smart contract code is written in Solidity
- Compilation
Compiler takes the Solidity code and produces EVM bytecode
- Transaction creation
The hex encoded bytecode is sent as transaction to the network.
- Transaction mined
The bytecode is put into a block and mined. The contract can now be used.
struct Tutor {
string firstName; string lastName;
}
mapping (address => Tutor) tutors; address professor;
-> What is this?
State variables
What is there to know about state variables?
- permanently stored in the contract’s storage
- changing the state requires a transaction and therefore costs ether
- reading the state of a contract is free and does not require a transaction
What is there to know about function modifiers?
- convenient way to reuse pieces of code
- changes the behavior of a function
- can execute code before or/and after the actual function
- low dash_ indicates where the actual function code is injected.
- often used for authentication
What is there to know about the constructor?
- exectued once when the contract is created
- cannot be called after the creation of the contract
- execution costs gas and more complex contructors lead to higher deployment costs
What is there to know about functions?
- can be used to read the state of the contract
- can be used to change the state of a contract
- consist of a name, signature, visibility, a type, list of modifiers, return type.
pure in a function
has no side effects so does not modify the blockchain
Can be seen as a subset of view functions which don’t modify the state but also don’t read from the state.
function add(uint a, uint b) public pure returns (uint sum) { return a +b }
constant in a function
function cannot be overwritten
view in a function
read-only access to the data. They do not modify any state variable nor alter the state of the blockchain. However, it can read from the state variables.
uint state = 5;
function add(uint a, uint b) public view returns (uint sum) { return a + b + state}