-
Notifications
You must be signed in to change notification settings - Fork 425
[v10] MEV Shield Protection support
#3163
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
Open
basfroman
wants to merge
62
commits into
SDKv10
Choose a base branch
from
feat/roman/MevShield
base: SDKv10
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
+6,332
−898
Conversation
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
Open
MEV Shield Protection support
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.
Using MEV Shield Protection in the SDK
The SDK now provides comprehensive support for MEV Shield protection, allowing you to encrypt transactions and protect them from front-running and MEV attacks. There are two primary ways to use MEV protection:
Changes
Core MEV Shield Implementation
submit_encrypted_extrinsic(sync & async): Core extrinsic function that encrypts and submits transactions through the MEV Shield palletmev_submit_encryptedtoSubtensorandAsyncSubtensor: High-level wrapper method for submitting encrypted transactionsget_mev_shield_current_key: Retrieves the current encryption keyget_mev_shield_next_key: Retrieves the next encryption keyget_mev_shield_submission: Retrieves a specific encrypted submissionget_mev_shield_submissions: Retrieves all encrypted submissions for an accountExtrinsic Updates
All extrinsic functions now support MEV protection via the
mev_protectionparameter:add_stake_extrinsic,add_stake_multiple_extrinsic,set_auto_stake_extrinsicunstake_extrinsic,unstake_all_extrinsic,unstake_multiple_extrinsicmove_stake_extrinsic,transfer_stake_extrinsic,swap_stake_extrinsicburned_register_extrinsic,register_extrinsic,register_subnet_extrinsic,set_subnet_identity_extrinsicroot_register_extrinsic,set_root_claim_type_extrinsic,claim_root_extrinsicserve_extrinsic,serve_axon_extrinsic,publish_metadata_extrinsicset_weights_extrinsic,commit_weights_extrinsic,reveal_weights_extrinsic,commit_timelocked_weights_extrinsicset_take_extrinsicset_children_extrinsic,root_set_pending_childkey_cooldown_extrinsicadd_liquidity_extrinsic,modify_liquidity_extrinsic,remove_liquidity_extrinsic,toggle_user_liquidity_extrinsictransfer_extrinsicstart_call_extrinsicAPI Updates
SubtensorandAsyncSubtensormethods: All methods that call the above extrinsics now accepts:mev_protection: bool = DEFAULT_MEV_PROTECTIONas a keyword-only argumentwait_for_revealed_execution: bool = Trueas a keyword-only argumentExtrinsicResponse: Addedmev_extrinsic_receiptfield to store the receipt of the revealed (decrypted and executed) MEV Shield extrinsicBT_MEV_PROTECTIONlocal environment variable. Used forDEFAULT_MEV_PROTECTIONconstants, if passed. @MichaelTrestman please reflect this local env variable in related document.Usage
1. Direct MEV Shield Submission
You can use the
mev_submit_encryptedmethod on theSubtensorinstance or call thesubmit_encrypted_extrinsicfunction directly. This approach gives you full control over the encryption and submission process.Key Parameters
signer_keypair(optional): The keypair used to sign the inner call. This parameter is only available when callingsubmit_encrypted_extrinsicdirectly. If not provided, the wallet's coldkey is used as the default signer. When using other extrinsics withmev_protection=True, the wallet's coldkey is automatically used for signing the inner call.wait_for_revealed_execution(default:True): Whether to wait for theDecryptedExecutedevent, indicating that validators have successfully decrypted and executed the inner call. IfTrue, the function polls subsequent blocks for the event matching this submission's commitment.blocks_for_revealed_execution(default:5): Maximum number of blocks to poll for theDecryptedExecutedevent after inclusion. The function checks blocks fromstart_block + 1tostart_block + blocks_for_revealed_execution. It returns immediately if the event is found before the block limit is reached.mev_extrinsic_receipt: Whenwait_for_revealed_execution=True, theExtrinsicResponseobject will contain amev_extrinsic_receiptattribute. This contains the execution details of the revealed (decrypted and executed) extrinsic, including triggered events such asDecryptedExecutedorDecryptedRejected, block information, and other execution metadata.Important: If
wait_for_inclusion=False, you cannot setwait_for_revealed_execution=True. This will raise aValueErrorbecause we need to know the block where the encrypted transaction was included to search for the revealed execution event.Example: Staking with Direct MEV Shield Submission
2. MEV Protection via Extrinsic Parameter
All extrinsics now support a
mev_protectionparameter (default:False). When set toTrue, the extrinsic is automatically encrypted and submitted through the MEV Shield pallet, providing the same protection as direct submission.When
mev_protection=True:ExtrinsicResponsewill containmev_extrinsic_receiptwith the revealed execution details (ifwait_for_revealed_execution=True, which is the default for extrinsics using MEV protection)Example: Staking with MEV Protection Parameter
Example: Subnet Registration with MeV Res
3. Keyword-Only Arguments
All extrinsics and related
Subtensormethods now use keyword-only arguments (indicated by*,in the function signature). This means that certain parameters must be passed by name rather than position, improving code clarity and preventing accidental argument misplacement.The
*symbol in Python function signatures creates a boundary: all parameters after*must be passed as keyword arguments. This helps prevent bugs from positional argument confusion, especially when functions have many parameters.Example: Keyword-Only Arguments
Related links:
mlkem768encryption support bittensor-drand#63