Smart Contracts in Blockchain: What They Are, How They Work

They’re both simpler and more complicated than you’d think.

Written by Lisa Bertagnoli
Image: Shutterstock / Built In
Image: Shutterstock / Built In
UPDATED BY
Hal Koss | Sep 28, 2022
REVIEWED BY

Chances are good you’ve used a vending machine. You choose your item, then feed enough money into the machine to pay for it. The machine verifies that it’s the correct amount, then fetches and dispenses your item. You get said item only if you’ve fed the machine the right amount of money.  

Smart contracts aren’t all that different.  

What Is a Smart Contract? 

“A smart contract is a program that automatically executes an agreement between parties based on rules written into a blockchain. Smart contracts can save businesses time and money by processing transactions more efficiently, transparently, and anonymously. They're part of the movement to decentralize finance (DeFi) and de-commodify our personal data.” – Guy Gotslak, president, My Digital Money

Smart contracts provide a way to transact business on blockchains. They are digital documents that make NFTs, crypto trading and many more Web3 tasks possible. And they have plenty of benefits, including transparency and immutability, and quite a few challenges, including transparency and immutability.

More on Blockchain Innovation50 Blockchain Companies Paving the Way for the Future

 

What’s a Smart Contract in Blockchain?

“A smart contract is a program that runs on a blockchain that enables the conversion of traditional contracts into digital counterparts. Like crypto wallets, smart contracts have a balance and can send transactions over the network. 

The difference is that once deployed, smart contracts run as programmed. Like a standard contract, smart contracts can establish rules and then have the system automatically enforce those rules through code. Smart contract interactions are irreversible by default and cannot be undone.”

Adam Adler supplied that description and passed along the vending-machine analogy. “With both a vending machine and smart contracts, a specific input guarantees predetermined outputs,” said Adler, co-founder and head of creative at Myntr, a New York-based NFT and digital agency. The analogy and the term “smart contract” are both attributable to computer scientist Nick Szabo, Adler said. 

Smart Contract Terms to Know 

  • EVM: Ethereum Virtual Machine, or the framework that enables the execution of transactions such as smart contracts.
  • IDE: Integrated Development Environment; tools such as Truffle that are necessary for deploying smart contracts.
  • Fungible token: An interchangeable token associated with a blockchain; cryptocurrency is a fungible token. 
  • Gas: The fees (gas is most commonly associated with Ethereum) charged for executing transactions on a blockchain. Gas fees are payable in a token native to the chain.
  • NFT: Non-fungible token, most commonly a unique piece of art, made and managed via a smart contract.
  • Node: Forms the infrastructure of the blockchain, which is composed of thousands of nodes.
  • Solidity: The programming language, similar to JavaScript, used to write smart contracts.

 

Uses for Smart Contracts 

Smart contracts are usually associated with NFTs, or non-fungible tokens. That’s hardly their only application, though, for instance, any kind of transaction that would be handled via a paper contract can be handled via a smart contract. “The applications are nearly endless,” said Alex Wykoff, director of product at Wursta, a tech consultancy based in Austin, Texas. 

Here are a few examples:

Loans and Mortgages to Purchase a Home or Vehicle

Terms of these contracts are set in stone, meaning neither party can back out or fiddle with the price. While smart contracts do entail fees (charged for the computing power of the blockchain), they can be less expensive to execute because there’s no middle party (for instance an attorney or a centralized financial institution). 

Insurance

Smart contracts can be set up to trigger and resolve a claim right after an accident occurs. This cuts the considerable red tape and back-and-forth that an accident claim can generate, according to blog 101 Blockchains.

Gaming

Gaming NFTs have risen in popularity as a way to buy in-game purchases, sell them to other players, or even move them to other supported games, said Wykoff. “The unique ability for NFTs to retain value beyond a single game gives the limitless potential for new experiences within gaming,” he said.

Retail

Home Depot has used smart contracts to resolve vendor disputes since 2018, Adler said. “The transparency of smart contracts allows retailers and suppliers to communicate and address issues in real time,” he said.

Ticketing

Smart contracts could shake up the live-event ticketing industry. Case in point: Ticketmaster’s vice president of blockchain products Sandy Khaund signaled the company’s interest in pursuing smart contract technology to make its ticketing processes smoother and more secure.

More on Smart Contract Applications8 Crypto Loan Companies Using Blockchain for Lending

 

How Do Smart Contracts Work? 

Smart contracts run autonomously. They require no need for manipulation by humans, as is the case with paper contracts. There are no intermediaries, and trust is not a requirement because the blockchain automatically executes the contract once terms are met. “In a non-smart contract, even if the conditions are met, one still needs to trust another party to honor the agreement,” Adler said.  

