Solidity
One of the languages supported by Ethereum is Solidity. It is updated quite frequently, breaking changes should be expected.
Solidity is used for the creation of smart contracts.
A simple program:
A deployed smart contract is accessible to anyone who has an identity on the blockchain (human or a program).
Features
Data types
address
- refers to identity of participantsstruct
- allows creation of structures of datamapping
- defines a dictionary/map/hashtableenum
- enumerationarray
- e.g.Proposal[] proposals;
Structs
Example
Enums
Example:
Loops
Loops looks like in C. If the content is just one-line ling, curly brackets may be skipped.
Functions
Storage
Variables within functions may either end up in the blockchain or not. Simple variables, by default, are not recorded on the blockchain. Structs are recorded (by default). We can change that behavior with keywords:
memory
- variable is not storedstorage
- variable is stored
Example:
Voter
is a struct. By adding the memory
keyword, we made the variable
temporary. It will not be stored on the blockchain.
View Functions
If a function is marked with a view
keyword, it is not allowed to modify the
state and its execution is not recorded on the blockchain.
If view
is missing, every invocation is recorded in the ledger.
Returning data
Functions may return data. In such a case, we may define a variable that will be returned and write some data into it.
Constructor
A contract might have a constructor. It is run during the deployment of the contract to the blockchain. The deployment is done by some participant of the blockchain. The deployment may be performed with some ethers attached. These ethers are added to the contact’s funds. It’s no different from other functions’ execution. These can also accept funds.
Visiblity Modifiers
Dat and functions may be defined as public
, making them reachable by the
public. Without that keyword, the data/function is internal to the contract and
may be called only within that contract.
It’s analogical to encapsulation in the OOP.
Access Modifiers
They are used to guard access to data/functions. They are similar to functions.
The _;
invokes the actual function that is guarded by the modifier.
(can we do some actions in the modifier post function execution?)
To use a modifier, we need to state it in the “header” of a function, like this:
The function above will invoke the modifier with the value Phase.Regs
(it’s an
enum) passed to it. Only if that modifier succeeds, the transaction may be
registered.
Payable
A function with the payable
keyword accepts payments (in msg.value
).
Sender
Every function invocation has access to the msg.sender
field that reveals the
address of the caller.
Built-in functions
Revert
If something is wrong in the execution (e.g., some validation failed),
revert()
reverts the transaction. Revert is called automatically by the Smart
Contract in case of runtime error.
In case of a revert, all changes done during the execution are reverted.
Require
require(condition)
is a basic assert operation used for validation. If the
provided condition if false, revert()
will be called.
require
does not consume all the remaining gas offered to pay (like assert
does). It should be used in cases where the condition being false is a normal
situation that is expected to happen (e.g., when validating user inputs).
It’s useful in access modifiers.
Assert
assert(condition)
works basically the same as require
, but it uses up all
the remaining gas.
It should be used when checking conditions that are crucial and are not expected to ever be false.
Events
Smart contracts may define and emit events.
A definition example:
Emitting example:
Events are stored on the blockchain in the block’s receipt tree. These events can be retrieved by dapps.
Remix IDE
It’s a web-based IDE for Solidity. It can be found at https://remix.ethereum.org/. It’s suitable for playground/learning scenarios. Production apps should use something like Truffle.