Attacking the DeFi Ecosystem with Flash Loans for Fun and Profit

  • Conference paper
  • First Online:
Financial Cryptography and Data Security (FC 2021)

Part of the book series: Lecture Notes in Computer Science ((LNSC,volume 12674))

Included in the following conference series:

Abstract

Credit allows a lender to loan out surplus capital to a borrower. In the traditional economy, credit bears the risk that the borrower may default on its debt, the lender hence requires upfront collateral from the borrower, plus interest fee payments. Due to the atomicity of blockchain transactions, lenders can offer flash loans, i.e., loans that are only valid within one transaction and must be repaid by the end of that transaction. This concept has lead to a number of interesting attack possibilities, some of which were exploited in February 2020.

This paper is the first to explore the implication of transaction atomicity and flash loans for the nascent decentralized finance (DeFi) ecosystem. We show quantitatively how transaction atomicity increases the arbitrage revenue. We moreover analyze two existing attacks with ROIs beyond 500k%. We formulate finding the attack parameters as an optimization problem over the state of the underlying Ethereum blockchain and the state of the DeFi ecosystem. We show how malicious adversaries can efficiently maximize an attack profit and hence damage the DeFi ecosystem further. Specifically, we present how two previously executed attacks can be “boosted” to result in a profit of  829.5k USD and 1.1M USD, respectively, which is a boost of 2.37\(\times \) and 1.73\(\times \), respectively.

This is a preview of subscription content, log in via an institution to check access.

Access this chapter

Subscribe and save

Springer+ Basic
EUR 32.99 /Month
  • Get 10 units per month
  • Download Article/Chapter or Ebook
  • 1 Unit = 1 Article or 1 Chapter
  • Cancel anytime
Subscribe now

Buy Now

Chapter
EUR 29.95
Price includes VAT (France)
  • Available as PDF
  • Read on any device
  • Instant download
  • Own it forever
eBook
EUR 96.29
Price includes VAT (France)
  • Available as EPUB and PDF
  • Read on any device
  • Instant download
  • Own it forever
Softcover Book
EUR 126.59
Price includes VAT (France)
  • Compact, lightweight edition
  • Dispatched in 3 to 5 business days
  • Free ship** worldwide - see info

Tax calculation will be finalised at checkout

Purchases are for personal use only

Institutional subscriptions

Similar content being viewed by others

Notes

  1. 1.

    Besides the risk of smart contract vulnerabilities.

  2. 2.

    https://www.scipy.org/. We use the minimize function in the optimize package.

  3. 3.

    We collect in total 5, 616 flash loans with one transaction performing two flash loans.

