Solidity Flashcards
Variable scope
public -> Can be used internally & externally. Automatically creates a getter function
external -> Meant to be called ONLY from other contracts ( state variables cannot be external )
internal -> Can only be called from within the contract or from derived ones
private -> Can only be called from within the contract - not even from derived ones
Pure function
int_a = 6
function square(uint 256 int_a) public pure { int_a * int_a }
No cost to this functions; pure computation and not creating / changing any state variables. Not affecting the blockchain.
‘memory’ keyword
=> Only stored during execution of the function call
string person;
function assignName(string memory _name) public { person = name }
mapping
A data structure -> hash table?
mapping(address => uint256) public addressToAmountFunded;
addressToAmountFunded[msg.sender] += msg.value; // value associated with address increased every time. Not overwritten
‘payable’ keyword
In this function eth/wei is being transferred.
Whether it’s withdrawn or deposited.
‘msg’ keyword
In every contract. (Incoming call?)
msg. sender -> address from incoming function call [type address]
msg. value -> value associated with txn / call [type uint256]
nodes cannot do API calls because…
Because blockchains and smart contracts are deterministic systems. Nodes might call APIs at different times and get different results, and then wouldn’t be able to verify the other results.
Nodes can also not agree on a random number together
Oracle issue
An external oracle supplying data to the blockchain could represent a single point of failure - something which is completely avoided with blockchain otherwise
Variable types
State Variables − Variables whose values are permanently stored in a contract storage.
Local Variables − Variables whose values are present till function is executing.
Global Variables − Special variables exists in the global namespace used to get information about the blockchain.
Sender of the transaction [GV]
tx.origin (address payable)
Gas price of the transaction [GV]
tx.gasprice (uint)
Current block timestamp [GV]
now (uint)
First four bytes of the calldata (function identifier) [GV]
msg.sig (bytes4)
Complete calldata [GV]
msg.data (bytes calldata)
Remaining gas [GV]
gasleft() returns (uint256)