INITIALLY, I THOUGHT THAT ETHEREUM WAS A THING THAT WOULD BE USED FOR PEOPLE TO WRITE SIMPLE FINANCIAL SCRIPTS. AS IT TURNS OUT, PEOPLE ARE WRITING STUFF LIKE AUGUR ON TOP OF IT. -VITALIK BUTERIN
In last years, with the increasing of Blockchain-based technologies and with the born of always new cryptocurrencies, the concept of Smart Contract started to sound louder and louder. Let’s try to briefly explain what they are and why they are becoming increasingly important and used in in the most varied contexts.
A Smart Contract is just a program, written in a particular programming language (either Turing-complete or not) that runs as programmed, without being able to be censured or manipulated, where the code is law and with the aim to facilitate operations between people and institutions.
We can find Smart Contracts in many environments.
For example, Bitcoin utilizes Script, a Forth-like reverse-polish notation stack-based, processed from left to right, very simple programming language. A Smart Contract in Script is just a list of Opcodes instructions recorded with each transaction. However, although Bitcoin Smart Contracts are functional to the objectives set, they are not widely used in many scenarios. One reason could be the fact that Script is not Turing-complete, avoiding the possibility of using loops or cycles.
Currently, the most known Smart Contracts are those of Ethereum, which are also the main focus of this brief discussion. They can be written in several programming languages such as Solidity, Serpent and LLL. Solidity, the most popular among these, is a high-level language, influenced by C++, Python and JavaScript. It is designed to target the Ethereum Virtual Machine, the Ethereum byte-code execution environment that every node runs to be able to send and receive transactions.
A Solidity Smart Contract looks like this
pragma solidity ^0.4.21;
contract Coin {
// “public” rende le variabili
// accessibili dall’esterno
address public minter;
mapping (address => uint) public balances;
// Gli eventi permettono ai client di
// visualizzare i cambianti nella Blockchain
// in maniera efficiente
event Sent(address from, address to, uint amount);
// Costruttore che viene eseguito
// solamente quando il contratto viene creato
function Coin() public {
minter = msg.sender;
}
function mint(address receiver, uint amount) public {
// msg.sender è l’indirizzo di colui che fa la transazione
if (msg.sender != minter) return;
balances += amount;
}
function send(address receiver, uint amount) public {
if (balances < amount) return;
balances -= amount;
balances += amount;
emit Sent(msg.sender, receiver, amount);
}
}
As one can see, Solidity is also a contract-oriented programming language, meaning that there exist keywords dedicated to who makes transactions, to sent and received ether and much more. We don’t go deeper in further technical details about the syntax. A complete guide can be found here.
With a Smart Contract one can program many type of scenarios, from a simple storage of data, to the triggering of certain operation after receiving money, from decentralized application (Dapps) to decentralized games.
As all the transactions in Blockchain, also Smart Contracts are permanently stored without any possibility of modifications. This means that once done the deploy of the contract on the chain, it cannot be altered. For this reason, security in Smart Contract is of fundamental importance. In fact, just think that ether can be managed by them and so, even just a small error in the logic of programming, can result in large economic losses. A “famous” example is about the drama of DAO, where wrong implementations of a part of the code led to millions of dollars/ether losses.
Developing Smart Contracts is not an easy task. To help with it, there exist some best practices, that allow a programmer to know how not to set up a contract preventing serious points of failures. Also, as support tools there exist many software, such as Truffle in case of Ethereum, that allow to safety code in a local environment to better test in all possible ways Smart Contracts before the final deploy.
Smart Contracts can be a very powerful tool able both to create value, security and efficiency, but also to being used to create chaos from even simple errors. For this reason, one should be particularly careful in coding them and deploying in the main network, if they manage sensitive data or money.