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).
address- refers to identity of participants
struct- allows creation of structures of data
mapping- defines a dictionary/map/hashtable
Loops looks like in C. If the content is just one-line ling, curly brackets may be skipped.
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 stored
storage- variable is stored
Voter is a struct. By adding the
memory keyword, we made the variable
temporary. It will not be stored on the blockchain.
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.
view is missing, every invocation is recorded in the ledger.
Functions may return data. In such a case, we may define a variable that will be returned and write some data into it.
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.
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.
They are used to guard access to data/functions. They are similar to functions.
_; 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
A function with the
payable keyword accepts payments (in
Every function invocation has access to the
msg.sender field that reveals the
address of the caller.
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(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
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(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.
Smart contracts may define and emit events.
A definition example:
Events are stored on the blockchain in the block’s receipt tree. These events can be retrieved by dapps.
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.