General Solidity I Flashcards
What is a mapping variable
A mapping is a data structure in solidity that associates a key with a value. Mappings can be used to store and retrieve data efficiently in smart contracts on the blockchain.
What is a struct
A struct is a user-defined data type that groups together variables of different data under the same name.
Struct Guns {
Bool Ak47;
Uint shots fired;
}
What is the difference between a uint and an int?
Unassigned intergers can only store positive values while signed intergers can store both a negative and positive value.
What is the visibility of public functions/variables in solidity?
Public functions/variables can be accessed externally or internally.
What is the visibility of internal functions/variables in solidity?
Internal functions/variables can be accessed internally or by derived/inherited contracts.
In solidity, what is a transaction? (writing)
A transaction is a message sent from one contract or account to another that modifies the state of the blockchain.
In solidity, what is a call? (Reading)
A call is a message sent from one smart contract to another that does not modify the state.
What is a “receive” function?
A recieve function is a payable function that is used to send value with no data attached.
recieve() external payable {
What is “unchecked {“ in solidity
It is used to disable run time checks for mathematical operations (over/underflow). Less relevant in new versions where patched.
What is over/under flow in solidity
Over/under flow was a feature in older versions of solidity that allowed numbers to wrap around after breaching their maximum/minimum value. It has uses, such as saving on gas in certain cases, but can also open contracts up to malicious attacks. In versions 0.8+, you can activate this feature by entering in “unchecked {“.
What is a “require” statement inside of solidity
A conditional statement that requires something to be true or it will revert, throw an error, and can add a string to let the user understand why the transaction failed. If triggered, the user will only lose the gas used up to the requirement.
What is an invariant violation in solidity?
An invariant violation occurs when a condition that should always be true is not true. For example, if a contract assumes that a certain variable will always have a value within a certain range, if it ends up somehow outside of that range, this would be an invariant violation.
What is assert in solidity, and when should it be used?
Assert checks for a condition to be true. If it is false, it will revert all changes made to the state of the contract. Unlike require, it will not return any gas. Assert would best be used to check for internal errors or invariant violations. It should not be used in any situation where it can be accidentally triggered. If we are setting parameters to validate user inputs or set function preconditions, we should use require instead.
When does Solidity automatically generate a getter function for a mapping variable?
When a mapping is declared to be public. If our mapping is private or internal, we must make our own. The automatically generated getter function will have the same name as the original mapping variable.
Ex: mapping(address => unit) public balances;
•The getter function generated would be called “balances” and take an address argument.
How can we access specific variables from a struct?
As an example:
struct Funder {
address addr;
uint amount;
}
We could then say:
Funder public _funder;
_funder.addr = 0x123
_funder.amount= 50000
(_funder.var inside of function tho)