DeFi Security: Top Practices and Security Tips

DeFi development is on the rise, with more developers shifting from traditional firms to crypto startups. However, security is still a major concern in blockchain space – and DeFi field in particular, no matter the stage – before releasing apps to the public or after new feature release for a more established protocol.

With this in mind, we’d like to provide best practices and ideas for improving overall protocol security.

Focusing on DeFi security

Most developers focus on existing drawbacks and vulnerabilities associated with the specific application type while developing a DeFi application. To design a secure codebase, they must have a thorough grasp of both code and attack vectors, as well as research comparable apps and any flaws that have occurred to them in the past.

With that in mind, let’s look at the most prevalent attack vectors and risks that developers may encounter while developing a DeFi application.

Reentrancy attack

One of the most common exploit types in the DeFi area is the reentrancy attack. It is modeled after the infamous DAO attack, with a smart contract getting data from an external contract before status update.

Because smart contracts are a fundamental component of every DeFi application, their security is critical. If smart contract is tampered with as a result of reentrancy attack, it end up in funds being withdrawn without the application’s or the user’s permission.

The recommended approach here is to ensure that any adjustments to the smart contract’s state occur before calling external contracts that are unrelated to the project. Another suggestion is to employ function modifiers that block reentry.

Oracle manipulation

Oracle vulnerabilities are next in line, with hackers using AMM and/or DEX reserves as the price oracle. When the DeFi protocol uses a centralized price oracle and obtains market data from a single source, it creates a easily exploitable weak point in the protocol, as bad actors can manipulate the market or oracle price by using flashloans.

In such instances, the optimal method would be to employ a decentralized oracle network and TWAPs to increase the cost of the hacker’s attack while still obtaining accurate market data.

Audits & Risk Management Solutions

When exploiting a protocol, there are several attack methods and pathways that bad actor might take. Even the greatest developers and in-house security specialists sometimes miss little backdoors in code that can lead to an exploit – code lines blur with time, something is missed during tests or they are not extensive enough, a new patch opens up a simple exploit path, and so on.

In such instances, the recommended approach is to conduct an external audit prior to mainnet deployment (be it an application launch or just a new feature). Auditors examine the code as a whole with a new pair of eyes, detecting minor inconsistencies and loopholes and providing you with a more thorough evaluation of the code.

Another security approach is using risk management tools and platforms (such as Apostro) to protect the protocol against economic and technical attacks. Such platforms filter and analyse both blockchain and market data to provide an in-depth analysis on the protocol state and warn developers if anything goes wrong.

Doomsday Plan

We’d like to finish this atricle with the most practical feature of any DeFi developer shoul have up his sleeve: a disaster recovery plan. No matter how confident you are in the DeFi application’s security, you must have a backup plan in place if your application suffers an exploit.

The most coomon recommendations are to include an emergency pause in the protocol’s smart contracts and to have a well defined upgrade and development pipelines. Another option would be to collaborate with insurance protocols to provide users and protocol with financial protection.