HOLIDAY OFFER: Get the gift of up to $70 of Bitcoin. While supplies last!

Shop now

Up your Web3 game

Ledger Academy Quests

  • Test your knowledge
  • Earn POK NFTs
Play now See all quests

The Classroom

PATHWAY L) Doing Your Own Research (DYOR)

chapter 4/5

Introduction to Smart Contract Risks

Read 7 min
Medium
bored ape on grey background
KEY TAKEAWAYS:
— Be aware of the most common types of Smart Contract Risks in the EVM ecosystem, such as Reentrancy and Signature Malleability.

— Understand Proxy contracts and what they mean for the projects you buy.

— Learn what to look out for on Smart Contract Audit Reports.

The Ethereum Network is a publicly accessible network for transacting in almost any way imaginable using programs called Smart Contracts. Smart contracts were first introduced on Ethereum, and they are like terms of an agreement directly written into lines of code. The code controls the execution, allowing decentralized applications to operate without middlemen.

But even without middlemen, smart contracts face their own challenges—challenges that could affect your funds if you interact with them. 

Today we’ll discuss some common pitfalls developers can encounter when writing smart contracts, and how to identify them before interacting with an unknown contract.

What Is a Smart Contract Again?

Think of Ethereum as a vast, decentralized computer. It operates just like your computer at home, but in actuality, it is just a software emulation of a physical computer, also known as a virtual machine. On the Ethereum Virtual Machine (EVM), a smart contract is a computer program that automatically executes when predetermined conditions are met. These contracts are typically used to execute agreements so all participants can immediately know the outcome without intermediary involvement.

Smart contracts are the engine that enables decentralized applications (dApps), from decentralized finance (DeFi) applications and games to decentralized autonomous organizations (DAOs).

The Risks Smart Contracts Face

Because smart contracts have so much authority and generally have access to a great deal of financial assets, they are prime targets for hackers and thieves. What’s more, smart contracts are powered by code, which can include potential vulnerabilities or malicious functions. This means that their attack surface area increases as they become more complex. Here are some of the most common attacks smart contracts can face:

Reentrancy attacks

Reentrancy refers to an attacker being able to “re-enter” a contract call before it finishes executing, allowing them to take advantage of updates that haven’t happened yet (such as balance reductions). Most famously, this occurred in the DAO hack that led to the fork of Ethereum into Ethereum and Ethereum Classic.

Integer overflow and underflow

Overflow and underflow can happen during arithmetic operations. If not accounted for, integers can “underflow” by going below zero, wrapping around to a very large number, or “overflow” by surpassing the very large number and wrapping back around to zero. This can lead to improper accounting and allow attackers or unknowing users to either lose funds or gain access to funds that don’t belong to them. 

Signature malleability

Signatures are often used for verification in smart contracts. However, at least two variations of a signature are usually valid representations of the same data. If a smart contract keeps track of specific signatures that have been submitted (to prevent reuse), then the alternate variation can be used to bypass the check. Contracts should avoid reuse by using other indicators, such as nonce.

Access control issues

Smart contracts often have functions that are restricted to admin use. These functions are usually marked by an ‘onlyOwner’ tag. Any such functions should be carefully inspected to ensure they don’t allow for unwarranted power, such as unauthorized transfers or burns of NFTs or other assets. There are also cases where a function that should be restricted is not, which exploiters can use to their advantage.

Floating point precision loss

The EVM does not recognize decimal numbers, which can often cause issues during division. For instance, we know that 3/2 = 1.5. However, in the EVM world 3/2 = 1!  To deal with this, most values are expressed in wei (e.g. 1.0 ETH becomes 1,000,000,000,000,000,000 wei). However, if your smart contract does not take the appropriate measures, missed calculations could lead to lost funds.

Proxy Contract Upgrade Issues

Ethereum contracts are immutable by nature, which is a feature, not a bug. Considering the value they handle, it is important to be able to rely on their logic to remain static so that they continue to behave as you’d expect. However, developers have come up with a way to circumvent the immutability by using something called ‘delegatecall’ to draw all execution logic from another contract address.

To upgrade the logic of your contract, you just swap out the address that delegates calls! This offers the advantage of being able to squash bugs and make improvements on the fly. Still, it is considerably less secure than your standard smart contract because you lose the guarantee of immutability.

How to Analyze a Smart Contract

One way to analyze smart contracts is to learn to read code, which requires time and dedication.

But if you’re trying to learn about smart contracts without any coding knowledge, you can first check if the smart contract has been audited. A secure smart contract should have publicly available audit reports detailing any vulnerabilities and how they were addressed. An effective audit report should cover:

  • Scope of the audit: What was covered and what was not
  • Findings: Detailed description of each issue found
  • Severity ratings: How critical each issue is
  • Recommendations: How to address each issue
  • Auditor’s credentials: Ensure reputable and experienced professionals perform the audit

Ideally, you want to see multiple independent audits from distinct trusted sources. Any high- or critical-severity vulnerabilities should have been addressed and marked as resolved. Though audits are a fantastic tool and will catch a high percentage of potential vulnerabilities, they never guarantee security.

If a contract has not been audited and you can’t read the code yourself, there’s no way of knowing if it contains a vulnerability – or, even worse, a malicious function. Of course, you can check a few red flags, even with little experience. You should also look at how long the contract has existed. You can assume that any deployed smart contract is under a constant state of attack, so the longer a smart contract is in use without an incident, the less likely it is that one is possible.

Of course, the incentive to attack a smart contract scales with Total Value Locked (TVL). A smart contract with a small amount of money is not as appealing to an attacker as one with a large amount, so a contract with $1M TVL can be considered more secure than one with $100.

But this information is the bare minimum and will only likely alert you to the most basic risks. If you decide to interact with an unaudited contract and can’t read it yourself, use a wallet containing minimal value and don’t risk more than you can afford to lose.

Conclusion

Smart contracts revolutionize how we think about agreements in the digital realm, providing a secure, transparent, and efficient means of enforcing sets of rules without intermediaries. Understanding the intricacies of how they operate and how they can fail is essential for developers and anybody participating in the space.

In the following article, we will delve into the concept of tokenomics, exploring how the functionality and security of smart contracts underpin the economic models of tokens and their impact on market dynamics.

By ensuring a solid foundation in understanding smart contracts, you will be better prepared to appreciate the complex interrelations of token mechanisms in the EVM world.

This article was written by Quit from Boring Security. Follow him on X here, and check out Boring Security’s X accountDiscord server, and official website for more information.

Stay tuned for the next article in this series where Intelligence On-Chain dives into the impact of tokenomics on a crypto project.


Stay in touch

Announcements can be found in our blog. Press contact:
[email protected]

Subscribe to our
newsletter

New coins supported, blog updates and exclusive offers directly in your inbox


Your email address will only be used to send you our newsletter, as well as updates and offers. You can unsubscribe at any time using the link included in the newsletter.

Learn more about how we manage your data and your rights.