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)
What are instances in solidity
Instances are new instances of a contract with their own state and behavior/address added to the blockchain and interacted by, with other contracts or external users.
Typically utilizing the “new” keyword.
What does changing the state mean?
In general, it refers to modifying the data stored on the blockchain. It costs more gas than reading from the chain.
What is the difference between revert and require?
A require statement will return all gas not used after the require statement. A revert statement will consume all gas before and after.
What are the differences between EOA’s and contract accounts?
•EOA’s are regular externally owned accounts controlled by a private key. EOA’s have a balance of eth and can initiate transactions on their own.
•Contract accounts are special accounts associated with a smart contract. They could be the owner of a token, for example, who might use this for the obvious added functionality. Or normal people who want to trade on a DEX.
How can we tell the difference between an EOA and a smart contract address?
•EOA addresses will always start with a non-zero character. •Smart contract addresses may start with a 0 but do not have to. •In practice, when interacting with the eth network, most wallets/tools can automatically distinguish between the two. Some wallets and blockchain explorers may also provide additional relevant details to help identify the address type.
What is the gas price in solidity? How does this affect a transaction?
The gas price is how much ether you are willing to spend to process a transaction. The more transactions being processed and the number of available miners at any given time will also affect the gas price. The more gas you are willing to pay to process a transaction, the quicker it will go through.
Why do writing transactions not return a value? How can we still include a return value?
Writing transactions do not return a value because their transactions are not instantaneous. Considering confirmation time on a transaction may vary on network activity and other factors, it is difficult to know exactly when a transaction will be confirmed, or if it will simpily fail. We can still get a return value externally by using and emitting events. This will log the data where we can access the values elsewhere.
What are the differences between ERCs and EIPs regarding Ethereum?
Ethereum Request for Comments is a formalized process for suggesting token standards to ensure interoperabillity/compatabillity with other Eth contracts/wallets.
Ethereum Improvement Proposals are proposals for Improving Ethereum protocal itself. Outlining changes/improvements/features at the protocal level to improve aspects of the Eth network itself.
What is the difference between an “ownable” token contract and a token contract with “roles”?
Ownable contracts allow us to specify a single owner address with special privileges and access to certain restricted operations (IE: Transferring/Renouncing ownership)
Token contracts with roles, allow us to specify multiple different addresses to specific tasks/roles. We could declare a “minter” role to an address for instance, who has the abillity to mint new tokens.