crypto.bi

Understand the basics of smart contracts

What’s a smart contract? What makes it smart? How can I deploy my own contracts on the Ethereum blockchain? In this article we explore these questions and give you an overview about smart contracts and their revolutionary contribution to the world of cryptocurrencies.

In the beginning there was Bitcoin. It’s a protocol that allows you to move coins up and down, right? Well, not only that. Bitcoin is actually the result of the processing of a stack-based computer programming language contained within each Bitcoin transaction!

Here’s what a Bitcoin transaction actually looks like, in practice:

  • Add the amounts up in the source transactions. Do they add up to more than is being transferred?
  • Verify that the source info with this new transfer hash up to the exact same hash provided by the sender.
  • Does the signature match that of the public key used in the previous input? If so, then the Bitcoin gets transferred.
  • If it does not match then the transaction is refused.

Does this look a little like a computer program to you? Well, it’s because it is a program, albeit a very simple one. Bitcoin is actually a scripting language! The above script checks everything in a transaction is OK before committing it into the blockchain. Once the script runs, the end result must be “OK”, or “true” in boolean logic. If the very last result of the program is true, then the transaction goes forward.

So what does this have to do with smart contracts? It just so happens that contracts are nothing but computer programs as well. If the results of the contract’s intermediary steps is true or positive, it means those steps were run successfully. Smart contracts are simply a more complete programming language than that offered by Bitcoin. This step forward to a fully featured programming language leads us to call Ethereum and similiar platforms as second generation cryptocurrencies. These are very similar to Bitcoin, but instead of a tiny stack-based language (where results get put on a stack, like a stack of plates, as they appear), they have large fully featured languages that allow us to encode much more complex logic than Bitcoin does.

Although Bitcoin’s tiny embedded language is very limited, it can still do a lot! It can handle multiple signatures, it can check cryptographic hashes, it can encode transactions and verify private key signatures and so on. But it’s really not enough to encode a complex system like a casino or a decentralized exchange.

Ethereum, on the other hand, boasts a fully featured programming language called Solidity. Solidity looks a lot like Javascript, but it is contract-oriented and has special limitations in place in order to run on a worldwide virtual computer called the Ethereum Virtual Machine (EVM). A Solidity program is compiled from humanly readable code to a computer language representation called byte code. It’s called that way because all the humanly readable commands end up as bits and bytes which represent commands and values. These bytes are then stored on the Ethereum blockchain. This, in part, is why Ethereum blockchain exploded in size and now takes months to get verified on a full node: every single program, every single contract, must reside on the blockchain and must get verified by full nodes. Every Ethereum contract is located on the blockchain and the execution of these programs depends on a worldwide collaborating network of full nodes. It really is amazing and it’s where the Ethereum name comes from: ETH programs run on the Ether, they run nowhere and everywhere, it’s a massive distributed and decentralized computer which runs code found on the blockchain.

In this page a complete auction system is presented in Solidity. As you can see the Solidity code looks a lot like Javascript, but it contains special instructions that limit what people can do. Some instructions are reserved for the contract owner, for example who can stop the contract in case disaster strikes? Who can withdraw funds sent to the contract? If these were not restricted commands, then anyone could sabotage the smart contract. And it has happened in the past.

Here’s a complete Hello World program written in Solidity:

pragma solidity ^0.4.9;
contract mortal {
/* Define variable owner of the type address*/
address owner;
/* this function is executed at initialization and sets the owner of the contract */
function mortal() { owner = msg.sender; }
/* Function to recover the funds on the contract */
function kill() { if (msg.sender == owner) selfdestruct(owner); }
}
contract greeter is mortal {
/* define variable greeting of the type string */
string greeting;
/* this runs when the contract is executed */
function greeter(string _greeting) public {
greeting = _greeting;
}
/* main function */
function greet() constant returns (string) {
return greeting;
}
}

This contract needs to be compiled to bytecode using a program cold solc, which stands for solidity compiler. The result is a binary interface that gets stored on the blockchain. The binary data stored on the blockchain together with a interface specification can be used to call functions from programs existing on the blockchain. If I have a tiny bit of Ethereum to pay for GAS and send this contract a specially coded message requesting the function greet() be called, I’ll be greeted with the contents of the variable called greeting, whatever it may have been initialized to by the contract creator when the greeter() constructor was called.

While this contract doesn’t do much, it goes to show how much more complex the Ethereum programming language is when compared to the simple stack-based language contained in Bitcoin. As you can see, complex logic can be encoded into such a program.

For example, a car sale can be encoded in a smart contract. A smart contract could track 3 variables: car paid, car transferred and contract finished. As the agreed upon amount is received from the buyer, the variable car paid is set to true. The smart contract then proceeds to change the owner name and ID to that of the buyer. After completing this the smart contract would mark car transferred to true. It would then verify some additional business logic and if everything’s ok, mark the contract as finished. All this happens without the intervention from a human, the contract is self enforcing code that runs on the Ethereum blockchain. Once the car sale is completed, anyone, anywhere, can verify that this is the case by inspecting the status of the contract.

Smart contracts can be designed to trigger specific events once other events happen. For instance it may pay the wages in a organization every X number of blocks. Since block time is relatively stable, a smart contract can be programmed to pay weekly or monthly wages almost in time, without any human’s intervention. In fact, that was the idea behind The DAO – a fully decentralized organization, with jobs, promotions, wages and full administration using a decentralized computer program. It was a very complex smart contract and it showed, when hackers found a way to subvert it and promote chaos by stealing all the funds contained in the smart contract.

This brings us to a very important aspect of smart contracts: security and good programming practices are an absolute must. It is estimated that a large percentage of smart contracts have trivial vulnerabilities in them. The same vulnerability exploited in The DAO, for example, still exists in hundreds of contracts currently deployed. All that is needed is for a hacker with an automated tool and some spare Ethereum to exploit these, stealing funds from random contracts automatically.

Conclusion

As you can see, smart contracts have been a part of even the earliest cryptocurrency, but in a much more simplified form. Ethereum revolutionized the cryptocurrency world with a highly powerful programming language that allows businesses to encode complex logic in self-enforcing fully decentralized contracts. New generations of smart contract languages, that like found on Cardano ADA, promess to extend this powerful functionality into a 3rd generation cryptocurrency ecosystem by including compliance and regulations into the mix, making a complete business solution possible out of the box. We hope you’ve enjoyed this tour through the world of smart contracts!

About the Author
Published by Crypto Bill - Bill is a writer, geek, crypto-curious polyheurist, a dog's best friend and coffee addict. Information security expert, encryption software with interests in P2P networking, decentralized applications (dApps), smart contracts and crypto based payment solutions. Learn More About Us