References

  1. Aavewatch - live protocol stats! https://aavewatch.now.sh/

  2. Bti market surveillance report - september 2019 - bti. https://www.bti.live/bti-september-2019-wash-trade-report/, Accessed 24 Feb 2020

  3. bzx - a protocol for tokenized margin trading and lending. https://bzx.network/

  4. Compound. https://compound.finance/

  5. Consensys/0x-review: Security review of 0x smart contracts. https://github.com/ConsenSys/0x-review

  6. Ganache — truffle suite. https://www.trufflesuite.com/ganache

  7. Home — prevent flash loan attacks. https://preventflashloanattacks.com/

  8. marbleprotocol/flash-lending: Flash lending smart contracts. https://github.com/marbleprotocol/flash-lending

  9. Report of investigation pursuant to section 21(a) of the securities exchange act of 1934: The dao. https://www.sec.gov/litigation/investreport/34-81207.pdf

  10. Slippage definition & example. https://www.investopedia.com/terms/s/slippage.asp

  11. Uniswap. https://uniswap.org/

  12. U.s. corporate debt soars to record \$10.5 trillion - marketwatch. www.marketwatch.com/story/u-s-corporate-debt-soars-to-record-10-5-trillion-11598921886#:~:text=U.S.%20corporations%20now%20owe%20a, new%20BofA%20Global%20Research%20report

  13. Aave: Aave Protocol (2020). https://github.com/aave/aave-protocol

  14. Amani, S., Bégel, M., Bortin, M., Staples, M.: Towards verifying ethereum smart contract bytecode in isabelle/hol. In: Proceedings of the 7th ACM SIGPLAN International Conference on Certified Programs and Proofs, pp. 66–77 (2018)

    Google Scholar 

  15. Bentov, I., et al.: Tesseract: real-time cryptocurrency exchange using trusted hardware. In: Conference on Computer and Communications Security (2019)

    Google Scholar 

  16. Bhargavan, K., et al.: Formal verification of smart contracts: short paper. In: Proceedings of the 2016 ACM Workshop on Programming Languages and Analysis for Security, pp. 91–96 (2016)

    Google Scholar 

  17. Bonneau, J., Miller, A., Clark, J., Narayanan, A., Kroll, J.A., Felten, E.W.: Sok: research perspectives and challenges for bitcoin and cryptocurrencies. In: 2015 IEEE Symposium on Security and Privacy (SP), pp. 104–121. IEEE (2015)

    Google Scholar 

  18. Breidenbach, L., Daian, P., Tramèr, F., Juels, A.: Enter the hydra: towards principled bug bounties and exploit-resistant smart contracts. In: 27th \(\{\)USENIX\(\}\) Security Symposium (\(\{\)USENIX\(\}\) Security 18), pp. 1335–1352 (2018)

    Google Scholar 

  19. Brent, L., et al.: Vandal: a scalable security analysis framework for smart contracts (2018). ar**v preprint ar**v:1809.03981

  20. CoinMarketCap: Bitcoin market capitalization (2019)

    Google Scholar 

  21. Crytic: Echidna: Ethereum fuzz testing framework. https://github.com/crytic/echidna

  22. Daian, P., et al.: Flash Boys 2.0: frontrunning, transaction reordering, and consensus instability in decentralized exchanges. In: IEEE Security and Privacy 2020 (2020)

    Google Scholar 

  23. DiCiccio, T.J., Efron, B.: Bootstrap confidence intervals. In: Statistical Science, pp. 189–212 (1996)

    Google Scholar 

  24. dYdX: dYdX (2020). https://dydx.exchange/

  25. Eskandari, S., Moosavi, S., Clark, J.: SoK: transparent dishonesty: front-running attacks on blockchain. In: Bracciali, A., Clark, J., Pintore, F., Rønne, P.B., Sala, M. (eds.) FC 2019. LNCS, vol. 11599, pp. 170–189. Springer, Cham (2020). https://doi.org/10.1007/978-3-030-43725-1_13

    Chapter  Google Scholar 

  26. Gandal, N., Hamrick, J., Moore, T., Oberman, T.: Price manipulation in the Bitcoin ecosystem. J. Monet. Econ. 95(4), 86–96 (2018)

    Article  Google Scholar 

  27. Grishchenko, I., Maffei, M., Schneidewind, C.: A semantic framework for the security analysis of ethereum smart contracts. In: Bauer, L., Küsters, R. (eds.) POST 2018. LNCS, vol. 10804, pp. 243–269. Springer, Cham (2018). https://doi.org/10.1007/978-3-319-89722-6_10

    Chapter  Google Scholar 

  28. Hamrick, J., et al.: The economics of cryptocurrency pump and dump schemes (2018)

    Google Scholar 

  29. Hildenbrandt, E., et al.: Kevm: a complete semantics of the ethereum virtual machine. Technical report (2017)

    Google Scholar 

  30. Hirai, Y.: Defining the ethereum virtual machine for interactive theorem provers. In: Brenner, M., Rohloff, K., Bonneau, J., Miller, A., Ryan, P.Y.A., Teague, V., Bracciali, A., Sala, M., Pintore, F., Jakobsson, M. (eds.) FC 2017. LNCS, vol. 10323, pp. 520–535. Springer, Cham (2017). https://doi.org/10.1007/978-3-319-70278-0_33

    Chapter  Google Scholar 

  31. Jiang, B., Liu, Y., Chan, W.: Contractfuzzer: fuzzing smart contracts for vulnerability detection. In: Proceedings of the 33rd ACM/IEEE International Conference on Automated Software Engineering, pp. 259–269. ACM (2018)

    Google Scholar 

  32. Kalodner, H.A., Carlsten, M., Ellenbogen, P., Bonneau, J., Narayanan, A.: An empirical study of namecoin and lessons for decentralized namespace design. In: WEIS. Citeseer (2015)

    Google Scholar 

  33. Kamps, J., Kleinberg, B.: To the moon: defining and detecting cryptocurrency pump-and-dumps. Crime Sci. 7(1), 1–18 (2018). https://doi.org/10.1186/s40163-018-0093-5

    Article  Google Scholar 

  34. Kyber: Kyber (2020). https://kyber.network/

  35. Labs, A.: Idex: a real-time and high-throughput ethereum smart contract exchange. Technical report (2019)

    Google Scholar 

  36. Liu, C., Liu, H., Cao, Z., Chen, Z., Chen, B., Roscoe, B.: Reguard: finding reentrancy bugs in smart contracts. In: 2018 IEEE/ACM 40th International Conference on Software Engineering: Companion (ICSE-Companion), pp. 65–68. IEEE (2018)

    Google Scholar 

  37. Luu, L., Chu, D.H., Olickel, H., Saxena, P., Hobor, A.: Making smart contracts smarter. In: Proceedings of the ACM SIGSAC Conference on Computer and Communications Security, pp. 254–269 (2016). https://doi.org/10.1145/2976749.2978309, http://dl.acm.org/citation.cfm?doid=2976749.2978309

  38. Luu, L., Chu, D.H., Olickel, H., Saxena, P., Hobor, A.: Making smart contracts smarter. In: Proceedings of the 2016 ACM SIGSAC Conference on Computer and Communications Security, pp. 254–269 (2016)

    Google Scholar 

  39. Maker: Makerdao (2019). https://makerdao.com/en/

  40. MakerDao: Intro to the oasisdex protocol (2019). Accessed 12 Nov 2019, https://github.com/makerdao/developerguides/blob/master/Oasis/intro-to-oasis/intro-to-oasis-maker-otc.md

  41. Mavroudis, V.: Market manipulation as a security problem (2019). ar**v preprint ar**v:1903.12458

  42. Mavroudis, V., Melton, H.: Libra: fair order-matching for electronic financial exchanges (2019). ar**v preprint ar**v:1910.00321

  43. Mueller, B.: Mythril-reversing and bug hunting framework for the ethereum blockchain (2017)

    Google Scholar 

  44. Nakamoto, S.: Bitcoin: a peer-to-peer electronic cash system (2008)

    Google Scholar 

  45. Nikolić, I., Kolluri, A., Sergey, I., Saxena, P., Hobor, A.: Finding the greedy, prodigal, and suicidal contracts at scale. In: Proceedings of the 34th Annual Computer Security Applications Conference, pp. 653–663 (2018)

    Google Scholar 

  46. Shleifer, A., Vishny, R.W.: The limits of arbitrage. J. Finan. 52(1), 35–55 (1997)

    Article  Google Scholar 

  47. Tikhomirov, S., Voskresenskaya, E., Ivanitskiy, I., Takhaviev, R., Marchenko, E., Alexandrov, Y.: Smartcheck: static analysis of ethereum smart contracts. In: Proceedings of the 1st International Workshop on Emerging Trends in Software Engineering for Blockchain, pp. 9–16 (2018)

    Google Scholar 

  48. Tsankov, P., Dan, A., Drachsler-Cohen, D., Gervais, A., Buenzli, F., Vechev, M.: Securify: practical security analysis of smart contracts. In: Proceedings of the 2018 ACM SIGSAC Conference on Computer and Communications Security, pp. 67–82. ACM (2018)

    Google Scholar 

  49. Wood, G.: Ethereum: a secure decentralised generalised transaction ledger. Ethereum Project Yellow Paper (2014)

    Google Scholar 

  50. Wüstholz, V., Christakis, M.: Harvey: a greybox fuzzer for smart contracts (2019). ar**v:1905.06944

  51. Xu, J., Livshits, B.: The anatomy of a cryptocurrency pump-and-dump scheme. In: Proceedings of the Usenix Security Symposium (2019)

    Google Scholar 

  52. Zhou, L., Qin, K., Torres, C.F., Le, D.V., Gervais, A.: High-frequency trading on decentralized on-chain exchanges (2020). ar**v preprint ar**v:2009.14021

