Intermediate DeFi · 🕑 9 min read

Composability and Smart Contract Interactions: Building DeFi Legos

Learn how smart contracts interact across DeFi protocols, why composability creates both powerful opportunities and hidden risks, and how to evaluate the safety of multi-protocol strategies.

Introduction: The Promise and Peril of DeFi Composability

One of DeFi's most compelling features is composability — the ability of different smart contracts and protocols to interact seamlessly with each other. This characteristic has earned DeFi the nickname "money legos," because developers can stack protocols on top of each other to build new financial products without permission.

A trader might deposit collateral into a lending protocol, use those deposits as collateral for a loan, swap the borrowed tokens on a decentralized exchange, provide liquidity to an AMM, and stake the LP tokens in a yield farm — all in a single transaction. This flexibility is revolutionary and terrifying in equal measure.

For intermediate crypto investors, understanding composability is critical. It explains why certain yield opportunities exist, how protocols create network effects, and why one vulnerability in a single protocol can cascade across the entire DeFi ecosystem. This lesson will show you how composability works, why it matters for your portfolio, and how to navigate its risks.

How Smart Contracts Compose: The Technical Foundation

Smart contract composability happens because blockchains are transparent, deterministic, and programmatically accessible. When you deploy a contract on Ethereum or any EVM-compatible chain, any other smart contract can call its functions and read its state. This creates a web of interactions that makes DeFi more powerful than traditional finance.

Consider a concrete example: a protocol called Compound (a lending protocol) allows users to deposit tokens and earn interest. Its smart contract maintains a public record of user balances and allows anyone to query it. A developer can build a wrapper contract that deposits into Compound, monitors the interest being earned, and automatically reinvests profits. Then another developer can build a contract that bundles multiple yield-generating strategies and lets users pick from them. Each layer builds on what came before.

The magic happens through what's called external function calls. When your transaction interacts with multiple protocols, the blockchain executes them atomically — meaning either all steps succeed, or the entire transaction reverts. This eliminates a major risk: you can't get halfway through a complex operation and be left in a broken state.

However, this transparency cuts both ways. Because contract code is public and execution is transparent, attackers can see exactly how contracts interact and engineer attacks that exploit these relationships.

Composability Creates Yield Opportunities (And Hidden Risks)

The most visible benefit of composability is yield stacking — using multiple protocols simultaneously to earn returns on the same capital. This is how yield farmers achieve the stratospheric APYs you see advertised.

Here's a simplified example of a yield stack:

  • Deposit 1 ETH into Aave (lending protocol) → earn 2% APY on your deposit
  • Aave gives you aETH (a receipt token representing your deposit)
  • Deposit aETH into Curve (an AMM for stablecoins and similar assets) as liquidity → earn 5% in trading fees
  • Stake your Curve LP tokens in a yield farm → earn 20% in governance tokens
  • Total yield: potentially 27% annually (before fees and risks)

This is why composability creates real economic value. The yield farm couldn't exist without Aave and Curve existing first. Developers built on top of existing infrastructure instead of rebuilding it from scratch.

But here's the critical risk: each protocol in the stack introduces its own failure point. If any protocol is hacked, experiences a bug, or has an oracle failure, it can cascade through the entire stack. Your deposit in Aave could become inaccessible if the aETH contract gets exploited. Your LP tokens in Curve lose value if its AMM mechanism breaks. Your yield farm rewards become worthless if the governance token crashes due to a failed protocol.

Moreover, composability introduces liquidation cascades. Imagine you deposit collateral into Aave to borrow DAI, then use that DAI as collateral in another lending protocol to borrow more tokens. If the price of your original collateral drops, Aave liquidates your position. This triggers a sale that moves the market, which then triggers liquidations in the second protocol, which triggers more sales. One protocol's liquidation becomes another's market crash.

Evaluating Composability Risk: A Framework

To make smart decisions about composable strategies, you need to assess the risks at each layer. Here's a practical framework:

1. Protocol Risk Assessment

