-
coding practice
-
[x] Hello World
- contract structure:
- spdx license identifier by open-zeppelin
- pragma statement: solidity version
- contract
-
[x] First App
- simple app that counts:
- increments(+=) & decrement(-=)
- gets current count
-
[x] Primitive Data Types
- data types
- bool
- unit: only has positive integers
- int: has both negative & positive integers:
- int range: -2 ** 256 -1 to 2**256-1
- dynamically-sized byte arrays
-
[x] Variables
- state variables: saved on the blockchain: declared
- local state: not saved on the blockchain
- global state: accessible from the blockchain e.g timestamp, msg.sender
-
[x] Constants
- hard-coding constants using upper-case that can’t be modified to save gas costs
-
[x] Immutable
- hardcoding immutable and setting them to the constructor: no modifications
-
[x] Reading and Writing to a State Variable
- reading state variable without sending transaction
- writing to state variable by sending a transaction
-
[x] Ether and Wei
- units of Ether i.e one ether equal to 10 ** 18 Wei
-
[x] Gas and Gas Price
- gas is the amount of Ether you need for a transaction
- gas - computational unit
- gas price - the amount of Ether willing to pay per gas spent
- the transaction with a higher gas price - has a high priority to be included in the block
- unspent gas is refunded
- if all gas is used transaction might fail and its state changes to undone and gas that has been spent isn’t refunded
-
[x] If / Else
- using if & else loops with return statements for all checks
-
[x] For and While Loop
- for loop: good for dynamic arrays & undetermined data —- a good option
- while loop: for deterministic data its a slave option
-
[x] Mapping
mapping(keyType => valueType)
- keyType could be a built-in value type, bytes, string, or any contract while value type could be any type or even another mapping or array.
- mapping returns a value if it wasn’t set it will return a default value
- mapping value can be updated
- mapping value can be deleted back to its default value
- nested mapping
mapping(address => mapping(uint => bool)) public nested;
- can get the value of nested mapping even if it wasn't initialized
-
[x] Array
- Arrays can have compile-time fixed/dynamic size
- fixed arrays all their elements initialize to zero
unit[10] public myFixedSizeArr
- array methods in solidity:
- getArray: get all elements of the array
- push: increase the length by one
- pop: remove last element of the array
- remove: delete an element of an array but it will be returned to 0/default
- getLength: get the actual length of array/ number of array elements
- create array elements in memory: only fixed-size arrays can be created not dynamic
- Array methods: shift, push, pop, length, delete, checking index
-
[x] Enum
- solidity supports enumerables & they’re useful to model choice & keep the track safe. They’re declared outside a contract and imported to the contract.
- the first element is the default element listed
-
[x] Structs
- define your own type by creating a struct. It's useful for grouping related data together. Can be declared outside a contract & imported
- initializing struct by:
- calling it like a function
- key-value mapping
- initialize empty struct then update it
-
[x] Data Locations - Storage, Memory, and Call data
-
variables used to declare or explicitly specify data location are memory, calldata, or storage.
- storage: its a state variable(stored on the blockchain)
- memory: exists while the function is called locally
- calldata” special data location that contains function arguments
-
[x] Function
-
there are several ways to return outputs from a function