In essence, a smart contract acts as a self-executing notary in a traditional contract - it witnesses the agreement between two parties and holds the money in escrow until the agreement is fulfilled.
As they’ve been touted to replace many middlemen jobs — clerks, insurance writers, even lawyers — writing smart contracts sounds like the domain of a programming prodigy or high-end tech firms. While there are firms offering the service, becoming proficient in writing smart contracts isn’t reserved for the Phd in computer science.
As it is still in its early days there is no guarantee the de facto official Ethereum language Solidity will survive, and this month the Ethereum Foundation released another (python-esque) language, Vyper, to update the security of smart contracts.
To start writing your own smart contracts, or even DApps, the first thing to do is create an Ethereum Wallet and buy some ETH. The wallet is a gateway to the Ethereum blockchain, allowing you to write and deploy smart contracts as well as allowing you to hold ether and other ERC20 tokens.
Watch your language
Although you could argue the merits of using one language over another, it is important to choose the language you are most adept at as a smart contract written with inefficient code will cost more in Gas fees to transact.
Vyper is the latest iteration of Solidity, designed to be more security-focused and more code restrictive to make it harder to write misleading code. Although security improvements have been made to Solidity over the past year a recent University College of London study found 34,000 smart contracts with vulnerabilities written in Solidity.
Vyper: More solid than Solidity?
As opposed to being a complete replacement for Solidity, Vyper is an alternative language to use when security is paramount to a project, such as writing smart contracts containing a patient's health records.
Vyper is essentially an upgrade of the first smart contract language Serpent, which was made redundant after fundamental security flaws were found in its code. Ethereum developers migrated to Solidity last year after vulnerabilities in the code of the Augur REP token were deemed unavoidable because they were endemic to the Serpent language. Vyper, like Serpent, was built to be as similar to Python as possible though there are many syntactical differences to familiarize with.
Easy on the Gas, but not too little either
This is where writing smart contracts gets a bit tricky. Gas is not an individual token but a nominal unit of ether that is “spent” on operating transactions - equivalent to a mining fee in bitcoin. It is the writer of the smart contract who specifies the amount of ETH it is willing to pay for each unit of gas in a transaction, and then it’s left to the market to decide what gas price that transaction incurs.
The miners quote their own spot price for Gas which you are free to accept or quote your own price.
We should think of Gas as the miners’ pro rata rate for doing the computational work - if your gas runs out before the transaction is completed, however, the gas is “spent”, the miner keeps the fee for the work they did and your transaction goes to the blockchain as a “failed transaction”. If you quote too much ETH for each unit of gas than your transaction requires, your request will be pushed to the front of the line (like mining fees in bitcoin) but your fee will be similarly irrefundable.
You can pay for your Gas in ETH at the eth gas station.
The one scenario you will be refunded for is if you send too much ETH to pay for a gas fee - the excess will be returned. The reason ether isn’t used as a form of payment is because the price fluctuates too much to find a fair quote price for computation and fees would have to be continuously repriced.
When running a decentralized application (Dapp) every execution on it is executed on every node in the network. Gas is the mechanism that ensures a program will not be run indefinitely on the network, so incentivises the writer of the program to write good code as they are the ones paying for it.
Once you're happy with how your smart contract is written up you next have to compile it, which effectively turns your programming language into machine-readable code. Download the solc compiler (from the C++ libraries) or use a web-based compiler like Cosmo, or the Solidity real-time compiler.
Do I need to run an Ethereum node to write smart contracts?
Ethereum has its own integrated development environment (IDE) called Remix to write smart contracts in Solidity.
While you don’t have to run an Ethereum node to write contracts, it is an easy process to set one up and you get a better understanding of the mining and transaction process. Ethereum nodes can be run on many different client implementations, C++, Go, Python, Java, Haskell etc, and although this allows for diversity in the Ethereum ‘world computer’ ecosystem, it also means there is no 'universal' Ethereum installer which can make things a bit confusing for the end user.
The most popular clients
When to outsource your smart contracts
Smart contracts are still far from being widely used in day to day commerce and for the most part, are used for token offerings. While the above guidance was for those interested in writing simple "if-then" financial contracts, coding blockchain protocols is a Herculean task.
There are a few good smart contact developer firms that can put your project's concept into code and generate an ICO. Blockchain Labs have done the coding and delivery of tokens for Golem, Polymath, Leverj, LaToken, WePower and Sphere Identity among other prominent ICOs. Blockchain IL, Open Zepplin and Consensys offer similar services.