Building a smart contract: It’s easier than you think
Anybody can build a smart contract
Smart Contracts will facilitate how you do business in the new decentralized economy. Most smart contracts are made on DApp (Decentralized Application) platforms like Ethereum, Lisk or Cardano, and big tech companies like IBM are starting their own businesses building them. This makes sense, as there are plenty of companies that would rather trust a tech giant like IBM to build a product for them than tackle a new technology themselves.
Smart contract development can become a marketable skill for developers that know how to build them. A surprising part of smart contracts is that they aren’t hard to build. The DApp platforms and surrounding tools make it possible to create them and easily build your own blockchain technology.
Planning a smart contract
When first creating a smart contract, you should decide what you want it to facilitate.
For this example, let’s focus on event ticketing. Ticketing has huge issues with scalpers buying up a ton of tickets and with people using fake tickets. Blockchain technology excels at many things; two of them being facilitating transactions and keeping track of information.
Because of this, a simple line of code in the contract can cap ticket resale prices. This prevents scalpers from buying a bunch of tickets because selling them would now be unprofitable. Using smart contracts, you can also generate unique tokenized tickets that are owner specific and can’t be forged. If you’re somebody who likes to attend concerts, festivals and sporting events, you can see the appeal.
DApp platform options
The next thing to choose is which DApp platform to build the contract on.
Ethereum is largest and most established platform. It also has the most active user base, and in any type of development, an active community to answer your questions is key to finishing your product. One downside, though, is that it uses its own language, Solidity, to write contracts.
You could also try out Cardano which was built by a founder of Ethereum and has a large, impressive team. Their product is still new, but if they fulfill their promise to separate contract information from transactional data, it could be the right choice moving forward.
For now, let’s go over Ethereum or Lisk as the platform of choice.
However, don’t count out Ethereum. The development community is so robust that you can make up for any misunderstanding of Solidity with the excess information available. Ethereum also includes the Truffle framework, created by Consensys, that easily allows you to integrate and test your smart contract application.
Building a smart contract
When building your first Ethereum contract we recommend using the development environment at remix.solidity.org. It gives you an interactive environment to deploy your contract and interact with it from different Ethereum accounts. A drop-down menu lets you switch accounts and use any of your contract’s function calls by pushing the corresponding buttons.
What to look out for
The unique thing about Solidity is its use of addressing. All Ethereum accounts have unique addresses, and those addresses represent the parties agreeing to a contract.
In our example ticketing contract, whoever creates the event would have their account address attached to it. Whenever anybody buys a new ticket, funds transfer directly to the event owner, taking out the middleman. The buyer makes a call, and funds are taken out of their account, identified by their address, and passed through to the owner.
Solidity also has a unique way of attaching funds to a smart contract call. Every function call comes with a message. This message can contain unique information from the sender including their account address as well as an ether value they want to send to the contract owner.
It’s also worth noting that in Solidity, not all contract calls are free. To change information in the contract, e.g. buy or use a ticket, the network of machines that confirm the contract execution charge “gas”. Gas is basically a toll for taking advantage of the computing power on the network.
Other calls that just query the contract for information, e.g. looking up the price of a ticket or getting the number of remaining tickets, are free. This is because no information on the contract is updating; there’s no risk in these activities.
If you’re looking for more information on this process, Truffle does a great job of outlining it in their docs and example boxes found at their website.
Another tough thing to overcome when creating a smart contract is how to conceptualize it. Thinking of the blockchain like a web developer, you can look at it as a replacement for the database. Instead of one centralized source for all of your information, there’s a chain of contracts and transactions to follow that lead to the information.
Because a blockchain is like a database, the natural thing to do is connect the contract to your server. Creating an API layer for the contract makes accessing information from the client side of the application as easy as making an AJAX request. There’s a good example box on the truffle website for getting this setup.
While the blockchain is similar to a database, it’s not completely identical. It has an entirely unique protocol involving transactions, hashing, and block creation. This may be the hardest part of Ethereum development. In our ticketing example, you need to structure a contract call for buying a ticket differently from the call for using a ticket. The Ethereum Virtual Machine is picky about these calls.
This is another area where the large Ethereum development community is useful. There’s a good chance that any question you have has already been answered on the Ethereum Stack Exchange, and if not, other developers are more than willing to help you through your contract.
The technology behind blockchain may be difficult to understand, but like any other framework or developer tool, you don’t necessarily need to know how it was built in order to use it. Don’t let blockchain’s mysterious aura stop you from building something great.