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}
payable in a function
By default, it is not possible to send ether to a function because the function will by default revert the transaction. This prevents that Ether is accidentally lost.
Therefore, Solidity implements payable functions.
mapping
generates key-value pair
Build-in first level object block
allows you to access the current block that is mined.
Build-in first level object msg
message object
Build-in first level object tx
transaction
external
External methods can be called by other contracts via transactions issued by a certain wallet. Methods declared as external are always publicly visible and can’t be called directly by the contract itself.
public
Can be called internally but also externally by other contracts and via transactions. State variables which are defined as public will by default have getter method created.
Internal
Internal methods can only be accessed by the contract itself or by any contract derived from it.