L7 - Ethereum Smart Contracts Flashcards

1
Q

Is the source code of a contract stored on the blockchain?

A

No only the byte code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which state does a contract on the blockchain have?

A

A contract has its own, persistent state which is defined by state variables.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Steps from a Solidity Code to a Deployed Smart Contract

A
  1. Contract development
  2. Compilation
  3. Transaction creation
  4. Transaction mined
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Which steps take place on chain?

A
  1. Transaction creation

4. Transaction mined

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q
  1. Contract development
A

Smart contract code is written in Solidity

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q
  1. Compilation
A

Compiler takes the Solidity code and produces EVM bytecode

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q
  1. Transaction creation
A

The hex encoded bytecode is sent as transaction to the network.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q
  1. Transaction mined
A

The bytecode is put into a block and mined. The contract can now be used.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

struct Tutor {
string firstName; string lastName;
}
mapping (address => Tutor) tutors; address professor;

-> What is this?

A

State variables

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is there to know about state variables?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is there to know about function modifiers?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is there to know about the constructor?

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is there to know about functions?

A
  • 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.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

pure in a function

A

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 }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

constant in a function

A

function cannot be overwritten

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

view in a function

A

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}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

payable in a function

A

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.

18
Q

mapping

A

generates key-value pair

19
Q

Build-in first level object block

A

allows you to access the current block that is mined.

20
Q

Build-in first level object msg

A

message object

21
Q

Build-in first level object tx

A

transaction

22
Q

external

A

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.

23
Q

public

A

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.

24
Q

Internal

A

Internal methods can only be accessed by the contract itself or by any contract derived from it.

25
Q

Private

A

can only be called internally by the contract who owns the method. Derived contracts cannot access a private method of their parent contract.

26
Q

What is the stack?

A

because the EVM is a stack machine rather than a register machine, all computations are done on a data region called the stack.

27
Q

Difference between storage and memory

A
  • 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.
28
Q

Where are state variables and local variables stored?

A

in storage by default

29
Q

Where can memory be used?

A

Inside a function

30
Q

Were are function arguments saved?

A

In memory by default .

31
Q

What happens if we use the keyword “storage” when declaring an array called “myArray”?

A

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.

32
Q

What happens if we use the keyword “memory”?

A

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.

33
Q

Where is the payable keyword required?

A

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>
34
Q

What is a Fallback function?

A

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.

35
Q

What is function overloading?

A

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.

36
Q

What are named function calls?

A

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
});
37
Q

What happens when the parent contract contains a function that is also present in the sub contract?

A

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.

38
Q

What keyword is used for inheritance in Solidity?

A

“is”

39
Q

What is there to know about the FRO?

A

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.

40
Q

When is a contract abstract in Solidity?

A

A function is abstract if it does not have a body. A contract is abstract if one or more functions are abstract.

41
Q

Can an abstract contract be compiled to bytecode?

A

No. A contract that inherits from an abstract contract must implement and override all methods from the base contract to be compliable.

42
Q

What is an interface?

A

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.