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.
Private
can only be called internally by the contract who owns the method. Derived contracts cannot access a private method of their parent contract.
What is the stack?
because the EVM is a stack machine rather than a register machine, all computations are done on a data region called the stack.
Difference between storage and memory
- storage is comparable to a hard drive. Reading the storage is expensive and initializing and modifying storage is even more expensive.
- memory is temporary. –> memory has a lower gas consumption. It is better to do intermediary calculations here and store the result in storage.
Where are state variables and local variables stored?
in storage by default
Where can memory be used?
Inside a function
Were are function arguments saved?
In memory by default .
What happens if we use the keyword “storage” when declaring an array called “myArray”?
Then we create a pointer to the numbers array’s storage location. If the storage keyword was not used, Solidity would still have created myArray as a pointer to the numbers array’s storage location.
What happens if we use the keyword “memory”?
int[] memory myArray = numbers;
// has no influence: myArray[0] = 0;
Then the array “myArray” only exists locally. So we create a copy of the array numbers and if we change something in myArray then the numbers array is not altered.
Where is the payable keyword required?
For declaring constructors and addresses that can receive/send Ether.
(e.g., constructor payable { /* … / }, function withdraw (address payable _to) public { / … */ }).
While implicit conversions are allowed from address payable to address, a casting function called
payable (<address>) must be used for conversions from address to address payable.
address public customer;
function transfer (uint amount) public { payable(customer).transfer(amount); } </address>
What is a Fallback function?
The fallback function is called when no other function matches the function call. (e.g. when Ether is sent to a contract without a receive function). It can’t have any parameters and doesn’t return anything.
What is function overloading?
When you define the same function twice with a different signature. This can be helpful if a method needs to be adapted to certain situations.
Example function sendEther(uint amount) { require(this.balance >= amount); payable(msg.sender).transfer(amount); } function sendEther(uint amount, address payable to) { require(this.balance >= amount); to.transfer(amount); }
If sendEther() is called without the address argument, the Ether will be sent to the caller. Otherwise, it will be sent to the address passed as parameter to the function.
What are named function calls?
Usually, function parameters are passed by their defined signature order. Solidity supports the concept of named calls. The named calls principle allows to pass function parameters explicitly via a dictionary.
return myAddFunction({ b: 2, a:4 });
What happens when the parent contract contains a function that is also present in the sub contract?
Then the functions are overloaded. In case both functions have the same signature, the sub contract’s function will overload the parent’s function. However, the parent function can still be explicitly accessed using the super keyword.
What keyword is used for inheritance in Solidity?
“is”
What is there to know about the FRO?
The function resolution order is not intuitive. When you draw the contracts in a tree like diagram then you start at the bottom go up and then horizontal before you go up further.
When is a contract abstract in Solidity?
A function is abstract if it does not have a body. A contract is abstract if one or more functions are abstract.
Can an abstract contract be compiled to bytecode?
No. A contract that inherits from an abstract contract must implement and override all methods from the base contract to be compliable.
What is an interface?
Similar to an abstract class but more restrictive. Does not allow to define constructor, variables, structs and enums. It cannot inherit from a contract or implement another interface.
Example interface CarInsurance { function payMonthlyFee() returns (boolean result); }
One contract can implement multiple interfaces at once.