Download references

Acknowledgments

We thank the anonymous reviewers and Johannes Krupp for providing valuable comments and helpful feedback that significantly strengthened the paper. We are moreover grateful to the Lucerne University of Applied Sciences and Arts for generously supporting Kaihua Qin’s Ph.D.

Author information

Authors and Affiliations

Authors

Corresponding author

Correspondence to Kaihua Qin .

Editor information

Editors and Affiliations

Appendices

A Classifying Flash Loan Use Cases

In Fig. 11, we present the DeFi platforms that use a total of 5, 615 Aave flash loan transactionsFootnote 3 between the 8th of January, 2020 and the 20th of September, 2020. We find that more than 30% of the flash loans are interacting with Kyber, MakerDAO, and Uniswap. Compound and MakerDAO accumulate 433.81M USD flash loans which occupy 90% of the total flash loan amount. On average, a flash transaction uses 1.43M gas, while the most complex one consumes 6.3M gas.

Fig. 11.
figure 11

Classifying the usage of flash loans in the wild, based on an analysis of transactions between the 8th of January, 2020 and the 20th of September, 2020 on Aave [13]. Others include the platform combinations that appear less than five times and the ones of which the owner platforms are unknown to us. The total amount is calculated at the price – DAI ($1); ETH ($350); USDC ($1); BAT ($0.2); WBTC ($10, 000); ZRX ($0.3); MKR ($500); LINK ($10); USDT ($1); REP ($15), KNC ($1.5), LEND ($0.5), sUSD ($1).

B Flash Loan Use Cases

1.1 B.1 Wash Trading

The trading volume of an asset is a metric indicating its popularity. The most popular assets therefore are supposed to be traded the most—e.g., Bitcoin to date enjoys the highest trading volume (reported up to 50T USD per day) of all cryptocurrencies.

Malicious exchanges or traders can mislead other traders by artificially inflating the trading volume of an asset. In September 2019, 73 out of the top 100 exchanges on Coinmarketcap [20] were wash trading over 90% of their volumes [2]. In centralized exchanges, operators can easily and freely create fake trades in the backend, while decentralized exchanges settle trades on-chain. Wash trading on DEX thus requires wash traders to hold and use real assets. Flash loans can remove this “obstacle” and wash trading costs are then reduced to the flash loan interest, trading fees, and (blockchain) transaction fees, e.g., gas. A wash trading endeavor to increase the 24-h volume by 50% on the ETH/DAI market of Uniswap would for instance cost about 1, 298 USD (cf. Fig. 12). We visualize in Fig. 12 the required cost to create fake volumes in two Uniswap markets. At the time of writing, the transaction fee amounts to 0.01 USD, the flash loan interests range from a constant 1 Wei (on dYdX) to 0.09% (on Aave), and exchange fees are about 0.3% (on Uniswap).

Fig. 12.
figure 12

Wash trading cost on two Uniswap markets with flash loans costing \(0.09\%\) (Aave) and a constant of 1 Wei (dYdX) respectively. The 24-h volumes of ETH/DAI and ETH/WBTC market were 963, 786 USD and 67, 690 USD respectively (1st of March, 2020).

Wash Trading Example: On March 2nd, 2020, a flash loan of 0.01 ETH borrowed from dYdX performed two back-and-forth trades (first converted 0.01 ETH to 122.1898 LOOM and then converted 122.1898 LOOM back to 0.0099 ETH) on Uniswap ETH/LOOM market (cf. 0xf65b384ebe2b7bf1e7bd06adf0daac0413defeed42fd2cc72a75385a200e1544). The 24-h trading volume of the ETH/LOOM market increased by 25.8% (from 17.71 USD to 22.28 USD) as a result of the two trades.

1.2 B.2 Collateral Swap**

We classify DeFi platforms that rely on users providing cryptocurrencies [13, 24, 39] as follows: (i) a DeFi system where a new asset is minted and backed-up with user-provided collateral (e.g., MakerDAO’s DAI or SAI [39]) and (ii) a DeFi system where long-term loans are offered and assets are aggregated within liquidity pools (e.g., margin trading [3] or long term loans [13]). Once a collateral position is opened, DeFi platforms store the collateral assets in a vault until the new/borrowed asset are destroyed/returned. Because cryptocurrency prices fluctuate, this asset lock-in bears a currency risk. With flash loans, it is possible to replace the collateral asset with another asset, even if a user does not possess sufficient funds to destroy/return the new/borrowed asset. A user can close an existing collateral position with borrowed funds, and then immediately open a new collateral position using a different asset.

Fig. 13.
figure 13

Flash mint example.

Collateral Swap** Example: On February 20th, 2020, a flash loan borrowed 20.00 DAI (from Aave) to perform a collateral swap (on MakerDAO), cf. 0x5d5bbfe0b666631916adb8a56821b204d97e75e2a852945ac7396a82e207e0ca. Before this transaction, the transaction sender used 0.18 WETH as collateral for instantiating 20.00 DAI (on MakerDAO). The transaction sender first withdraws all WETH using the 20.00 DAI flash loan, then converts 0.18 WETH for 178.08 BAT (using Uniswap). Finally the user creates 20.03 DAI using BAT as collateral, and pays back 20.02 DAI (with a fee to Aave). This transaction converts the collateral from WETH to BAT and the user gained 0.01 DAI, with an estimated gas fee of 0.86 USD.

1.3 B.3 Flash Minting

Cryptocurrency assets are commonly known as either inflationary (further units of an asset can be mined) or deflationary (the total number of units of an asset are finite). Flash minting is an idea to allow an instantaneous minting of an arbitrary amount of an asset—the newly-mined units exist only during one transaction. It is yet unclear where this idea might be applicable to, the minted assets could momentarily increase liquidity.

