Online IDE: remix.ethereum.org/
Particulars of remix:
- javascript VM: simulates the network in your browser memory, so you can speed up the development. It doesn’t depend on testing networks being down or slow
- ganache is another alternative: it runs a network locally, you can just pick a web3 provider in the deploy menu
Solidity:
- variable initialization defaults
- strings are empty, ints are zero, booleans are false, so there are no null pointer errors
- public variables automatically generate a getter function
- Example of throwing a validation exception
require(msg.sender == owner, "You are not the owner");
- msg is a special object that encapsulates the address that calls the smart contract
- in this case, we return the specific error, and the whole transaction rolls back
- transactions are atomic: if an exception happens, they will revert. There is no catch mechanism, so they will cascade thru methods (like Address.send, Address.call, etc)
- difference between assert vs require:
- require when revert returns reminding gas
- require is used to validate user’s input
- require lets you return an error
- require triggers: function calls that do not return, receive ether without payable type, transfer errors
- you can also user revert(“your error message here”) instead of require, to throw explicitly
- assert doesn’t let you customize the error message
- assert consumes the gas
- assert validates things that are unexpected, invariants (out of bounds array, division by zero, non initiated value of internal functions type, or assert(false)
- Mappings are like hash maps, except they don’t have a length, if you need it, it needs to be stored separately
- their key type can be pretty much any elementary value
- their value type can be anything
- Strucs allow you to create your own data type. But they can’t contain other structs internally
- Arrays can have both: fixed or dynamic size. The length of arrays can actually result in higher gas consumption
- Enums are available
- Functions
- View function: reading from state (“constant” functions)
- Pure function: Not reading or modifying state
- Constructor: only called once during deployment, it is either public or external
- Fallback function: it is called when you send ether to a contract without specifying the function to call (kind of the default)
- it can only be external
- Function visibility:
- public: can be called from outside the contract
- private: can only be called from within the contract
- external: can be called from other contracts or externally
- internal: only from the contract itself, or derived contracts, can’t be invoked by transactions
- Inheritance, Modifiers, Importing of files:
- It is similar to currying functions in javascript, where you can composite a modifier function and include it on others, and it will just run automagically.
- Usually used to do precondition checks, where you need to do the same ones in multiple functions
- ABI: application binary interface
- it contains all the functions / arguments / return values, everything needed to interact with an smart contract
- the more instructions resulting there from the compilation of your programs, the more gas you will have to pay for transactions against that contract
- Gas also depends on how congested the network is, not only on the number of complex operations stored in the ABI
- In the network, the more Gas you pay, the faster your transaction gets processed in the queue of waiting transactions
- Libraries: they are executed in the context of your smart contract. They can’t receive Ether,