DeFi:解决 EIP-4626 中的滑点问题

去中心化金融社区Published on 2022-11-24Last updated on 2022-11-24

Abstract

EIP-4626提供了一种将代币投资到投资池(通常称为金库)的标准方法。

简介

EIP-4626提供了一种将代币投资到投资池(通常称为金库)的标准方法。当我们存入自己的资产(ERC-20 代币)时,我们会收到一个份额代币,代表我们在金库里的资产。金库将把汇集的资产投资到一个或多个基础平台,为持有者产生收益。

EIP-4626标准的一个结果是,存款和铸币函数没有提供指定回报的最小份额或资产金额的方法。这通常用于防止高滑点或三明治攻击。mStable 如何通过其 Meta Vaults 解决这个问题——在保持符合标准的同时减轻高滑点攻击?本文描述了这些挑战,并解释了他们的方法是如何工作的。

EIP-4626和mStable金库存款

mStable EIP-4626的首个金库将投资于基于Curve 3Pool的Convex池。从EIP-4626的角度来看,金库的资产是Curve 3Pool的流动性提供者代币(3Crv)。存款函数是EIP-4626规范的一部分,它指定要存入多少资产以及将接收金库份额的帐户。存款函数返回给接收方会铸造多少金库份额。

例如,存入3Crv Convex mUSD金库将从调用方转移3Crv,并将vcx3CRV-mUSD金库份额转移到接收方。

EIP-4626标准的强大之处在于,在投资池中有一种通用的投资方法,但对资产可以投资到底层平台的内容和时间没有限制。对于mStable的3Crv Convx mUSD 金库来说, 3Crv被添加到Curve mUSD Metapool中,然后产生的流动性提供者代币(musd3Crv)被存入Convex mUSD池中,该池会投资于Curve mUSD gauge并获得更高的回报。

这个过程中的一个技术挑战是如何防止三明治攻击。

什么是三明治攻击?如何预防它们?

当我们向Curve Metapool(或任何其他池)添加流动性时,我们指定自己想存入的资产数量和流动性提供者(LP)代币的最小数量。对于mUSD Metapool,金额是一个包含两项的数组。第一个是mUSD的量,第二个是3Crv的量。3Crv Convex金库只存3Crv,因此金额数组的第一项将为零。

开发金库时的一个技术挑战是我们如何设置预期流动性提供者代币的最小数量。

仅仅将min_mint_amount设置为零是不够的,因为它会让存款交易受到三明治攻击。但在我们深入了解三明治攻击是如何工作之前,我们需要更多地了解Curve Metapool定价是如何工作的。由于金库只添加两个池代币(mUSD和3Crv)中的一个,因此它接收到的Metapool流动性提供者(LP)代币的数量将取决于Metapool中mUSD和3Crv的余额。池中的 3Crv 越多,当仅将 3Crv 添加到 Metapool 时,返回的 LP 代币就越少。

例如,如果Curve的mUSD Metapool添加了 200 万个 mUSD,600 万个 3Crv 和 100k 个 3Crv,则将收到 100,068 个 LP 代币 (musd3Crv)。如果 Metapool 有 600 万个 mUSD,添加了 200 万个 3Crv 和 100k 个 3Crv,将收到 100,892 个 LP 代币 (musd3Crv)。

那么三明治攻击是如何实现的呢?

攻击者在将交易包含到区块之前,就会监控Mempool中可能被利用的交易。为了利用交易,他们贿赂区块生产者,将他们的交易包含在可利用的交易之前和之后。也就是说,他们将易受攻击的交易与自己的交易夹在一起。如果有一笔交易将 3Crv 添加到最低 LP 金额为零的 mUSD Metapool,则攻击者的第一笔交易将是减少 Metapool 中的 mUSD 数量。这意味着在易受攻击的添加流动性交易中收到的 Metapool LP 代币数量远低于应有的数量。在第三个交易中,攻击者返还在第一个交易中删除的mUSD,并将收益装入囊中。

例子

使用Curve的mUSD Metapool,池中有6,000,000 mUSD和3Crv, 11,917,295个LP代币(musd3Crv)和1.018095美元的虚拟价格。

