Simple guide to smart contracts
A non-technical overview of what they are & how they work.
Contrary to their name, ‘smart contracts’ aren’t really “smart” and they aren’t really “contracts” in the traditional sense. Although they conjure up thoughts of an AI executing a legal contract, that is actually very misleading*.
In reality, they are a small snippet of pretty simple† code that manages a cryptocurrency wallet and are run in the virtual machine (VM) on a node in a blockchain network.
Where a typical cryptocurrency wallet or account has a user who manages the private keys, in the case of a smart contract only the code linked to the contract can initiate payments out of the wallet. When there is a transaction into the wallet or a function call to the wallet address, the blockchain node loads the smart contract code in a VM and runs it. The results are stored in a transaction in the wallet.
* Technically, the code is spun up and run (on a distributed VM) when called without human intervention, hence: “Smart”, and because blockchains are immutable they are binding, so: “Contract”.
† They typically need to be ‘pretty simple’ to minimise gas costs, audit-ability, and limitations of the languages used to write them.
Note: This post focuses primarily on Ethereum smart contracts for simplicity.
How they work
Virtual Machines are pretty common in cloud computing and have been around for many years. They’re used to run processes and programs on remote servers. It’s essentially a remote server running a program without showing a UI on a screen. Running a distributed VM (dVM) on a blockchain is similar, except that it can be run on any of the connected nodes in the blockchain network. (You don’t know which server will run the code.)
There are 2 main types of blockchains. Firstly, ones such as Bitcoin, Ripple, and Monero that are optimized for payments and as stores of value, and secondly, others like Ethereum, EOS, and Aion, that also power a virtual machine (VM) that can run code and power dApps. You can use the latter to make payments but what’s unique is their capabilities to use the VMs of the blockchain nodes to run and process code snippets. These code snippets are Smart Contracts.
Smart contracts are typically written in a higher level language — like Solidity or C++ — and then compiled down to byte code or web assembly and saved on a blockchain when deployed. This allows the developer to write the contract in a human readable language that is easier to work with. Compiling down to byte code or web assembly allows the program to be very memory efficient and lightweight.
When the code is compiled and deployed, an Application Binary Interface (ABI) is generated that contains the mapping of the plain english code to the numerical data in the byte code.
Aside: Technically everything on a public blockchain is viewable to anyone, but as it’s all stored in byte code it means that it’s normally pretty meaningless without the original code or ABI to map it. It all just looks like a long string of hexadecimal numbers. So, although it’s public, it’s often still illegible.
In Ethereum, when you transact with a smart contract (either by making a payment into the wallet or a call to one of its functions) the blockchain node that processes it runs the code in its VM. This requires computing resources which on the ethereum network is paid for in
gas. Like the fuel you put in your car, this is the amount of resources that your contract needs to run. The
gasPrice at the time of running the contract changes with demand (like the fuel price) but the
gasUsed for a particular contract remains constant. (Assuming constant array loops in the code.) So every time your contract is run, you need to pay in ether (or wei) that is the
gasUsed x current
gasPrice at the time of your contract being run.
With Ethereum, typically the user pays the gas cost for running the contract. In EOS the payment and terms differ but the concept is the same, although generally the creator pays in order to mimic the way the web works.
Transactions and events
As smart contracts are code attached to a wallet, you can transact with them by making payments into the wallet. When you do this you can also pass along additional data as “function calls” that can initiate the functions of the smart contract — like API endpoints. When a node processes the transaction on its VM, it passes the function into the code to run.
This code can run like any other, calling more internal functions or running processes, as well as make payments out of the wallet. They can also throw
events like errors or logging. When the code has finished running, the code is terminated in the VM and the transactions are stored in the transaction ledger of the wallet with the events as metadata (under the
topics field) along with the
blockNumber and some other values.
Note that the only information that is stored for the contract is what is in the transaction ledger. Any record of functions that were called in the VM or events that weren’t persisted are lost.
What are they good for?
There are a bunch of things we do today that smart contracts help do better- and some that weren’t possible before. Being code, they can technically do almost anything, but here are some common use cases:
- When you need multiple signatures for approval. (multi-sig)
e.g. Verification that goods have been delivered.
e.g Hold the money in a trusted 3rd party until approved for release.
- Collateralized debt instruments.
e.g. Commit this asset and get currency issued like in an exchange. (ETH-DAI)
- Decentralized If-This-Then-That
e.g. If there is a hurricane pay out insurance.
Smart contracts are running on a decentralized network of nodes - which provides trust through transparency as anyone (with the right skills) can audit the logic and no-one can change it once deployed.
There are many other things that they make possible: from decentralized community governance to supply chain management and much, much more.
The good… and the bad.
Despite their promise, smart contracts aren’t the solution to all problems. There are currently a number of issues around using them for real world use cases:
- These networks are very immature and all options today have only two of the following: decentralization, low cost, and fast transactions. In most cases where you just want low cost with fast transactions you’d be better off not using a blockchain at all as a normal database will perform better.
- They are very limited in capacity which makes storage and processing very expensive. Typically, you’re going to only store hashed keys or references on the blockchain and leverage off-chain servers to do the heavy lifting. Most cases we’ve see have people storing kilobytes of data -not megabytes and definitely not gigabytes.
- Variable costs make it hard to predict. As blockchain networks are highly volatile in price, and volume significantly affects
gasPrice,and the price of calling functions can vary from cents to dollars in a day. This makes them very difficult to build a reliable business model around. You also need to ensure that the wallets always have enough currency in them to run your code.
- It’s really hard to get usage data out as the dVM means you don’t know which servers they are running on and if they fail they mostly throw an
insufficient gaserror which is normally not the real problem. Simple monitoring of your code is non-existent. If you’re not diligent in coding in detailed
events, then problems can be very difficult to troubleshoot.
- Once deployed, they are immutable so you can’t change the code. Simple things like versioning require complex ‘proxy contracts’ that allow users to keep pointing at the same wallet that then redirects the function calls to where your latest version is deployed.
For these reasons, smart contracts are typically written to be very small, simple logic steps that are then strung together (often through proxies) to optimize costs, allow flexibility for updating, and reuse of key functions. Tracing transactions through multiple contracts can become a nightmare.
The decentralized nature of the blockchain networks that smart contracts run on do offer some unique advantages. And as these networks get faster and more reliable, their value proposition will only increase.
We believe that smart contracts will have a pivotal role to play in the future in managing how the rules of engagement between disparate systems will be governed in a world that is increasing in complexity. (Along with crypto currencies for value transfer.)
Thats what we’re up to at Trixta.