Ethereum Smart Contracts Explained
Learn the details of how smart contracts work on the Ethereum blockchain.
Overview
Smart contracts are Ethereum’s first-class citizens, and they’re an important aspect of the platform’s evolution because, unlike Bitcoin, Ethereum finds applications in numerous areas, including DeFi, smart contracts, and NFTs. We’ll look at how smart contracts function on the Ethereum blockchain in this guide. Today, with the growing adoption of Ethereum and the support of blockchain technologies, smart contracts are growing in popularity. This article will cover everything from setting up an Ethereum IDE to developing and deploying a smart contract.
What is a smart contract?
A smart contract is simply an agreement between several parties written in the form of computer code. They are automated transaction protocols that carry out contract terms allowing transactions to be traced, transparent, and irrevocable. Nick Szabo, an American computer scientist, and digital currency researcher invented the concept of smart contracts in 1994.
Ethereum accounts come in the form of smart contracts. This indicates that they have a balance and can send transactions across the network. They are not, however, controlled by a user; instead, they are deployed to the network and run according to a set of instructions. User accounts can then engage with a smart contract by submitting transactions that execute the smart contract’s declared function. Smart contracts, like conventional contracts, can set rules and have them enforced automatically through programming. Smart contracts are not altered by default, and their interactions are irrevocable.
The EVM (Ethereum Virtual Machine) is the Ethereum Blockchain’s primary component that runs the code for all of its decentralized apps. The EVM is in charge of determining how to compute a new valid state from one block to the next. The Ethereum Virtual Machine (EVM) is a powerful, sandboxed virtual stack that runs contract bytecode execution on each Ethereum node. Contracts are typically authored in high-level languages like Solidity and then compiled to EVM bytecode. As a result, the EVM acts as a large decentralized or master computer on the blockchain, performing numerous tasks.
To get our smart contracts running on a vast decentralized network, we have to create a smart contract on the Ethereum blockchain. Ethereum is special because it’s the first blockchain system to include a Turing Complete virtual machine built on top of it. This means that an Ethereum smart contract can perform any calculations as well as run practically any program.
How does a smart contract work?
A smart contract can function on its own, but it can also be used in conjunction with other smart contracts. They are also capable of working together. The successful completion of one smart contract can trigger the execution of another, and so on.
The smart contract terms are finalized and then converted into programming code. Essentially, the code is a collection of conditional statements that explain several circumstances for a future transaction. The code is written and stored on the blockchain network, where it is replicated among the blockchain members. The code is then run and executed over the entire network. The relevant transaction is completed if the contract’s terms are met and it is verified by all blockchain network participants.
People frequently compare smart contracts to vending machines to describe how they work. When you press the appropriate button on that familiar box in your office that looks like a fridge, it is programmed to take your money and dispense the currency. You feed cash into the machine to start it up, and then an internal detection system, which these boxes all contain, determines whether the bills you put into the money slot are genuine or not.
Smart contracts are one of the most beneficial aspects of blockchain technology because they eliminate administrative overhead. Financial transactions are also made transparent, easy, and efficient.
The smart contract is configured to listen for event updates from an oracle, an oracle which is simply a service that connects the smart contracts to the off-chain data. When one or more oracles provide the right combination of events, the smart contract will run.
How are smart contracts deployed?
Ethereum smart contracts are written in Solidity which is a high-level programming language for implementing smart contracts on the Ethereum Virtual Machine. The Solidity compiler turns code into EVM bytecode, which is then deployed to the Ethereum network. Ethereum Remix IDE is an open-source IDE for writing and testing smart contracts.
Getting Started
Now you know the history of smart contracts on Ethereum and their purpose. How about we dive right in and build one together to really understand the concept?
For building this smart contract we will be making use of the following tools: Remix IDE and the Metamask Browser Extension. We will be deploying on the testnet, the Rinkeby testnet. We would also need to get some test ETH from the Rinkeby Testnet.
After installing the MetaMask browser compatible with your PC, you will then need to change the network from Ethereum Mainnet as shown in the image below:
You will then click on the Networks tab in settings, select the Rinkebey Testnet as shown in the image below, and hit “Add a network”. Testnets are Ethereum networks used as a learning tool on which the Ether has no monetary value. These playgrounds are used by developers to test programs before releasing them to the mainnet.
Lastly, copy your Rinkeby address and head over to the official Rinkeby Testnet Faucet or from the Chainlink Rinkeby to get some test ETH (It could take a while to arrive). Next, head over to the Ethereum Remix IDE and make a new Solidity file, title it “first.sol”
Then copy the following code into your new Solidity script:
Let’s go through it line by line:
// SPDX-License-Identifier: MIT
The SPDX license type is specified here. SPDX license IDs are introduced in Solidity 0.6.8, allowing developers to define the exact license that the contract uses.
pragma solidity 0.8.4;
This means the source code was created for Solidity version 0.8.4 or a later version that does not break functionality. This is to ensure that the code behaves consistently across compiler versions.
contract Token {
}
This tells us the name of our new contract which is Token
. The contract is a collection of code (its functions) and data (its state) stored on the Ethereum blockchain.
uint256 amountOfTokens;
The line above declares a state variable called amountOfTokens
of type uint (unsigned integer of 256 bits). Remember that this state variable will be assigned as an unsigned 256-bit integer (unsigned means that this type can only be positive integers, not both positive and negative). The Ethereum Virtual Machine’s architecture is 256 bits in size.
function addTokens() public {
amountOfTokens += 1;
}
We have a tokens()
function that is open to the public, so it can be accessed outside the function and by other contracts, incrementing the value of count by 1.
function getAmountOfTokens() public view returns (uint256) {
return amountOfTokens;
}
We also have a getAmountOfTokens()
function that returns the number of tokens received by the contract. To compile the smart contract, go to the left menu and click the second icon (Solidity icon), then Compile first.sol
.
A green tick indicating that our project was successfully compiled will be visible in the left corner of the menu tab. Then we will deploy the project by selecting Injected Web3
from the drop-down menu. A pop-up from Metamask will appear and we have to connect our project to the wallet. Be sure to connect to the Rinkeby testnet on MetaMask before deploying the contract.
After a successful connection, the Rinkeby network
will be visible below the Injected Web drop-down indicating we are on the right network (Rinkeby Testnet). Then hit Deploy. A pop-up will appear asking you to approve the transaction.
When your contract is deployed you notice a green tick icon as shown below, you can locate all your deployed contracts under the Deployed Contracts
section.
Now we can interact with the smart contract we created, by adding tokens and also getting the amount of tokens we added:
Adding tokens would initiate a smart contract transaction, and then you would have to confirm the contract and call the getAmountOfTokens
to know how many tokens we have added as shown above.
We have now learned how to deploy and invoke a smart contract from within Remix. In future guides, we will explain how to invoke this contract from your web app using ethers.js.
What are the use cases of smart contracts?
Smart contracts reduce the number of middlemen in a complex transaction (such as a loan or finance). When more than one person is involved in a transaction, there is a chance that the parties will lack trust. Smart contracts are utilized for a variety of transactions across a variety of industries due to their ease of usage.
Here are several use cases of smart contracts:
- Decentralized finance (DeFi) — Smart contract development for DeFi is the process of creating, auditing, and implementing a smart contract that automatically delivers decentralized finance services without the need for a central authority. It shortens the time it takes to complete decentralized financial transactions like lending, investing, insurance, and banking.
- Non-Fungible Tokens (NFTs) — By combining NFTs and smart contracts, users will be able to access a wide range of use cases. NFT smart contracts are also called ERC-721. This is because it was first posted on the Ethereum Improvement Proposal (EIP) website. When the proposal was adopted, EIP-721 became ERC-721. They specify which interfaces and functions will be implemented by the smart contract. They could also serve as a mechanism for implementing a sale agreement between the NFT owner and the buyer.
- Decentralized autonomous organizations (DAOs) — Smart contracts are essentially the backbone of DAOs. DAOs can simply impose their rules, norms, and functionalities by employing DAO smart contracts. Proposals and voting can readily regulate decisions in this manner. This ensures that everyone has a voice in the governance of the DAO.
- Metaverses — Smart contracts are used in the Metaverse to automate processes and ensure that things like trade and transactions follow preset norms. Smart contracts are blockchain-based digital contracts that are programmed and run. Smart contract contracts are automatically performed once preset circumstances are met.
- Gaming — Smart contracts will assist in avoiding trust difficulties between gamers and game producers making precise information about players easily accessible. Transparent information will improve the gaming community’s relationship. It is also now easier to build an NFT marketplace with smart contracts even right from your basement. As a result, avatars, special objects, and other gaming assets can be sold for more profit.
Conclusion
Well done for making it all the way! You now understand how the EVM (Ethereum Virtual Machine) functions. You know how to use the Ethereum Remix IDE to test and write smart contracts. You created a smart contract, learned how to obtain test ETH, and then deployed it on the Rinkeby Testnet. You also learned how to use Remix IDE to interact with a smart contract.
What is Crossmint?
Our goal at Crossmint is to help make NFTs as accessible as possible. Our first product, Crossmint Pay, is a tool for NFT creators to accept credit card payments. It allows anyone to create an NFT in under a minute using only their email and credit card, with no need for a wallet or cryptocurrency. We also have our minting API as a live product in beta, you can reach out to us to get access. If you want to skip all these steps, you can also create an NFT smart contract with one line of code making it easy for anyone to produce NFT collections and expand the audience that can own them. Creators can use Crossmint for free, and we currently support Solana, Ethereum, Polygon, and Avalanche.
Follow us on Medium so you are notified when we post another article! If you have any feedback, feel free to reach out to us via Twitter. You can always chat with us on our Discord community server, featuring some of the coolest developers you’ll ever meet.
Are you a developer ready to get started? Visit the Crossmint documentation and see how you can integrate Crossmint into your project in less than 5 lines of code.