For each protocol in your stack, ask:

  • Has it been audited? Look for reputable audit firms (OpenZeppelin, Trail of Bits, Consensys Diligence). Audits aren't guarantees, but they reduce risk significantly. Unaudited protocols are lottery tickets, not investments.
  • How long has it been live? Older protocols have been battle-tested by hackers, market stress, and edge cases. A protocol that's been running for 2+ years with substantial TVL (total value locked) has survived attacks that killed younger competitors.
  • What's its TVL and concentration? A protocol with $10 billion locked is attacked constantly; if it survives, it's likely more secure than a $50 million protocol that hasn't attracted attention yet. But also check: if one protocol controls 80% of a strategy's returns, one hack damages you catastrophically.
  • Who are the core developers? Anonymous teams introduce governance risk. Identifiable founders and teams with track records are more accountable.

2. Interaction Complexity Risk

Count how many protocols your strategy touches. A 2-protocol strategy is orders of magnitude safer than a 5-protocol one. Each additional protocol multiplies risk:

  • 2 protocols: Risk = Risk(A) + Risk(B) + [small interaction risk]
  • 5 protocols: Risk compounds, and you now need to understand not just each protocol, but how they fail together

3. Oracle and Price Feed Risk

Many composable strategies depend on price oracles. If protocols use different oracles or the oracle is manipulated, liquidations can trigger unexpectedly. Protocols using Chainlink are generally safer than those using a single exchange as an oracle source.

4. Liquidity Risk

The more complex your strategy, the harder it is to exit quickly. If you've staked LP tokens in a farm with a 7-day unstaking period, deposited the LP tokens into a lending protocol, and borrowed against them, exiting takes days. During a market crash, you might face liquidation before you can exit.

5. Economic Model Risk

Many yield farms pay returns in new governance tokens. These tokens have no intrinsic value—they're only worth what markets will pay. A 50% APY farm paying in newly minted tokens might sound amazing, but if those tokens are being minted infinitely to pay yields, the token will dilute to zero. Check token emission schedules and whether farm incentives are unsustainable.

Building a Composability Risk Budget

Rather than avoiding composability entirely, sophisticated investors build a risk budget. Decide upfront: what percentage of your portfolio are you willing to expose to multi-protocol strategies?

A reasonable framework might be:

  • 0-10% of portfolio: High-risk, experimental strategies. New protocols, novel interactions, governance token yields.
  • 10-30% of portfolio: Moderate risk. Proven 2-3 protocol combinations. Audited contracts with 1+ year track record.
  • 30%+ of portfolio: Lower-risk strategies. Single protocols or tightly tested combinations. Established projects with substantial TVL and long track records.

This prevents a single failure from destroying your overall position while allowing you to capture the genuine returns that composability enables.

Real-World Example: The Terra Collapse

In May 2022, Terra (LUNA) collapsed, erasing $40 billion in value. Much of this damage occurred because of composability.

UST, Terra's algorithmic stablecoin, depended on a complex interaction between the Luna Foundation Guard, the Luna token, the Anchor Protocol (which offered 20% APY on UST deposits), and multiple other protocols. Users stacked these protocols to earn yield—depositing UST in Anchor, using Anchor's aUST receipt tokens as collateral in other lending protocols, and borrowing more to repeat the process.

When UST's peg broke, all these stacked positions liquidated simultaneously. The liquidations crashed the price further, triggering more liquidations. People who thought they were earning 20% APY on a "stablecoin" lost 99% of their investment. The composability that made the yield possible also amplified the collapse.

The lesson: high yields backed by unstable tokens, earned through composable protocols, are fundamentally fragile.

Key Takeaways

  • Composability is DeFi's superpower and its Achilles heel. It enables innovative yields but concentrates risk across multiple protocols. One failure can cascade.
  • Evaluate each protocol in your stack independently. Audit status, TVL, age, and core team matter more as you add protocols to your strategy.
  • Complexity has a cost. Simple 1-2 protocol strategies are safer than elaborate 5-protocol combinations. The extra yield rarely compensates for the extra risk.
  • Atomic execution is a feature, not a guarantee. Yes, your transaction reverts if it fails. But liquidations and market crashes can still cascade across protocols even though no single protocol fails.
  • Build a risk budget, not a yield budget. Decide how much portfolio risk you're comfortable with, then allocate capital accordingly—not the other way around.
  • Watch for unsustainable token incentives. If a farm's high APY comes from minting new governance tokens infinitely, it's temporary. Calculate when emissions end and token prices will decline.
← Back to all lessons
Scroll to Top