Flash Minting Example: A flash mint function (cf. Fig. 13) can be integrated into an ERC20 token, to mint an arbitrary number of coins within a transaction only. Before the transaction terminates, the minted coins will be burned. If the available amount of coins to be burned by the end of the transaction is less than those that were minted, the transaction is reverted (i.e., not executed). An example ERC20 flash minting code could take the following form (cf. 0x09b4c8200f0cb51e6d44a1974a1bc07336b9f47f):

C DeFi Models

In the following, we detail the quantitative DeFi models applied in this work. Note that we do not include all the states involved in the DeFi attacks but only those relevant to the constrained optimization.

Flash Loan: We assume a flash loan platform \(\mathbb {F}\) with \(z_\mathsf {X}\) amount of asset \(\mathsf {X}\), which the adversary \(\mathbb {A}\) can borrow. The required interest to borrow b of \(\mathsf {X}\) is represented by \({\text {interest}}(b)\).

State: In a flash loan, the state is represented by the balance of \(\mathbb {A}\), i.e., \(\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S})\).

Transitions: We define the transition functions of in Eq. 8 and in Eq. 9, where the parameter \(b_{\mathsf {X}}\) denotes the loaned amount.

$$\begin{aligned}&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}') = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) + b_{\mathsf {X}} \nonumber \\&\qquad \,\,\, \text {s.t.} \quad z_{\mathsf {X}} - b_{\mathsf {X}} \ge 0 \end{aligned}$$
(8)
$$\begin{aligned}&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}') = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - b_{\mathsf {X}} - {\text {interest}}(b_{\mathsf {X}})\nonumber \\&\,\,\,\,\, \text {s.t.} \quad \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - b_{\mathsf {X}} - {\text {interest}}(b_{\mathsf {X}}) \ge 0 \end{aligned}$$
(9)

Fixed Price Trading: We define the endpoint that allows the adversary \(\mathbb {A}\) to trade \(q_\mathsf {X}\) amount of \(\mathsf {X}\) for \(\mathsf {Y}\) at a fixed price \(\mathsf {p}_m\). \(\mathsf {maxY}\) is the maximum amount of \(\mathsf {Y}\) available for trading.

State: We consider the following state variables:

  • Balance of asset \(\mathsf {X}\) held by \(\mathbb {A}\): \(\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S})\)

  • Balance of asset \(\mathsf {Y}\) held by \(\mathbb {A}\): \(\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S})\)

Transitions: Transition functions of are defined in Eq. 10.

$$\begin{aligned}&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}') = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - q_\mathsf {X} \nonumber \\&\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}') = \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}) + \frac{q_\mathsf {X}}{\mathsf {p}_m} \nonumber \\&\quad \, \text {s.t.}\quad \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - q_\mathsf {X} \ge 0\nonumber \\&\qquad \quad \mathsf {maxY} - \frac{q_\mathsf {X}}{\mathsf {p}_m} \ge 0 \end{aligned}$$
(10)

Constant Product Automated Market Maker: The constant product AMM is with a market share of 77% among the AMM DEX, the most common AMM model in the current DeFi ecosystem [11]. We denote by \(\mathbb {M}\) an AMM instance with trading pair \(\mathsf {X}/\mathsf {Y}\) and exchange fee rate \(\mathsf {f}\).

State: We consider the following states variables that can be modified in an AMM state transition.

  • Amount of \(\mathsf {X}\) in AMM liquidity pool: \(u_\mathsf {X}(\mathsf {S})\), which equals to \(\mathcal {B}(\mathbb {M};\mathsf {X};\mathsf {S})\)

  • Amount of \(\mathsf {Y}\) in AMM liquidity pool: \(u_\mathsf {Y}(\mathsf {S})\), which equals to \(\mathcal {B}(\mathbb {M};\mathsf {Y};\mathsf {S})\)

  • Balance of \(\mathsf {X}\) held by \(\mathbb {A}\): \(\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S})\)

  • Balance of \(\mathsf {Y}\) held by \(\mathbb {A}\): \(\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S})\)

Transitions: Among the endpoints of \(\mathbb {M}\), we focus on and , which are the relevant endpoints for the DeFi attacks discussed within this work. \(p_{\mathsf {X}}\) is a parameter that represents the amount of \(\mathsf {X}\) the adversary intends to trade. \(\mathbb {A}\) inputs \(p_\mathsf {X}\) amount of \(\mathsf {X}\) in AMM liquidity pool and receives \(o_\mathsf {Y}\) amount of \(\mathsf {Y}\) as output. The constant product rule [11] requires that Eq. 11 holds.

$$\begin{aligned} u_\mathsf {X}(\mathsf {S})\times u_\mathsf {Y}(\mathsf {S}) = \left( u_\mathsf {X}(\mathsf {S}) + (1-\mathsf {f}) p_\mathsf {X}\right) \times \left( u_\mathsf {Y}(\mathsf {S})-o_\mathsf {Y}\right) \end{aligned}$$
(11)

We define the transition functions and constraints of in Eq. 12 (analogously for ).

$$\begin{aligned}&\quad \,\, \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S'}) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - p_{\mathsf {X}} \nonumber \\&\quad \,\, \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S'}) = \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}) + o_{\mathsf {Y}} \nonumber \\&\qquad \quad \,\, u_\mathsf {X}(\mathsf {S}') = u_\mathsf {X}(\mathsf {S}) + p_{\mathsf {X}} \nonumber \\&\qquad \quad \,\, u_\mathsf {Y}(\mathsf {S}') = u_\mathsf {Y}(\mathsf {S}) - o_{\mathsf {Y}} \nonumber \\&\text {where} \quad o_{\mathsf {Y}} = \frac{p_{\mathsf {X}}\times (1-\mathsf {f})\times u_\mathsf {Y}(\mathsf {S})}{u_\mathsf {X}(\mathsf {S}) + p_{\mathsf {X}}\times (1-\mathsf {f})} \nonumber \\&\qquad \text {s.t.} \quad \mathcal {B}(\mathbb {M};\mathsf {X};\mathsf {S}) - p_{\mathsf {X}} \ge 0 \end{aligned}$$
(12)