Smart contracts run on if-then conditional arguments written in code, he explained. A network of computers continuously monitors when rules are met and verified and, in turn, executes predetermined actions, for instance buying digital assets or exchanging currency.

 

How to Create a Smart Contract 

The process involved in writing and creating smart contracts can be broken down into seven steps, said Weijia Zhang, co-chair of the cross-chain interoperability working group for the Ethereum Enterprise Alliance, a Wakefield, Massachusetts-based group that promotes the use of Enterprise Ethereum and Mainnet Ethereum. 

 

Do Your Homework

Before wading into smart-contract territory, you need a basic understanding of how blockchains work. You also need to know why you’re using blockchain to solve a particular problem in the first place, Zhang said.

 

Study Solidity

Solidity is the leading programming language used to write smart contracts on various blockchains — most notably Ethereum, the most widely used blockchain for smart contracts, Zhang said. Its syntax is similar to JavaScript, and thus will look familiar to coders who know scripting languages. Writing smart contracts in Solidity is necessary to run smart contracts on the Ethereum Virtual Machine, a virtual environment baked into Ethereum and other blockchains that functions as a virtual computer system, Zhang explained.

 

Write Simple Code 

Solidity, a powerful programming language, can be used to build just about any application a developer can dream up, Zhang said. Because of this, smart contracts can be extremely complex. For more complex smart contracts, the actors, roles, access privilege for each function should be defined and implemented, he said. 

 

Choose a Blockchain

Ethereum isn’t the only blockchain that has smart contracts. Avalanche, Polygon, Wanchain and others also support Solidity smart contracts. Research each to understand their pros and cons, and choose a blockchain on which to deploy your smart contract.

 

Get Tokens

Deploying smart contracts on a blockchain is not free. Every blockchain charges “gas” fees to issue smart contracts that must be paid in the blockchain’s native token. Ethereum, for example, makes users pay for “gas” using ETH; similarly, Wanchain makes users pay for “gas” using WAN. The more complicated the contract, the higher the “gas” fees, Zhang said.

 

Deploy Your Smart Contract

Truffle and Remix, which are integrated development environments, are popular tools for deploying smart contracts, Zhang said. These tools consolidate all the parts of smart contracts into a single graphical user interface, or GUI. They include debuggers and editors plugins. 

 

Save the Address 

Each smart contract has a unique address on the blockchain that is generated when the smart contract is deployed. Find it, save it, and send it to the other parties so they have access to your smart contract. Take care when saving your smart contract’s address. If you send tokens to the wrong address, you could lose access to your assets. “This is very important,” Zhang said. Without the correct address, you can’t access your smart contract or its functions. 

 

Smart Contract Examples 

When learning Solidity, it’s better to start with simple smart contracts like the following HelloWorld smart contract: HelloWorld.sol, said Zhang of the Ethereum Enterprise Alliance.

Here’s what it looks like:

/** 
 * SPDX-License-Identifier: MIT
 * @title HelloWorld
 * @dev Implements the hello world program
 */
pragma solidity >=0.7.0 <0.9.0;
contract HelloWorld {
    string helloworld = "Hello World";
    function justHelloWorld() public view returns(string memory) {
        return helloworld;
}
    function showHelloWorld(string memory me) public view returns(string memory) {
        string memory result = string(abi.encodePacked(helloworld, " from ", me));
        return result;
    }
}

The source code, Zhang explained, begins with comments that describe what the smart contract is about, before introducing specific functions. The functions themselves are also simple. The first function, justHelloWorld(), outputs a string “HelloWorld”. The second function takes an input variable of me and then outputs “HelloWorld from [me]”. This simple smart contract can be compiled, deployed, and executed on a blockchain. 

Want to see more examples? JB Larson, a digital marketer at Gillware, a data recovery company based in Madison, Wisconsin, provided two examples from his Github page. One’s a practice contract and the other is an escrow contract

Find out who's hiring.
See all Developer + Engineer jobs at top tech companies & startups
View 10000+ Jobs

 

Benefits of Smart Contracts 

They have many, including immutability and transparency (both of which can also be drawbacks) and security.

 

They’re Secure

“The major benefit of smart contracts is their capacity to execute clearly defined programmatic logic with a miniscule chance of bad actors tampering with the program,” said Larson. Once a Solidity smart contract is live in the Ethereum Virtual Machine / Ethereum blockchain, there is no way to change it, he said. 

 

They’re Fast 

How many times have you heard “you need to wait X business days” for a paper contract to be completed? That’s not the case with smart contracts, as they live in a place where there’s no such thing as business days, said Pavel Tantsiura, CEO of The App Solutions Company, based in Delaware. 

 

