Building a Smart Contract: It’s Easier Than You Think | CoinCentral


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.

Javascript developers shouldn’t be intimidated by blockchain. With a little more understanding, blockchain becomes like most other frameworks you would normally implement.

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.

Another option, Lisk, is a DApp platform based in Javascript, so you won’t get any new syntax thrown at you. It also has its own DApp store (think Google play or iTunes store) where you can publish your app when finished.

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.

As a Javascript developer, Lisk may seem like the logical decision because you’re already experienced in the language.

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.

ethereum smart contract frameworks

Building a Smart Contract

Solidity sounds more intimidating than it actually is. The first thing you may notice about the language is the striking resemblance it has to a JavaScript class. There are some subtle differences, though, like the requirement of semicolons and the use of hashing functions. The hashing functions are used to create unique keys and map them to other variables. In our example, we would use them to create unique tickets and map them to the accounts that buy them.

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.

Connecting to JavaScript

The most challenging part of working in Solidity is integrating your contract with Javascript. This is all done using the Truffle framework.

The first step in integrating your contract is to deploy it and migrate it to your test network. Truffle provides their own RPC, a separate Ethereum test network, called Ganache. This process gives you a JSON version of the contract that you can call using the addresses of your test network through your Javascript application.

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.

Truffle boxes

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.

Conclusion

This article wasn’t meant to teach you how to start writing Solidity. Instead, it was to give you a walkthrough of what a smart contract is and how you can implement it in a Javascript application. Building on the blockchain is possible for anybody who knows web development.

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.

Read original article at coincentral.com.
Author: Jake Bergal

Please follow and like us:

Related Articles

Social media & sharing icons powered by UltimatelySocial