Save
Busy. Please wait.
Log in with Clever
or

show password
Forgot Password?

Don't have an account?  Sign up 
Sign up using Clever
or

Username is available taken
show password


Make sure to remember your password. If you forget it there is no way for StudyStack to send you a reset link. You would need to create a new account.
Your email address is only used to allow you to reset your password. See our Privacy Policy and Terms of Service.


Already a StudyStack user? Log In

Reset Password
Enter the associated with your account, and we'll email you a link to reset your password.
focusNode
Didn't know it?
click below
 
Knew it?
click below
Don't Know
Remaining cards (0)
Know
0:00
Embed Code - If you would like this activity on your web page, copy the script below and paste it into your web page.

  Normal Size     Small Size show me how

Solidity by Example

An introduction to Solidity with simple examples

QuestionAnswer
What primitive data types are available in Solidity? The primitive data types available in Solidity are boolean, uint, int, and address.
How many types of variables are there in Solidity? There are 3 types of variables in Solidity.
What are the types of variables? The types of variables are local, state, and global. Local variables are declared inside a function and not stored on the blockchain, while state variables are declared outside a function and stored on the blockchain. Global variables provide information
What are constants? Constants are variables that cannot be modified. Their value is hard coded and using constants can save gas cost.
What are immutable variables? Immutable variables are like constants. Values of immutable variables can be set inside the constructor but cannot be modified afterwards.
How do you write or update a state variable? To write or update a state variable you need to send a transaction. On the other hand, you can read state variables, for free, without any transaction fee.
How are transactions paid? Transactions are paid with ether. Similar to how one dollar is equal to 100 cent, one ether is equal to 1018 wei.
How much ether do you need to pay for a transaction? You pay gas spent * gas price amount of ether, where gas is a unit of computation, gas spent is the total amount of gas used in a transaction, and gas price is how much ether you are willing to pay per gas.
What is the gas limit? The gas limit is the maximum amount of gas you're willing to pay to run a transaction.
What happens to unspent gas? Unspent gas will be refunded.
How do transactions with higher gas price have higher priority to be included in a block? Transactions with higher gas price have higher priority to be included in a block because they are willing to pay more for the same amount of gas.
What conditional statements does Solidity support? Solidity supports conditional statements if, else if and else.
What loops does Solidity support? Solidity supports for, while, and do while loops.
Why should loops not be unbounded? Loops should not be unbounded as this can hit the gas limit, causing your transaction to fail.
Why are while and do while loops rarely used? While and do while loops are rarely used because of the risk of hitting the gas limit, causing the transaction to fail.
How are maps created? Maps are created with the syntax mapping(keyType => valueType), where the keyType can be any built-in value type, bytes, string, or any contract.
What types can the valueType be? The valueType can be any type including another mapping or an array.
Are mappings iterable? No, mappings are not iterable.
What size can an array have? An array can have a compile-time fixed size or a dynamic size.
What are enumerables and how are they used in Solidity? Enumerables are data types that allow you to declare a set of named constants. They are useful for modeling choice and keeping track of state in Solidity. Enums can be declared outside of a contract.
What is a struct in Solidity? A struct is a user-defined type in Solidity which can group related data together.
Can structs be declared outside of a contract and imported in another contract? Yes, structs can be declared outside of a contract and imported in another contract.
How many variable locations are there in Solidity? In Solidity, there are three variable locations: storage, memory, and calldata.
What is the purpose of the calldata location? The calldata location is a special data location that contains function arguments.
How can outputs be returned from a function? Outputs from a function can be returned in several ways.
Are there any restrictions on what data types can be used as inputs and outputs for a public function? Yes, public functions cannot accept certain data types as inputs or outputs.
What are getter functions? Getter functions are functions that allow users to access the state variables of a contract.
What is the difference between view and pure functions? A view function declares that no state will be changed while a pure function declares that no state variable will be changed or read.
Are getter functions always view or pure? Yes, getter functions must be declared view or pure.
What happens when an error occurs in a transaction? An error will undo all changes made to the state during a transaction.
What are the three functions used to throw an error? The three functions used to throw an error are require, revert and assert.
What is require used for? Require is used to validate inputs and conditions before execution.
What is the purpose of assert? Assert is used to check for code that should never be false. Failing assertion probably means that there is a bug.
What is a Modifier? A Modifier is code that can be run before and/or after a function call.
What are the advantages of using Modifiers? Modifiers can be used to restrict access, validate inputs and guard against reentrancy hack.
How can Modifiers be used to restrict access? Modifiers can be used to restrict access to a function by specifying who can call the function.
How can Modifiers be used to guard against reentrancy hack? Modifiers can be used to detect and prevent reentrancy hack by checking whether the caller has already called the function before.
What are Events? Events allow logging to the Ethereum blockchain and can be used for various purposes.
What are some use cases for Events? Some use cases for Events are listening for events and updating user interface, and using them as a cheap form of storage.
What is a constructor? A constructor is an optional function that is executed upon contract creation and can be used to pass arguments to the contract.
How can arguments be passed to constructors? Arguments can be passed to constructors using the constructor parameters or through a constructor call in the deployer code.
What is multiple inheritance? Multiple inheritance is when a contract inherits another contract by using the ‘is’ keyword.
What must be declared as virtual when overriding a function in a child contract? The function that is going to be overridden by a child contract must be declared as virtual.
What keyword must be used when overriding a parent function? The function that is going to override a parent function must use the keyword ‘override’.
What is the importance of order when it comes to inheritance? Order of inheritance is important. You have to list the parent contracts in the order from “most base-like” to “most derived”.
Is it possible to override state variables in a child contract? No, it is not possible to override state variables in a child contract. Unlike functions, state variables cannot be overridden by re-declaring it in the child contract.
How can parent contracts be called? Parent contracts can be called directly, or by using the keyword 'super'.
What does the keyword 'super' do? By using the keyword 'super', all of the immediate parent contracts will be called.
What do functions and state variables have to declare? Functions and state variables have to declare whether they are accessible by other contracts.
What access levels are available for functions? Functions can be declared as public, private, internal, or external.
What access levels are available for state variables? Functions can be declared as public, private, internal, or external.
What does public access mean for functions? Public access for functions means that any contract and account can call the function.
What does private access mean for functions? Private access for functions means that the function can only be accessed inside the contract that defines the function.
What does internal access mean for functions? Internal access for functions means that the function can only be accessed inside the contract that inherits the internal function.
What cannot be declared in an interface? An interface cannot declare a constructor or state variables.
What is the purpose of an interface? The purpose of an interface is to define a contract within code as well as contracts with code. It provides a way to name types and enforce contracts that must be implemented.
Can functions and addresses declared payable receive ether? Yes, functions and addresses declared payable can receive ether into the contract.
What is the transfer function used for? The transfer function is used to send Ether with 2300 gas and throws an error when used.
What is the send function used for? The send function is used to send Ether with 2300 gas and returns a boolean (true or false) when used.
What is the call function used for? The call function is used to send Ether with either forward all gas or with a specified amount of gas and returns a boolean (true or false) when used.
What is the recommended way to implement the withdraw pattern? The recommended way to implement the withdraw pattern is to use the msg.sender.transfer{gas: max}(2 ether); function.
What is the receive() function used for? The receive() function is used to receive Ether and is called if msg.data is empty.
What is the fallback() function used for? The fallback() function is used to receive Ether and is called if there is data in the msg.data but no function matches it.
What are the two functions a contract must have to receive Ether? A contract must have at least one of the following functions to receive Ether: receive() external payable or fallback() external payable.
What is the syntax of the receive() function? The syntax of the receive() function is receive() external payable {...} (without the function keyword).
What is the recommended method to use after December 2019? The recommended method to use after December 2019 is call in combination with re-entrancy guard.
How can you guard against re-entrancy? You can guard against re-entrancy by making all state changes before calling other contracts and using re-entrancy guard modifier.
What is the purpose of the re-entrancy guard modifier? The purpose of the re-entrancy guard modifier is to prevent malicious actors from performing a re-entrancy attack on a smart contract.
What is the syntax of the re-entrancy guard modifier? The syntax of the re-entrancy guard modifier is ReentrancyGuard : A modifier that can prevent reentrancy during certain functions.
What is the fallback function? The fallback function is a special function that is executed either when a function that does not exist is called or Ether is sent directly to a contract but receive() does not exist or msg.data is not empty.
What is the gas limit of the fallback function? The gas limit of the fallback function is 2300 when called by transfer or send.
When is the fallback function called? The fallback function is called either when a function that does not exist is called or Ether is sent directly to a contract but receive() does not exist or msg.data is not empty.
What is the main use case of the pre-0.6.x fallback function? The main use case of the pre-0.6.x fallback function is to receive data but not Ether.
What is the call function used for? The call function is a low level function used to interact with other contracts, especially when sending Ether via calling the fallback function.
What is the recommended way to call existing functions? The recommended way to call existing functions is to import the interface of the contract to call functions on it.
Why is low-level call not recommended? Low-level call is not recommended because reverts are not bubbled up, type checks are bypassed, and function existence checks are omitted.
Does call consume less gas than calling the function on the contract instance? Yes, call consumes less gas than calling the function on the contract instance.
How to specify gas and transfer amount when using call? It is possible to supply amount of gas and ether to the call method using the syntax _addr.call {value: 1 ether, gas: 1000000} (abi.encodeWithSignature("myFunction(uint,address)", 10, msg.sender));.
What is delegatecall? Delegatecall is a low level function similar to call. When contract A executes delegatecall to contract B, B's code is executed with contract A's storage, msg.sender, and msg.value.
What are the differences between call and delegatecall? The main difference between call and delegatecall is that the context of code and storage changes are made in the calling contract when using delegatecall, whereas with call, the code and storage changes are made in the called contract.
What are the security implications of using delegatecall? The security implications of using delegatecall include the risks of executing malicious code and overwriting or incorrectly interpreting state variables. It is important to use permissions, authentication, or some other form of control for specifying or
What are the first 4 bytes of calldata used for? The first 4 bytes of calldata are used to specify which function to call. This 4 bytes is called a function selector.
How is a function selector used? A function selector is used to perform dynamic invocation of a function, based on the name of the function and the type of each one of the input arguments.
What is the first 4 bytes returned from abi.encodeWithSignature(...) used for? The first 4 bytes returned from abi.encodeWithSignature(...) is the function selector.
How can you save a tiny amount of gas by precomputing and inlining the function selector? Precomputing and inlining the function selector in your code can save a tiny amount of gas by removing the need for the EVM to compute the selector for the given function call.
How can contracts call other contracts? Contracts can call other contracts in two ways — either you just call it like A.foo(x, y, z) or you use the low-level call.
What is the low-level call? The low-level call is a method to interact with other contracts, but it is not recommended due to its lack of type checks, function existence checks, and ability to bubble up reverts.
What are the advantages of using the low-level call? The low-level call is more efficient in terms of gas consumption and can be used to send Ether via calling the fallback function.
What is the new keyword used for in contracts? The new keyword is used to create an instance of an object which has a constructor function. Contracts can be created by other contracts using the "create2" new keyword.
What is the create2 feature and how is it supported by the new keyword? The create2 feature allows contracts to create other contracts, and it is supported by the new keyword by specifying salt options. This allows for more secure contract creation.
What type of errors can be caught with the try / catch statement? The try / catch statement can only catch errors from external function calls and contract creation. It cannot be used for internal function calls.
How can files be imported in Solidity? You can import both local and external files in Solidity. To import local files, use a relative path (./). For external files, you can use an import statement from NPM, a URL (like Github, an IPFS gateway, or a Swarm gateway), or a remix command in the co
How can files be imported from GitHub in Solidity? You can import from GitHub by simply copying the URL into an import statement. This will download the file from GitHub and import its contents into the current global scope.
How are libraries different from contracts in Solidity? Libraries are similar to contracts, but they cannot declare any state variables or send ether.
What must be done for a library to be embedded into a contract? All library functions must be internal for a library to be embedded into a contract.
What must be done for a library to be linked to a contract? The library must be deployed and then linked before the contract is deployed.
What does abi.encode do? abi.encode encodes data into bytes.
What does abi.decode do? abi.decode decodes bytes back into data.
What does the Keccak256 function do? Keccak256 computes the Keccak-256 hash of the input.
What are some use cases for the Keccak256 function? Some use cases are: creating a deterministic unique ID from an input, Commit-Reveal scheme, and compact cryptographic signature (by signing the hash instead of a larger input).
How can messages be signed and verified using a smart contract? Messages can be signed off chain and then verified on chain using a smart contract.
What are some gas saving techniques? Some gas saving techniques include replacing memory with calldata, loading state variable to memory, replacing for loop i++ with ++i, caching array elements, and short circuit.
What happens when a number overflows or underflows in Solidity 0.8? In Solidity 0.8, an error is thrown when a number overflows or underflows.
How can overflow / underflow checks be disabled? Overflow / underflow checks can be disabled by using unchecked.
Created by: jauvany
Popular Engineering sets

 

 



Voices

Use these flashcards to help memorize information. Look at the large card and try to recall what is on the other side. Then click the card to flip it. If you knew the answer, click the green Know box. Otherwise, click the red Don't know box.

When you've placed seven or more cards in the Don't know box, click "retry" to try those cards again.

If you've accidentally put the card in the wrong box, just click on the card to take it out of the box.

You can also use your keyboard to move the cards as follows:

If you are logged in to your account, this website will remember which cards you know and don't know so that they are in the same box the next time you log in.

When you need a break, try one of the other activities listed below the flashcards like Matching, Snowman, or Hungry Bug. Although it may feel like you're playing a game, your brain is still making more connections with the information to help you out.

To see how well you know the information, try the Quiz or Test activity.

Pass complete!
"Know" box contains:
Time elapsed:
Retries:
restart all cards