Because an AMM DEX \(\mathbb {M}\) transparently exposes all price transitions on-chain, it can be used as a price oracle by the other DeFi platforms. The price of \(\mathsf {Y}\) with respect to \(\mathsf {X}\) given by \(\mathbb {M}\) at state \(\mathsf {S}\) is defined in Eq. 13.

$$\begin{aligned} \mathsf {p}_\mathsf {Y}(\mathbb {M};\mathsf {S}) = \frac{u_\mathsf {X}(\mathsf {S})}{u_\mathsf {Y}(\mathsf {S})} \end{aligned}$$
(13)

Automated Price Reserve: The automated price reserve is another type of AMM that automatically calculates the exchange price depending on the assets held in inventory. We denote a reserve holding the asset pair \(\mathsf {X}/\mathsf {Y}\) with \(\mathbb {R}\). A minimum price \(\mathsf {minP}\) and a maximum price \(\mathsf {maxP}\) is set when initiating \(\mathbb {R}\). \(\mathbb {R}\) relies on a liquidity ratio parameter \(\mathsf {lr}\) to calculate the asset price. We assume that \(\mathbb {R}\) holds \(k_\mathsf {X}(\mathsf {S})\) amount of \(\mathsf {X}\) at state \(\mathsf {S}\). We define the price of \(\mathsf {Y}\) in Eq. 14.

$$\begin{aligned} \mathsf {P}_\mathsf {Y}(\mathbb {R};\mathsf {S})=\mathsf {minP}\times e^{\mathsf {lr}\times k_\mathsf {X}(\mathsf {S})} \end{aligned}$$
(14)

The endpoint provided by \(\mathbb {R}\) allows the adversary \(\mathbb {A}\) to exchange \(\mathsf {X}\) for \(\mathsf {Y}\).

State: We consider the following state variables:

  • The inventory of \(\mathsf {X}\) in the reserve: \(k_\mathsf {X}(\mathsf {S})\), which equals to \(\mathcal {B}(\mathbb {R};\mathsf {X};\mathsf {S})\)

  • Balance of \(\mathsf {X}\) held by \(\mathbb {A}\): \(\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S})\)

  • Balance of \(\mathsf {Y}\) held by \(\mathbb {A}\): \(\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S})\)

Transitions: We denote as \(h_\mathsf {X}\) the amount of \(\mathsf {X}\) that \(\mathbb {A}\) inputs in the exchange to trade against \(\mathsf {Y}\). The exchange output amount of \(\mathsf {Y}\) is calculated by the following formulation.

$$\begin{aligned} j_\mathsf {Y} = \frac{e^{-\mathsf {lr}\times h_\mathsf {X}} - 1}{\mathsf {lr}\times \mathsf {P}_\mathsf {Y}(\mathbb {R};\mathsf {S})} \end{aligned}$$

We define the transition functions within Eq. 15.

$$\begin{aligned}&\qquad k_\mathsf {X}(\mathsf {S}') = k_\mathsf {X}(\mathsf {S}) + h_\mathsf {X} \nonumber \\&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}') = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - h_\mathsf {X} \nonumber \\&\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}') = \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}) + j_\mathsf {Y}\nonumber \\&\,\, \text {where} \quad j_\mathsf {Y} = \frac{e^{-\mathsf {lr}\times h_\mathsf {X}} - 1}{\mathsf {lr}\times \mathsf {P}_\mathsf {Y}(\mathbb {R};\mathsf {S})} \nonumber \\&\,\,\,\, \text {s.t.} \quad \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - h_\mathsf {X} \ge 0 \nonumber \\&\quad \,\,\, \mathsf {P}_\mathsf {Y}(\mathbb {R};\mathsf {S}') - \mathsf {minP}\ge 0 \nonumber \\&\quad \,\,\, \mathsf {maxP}-\mathsf {P}_\mathsf {Y}(\mathbb {R};\mathsf {S}')\ge 0 \end{aligned}$$
(15)

Collateralized Lending and Borrowing: We consider a collateralized lending platform \(\mathbb {L}\), which provides the endpoint that requires the user to collateralize an asset \(\mathsf {X}\) with a collateral factor \(\mathsf {cf}\) (s.t. \(0< \mathsf {cf} < 1\)) and borrows another asset \(\mathsf {Y}\) at an exchange rate \(\mathsf {er}\). The collateral factor determines the upper limit that a user can borrow. For example, if the collateral factor is 0.75, a user is allowed to borrow up to 75% of the value of the collateral. The exchange rate is for example determined by an outsourced price oracle. \(z_{\mathsf {Y}}\) denotes the maximum amount of \(\mathsf {Y}\) available for borrowing.

State: We hence consider the following state variables and ignore the balance changes of \(\mathbb {L}\) for simplicity.

  • Balance of asset \(\mathsf {X}\) held by \(\mathbb {A}\): \(\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S})\)

  • Balance of asset \(\mathsf {Y}\) held by \(\mathbb {A}\): \(\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S})\)

Transitions: The parameter \(c_{\mathsf {X}}\) represents the amount of asset \(\mathsf {X}\) that \(\mathbb {A}\) aims to collateralize. Although \(\mathbb {A}\) is allowed to borrow less than his collateral would allow for, we assume that \(\mathbb {A}\) makes use the entirety of his collateral. Equation 16 shows the transition functions of .

$$\begin{aligned}&\quad \quad \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S'}) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - c_{\mathsf {X}}\nonumber \\&\quad \quad \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S'}) = \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}) + b_{\mathsf {Y}}\nonumber \\&\qquad \quad \,\,\,\, \text {where} \quad b_{\mathsf {Y}} = \frac{c_\mathsf {X}\times \mathsf {cf}}{\mathsf {er}} \nonumber \\&\text {s.t.} \quad \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S'}) - c_{\mathsf {X}} \ge 0; z_{\mathsf {Y}} - b_{\mathsf {Y}} \ge 0 \end{aligned}$$
(16)

\(\mathbb {A}\) can retrieve its collateral by repaying the borrowed asset through the endpoint . We show the transition functions in Eq. 17 and for simplicity ignore the loan interest fee.

