The Ultimate Guide to Smart Contract Auditing

Pari Tomar
9 min readApr 24, 2023

--

Smart contract auditing is crucial for ensuring the security and correctness of smart contract code. In this article, we’ll explore the various aspects of smart contract auditing, including the Ethereum Virtual Machine (EVM), Solidity programming concepts, and best practices for testing and identifying security vulnerabilities.

Understanding the Importance of Smart Contract Auditing

Smart contract auditing is a crucial process that ensures the security and reliability of smart contract code. In this section, we’ll explore the importance of smart contract auditing and why it’s necessary.

Why smart contract auditing is important

Smart contract auditing is essential for identifying potential security vulnerabilities, programming errors, or other issues that could affect a smart contract’s functionality. Unlike traditional software, smart contracts are self-executing and cannot be modified or updated once deployed on a blockchain. This makes it necessary to ensure that the code is secure and free of vulnerabilities before deployment.

Examples of high-profile smart contract security incidents

There have been several high-profile smart contract security incidents in the past, highlighting the importance of auditing. For example, the infamous DAO hack in 2016 resulted in a loss of $50 million worth of Ether. This incident was caused by a vulnerability in the smart contract code, which allowed an attacker to drain funds from the DAO’s smart contract.

How auditing can prevent security vulnerabilities and potential losses

By performing a thorough audit of smart contract code, developers can identify potential security vulnerabilities and ensure that the smart contract’s code is secure and reliable. This helps prevent losses and damages caused by security breaches, bugs, or programming errors. Smart contract auditing can also help identify potential improvements or optimizations, enhancing the contract’s overall functionality.

Kickstart your Smart Contract Auditing Journey

Before you dive into the roadmap, it is recommended to check out the Smart Contract Hacking Course by JohnnyTime. This comprehensive course is designed to give you a solid grounding in Smart Contract Auditing, setting the stage for your journey ahead.

This course covers everything you need to know about smart contract auditing. From beginner level to advanced.

Understanding the Ethereum Virtual Machine (EVM)

The Ethereum Virtual Machine (EVM) is a key component of the Ethereum network that executes smart contracts. In this section, we’ll explore the basics of blockchain technology and Ethereum, followed by an overview of EVM and how it works.

Blockchain Basics

Before delving into the specifics of the EVM, it’s essential to understand the basics of blockchain technology. Blockchain is a distributed ledger that allows secure and transparent transactions between parties without the need for intermediaries. Ethereum is a decentralized blockchain platform that enables the execution of smart contracts.

How the EVM works

The EVM is a virtual machine that executes smart contracts on the Ethereum network. It is a crucial component of the Ethereum ecosystem and is responsible for ensuring the security and reliability of smart contracts. Here’s an overview of the EVM and its components:

Overview of the EVM and its components

The EVM is a sandboxed environment that runs on every node in the Ethereum network. It is designed to be platform-agnostic, meaning that it can run on any operating system. The EVM has four components:

Storage, memory, and calldata

  • Stack: The stack is a last-in, first-out (LIFO) data structure that stores values and data during the execution of smart contracts.
  • Memory: Memory is a byte array used to store data during contract execution.
  • Storage: Storage is a key-value store used to persist data between contract executions.
  • Calldata: Calldata is the input data passed to a contract during its execution.

Blocks, transactions, and gas

Blocks are batches of transactions that are validated and added to the blockchain. Transactions are the fundamental unit of work in Ethereum, representing any change made to the blockchain. Gas is the fee paid by users to execute smart contracts on the Ethereum network.

Basic understanding of opcodes

Opcodes are low-level instructions that the EVM uses to execute smart contracts. Opcodes include arithmetic and logical operations, memory and stack manipulation, and control flow instructions.

Solidity Concepts

Solidity is the most popular programming language used to write smart contracts on the Ethereum network. In this section, we’ll explore the basics of writing and deploying contracts using Solidity, tips for writing secure code, and an overview of ERC20/721 tokens.

Writing and Deploying Contracts

Smart contracts are self-executing contracts that are stored on the blockchain. They contain the terms of the agreement between parties and are executed automatically when specific conditions are met. Here are the basic concepts you need to know when writing and deploying contracts using Solidity:

Overview of contracts, modifiers, and functions

Contracts are the fundamental building blocks of smart contracts. They contain the state variables, functions, and events necessary to execute a particular agreement. Modifiers are used to modify the behaviour of functions, while functions are executable code blocks that perform specific tasks.

Tips for writing secure code

Writing secure smart contracts is essential to ensure the safety and reliability of the Ethereum network. Here are some tips for writing secure code:

  • Keep the code simple and easy to read.
  • Use standard libraries and avoid re-inventing the wheel.
  • Use well-audited contracts whenever possible.
  • Test thoroughly before deploying to the blockchain.

Writing and deploying contracts using Remix

Remix is a web-based Integrated Development Environment (IDE) used to write and deploy smart contracts on the Ethereum network. It provides a user-friendly interface for coding, testing, and deploying contracts.

Understanding ERC20/721 and how they work

ERC20 and ERC721 are two popular token standards used to create fungible and non-fungible tokens, respectively. ERC20 tokens are interchangeable, meaning that they can be exchanged for other tokens of the same type. ERC721 tokens are unique and non-interchangeable, representing a unique asset or collectible.

Advanced Solidity Concepts

Once you have a solid understanding of the basics of Solidity, it’s time to dive into more advanced concepts. Inheritance, proxies, and libraries are three important concepts to understand in Solidity.

Inheritance, proxies, and libraries

Inheritance allows a smart contract to inherit properties and methods from another contract, which can help reduce the amount of code you need to write. For example, you could create a base contract with common functionality and then have other contracts inherit from it to get that functionality.

