Small note: I am referencing Ethereum as it was the first blockchain platform to offer smart contracts built into the network. Since then there has been different EVM compatible networks (Meaning the smart contract code can be deployed to the different blockchain). An example of this is the Polygon network (Now called the matic network). Examples on this page might reference a specific example on matic instead of on Ethereum. This is done as a cost saving measure as gas fees are cheaper on matic. For all purposes taught on this site the concepts will carry over 1:1 between any EVM compatible chain.
What is Ethereum?
Ethereum is a decentralized blockchain platform that enables the creation and execution of smart contracts. In the context of Ethereum, a smart contract is a self-executing program with predefined rules and logic deployed on the blockchain. These contracts automatically execute when specific conditions are met, facilitating trustless and tamper-resistant transactions without the need for intermediaries.
Essentially, Ethereum acts as a decentralized global computer, and smart contracts are the programmable agreements that run on this platform, offering a secure and transparent way to automate various processes, from financial transactions to complex decentralized applications.
Smart contract use cases
Since smart contracts are programmable code, there is no limit to what applications can be created with them. Some examples of smart contracts are listed below.:
Tokens
Tokens can be created as a smart contract. This allows creating everything from stable coins (crypto tokens pegged to the price of a specific national currency) to non fungible tokens representing "digital deeds" to real world assets such as real estate.
Crowdfunding
Crowdfunding contracts can let creators raise funds to be held by an automated escrow removing the need for a trusted third party.
DEFI
Decentralized finance, this is a catchall term for smart contracts that enable financial services all done on chain instead of requiring a trusted third party like a bank. For example, borrowing or lending of money can be done via a smart contract that locks away collateral and allows any user to instantly access liquidity of their crypto assets in a safe and instant way.
There are opportunities for generating yields via providing liquidity to liquidity pools, free floating interest rates and the ability to arbitrage markets by leveraging flash loans.
Automated wills / Trust funds
A smart contract can hold money. You can programmatically create a savings account which will allow an authorized account to withdraw a set amount every week. A similar use case is creating a savings account which will automatically grant access to a child's key on their 18th's birthday. This smart contract could be modified to serve as an automatic family trust defining which assets can be accessed by who and on what date. Access can be granted to a specific user after a set amount of time.
These are just some of the examples of the type of things that smart contracts can be used for right now. This is just meant to give some example of the wide range of applications.
How do Ethereum smart contracts work?
Ethereum is powered under the hood by the Ethereum Virtual Machine, commondly reffered to as EVM. Ethereum smart contracts are usually writtenin a higher level language, such as solidity or serpent, and then are compiled down to EVM ready byte code.
There is a universal state that the EVM keeps. Every single transaction is verified as valid (correct sender sending an amount they have, no double spends, correct signature) and will check that the execution of the specific action (in the form of a transaction) is permissible in accordance with the EVM bytecode.
Since the EVM is a "global virtual machine" you can think of deployed smart contracts as objects on the blockchain and can be referenced by the address which they are deployed to. Deploying a smart contract means creating a transaction to the zero address (0x0000000000000000000000000000000000000000) with the compiled bytecode as a payload.
Here we have an example address of a smart contract deployed to the Matic blockchain.
0x36A3677be606E5EA7a5B052cef06e95972fEFe0B
This is a smart contract for a simple ERC20 token. We can look at it on a block explorer https://polygonscan.com/address/0x36A3677be606E5EA7a5B052cef06e95972fEFe0B#code
We can read attributes from the contract.
Here for example, we can see what the balance of a specific address is for the specified token.
Those functions to read those attributes were defined on the solidity source code. Solidity is then compiled to EVM bytecode. When you read from a contract you are calling a function that returns a value. You can call read functions without needing to call a transaction. Getting any variable state does not require a transaction. If you are calling a function that alters state then you need to create a transaction.
You can see this in the "write" contract field.
The write contract field lets you interact with the contract in ways that might change the state. This can be done with different tools, but since we are using the web UI available on this page we need to connect a web3 wallet (metamask in our example) to be able to interact with the contract.
To interact with the contract in a way that changes state you have to create a transaction. In this example I will show sending 1 token to the same address. Notice the hex value of the transaction. In the transaction there is a data field. The first 4 bytes of the data specify which function to call using the hash of the function's name and arguments.