$$\begin{aligned}&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S'}) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) + c_{\mathsf {X}} \nonumber \\&\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S'}) = \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}) - b_{\mathsf {Y}} \nonumber \\&\,\,\,\,\, \text {s.t.} \quad \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}) - b_{\mathsf {Y}} \ge 0 \end{aligned}$$
(17)

Margin Trading: A margin trading platform \(\mathbb {T}\) allows the adversary \(\mathbb {A}\) to short/long an asset \(\mathsf {Y}\) by collateralizing asset \(\mathsf {X}\) at a leverage \(\ell \), where \(\ell \ge 1\).

We focus on the endpoint which is relevant to the discussed DeFi attack in this work. We assume \(\mathbb {A}\) shorts \(\mathsf {Y}\) with respect to \(\mathsf {X}\) on \(\mathbb {F}\). The parameter \(d_{\mathsf {X}}\) denotes the amount of \(\mathsf {X}\) that \(\mathsf {A}\) collateralizes upfront to open the margin. \(w_\mathsf {X}\) represents the amount of \(\mathsf {X}\) held by \(\mathbb {F}\) that is available for the short margin. \(\mathbb {A}\) is required to over-collateralize at a rate of \(\mathsf {ocr}\) in a margin trade. In our model, when a short margin (short \(\mathsf {Y}\) with respect to \(\mathsf {X}\)) is opened, \(\mathbb {F}\) performs a trade on external \(\mathsf {X}/\mathsf {Y}\) markets (e.g., Uniswap) to convert the leveraged \(\mathsf {X}\) to \(\mathsf {Y}\). The traded \(\mathsf {Y}\) is locked until the margin is closed or liquidated.

State: In a short margin trading, we consider the following state variables:

  • Balance of \(\mathsf {X}\) held by \(\mathbb {A}\): \(\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S})\)

  • The locked amount of \(\mathsf {Y}\): \(\mathcal {L}(\mathbb {A};\mathsf {Y};\mathsf {S})\)

Transitions: We assume \(\mathbb {F}\) transacts from an external market at a price of \(\mathsf {emp}\). The transition functions and constraints are specified in Eq. 18.

$$\begin{aligned}&\qquad \qquad \,\, \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S'}) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - c_{\mathsf {X}} \nonumber \\&\qquad \qquad \,\, \mathcal {L}(\mathbb {A};\mathsf {Y};\mathsf {S}') = \mathcal {L}(\mathbb {A};\mathsf {Y};\mathsf {S}) + l_\mathsf {Y} \nonumber \\&\qquad \qquad \quad \,\, \text {where}\quad l_{\mathsf {Y}}=\frac{d_{\mathsf {X}}\times \ell }{\mathsf {ocr}\times \mathsf {emp}} \nonumber \\&\text {s.t.}\quad \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}) - c_{\mathsf {X}} \ge 0; w_{\mathsf {X}} + d_{\mathsf {X}} - \frac{d_{\mathsf {X}}\times \ell }{\mathsf {ocr}} \ge 0 \end{aligned}$$
(18)

D Optimizing the Pump Attack and Arbitrage

In the following, we detail the procedure of deriving the pump attack and arbitrage optimization problem. Figure 5 summarizes the on-chain state when the attack was executed (i.e., \(\mathsf {S}_0\)). \(\mathsf {X}\) and \(\mathsf {Y}\) denote ETH and WBTC respectively. For simplicity, we ignore the trading fees in the constant product AMM (i.e., \(\mathsf {f} = 0\) for \(\mathbb {M}\)). The endpoints executed in the pump attack and arbitrage are listed in the execution order as follows.

  1. 1.

    (dYdX)

  2. 2.

    (Compound)

  3. 3.

    (bZx) & (Uniswap)

  4. 4.

    (Uniswap)

  5. 5.

    (dYdX)

  6. 6.

    amp; (Compound)

In the pump attack and arbitrage vector, we intend to tune the following two parameters, (i) \(p_1\): the amount of \(\mathsf {X}\) collateralized to borrow \(\mathsf {Y}\) in the endpoint 2) and (ii) \(p_2\): the amount of \(\mathsf {X}\) collateralized to short \(\mathsf {Y}\) in the endpoint 3). Following the procedure of Sect. 4.2, we proceed with detailing the construction of the constraint system.

0): We assume the initial balance of \(\mathsf {X}\) owned by \(\mathbb {A}\) is \(\mathsf {B}_0\) (cf. Eq. 19), and we refer the reader to Fig. 5 for the remaining initial state values.

$$\begin{aligned} \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_0) = \mathsf {B}_0 \end{aligned}$$
(19)

1) . \(\mathbb {A}\) gets a flash loan of \(\mathsf {X}\) amounts \(p_1+p_2\) in total

$$\begin{aligned} \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_1) = \mathsf {B}_0 + p_1+p_2 \end{aligned}$$

with the constraints

$$\begin{aligned} \begin{aligned} p_1 \ge 0, p_2 \ge 0, v_{\mathsf {X}} - p_1 - p_2 \ge 0\\ \end{aligned} \end{aligned}$$

2) : \(\mathbb {A}\) collateralizes \(p_1\) amount of \(\mathsf {X}\) to borrow \(\mathsf {Y}\) from the lending platform \(\mathbb {L}\)

$$\begin{aligned}&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_2) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_1) - p_1 = \mathsf {B}_0 + p_2\\&\qquad \qquad \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_2) = \frac{p_1\times \mathsf {cf}}{\mathsf {er}} \end{aligned}$$
$$\begin{aligned} \text{ with } \text{ the } \text{ constraint }~z_\mathsf {Y} - \frac{p_1\times \mathsf {cf}}{\mathsf {er}} \ge 0 \end{aligned}$$

3) & . \(\mathbb {A}\) opens a short margin with \(p_2\) amount of \(\mathsf {X}\) at a leverage of \(\ell \) on the margin trading platform \(\mathbb {T}\); \(\mathbb {T}\) swaps the leveraged \(\mathsf {X}\) for \(\mathsf {Y}\) at the constant product AMM \(\mathbb {M}\)