They’re Transparent

Smart contracts live on the open decentralized ledger that is blockchain, and are visible to all people who have access to that blockchain. That means it’s tough to conceal private (or even shady) deals from prying eyes. 

 

They’re Immutable 

Once the terms of a smart contract are set, that’s it. “They can’t be interfered with by humans once they are set in motion,” said Dan Hollings, co-author of One Coin, Two Coin, What Coin? Bitcoin: Crypto for Grownups Made as Easy as Child’s Play. Take the purchase of a car, for instance. If the transaction were set up as a smart contract, the buyer wouldn’t have to worry about the seller backing out of the deal or jacking up the price.

 

Challenges of Smart Contracts

Smart contracts are immutable, so a small slip in the code can result in enormous and negative consequences. “I would strongly encourage new developers to stay on testnets until they feel very comfortable with the smart contracts they are creating,” said Wykoff of Wursta. He’d also advise everyone to use hardware or a cold-storage wallet, which stores private keys offline and helps keep smart contracts (and cryptocurrency) more secure. 

“The blockchain is global, but laws and regulations are still regional.”

Finally, “be aware of the regulatory environments you may find yourself or your users in,” Wykoff said, noting recent U.S. Treasury sanctions of a well-known cryptocurrency mixer. “The blockchain is global, but laws and regulations are still regional,” he said.

Here are a few more smart-contract challenges. 

 

They’re Immutable

Correctly coded smart contracts are fine in their immutability; those that are incorrectly coded can cause headaches. “If there is a flaw in the code that can be exploited, then the contract can be hacked and some or even all of the funds can be drained,” Hollings said. 

 

They’re Transparent

Smart contracts are visible to everyone on the blockchain on which they reside. Bugs, security holes and other vulnerabilities roll out the welcome mat for hackers, said Tantsiura of the App Solutions Company. That lack of privacy also makes smart contracts less than ideal for discreet transactions, for instance the purchase of art by a buyer who’d just as soon remain anonymous. 

 

They Can’t Be Read By Attorneys

Sophisticated coding knowledge is necessary to write and read a smart contract. That means that most attorneys are not able to read them. And smart contracts, like paper contracts, must adhere to local laws. “That can get tricky when you’re trying to comply with housing laws and such,” said Guy Gotslak, president and founder of Los Angeles-based crypto exchange My Digital Money. 

Even so, “protecting consumers is actually easier with blockchain technology because it’s more efficient, it’s all trackable and transparent,” Gotslak said. “We just need to figure out the security aspects.” 

 

They’re One-Offs 

Smart contracts, with very few exceptions, are meant to be written and deployed once. That makes them tricky for complicated situations, where conditions are still being debated or are subject to change, Wykoff said. In those instances, “perhaps you should consider an off-chain solution,” he said. 

 

They Can Be Expensive 

Just like you need to pay an attorney or third party to write a paper contract, you have to pay to execute a smart contract on a blockchain. Fees (called “gas” in Ethereum) can add up, especially if the value of the token soars during the operation. 

 

They’re Not Ubiquitous 

Analog contracts remain the standard in transactions; smart contracts are not used by everyone, everywhere, said Brian Platz, CEO and founder of Fluree, a North Carolina-based Web3 data platform. “Hurdles come with implementing this new technology, including issues concerning programming language as well as companies and industries that may hold out on adopting it,” Platz said. 

While slow or small-scale adoption could make the technology less effective and useful to the large pool of people who can benefit from it, smart contracts will become more commonplace with time and the increased efficiency of smart contracts, he added. 

Blockchain Platforms to Know20 Blockchain Platforms Driving the Industry

 

The Bottom Line on Smart Contracts

Given the relative newness of blockchains, smart contracts are remarkably stable, said Zhang of the Ethereum Enterprise Alliance.

“We have not seen issues with smart contracts where it’s the blockchain itself that actually breaks down,” Zhang said. Solidity, for example, is in v0.8.16. “It’s not even v1 yet,” Zhang said. “As with every new technology, there are some risks.” But, he added, “people are aware of the risks and the development community has been making steady progress.” 

“We have not seen issues with smart contracts where it’s the blockchain itself that actually breaks down ... It’s the people who write smart contracts.”

The biggest challenge isn’t the language, or EVM, or blockchains, Zhang said. “It’s the people who write smart contracts,” he said. A bug on just one line of code can damage the platform and spark the loss of millions of dollars. Complex smart contracts, he explained, require hundreds of lines of codes. In contrast, simple smart contracts, like those used to create basic NFTs, can be deployed with only 10 lines of code and an import function.

The takeaway? Smart contracts, as with so many things Web3, are a work in progress.

Explore Job Matches.