Proxies are smart contracts that act as an intermediary between users and other contracts. They can be used to implement upgradable contracts, where you can update the logic of a contract without needing to migrate all the data to a new contract.

Libraries are a way to share code between contracts. They allow you to write reusable code that can be called from multiple contracts, which can help reduce the amount of code you need to write and make your contracts more modular.

Learning to Use Foundry

Foundry is a tool that can help you deploy and test your smart contracts more efficiently. Here are some key concepts to understand when learning to use Foundry:

Deploying and Calling Functions

Foundry allows you to easily deploy your smart contracts and call their functions. It provides a simple user interface that lets you specify the contract you want to deploy, the function you want to call, and any required parameters.

How to set up simple tests

Foundry also makes it easy to write and run tests for your smart contracts. You can set up simple test cases that check the behaviour of your contracts under different conditions, which can help you catch bugs and ensure that your contracts work as intended.

Mainnet forking and its working

Foundry allows you to connect to a live Ethereum network and create a “fork” of the network, which lets you test your contracts in an environment that closely resembles the live network. This can be useful for testing how your contracts behave in real-world conditions.

Gas tracking

Gas is a measure of the computational resources required to execute a smart contract function. Foundry provides tools to track the gas usage of your contracts, which can help you optimize their performance and reduce their costs.

Interacting with Web3 Protocols

Interacting with Web3 protocols is a key part of building decentralized applications on Ethereum. Here are some ways to interact with Web3 protocols using Foundry:

Using smart contracts to automate tasks

Smart contracts can automate a wide range of tasks on Ethereum, from executing trades to managing complex financial instruments. By learning to use smart contracts effectively, you can create powerful decentralized applications that offer unique benefits to users.

Borrowing on Aave using Foundry

Aave is a decentralized lending protocol that allows users to borrow and lend cryptocurrencies. With Foundry, you can easily interact with the Aave protocol and borrow funds using your smart contracts.

Simple-hop and multi-hop swaps on Uniswap using Foundry

Uniswap is a decentralized exchange that allows users to swap tokens without an order book. With Foundry, you can create smart contracts that automate simple-hop and multi-hop swaps on Uniswap, which can be useful for executing complex trading strategies.

Lending on Compound and redeeming cTokens using Foundry

Compound is a decentralized lending and borrowing protocol that allows users to earn interest on their cryptocurrencies. With Foundry, you can create smart contracts that interact with the Compound protocol and lend or redeem cTokens, which represent your stake in the Compound lending pools.

Using testnet and mainnet forking

Testnet and mainnet forking allow you to test your smart contracts in a simulated environment that closely resembles the live Ethereum network. With Foundry, you can easily switch between testnet and mainnet forking to test your contracts under different conditions.

Mastering Writing Test Cases

When it comes to smart contract auditing, testing is crucial to ensure the security and functionality of the code. Writing effective test cases is essential to identify potential vulnerabilities and ensure that the contract behaves as expected. Here are some key points to keep in mind when mastering the art of writing test cases:

Understanding the creation of tests

Before diving into writing test cases, it’s important to understand what you’re trying to test. This involves thoroughly analyzing the smart contract code and identifying the key functionalities that need to be tested. Once you’ve identified these functions, you can start writing tests that will exercise them and verify that they behave as expected.

Using fuzzing to test for vulnerabilities

Fuzzing is a technique that involves generating random inputs and feeding them into a program to identify potential vulnerabilities. When it comes to smart contracts, fuzzing can be particularly useful for testing edge cases and identifying unexpected behaviours. By generating a wide range of inputs and observing how the contract responds, you can identify potential security vulnerabilities that might otherwise go unnoticed.

Reading logs generated by tests

When you run tests, they generate logs that provide information about how the contract behaved during the test. These logs can be useful for debugging and identifying potential issues. By carefully examining the logs, you can get a better understanding of how the contract works and identify potential vulnerabilities that might not be immediately apparent from the code.

Understanding Security and Reading Reports

Smart contract security is a critical concern, as even minor vulnerabilities can have significant financial consequences. It is important to take the time to review existing reports and studies that highlight common security issues and attack vectors.

Known attack vectors

One common attack vector is the reentrancy attack, where an attacker exploits the recursive nature of some smart contracts to withdraw funds repeatedly. Other vulnerabilities include integer overflow and underflow, which can lead to unintended behaviour, and the lack of proper access control, which can allow unauthorized parties to access and manipulate smart contract data.

Thinking like an attacker

To prevent such attacks, it’s crucial to think like an attacker and anticipate potential vulnerabilities that could be exploited. This requires a deep understanding of smart contract security and the ability to identify potential weaknesses in your code. It’s important to stay up-to-date on the latest security trends and best practices, such as using multiple layers of authentication and implementing appropriate access control measures.

Practice

To reinforce your understanding of smart contract auditing, it’s important to put your knowledge into practice. One way to do this is to complete challenges on platforms like Ethernaut and DamnVulnerableDeFi.

Completing challenges on Ethernaut and DamnVulnerableDeFi

Ethernaut is a web-based game that allows you to learn about smart contract security vulnerabilities in a gamified setting. The game features a series of challenges that teach you how to exploit common security vulnerabilities and reinforce your knowledge of auditing techniques.

DamnVulnerableDeFi, on the other hand, is a purposefully vulnerable DeFi platform that allows you to practice auditing skills in a realistic setting. By exploiting vulnerabilities in the platform, you can learn how to identify and remediate security flaws in real-world DeFi applications.

By completing challenges on Ethernaut and DamnVulnerableDeFi, you’ll gain valuable hands-on experience that will help you become a more effective smart contract auditor.

--

--

Pari Tomar

Researches, talks, shares know-how on building a career in blockchain space