攻击者通过使用 6,500,000 (54.5%) 池流动性提供者 (musd3Crv) 代币从池中提取 5,973,425 的mUSD,使用他们池中的大部分流动性提供者代币 (musd3Crv) 来平衡池。使用remove_liquidity_one_coin函数进行单边提款,池中剩下 0.43% mUSD 和 99.56% 3Crv。虚拟价格上涨了近1%,至1.019105,因为大量不平衡的提现为池收取了费用。

受害者使用add_liquidity函数将100,000个3Crv添加到不平衡的池中,且没有最小流动性提供者数量。如果池是平衡的,受害者得到81978个LP代币而不是100371个。这意味着受害者得到的LP代币比他们应该得到的少18,393个(18%)。以美元计算,受害者得到的美元价值减少了18,643(18%)。

对于第三个也是最后一个交易,攻击者使用add_liquidity将他们从第一个交易中提取的5,973,425个mUSD添加回池中,以接收6,503,610个LP代币(musd3Crv)。比第一次交易多取了3610美元。池的虚拟价格将增加1%至1.019216,因为这是另一个不平衡的交易。以美元计算,攻击者的LP价值从6,500,000 * 1.018095 = 6,617,617美元上升到6,503,610 * 1.019216 = 6,628,583美元,增加了10,966美元(1.65%)。

如果受害者损失了18643美元价值,而攻击者只获得了10966美元价值,那么缺失的7677美元价值在哪里?

使池失衡的0.04%费用由流动性提供者和Curve投票托管的CRV (veCRV)持有者平均分摊。攻击者未持有的 5,417,295 LP 代币的价值从 5,515,323 美元增加到 5,520,794 美元。这比池费用的 50% 增加了 5,471 美元。增加的美元价值归于托管 CRV (veCRV) 持有人。

Curve的保护

为了防止三明治攻击,在向Curve Metapool添加流动性时,需要指定一个合理的最小LP代币数量。通常,DeFi 协议会在交易中传入相当数量的金额。Curve池中的add_liquidity函数就是min_mint_amount的一个很好的例子。但是对于标准的EIP-4626存款函数,没有定义参数来指定最小金额,因此我们无法传入相当数量的链下计算的Metapool LP代币。

Curve池有一个calc_token_amount函数,它可以计算池代币存款收到的 LP 代币数量。但这不能用来防止三明治攻击。如果已经运行了一个交易来平衡池,那么calc_token_amount函数将只返回当前不公平的LP代币数量。

因此问题仍然存在,EIP-4626函数没有办法传递最小量。打破标准来添加这一点是不可取的,使用预言机也是次优的。我们需要链上方法。

mStable的方法

mStable的金库获得一个公平的Metapool LP代币价格的方法是使用Curve Metapool和Curve 3Pool的虚拟价格。get_virtual_price函数以美元为单位返回池的流动性提供者代币的价格。它通过计算池的不变式来实现这一点,该不变式是池中代币的美元价值除以代币的总供应量。由于池中代币的余额不影响池的不变值或总美元价值,虚拟价格不会受到三明治攻击。

对于存入mStable金库的存款,我们需要在Curve的3Pool LP代币(3Crv)中对Metapool LP代币进行定价,因为这是我们在金库中使用的资产。为此,我们得到3Pool虚拟价格,并将其除以Metapool LP代币价格。

一旦我们有了一个合理的价格,我们就可以通过目前配置为 1% 的滑点系数来降低它。这个调整后的公平价格用于计算在向池中添加3Crv流动性时可以接收的Curve Metapool LP代币(musd3Crv)的最小数量。

存款的全部流程如下:

结论

虽然标准在标准化和获得采用方面起着巨大的作用,但像这样的问题提醒我们,在DeFi方面没有轻松的胜利。我们需要认识到现有标准的局限性,并为它们寻找最佳的解决方案。

Trending Cryptos

Related Reads

27-Year Reign Ends: SK Hynix Market Cap Surpasses Samsung for First Time, an AI-Driven Reshuffle of Korean Chip Power

