Add MEV protection for liquidations and large transactions#403
Open
TUPM96 wants to merge 1 commit into
Open
Conversation
|
@TUPM96 is attempting to deploy a commit to the smartdevs17's projects Team on Vercel. A member of the Team first needs to authorize it. |
There was a problem hiding this comment.
Pull request overview
Note
Copilot was unable to run its full agentic suite in this review.
Adds MEV-protection primitives across the lending and hello-world contracts, including guarded commit/reveal with slippage/deadline checks, private mempool route receipts, gas-bid monitoring, and a liquidation batch auction flow.
Changes:
- Introduces a lightweight MEV guard module in
lending(commit/reveal + gas bid stats) and exposes it via the lending contract API. - Extends
hello-worldMEV protection with execution guards, private route registration/receipts, gas bid analytics, and liquidation batch auction lifecycle. - Adds new tests and updates error mappings + README API listing.
Reviewed changes
Copilot reviewed 7 out of 7 changed files in this pull request and generated 6 comments.
Show a summary per file
| File | Description |
|---|---|
| stellar-lend/contracts/lending/src/mev_protection.rs | New lightweight commit/reveal + gas-bid tracking for large tx MEV protection. |
| stellar-lend/contracts/lending/src/lib.rs | Exposes MEV guard entrypoints from the lending contract. |
| stellar-lend/contracts/hello-world/src/mev_protection.rs | Adds guarded commits, private routes/receipts, batch auctions, gas bid stats, and dashboard APIs. |
| stellar-lend/contracts/hello-world/src/lib.rs | Exposes new MEV APIs (guarded commits, auctions, private routes, dashboard) at contract level. |
| stellar-lend/contracts/hello-world/src/tests/mev_protection_test.rs | Adds coverage for guarded liquidation, private route receipts, auctions, and gas bid dashboard. |
| stellar-lend/contracts/hello-world/src/errors.rs | Maps newly added MEV protection errors into LendingError. |
| stellar-lend/contracts/hello-world/README.md | Documents new MEV protection-related public APIs. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| if let Some(ref execution_guard) = guard { | ||
| validate_guard_config(env, execution_guard)?; | ||
| } | ||
| if let Some(ref route_id) = private_route { |
Comment on lines
+66
to
+74
| if amount <= 0 | ||
| || quoted_output <= 0 | ||
| || min_output < 0 | ||
| || min_output > quoted_output | ||
| || !(0..=MAX_BPS).contains(&max_slippage_bps) | ||
| || deadline <= env.ledger().timestamp() | ||
| { | ||
| return Err(MevGuardError::BadConfig); | ||
| } |
Comment on lines
+1222
to
+1232
| fn validate_guard_config(env: &Env, guard: &ExecutionGuard) -> Result<(), MevProtectionError> { | ||
| if guard.quoted_output_amount <= 0 | ||
| || guard.min_output_amount < 0 | ||
| || guard.min_output_amount > guard.quoted_output_amount | ||
| || !(0..=MAX_BPS).contains(&guard.max_slippage_bps) | ||
| || guard.deadline <= env.ledger().timestamp() | ||
| { | ||
| return Err(MevProtectionError::InvalidConfig); | ||
| } | ||
| Ok(()) | ||
| } |
| ); | ||
| env.storage() | ||
| .persistent() | ||
| .remove(&MevDataKey::Commit(commit_id)); |
Comment on lines
+620
to
+642
| let existed: Option<PrivateMempoolRoute> = env | ||
| .storage() | ||
| .persistent() | ||
| .get(&MevDataKey::PrivateRoute(route_id.clone())); | ||
| let route = PrivateMempoolRoute { | ||
| route_id: route_id.clone(), | ||
| relay, | ||
| registered_by: caller, | ||
| registered_at: now, | ||
| expires_at: now.saturating_add(ttl_secs), | ||
| active: true, | ||
| }; | ||
| env.storage() | ||
| .persistent() | ||
| .set(&MevDataKey::PrivateRoute(route_id), &route); | ||
|
|
||
| if existed.is_none() { | ||
| let mut stats = get_private_route_stats(env); | ||
| stats.active_routes = stats.active_routes.saturating_add(1); | ||
| env.storage() | ||
| .persistent() | ||
| .set(&MevDataKey::PrivateStats, &stats); | ||
| } |
Comment on lines
+300
to
+303
| fn test_private_route_receipt_required_for_private_commit() { | ||
| let env = Env::default(); | ||
| env.mock_all_auths(); | ||
| let contract_id = setup_contract(&env); |
c5ffaf1 to
041858a
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Acceptance coverage
equires_mev_commit and guarded commits.
Validation
Closes #383