In my last post, I gave a brief introduction on Blockchains, and how it's all the hype because of its ability to completely eliminate the middle man during transactions - all you need are the two interested parties and a few lines of code to help validate the trade. These seemingly simple lines of code can actually be deceptively complex. If they have the ability to seal the deal between the two parties, why not refer to them as Smart Contracts?

The basic idea of Smart Contracts is that the code generated by the developers will attempt to validate the transaction, note that I say try, because this code was generated by humans, which means it's probably not completely bug-free. With a Smart Contract, certain conditions are pre-defined, and the contract can only be triggered and start a transaction if that set of conditions are met. Basically, if X happens, then so does Y - a pretty straight forward set of instructions.

The concept of Smart Contracts was first conceived in 1993 by the computer scientist Nick Szabo, and he described it as follows:

"New institutions, and new ways to formalize the relationships that make up these institutions, are now made possible by the digital revolution. I call these new contracts "smart", because they are far more functional than their inanimate paper-based ancestors. No use of artificial intelligence is implied. A smart contract is a set of promises, specified in digital form, including protocols within which the parties perform on these promises."

Smart contracts vary from one another, but the big unifying factor is that all these contracts are autonomous, and can be triggered by different sources, but will always process the transaction the same way as previously defined.

The following code, written in Solidity, was taken from the example provided by Ethereum

        contract MyToken {

               /* This creates an array with all balances */
                mapping (address => uint256) public balanceOf;

                /* Initializes contract with initial supply tokens to the creator of the contract */
                function MyToken(uint256 initialSupply) {
                       balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
                }

                /* Send coins */
                function transfer(address _to, uint256 _value) {
                        require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
                        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
                        balanceOf[msg.sender] -= _value; // Subtract from the sender
                        balanceOf[_to] += _value; // Add the same to the recipient
                }
        }

A mapping is an associative array, where you associate addresses with balances. The addresses are in the basic hexadecimal ethereum format, while the balances are integers.

        function MyToken(uint256 initialSupply) {
                balanceOf[msg.sender] = initialSupply; // Give the creator all initial tokens
        }

This first function creates all initial tokens for this contract. This is a special, startup function that runs once and once only when the contract is first uploaded to the network. So far, you've got a contract with an initial supply:

                /* Send coins */
                function transfer(address _to, uint256 _value) {
                        require(balanceOf[msg.sender] >= _value); // Check if the sender has enough
                        require(balanceOf[_to] + _value >= balanceOf[_to]); // Check for overflows
                        balanceOf[msg.sender] -= _value; // Subtract from the sender
                        balanceOf[_to] += _value; // Add the same to the recipient
                }

And now, you have a function that transfers tokens from one account to another given the condition that the sender has a high enough balance. Your smart contract can have multiple different triggers that start a transaction, ranging from a specific date, to the temperature of a room.

If you'd like to see what Solidity and a Smart Contracts look like in the Ethereum network, make sure you check this out, or, if you'd like to try it out on the Ethereum test network, then you can follow this very detailed tutorial by Pete Humiston.

Conclusion

In this article, we used Ethereum as an example, but the underlying principle of Smart Contracts can be applied to other blockchains that allow the usage of such features such as RSK sidechain, NEO, ADA, and many others. Smart Contracts can be autonomous, and you can even make Smart Contracts to support other Smart Contracts.

So, hopefully I've been able to help clarify the enigma that is Smart Contracts. I'm curious to know what your thoughts on Smart Contracts are. Leave them in the comments below!

 


Author

Rodrigo Sekimoto

I'm Rodrigo Sekimoto, first of my name, developer of iOS applications, creator of bugs, the last to arrive for meetings, and enthusiast of blockchain witchery.


ETLs: Connecting End-User Transactions to Business Insights

READ MORE

Microservices 101

READ MORE

How to Implement the File Upload Process In A REST API

READ MORE