$$\begin{aligned}&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_3) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_2) - p_2 = \mathsf {B}_0 \\&\qquad u_\mathsf {X}(\mathsf {S}_3) = u_\mathsf {X}(\mathsf {S}_0) + \frac{p_2 \times \ell }{\mathsf {ocr}}\\&\quad \,\,\,\,\, u_\mathsf {Y}(\mathsf {S}_3) = \frac{u_\mathsf {X}(\mathsf {S}_0)\times u_\mathsf {Y}(\mathsf {S}_0)}{u_\mathsf {X}(\mathsf {S}_3)}\\&\quad \mathcal {L}(\mathbb {A};\mathsf {Y};\mathsf {S}_3) = u_\mathsf {Y}(\mathsf {S}_0) - u_\mathsf {Y}(\mathsf {S}_3) \end{aligned}$$
$$\begin{aligned} \text{ with } \text{ the } \text{ constraint }~w_\mathsf {X} + p_2 - \frac{p_2 \times \ell }{\mathsf {ocr}} \ge 0 \end{aligned}$$

4) . \(\mathbb {A}\) dumps all the borrowed \(\mathsf {Y}\) at \(\mathbb {M}\)

$$\begin{aligned}&\qquad \qquad \,\,\,\,\, \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_4) = 0\\&\quad \,\,\, u_\mathsf {Y}(\mathsf {S}_4) = u_\mathsf {Y}(\mathsf {S}_3) + \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_2)\\&\quad \,\,\,\,\,\,\, u_\mathsf {X}(\mathsf {S}_4) = \frac{u_\mathsf {X}(\mathsf {S}_3)\times u_\mathsf {Y}(\mathsf {S}_3)}{u_\mathsf {Y}(\mathsf {S}_4)}\\&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_4) = \mathsf {B}_0 + u_\mathsf {X}(\mathsf {S}_3) - u_\mathsf {X}(\mathsf {S}_4) \end{aligned}$$

5) . \(\mathbb {A}\) repays the flash loan

$$\begin{aligned} \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_5) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_4) - p_1 - p_2 \end{aligned}$$
$$\begin{aligned} \text{ with } \text{ the } \text{ constraint }~\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_4) - p_1 - p_2 \ge 0 \end{aligned}$$

6) & . \(\mathbb {A}\) buys Y from the market with the market price \(\mathsf {p}_m\) and retrieves the collateral from \(\mathbb {L}\)

$$\begin{aligned} \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_6) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_5) + p_1 - \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_2)\times \mathsf {p}_m \end{aligned}$$

The objective function is the adversarial ETH revenue (cf. Eq. 20).

$$\begin{aligned} \begin{aligned} \mathcal {O}(\mathsf {S}_0; p_1; p_2) =\,\,&\mathcal {B}(\mathbb {A}; \mathsf {X}; \mathsf {S}_6) - \mathsf {B}_0\\ =\,\,&{u_\mathsf {X}(\mathsf {S}_0) + \frac{p_2 \times \ell }{\mathsf {ocr}}} - {u_\mathsf {X}(\mathsf {S}_4)} - p_2 \\&- \frac{p_1\times \mathsf {cf} \times \mathsf {p}_m}{\mathsf {er}} \end{aligned} \end{aligned}$$
(20)

E Optimizing the Oracle Manipulation Attack

In the oracle manipulation attack, \(\mathsf {X}\) denotes ETH and \(\mathsf {Y}\) denotes sUSD. Again, we ignore the trading fees in the constant product AMM (i.e., \(\mathsf {f} = 0\) for \(\mathbb {M}\)). The initial state variables are presented in Fig. 7. We assume that \(\mathbb {A}\) owns zero balance of \(\mathsf {X}\) or \(\mathsf {Y}\). We list the endpoints involved in the oracle manipulation attack vector as follows.

  1. 1.

    (bZx)

  2. 2.

    (Uniswap)

  3. 3.

    (Kyber reserve)

  4. 4.

    (Synthetix)

  5. 5.

    (bZx)

  6. 6.

    (bZx)

We construct the constrained optimization problem as follows.

1) : \(\mathbb {A}\) gets a flash loan of \(\mathsf {X}\) amounts \(p_1 + p_2 + p_3\)

$$\begin{aligned} \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_1) = p_1 + p_2 + p_3 \end{aligned}$$

with the constraints

$$\begin{aligned} \begin{aligned} p_1 \ge 0, p_2 \ge 0, p_3 \ge 0, v_{\mathsf {X}} - p_1 - p_2 - p_3\ge 0 \end{aligned} \end{aligned}$$

2) : \(\mathbb {A}\) swaps \(p_1\) amount of \(\mathsf {X}\) for \(\mathsf {Y}\) from the constant product AMM \(\mathbb {M}\)

$$\begin{aligned}&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_2) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_1) - p_1 = p_2 + p_3\\&\qquad \quad \,\,\,\,\,\,\, u_\mathsf {X}(\mathsf {S}_2) = u_\mathsf {X}(\mathsf {S}_0) + p_1\\&\qquad \quad u_\mathsf {Y}(\mathsf {S}_2) = \frac{u_\mathsf {X}(\mathsf {S}_0)\times u_\mathsf {Y}(\mathsf {S}_0)}{u_\mathsf {X}(\mathsf {S}_2)}\\&\qquad \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_2) = u_\mathsf {Y}(\mathsf {S}_0) - u_\mathsf {Y}(\mathsf {S}_2) \end{aligned}$$

3) : \(\mathbb {A}\) converts \(p_2\) amount of \(\mathsf {X}\) to \(\mathsf {Y}\) from the automated price reserve \(\mathbb {R}\)

$$\begin{aligned}&\quad \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_3)=\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_2) - p_2 = p_1\\&\qquad \qquad k_\mathsf {X}(\mathsf {S}_3) = k_\mathsf {X}(\mathsf {S}_0) + p_2\\&\quad \quad \, \mathsf {P}_\mathsf {Y}(\mathbb {R};\mathsf {S}_3)=\mathsf {minP} \times e^{\mathsf {lr}\times k_\mathsf {X}(\mathsf {S}_3)}\\&\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_3)=\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_2) + \frac{e^{-\mathsf {lr}\times p_2} - 1}{\mathsf {lr}\times \mathsf {P}_\mathsf {Y}(\mathbb {R};\mathsf {S}_0)}\\&\qquad \,\,\,\,\,\,\, \text {s.t.} \quad \mathsf {maxP} - \mathsf {P}_\mathsf {Y}(\mathbb {R};\mathsf {S}_3)\ge 0 \end{aligned}$$

