DeFi Basics
What Is a Smart Contract?
Learn what smart contracts are, how they work on blockchain networks, and why they are the foundation of DeFi applications like lending protocols and decentralized exchanges.
Learn what smart contract audits are, how they work, what auditors look for, and why they matter for DeFi security and protecting your crypto assets.
In traditional software, bugs might cause an app to crash or display incorrect data — annoying but usually recoverable. In DeFi, a smart contract bug can result in the permanent, irreversible loss of millions of dollars. This stark reality is why smart contract audits exist: they are the primary line of defense between a protocol's code and the funds it controls.
A smart contract audit is a rigorous, independent security review of a protocol's codebase. Professional auditors systematically examine every function, state transition, and external interaction to identify vulnerabilities before they can be exploited. For anyone using DeFi protocols — especially for high-value operations like Bitcoin-backed borrowing — understanding what audits do and don't guarantee is essential.
Once a smart contract is deployed to a blockchain, its code is (in most cases) immutable. Unlike a web application where you can push a hotfix in minutes, a deployed smart contract cannot be patched. If a vulnerability exists, it exists permanently — unless the protocol has built-in upgrade mechanisms (which themselves introduce governance risks).
The numbers speak for themselves. DeFi exploits have resulted in billions of dollars in cumulative losses. Major incidents include:
Every one of these exploits targeted code that had been reviewed to some degree. The question is never whether to audit, but how thoroughly.
The audit begins with the auditing firm understanding what the code is supposed to do. They review:
This phase establishes the baseline against which the code will be evaluated.
Auditors run the codebase through automated tools that catch common vulnerability patterns:
Automated tools catch the "low-hanging fruit" but cannot understand business logic or complex multi-step attack scenarios.
This is the most critical and time-intensive phase. Experienced auditors read through every line of code, asking questions like:
Manual review is where the most critical, protocol-specific vulnerabilities are discovered.
Auditors compile their findings into a detailed report, classifying each issue by severity:
| Severity | Description | Example |
|---|---|---|
| Critical | Direct loss of funds possible | Unauthorized withdrawal function |
| High | Significant financial impact likely | Price oracle manipulation |
| Medium | Limited financial impact or requires specific conditions | Rounding errors favoring one party |
| Low | Minor issues, best practices | Gas optimization, code clarity |
| Informational | Suggestions, no security impact | Documentation improvements |
After the development team addresses the findings, auditors verify the fixes. This is essential — poorly implemented fixes can introduce new vulnerabilities. The final report includes the original findings, the team's responses, and the auditors' assessment of each fix.
Reentrancy — When a contract calls an external contract before updating its own state, the external contract can "re-enter" the original function and exploit the stale state. The DAO hack was a reentrancy attack.
Access control flaws — Functions that should be restricted (e.g., to the protocol admin) but are callable by anyone. A missing onlyOwner modifier on a withdrawal function is a textbook critical vulnerability.
Oracle manipulation — DeFi protocols rely on price oracles to determine collateral values. If an attacker can manipulate the oracle price (e.g., through a flash loan), they can borrow more than their collateral is worth or trigger unfair liquidations.
Flash loan attacks — Composability allows attackers to borrow enormous sums, manipulate state, extract value, and repay — all in a single transaction. Auditors evaluate whether any function is vulnerable to these atomic operations.
Precision and rounding errors — Financial math on the blockchain uses integer arithmetic. Rounding errors that seem insignificant can compound across millions of transactions or be deliberately exploited.
Front-running and MEV — Transactions in a public mempool can be observed and front-run. Auditors check whether any operations are vulnerable to sandwich attacks or other MEV extraction.
For lending protocols — the kind aggregated by Borrow — auditors pay special attention to:
Formal verification uses mathematical proofs to verify that a smart contract behaves exactly as specified under all possible inputs. While audits rely on human judgment, formal verification provides mathematical certainty about specific properties. It is computationally expensive and limited in scope, but for critical functions (like a token's transfer logic), it provides the strongest possible guarantee.
Platforms like Code4rena and Sherlock run competitive audits where dozens or hundreds of independent auditors ("wardens") review the code simultaneously. This crowdsourced approach can surface vulnerabilities that a single team might miss, and the competitive structure incentivizes thorough review.
A bug bounty program offers financial rewards to anyone who discovers and responsibly discloses vulnerabilities in production code. Platforms like Immunefi host bounty programs for major DeFi protocols, with payouts reaching millions of dollars for critical findings. Bug bounties complement audits by providing ongoing security coverage after deployment.
Real-time monitoring tools (like Forta, OpenZeppelin Defender, and Hypernative) watch on-chain activity for suspicious patterns — large flash loans, unusual governance proposals, or anomalous price movements. These systems can trigger automated responses (like pausing a protocol) faster than humans can react.
When considering whether to deposit collateral or borrow from a DeFi protocol, assess its security posture:
When you use your Bitcoin as collateral on a lending protocol, you are trusting that protocol's smart contracts with your assets. The audit history of that protocol is one of the most important factors in assessing whether that trust is warranted.
Aggregators like Borrow list protocols that meet baseline security standards, including audit history and operational track record. By comparing across multiple audited protocols, you can choose not only the best rates but also the security profile that matches your risk tolerance.
It is critical to understand what audits cannot do:
Common Questions
A smart contract audit is a systematic review of a protocol's code by independent security experts. Auditors examine the source code for vulnerabilities, logic errors, gas inefficiencies, and deviations from the specification. The goal is to identify and fix security issues before the code is deployed to production, where bugs could lead to loss of user funds.
Related Questions
DeFi Basics
Learn what smart contracts are, how they work on blockchain networks, and why they are the foundation of DeFi applications like lending protocols and decentralized exchanges.
DeFi Basics
Learn about the key risks of decentralized finance (DeFi), including smart contract vulnerabilities, protocol risk, systemic risk, and how to protect your assets.
Borrow by Sats Terminal
Understand the safety measures, risk management, and security architecture behind Borrow by Sats Terminal. Learn how self-custody, audited protocols, and transparent operations protect your Bitcoin.
Advanced Topics
Learn what rollups are, how optimistic and zero-knowledge rollups work, and why they matter for scaling Ethereum and reducing gas fees in DeFi.