-
Notifications
You must be signed in to change notification settings - Fork 43
fix(morpho-sdk): forward-accrue VaultV1 deposit for share-price slippage #797
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@morpho-org/morpho-sdk": patch | ||
| --- | ||
|
|
||
| Fix `MorphoVaultV1.deposit` deriving `maxSharePrice` from pre-accrue vault state. MetaMorpho's `deposit` calls `_accrueInterest()`, so the execution-time share price is `>=` the build-time one; without a forward-accrual buffer the slippage bound could be too tight and revert GeneralAdapter1's check with `SlippageExceeded`. VaultV1 deposit now forward-accrues interest by 2h before computing shares, mirroring `MorphoVaultV2.deposit` and blue repay. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -223,7 +223,20 @@ export class MorphoVaultV1 implements VaultV1Actions { | |
|
|
||
| const totalAssets = amount + (nativeAmount ?? 0n); | ||
|
|
||
| const shares = vaultData.toShares(totalAssets); | ||
| // Forward-accrue 2h so maxSharePrice bounds the execution-time price, like | ||
| // VaultV2 deposit and blue repay. MetaMorpho has no vault-level lastUpdate, | ||
| // so accrue to the latest of now and every allocated market's lastUpdate | ||
| // (accrueInterest throws below any market's lastUpdate). | ||
| const accrualTimestamp = | ||
| vaultData.allocations | ||
| .values() | ||
| .reduce( | ||
| (max, { position }) => MathLib.max(max, position.market.lastUpdate), | ||
| Time.timestamp(), | ||
| ) + Time.s.from.h(2n); | ||
| const accruedVault = vaultData.accrueInterest(accrualTimestamp); | ||
|
|
||
| const shares = accruedVault.toShares(totalAssets); | ||
|
Comment on lines
+237
to
+239
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Root Useful? React with 👍 / 👎. |
||
| if (shares <= 0n) { | ||
| throw new NonPositiveSharesAmountError(this.vault); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For deposits where the exact share amount is fractional,
AccrualVault.toSharesdefaults to rounding up (packages/blue-sdk/src/vault/Vault.ts:190), while an ERC-4626depositmints the rounded-down share amount and the existing MetaMorpho operation path usesvault.toShares(assets, "Down")(packages/bundler-sdk-viem/src/actions.ts:841). This makesmaxSharePriceuse a larger denominator than the shares GA1 will actually observe, so withslippageTolerance: 0nor small/fractional deposits the transaction can still revert with the slippage check despite the forward accrual; pass"Down"here and keep the zero-share guard on that rounded-down value.Useful? React with 👍 / 👎.