4) : \(\mathbb {A}\) sells \(p_3\) amount of \(\mathsf {X}\) for \(\mathsf {Y}\) at the price of \(\mathsf {p}_m\)

$$\begin{aligned}&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_4) = \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_3) - p_3 = 0\\&\,\,\, \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_4) = \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_3) + \frac{p_3}{\mathsf {p}_m} \end{aligned}$$
$$\begin{aligned} \text{ with } \text{ the } \text{ constraint }~\mathsf {maxY} - \frac{p_3}{\mathsf {p}_m} \ge 0 \end{aligned}$$

5) : \(\mathbb {A}\) collateralizes all owned \(\mathsf {Y}\) to borrow \(\mathsf {X}\) according to the price given by the constant product AMM \(\mathbb {M}\) (i.e., the exchange rate \(\mathsf {er} = \frac{1}{\mathsf {P}_\mathsf {Y}(\mathbb {M};\mathsf {S}_2)}\))

$$\begin{aligned}&\qquad \qquad \qquad \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_5) = 0\\&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_5) = \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_4)\times \mathsf {cf} \times \mathsf {P}_\mathsf {Y}(\mathbb {M};\mathsf {S}_2) \end{aligned}$$

with the constraint

$$\begin{aligned} z_\mathsf {Y} - \mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_4)\times \mathsf {cf} \times \mathsf {P}_\mathsf {Y}(\mathbb {M};\mathsf {S}_2) \ge 0 \end{aligned}$$

6) : \(\mathbb {A}\) repays the flash loan

$$\begin{aligned} \mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_6)=\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_5) - p_1 -p_2 -p_3 \end{aligned}$$
$$\begin{aligned} \text{ with } \text{ the } \text{ constraint }~\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_5) - p_1 -p_2 -p_3 \ge 0 \end{aligned}$$

The objective function is the remaining balance of \(\mathsf {X}\) after repaying the flash loan (cf. Eq. 21).

$$\begin{aligned} \begin{aligned} \mathcal {O}(\mathsf {S}_0;p_1;p_2;p_3)=\,\,&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_6)\\ =\,\,&\mathcal {B}(\mathbb {A};\mathsf {X};\mathsf {S}_5) - p_1 -p_2 -p_3\\ =\,\,&\mathcal {B}(\mathbb {A};\mathsf {Y};\mathsf {S}_4)\times \mathsf {cf} \times \mathsf {P}_\mathsf {Y}(\mathbb {M};\mathsf {S}_2) \\&- p_1 -p_2 -p_3 \end{aligned} \end{aligned}$$
(21)

F Extended Discussion

In the following, we extend our discussion in Sect. 7.

Responsible Disclosure: It is somewhat unclear how to perform responsible disclosure within DeFi, given that the underlying vulnerability and victim are not always perfectly clear and that there is a lack of security standards to apply. We plan to reach out to Aave, Kyber, and Uniswap to disclose the contents of this paper.

Does Extra Capital Help: The main attraction of flash loans stems from them not requiring collateral that needs to be raised. One can, however, wonder whether extra capital would make the attacks we focus on more potent and the ROI greater. Based on our results, extra collateral for the two attacks of Sect. 3 would not increase the ROI, as the liquidity constraints of the intermediate protocols do not allow for a higher impact.

Potential Defenses: Here we discuss several potential defenses. However, we would be the first to admit that these are not foolproof and come with potential downsides that would significantly hamper normal interactions.

  • Should DEX accept trades coming from flash loans?

  • Should DEX accept coins from an address if the previous block did not show those funds in the address?

  • Would introducing a delay make sense, e.g., in governance voting, or price oracles?

  • When designing a DeFi protocol, a single transaction should be limited in its abilities: a DEX should not allow a single transaction triggering a slippage beyond 100%.

Looking into the Future: In the future, we anticipate DeFi protocols eventually starting to comply with a higher standard of security testing, both within the protocol itself, as well as part of integration testing into the DeFi ecosystem. We believe that eventually, this may lead to some form of DeFi standards where it comes to financial security, similar to what is imposed on banks and other financial institutions in traditional centralized (government-controlled) finance. We anticipate that either whole-system penetration testing or an analytical approach to modeling the space of possibilities like in this paper are two ways to improve future DeFi protocols.

Generality of the Optimization Framework: We show in Sect. 5 that our optimization framework performs efficiently on a given attack vector. To discover new attacks on a blockchain state with the framework, we may need to iterate over all the combinations of DeFi actions. The search space thus explodes as the number of DeFi actions increases. Our optimization framework requires to model every DeFi action manually. This, however, makes the framework less handy for users who are unfamiliar with the mathematical formulas of the DeFi actions. To make the framework more accurate, we can build gas consumption and block gas limit into the models, which requires to comprehend every DeFi action explicitly. We leave the automation of modeling for future work.

Rights and permissions

Reprints and permissions

Copyright information

© 2021 International Financial Cryptography Association

About this paper

Check for updates. Verify currency and authenticity via CrossMark

Cite this paper

Qin, K., Zhou, L., Livshits, B., Gervais, A. (2021). Attacking the DeFi Ecosystem with Flash Loans for Fun and Profit. In: Borisov, N., Diaz, C. (eds) Financial Cryptography and Data Security. FC 2021. Lecture Notes in Computer Science(), vol 12674. Springer, Berlin, Heidelberg. https://doi.org/10.1007/978-3-662-64322-8_1

Download citation

  • DOI: https://doi.org/10.1007/978-3-662-64322-8_1

  • Published:

  • Publisher Name: Springer, Berlin, Heidelberg

  • Print ISBN: 978-3-662-64321-1

  • Online ISBN: 978-3-662-64322-8

  • eBook Packages: Computer ScienceComputer Science (R0)

Publish with us

Policies and ethics

Navigation