Smart Contracts Simply Explained

Stefano Tempesta
5 min readSep 9, 2020

You may have heard about Smart Contracts when talking about blockchain, but what are they exactly and what problems do they solve?

The term “Smart Contract” was first used by Nick Szabo, a computer scientist and cryptographer known for his research in digital contracts and digital currency, back in 1997, long before Bitcoin was created. In in paper on “Formalizing and Securing Relationships on Public Networks”, Szabo compares smart contracts to vending machines. The theoretical machine takes in coins, and through simple computation, dispenses product and change according to the displayed selection of the user. He later proposes in the paper to embed smart contracts in a variety of properties that are then controlled by digital means.

The idea is simple (well, sort of): Let’s store contracts in a distributed and decentralized digital ledger!

A crowdfunding example

Now, smart contracts are just like contracts in the real world. The only difference is that they are completely digital. In fact, a smart contract is actually a tiny computer program that is stored inside a blockchain. An example will clarify how smart contracts work.

You probably are familiar with Kickstarter, the large fundraising platform. Anybody can go to Kickstarter, create a project, set a funding goal and start collecting money from others who believe in the idea. Kickstarter is essentially a third party that sits between creative and entrepreneurs and supporters. All people involved in the fund exchange need to trust Kickstarter to handle their money correctly. If the project gets successfully funded, the project team expects Kickstarter to give them the money. On the other hand, supporters want their money to go to the project if it was funded, or to get a refund when it hasn’t reached its goals. Both the product team and its supporters have to trust Kickstarter.

But with smart contracts we can build a similar system that doesn’t require a third-party to validate transactions (nothing personal here, Kickstarter 😊). We can program a smart contract so that it holds all the received funds until a certain goal is reached. The supporters of a project can now transfer their money to the smart contract. If the project gets fully funded, the contract automatically passes the money to the creator of the project. And if the project fails to meet the goal, the money automatically goes back to the supporters. And because smart contracts are stored on a blockchain, everything is completely distributed. With this technique, no one is in control of the money. Pretty awesome right?

But hold on a minute! Why should we trust a smart contract? Smart contracts are stored on a blockchain, thus they inherit some interesting properties: They are:

· Immutable: Once a smart contract is created, it can never be changed again. No-one can go behind your back and tamper with the code of your contract.

· Distributed: The output of your contract is validated by everyone on the network. A single person cannot force the contract to release the funds because other people on the network will spot this attempt and mark it as invalid. Tampering with smart contracts becomes almost impossible.

A Solid(ity) example

Now you might wonder where and how you can use smart contracts. Right now, there are a handful of blockchains that support smart contracts, but the biggest one is Ethereum. Ethereum was specifically created and designed to support smart contracts. These contracts can be programmed in a special programming language called Solidity, a programming language specifically created for Ethereum, with a syntax that resembles JavaScript.

A smart contract is identified by the contract keyword, as you would use class for an object-oriented programming language such as Java or C#. Parties involved in the contract agreement are identified by their address on the blockchain network. As we have funds being exchanged, the accounts are marked as payable.

We also want to keep track of the progress of the crowdfunded project. To keep things simple, you may want to define the following project states:

· Kickstarted (by the project owner)
· Funds deposited
· Funds released
· Fund returned

A smart contract acts as a bank, basically. Investors deposit their funds into a smart contract, and not directly to the project owner. Although transfer between two parties directly is possible, introducing a smart contract to mediate transactions is a typical design pattern in blockchain solutions. In Ethereum, a smart contract has a constructor to initialize the contract itself, and a receive() payable function that allows other parties to transfer ethers into it.

When depositing funds into the contract, some sort of validation occurs. This is what smart contracts are for, mainly, for validating conditions for actions to happen. If conditions are met, the specific transaction is authorized. Solidity uses the require function to set conditions that should be valid for that action to progress. In the Deposit() function in this crowdfunding example, the smart contract requires the amount to be greater than zero, and the sender (whoever is investing into the project) to have enough funds in their balance to transfer. When the conditions are met, the smart contract stores the address of the sender as the appointed Investor, and transfers the amount into its own (of the smart contract) balance, to keep it safe for future usage.

When milestones are reached, which are controlled in the external environment, funds can be released. Only the investor can trigger this action, and the smart contract will check for this constraint before authorizing a fund transfer.

But if the project doesn’t go ahead as planned, funds are returned to the investor.

C’est plus facile… 😊

--

--

Stefano Tempesta

Web Architect working at the crossroad of Web2 and Web3, to make the Internet a more accessible, meaningful, and inclusive space.