How smart contracts work
Smart contracts are special programs that run on blockchains. They're called smart contracts because, well, they're contracts that are smart! Smart contracts are self-executing contracts with the terms of the agreement being directly written into lines of code. The code and the agreements contained therein are stored and replicated on a blockchain network. The blockchain network provides the security, so parties in the transaction don't have to trust each other.
Benefits of smart contracts
Smart contracts allow for the automation of contract execution and enforcement, reducing the need for intermediaries and increasing the security and speed of executing contracts.
One simple example may be that Alice wants to trade 1 ETH from Bob for $1000. Alice could send Bob 1 ETH and hope Bob then sends Alice $1000, but then she'd have to trust that Bob will send her the money or find an intermediary. With a smart contract, they can agree to the transaction and the trade will be automatically executed. They don't have to trust each other or find an intermediary.
Smart contracts have a wide range of use cases, including automated payments, non-fungible tokens (NFTs), and identity verification. They offer numerous benefits, including efficiency and cost savings, increased security and reliability, and the automation of processes that previously required intermediaries.
What Blockchains Use Smart Contracts?
Ethereum is the first and most well-known blockchain platform for smart contracts, with a large developer community and a robust ecosystem of tools and resources. It offers a decentralized virtual machine, the Ethereum Virtual Machine (EVM), which executes smart contracts. The EVM is a key component of Ethereum, as it allows for the execution of arbitrary code, enabling the creation of a wide range of applications on the platform.
There are many blockchains that use the EVM to execute code, and many of them are built on top of Ethereum itself to extend the capability of the Ethereum network. Blockchains built on top of Ethereum are often called "Layer 2s" because they are an additional blockchain layer on top of Ethereum. They include Optimism, Arbitrum, Polygon, zkEVM, BnB Smart Chain, and many others.
How to Interact with Smart Contracts from Web Applications
One way developers can interact with smart contracts from web applications is by using a web3 library, such as web3.js and ethers.js. These libraries allow developers to interact with blockchain nodes directly.
Here's a quick example using ethers.js. Ethers.js is available as an npm package and can be installed using:
And simply imported into your application:
Once a connection is established, developers can use these libraries to send transactions to the network and interact with smart contracts deployed on the blockchain.
To make a connection and query the balance of an account, you can use the following code:
Where address is the blockchain address of the user's account. But how does a user get an account?
User Accounts on Ethereum
On Ethereum, there are two types of accounts: externally owned accounts (EOAs) and smart contract accounts. Blockchain accounts are often called wallets. EOAs are just a public-private key pair that is used to sign transactions on the blockchain. To create an account, a user just needs a random private key, from which a public address is derived. This puts a lot of burden on the user, since they have to manage a private key. If it is lost or compromised, they lose everything. Most Ethereum accounts today are EOA wallets, but that is changing.
The other kind of account is smart contract accounts, sometimes called smart contract wallets. Smart contract accounts are code and live on the blockchain. This enables user accounts to have a wide range of features that an EOA cannot have, such as the ability to recover accounts, pay transaction fees in flexible ways, authenticate with arbitrary logic, and execute multiple transactions at once. This is called Account Abstraction. Smart contract accounts allow users to manage their digital assets and interact with smart contracts in a secure and convenient manner.
One platform that provides an API and tools for interacting with blockchains and smart contract wallets is Stackup. The Stackup platform allows developers to easily build and deploy smart contract wallets, as well as integrate them into applications.
What is the Best Platform for Smart Contracts?
How to Run your First Smart Contract Application
Don't be intimidated by all of these new frameworks and tools! It takes some time to learn, but we have you covered.
Run your first smart contract application in under 5 minutes using Stackup's quick start guide!