Foundations · Exchanges, DEX & DeFi

Smart Contract Risk

Smart contract risk is the chance that the code holding funds in a blockchain application fails, is exploited or is changed, causing a loss that usually cannot be reversed.

  • Intermediate
  • 5 min read
  • Updated

Why it matters

In DeFi, a smart contract does the job that a bank, a broker or an exchange does elsewhere: it holds the funds. When that code fails, there is usually no company to complain to, no insurer and no way of reversing the transaction.

Every page on this shelf that mentions a protocol, a pool or a receipt token is describing something exposed to this risk.

How it works

Where smart contract risk comes from
Bugs in the codeA mistake in logic or arithmetic that lets funds be taken or frozen.
Privileged keysAdministrators who can upgrade the code, pause it or move funds.
OraclesOutside price data that can be wrong or manipulated.
DependenciesOther contracts, tokens and bridges the protocol relies on.
GovernanceToken votes that can change the rules.
A protocol is only as sound as the weakest of these.

A smart contract runs exactly as written, which is not always as intended. Smart contract risk is the chance of loss from that gap, or from anyone with the power to change the code.

Bugs. Contract code is public, holds valuable assets and cannot easily be patched. An attacker who finds a flaw can use it at once. Well-known classes include reentrancy, where a contract is called again before it has updated its own records, and plain errors in arithmetic or access checks.

Privileged keys. Many contracts are upgradeable: a holder of an administrator key can replace the code. The same keys may be able to pause withdrawals or move funds. This allows bugs to be fixed. It also means the protocol is as trustworthy as the key holders and the security of their keys.

Dependencies. Protocols build on each other. A lending market depends on its price feeds, on the tokens it accepts and often on a bridge. A failure in any of them arrives as a loss here.

In plain words

Think of a vending machine holding everyone’s money, with its wiring diagram posted on the wall. Most people use it as intended. Someone who studies the diagram and finds a fault can empty it, and there is no shopkeeper to stop them or to refund anyone.

Oracles

How a manipulated price becomes a loss
  1. Thin price sourceA protocol reads a token's price from one small pool.
  2. Price is pushedA large trade moves the pool far from the wider market.
  3. Protocol believes itCollateral is valued at the distorted price.
  4. Funds leaveMore is borrowed than the collateral is really worth.
The contract did exactly what it was written to do. The flaw was in what it was told.

A blockchain cannot see prices by itself. An oracle is a service that publishes outside data, such as prices, on-chain. Lending protocols use oracles to value collateral and decide liquidations.

If the oracle can be moved, the protocol can be fooled. The classic weakness is a price read from a single small liquidity pool, which one large trade can shift. The capital for such a trade can sometimes be borrowed and repaid within a single transaction. Sturdier designs average prices over time and combine several sources, which raises the cost of manipulation without removing it.

Audits

What an audit covers

AN AUDIT DOES

Outside reviewers read one version of the code.

  • Find many known kinds of bug
  • Record findings at a point in time
  • Show that outside review was sought

AN AUDIT DOES NOT

It is an opinion, not a warranty.

  • Prove that no bugs remain
  • Cover code changed afterwards
  • Cover keys, oracles or economic design unless in scope
  • Compensate anyone after a loss
Audited protocols have been exploited. An audit lowers the risk and leaves some of it in place.

An audit is a review of the code by an outside security firm. It reduces risk and is a reasonable thing to look for. It is also limited to a scope, a version and a date, and its findings may or may not have been fixed.

Going deeper

Fixed code or changeable code. Code that cannot be changed cannot be repaired when a bug is found. Code that can be changed depends on whoever holds the keys. Protections such as time delays on upgrades and keys shared among several signers narrow the risk on either side.

Time is evidence, not proof. A contract that has held large sums for years without incident has survived a lot of scrutiny. Flaws have still been found in long-running code.

Approvals outlast use. A token approval given to a contract remains after the user’s funds are withdrawn. If that contract is later exploited or upgraded maliciously, approved tokens in the wallet can be at risk.

Malice by design. Some contracts are written to take funds. That is fraud, not a bug, and is covered in rug pulls and honeypots.

Rates do not offset it. A rate quoted by a protocol is variable, is not guaranteed and is often paid in a token whose price can fall. It carries the risks of the mechanism behind it, including the loss of the whole deposit.

A hypothetical oracle manipulationHypothetical illustration

Made-up numbers, ignoring fees. A lending protocol values token T by reading one pool that holds 1,000 T and 100,000 of a stablecoin S. The pool price is 100,000 / 1,000 = 100 S per T, and k = 100,000,000.

Someone swaps 100,000 S into the pool:

  • new S balance: 200,000;
  • required T balance: 100,000,000 / 200,000 = 500;
  • new pool price: 200,000 / 500 = 400 S per T.

The wider market still prices T at 100. The protocol now values 100 T of collateral at 100 x 400 = 40,000 instead of 10,000. With a maximum loan-to-value of 75%, it allows a loan of 30,000 against collateral really worth 10,000.

When the pool price returns to 100, the protocol holds a loan of 30,000 backed by 10,000. The 20,000 gap is bad debt, borne by the people who supplied the borrowed tokens.

What it does NOT mean

  • Audited does not mean safe. An audit lowers the chance of known kinds of error. It does not remove risk.
  • Open source does not mean reviewed. Public code is available to read, which is not the same as having been read carefully.
  • Decentralized does not mean nobody can change it. Many protocols have keys or votes that can alter the code.
  • A long history does not rule out a flaw. It makes one less likely, not impossible.
  • A large protocol is not insured. Compensation after an exploit is rare, partial and at the discretion of whoever funds it.

Common misunderstandings

  1. Treating an audit badge as a guarantee. What matters is what was reviewed, when, by whom, and whether the findings were fixed.
  2. Looking at one contract only. The risk includes every oracle, token and protocol it depends on.
  3. Ignoring who holds the keys. An upgradeable contract can become a different contract.
  4. Assuming the blockchain will undo a theft. Confirmed transactions are final in almost all cases.
  5. Forgetting old approvals. Risk can persist in a wallet long after a protocol was last used.

Risks and limits

  • Losses from exploits can be large for the affected funds, and fast.
  • Users generally cannot assess code themselves and rely on the judgment of others.
  • New, complex or heavily layered protocols have had less scrutiny.
  • Legal recourse is uncertain, and those responsible are often unidentified.
  • This risk exists alongside counterparty risk, not in place of it.

Key takeaway

Smart contract risk is the risk that the code holding the funds, or something it depends on, fails or is changed. Audits, time and careful design reduce it. Nothing removes it, and losses are usually permanent.

Sources and further reading

  • Mastering Ethereum, Andreas M. Antonopoulos and Gavin Wood (2018). Includes a chapter on smart contract security and common vulnerabilities
  • Smart contract security (opens in a new tab), ethereum.org. Immutability makes stolen assets hard to recover; audits do not catch every bug; upgrade patterns and admin keys add risk
  • Oracles (opens in a new tab), ethereum.org. Smart contracts cannot access off-chain data by default; a corrupt oracle means bad data in the contract