On June 22, 2026, SK Hynix made history by surpassing Samsung Electronics in market capitalization, ending Samsung's 27-year reign as South Korea's most valuable company. This dramatic reversal is powered by the AI boom and SK Hynix's dominant position in High Bandwidth Memory (HBM), a critical component for AI model training. Once a heavily indebted firm on the brink of bankruptcy, SK Hynix bet early on HBM, which has evolved from a niche product to essential AI infrastructure. It now commands a 59% share of the global HBM market. Its financial performance is staggering, with Q1 2026 net profit soaring nearly fourfold year-over-year to KRW 40.35 trillion, translating to over 2 billion RMB in daily net profit. HBM now drives roughly 40% of its revenue with exceptionally high margins. In contrast, Samsung, with its broad portfolio spanning memory chips, smartphones, and foundry services, has lagged in the HBM race while facing headwinds in other divisions. This shift signifies a deeper restructuring of South Korea's economy, moving from consumer electronics to AI-driven growth. However, the future remains competitive. With major capacity expansions planned industry-wide by 2028 and Samsung aiming to catch up in HBM technology, the new market leader cannot afford complacency. This event marks a pivotal moment in the global semiconductor industry's ongoing power realignment.

marsbit13m ago

27-Year Reign Ends: SK Hynix Market Cap Surpasses Samsung for First Time, an AI-Driven Reshuffle of Korean Chip Power

marsbit13m ago

Why Does 'AGI Godfather' Ben Goertzel Believe the Future of AI Relies on Blockchain?

Ben Goertzel, known as the "AGI Godfather," argues that the future of Artificial General Intelligence (AGI) must be built on blockchain to prevent its control by a few corporations or venture capital firms. He believes the core AGI code should be free and open-source, but that this alone is insufficient without a decentralized infrastructure to run it affordably. His blockchain project, SingularityNET, and the broader Artificial Superintelligence Alliance aim to create a user-owned, decentralized network for hosting and deploying AGI, contrasting with the closed models of companies like OpenAI and Anthropic. Goertzel criticizes the shift of other labs from open to closed development. He argues that while a closed path is simpler, an open, decentralized model—akin to Linux and the internet—is both possible and ultimately better for humanity. He envisions an "Agent economy" where individuals orchestrate teams of AI agents to perform tasks, including transactions, on an open network rather than corporate clouds. While his current model relies on cryptocurrency, plans include offering paid AI services to businesses with the decentralized blockchain as the backend. Goertzel predicts human-level AGI could arrive by 2029 and warns that a gap in understanding and access to AGI could drastically worsen inequality. The first test of his decentralized approach will be the upcoming release of the Agent Omega Claw.

Foresight News44m ago

Why Does 'AGI Godfather' Ben Goertzel Believe the Future of AI Relies on Blockchain?

Foresight News44m ago

Trading

Spot
Futures

Hot Articles

How to Buy CRV

Welcome to HTX.com! We've made purchasing Curve DAO Token (CRV) simple and convenient. Follow our step-by-step guide to embark on your crypto journey.Step 1: Create Your HTX AccountUse your email or phone number to sign up for a free account on HTX. Experience a hassle-free registration journey and unlock all features.Get My AccountStep 2: Go to Buy Crypto and Choose Your Payment MethodCredit/Debit Card: Use your Visa or Mastercard to buy Curve DAO Token (CRV) instantly.Balance: Use funds from your HTX account balance to trade seamlessly.Third Parties: We've added popular payment methods such as Google Pay and Apple Pay to enhance convenience.P2P: Trade directly with other users on HTX.Over-the-Counter (OTC): We offer tailor-made services and competitive exchange rates for traders.Step 3: Store Your Curve DAO Token (CRV)After purchasing your Curve DAO Token (CRV), store it in your HTX account. Alternatively, you can send it elsewhere via blockchain transfer or use it to trade other cryptocurrencies.Step 4: Trade Curve DAO Token (CRV)Easily trade Curve DAO Token (CRV) on HTX's spot market. Simply access your account, select your trading pair, execute your trades, and monitor in real-time. We offer a user-friendly experience for both beginners and seasoned traders.

5.1k Total ViewsPublished 2024.03.29Updated 2026.06.02

How to Buy CRV

Discussions

Welcome to the HTX Community. Here, you can stay informed about the latest platform developments and gain access to professional market insights. Users' opinions on the price of CRV (CRV) are presented below.

活动图片