0

I am currently working on a project which involves swapping token (raydium / jupiter) and I just wanted to know if there is any reliable methods for anti-mev, such that I can prevent sandwich attacks, etc when performing swaps.

I have found this github project which seems to somewhat be a step in the right direction for what Im looking for but Im not sure it will help me in my current use case.

Keep in mind that I want to achieve this solely in Python or Typescript.

Any information or help will be appreciated.

1 Answer 1

1

It depends on how you define "anti-MEV". At a conceptual level, there's nothing you can do to stop someone from front-running or back-running your transactions, unless you're the leader proposing the block to the network.

Guard transactions (via Lighthouse or otherwise) that surround your transaction are a great option, but require that the node processes your transactions serially, which is only guaranteed on... MEV nodes.

You can also include guard instructions in your transaction. For example, if you're swapping tokens, and you want to make sure that you end up with at least 100 tokens, you can include a self-transfer for 100 tokens at the end of the transaction, ie:

spl_token::instruction::transfer(
    &spl_token::id(),
    &my_account_pubkey,
    &my_account_pubkey,
    &my_wallet_pubkey,
    &[],
    expected_amount
)
5
  • 1
    Hi, thank you for the response. I am having some trouble conceptualising how to add these guard instructions to my transactions. The context for my use case is that I have two types of transaction being done in my program. One is in python which uses solders/solana-py to do swaps using JUP. And the other is in typescript which is using the raydium swap sdk. I know it doesn't mean much to you but this issue is quite urgent to me and I would really appreciate some more insight how I could integrate guard instruction. Thank you very much in advance. Commented Mar 15 at 10:03
  • 1
    Transactions are made up of one or more instructions. When you do a jupiter or raydium swap, your transaction contains instructions to those programs. To add a guard to your transaction, you just need to add another instruction to check that the account has the expected amount at the end, just like what I showed. So just append another instruction to the array in your transaction, nothing else needed
    – Jon C
    Commented Mar 17 at 23:39
  • @JonC isn't this guard basically a slippage ? I mean I can't swap for x amount of tokens and expect to get 2x of that, right ?
    – C.Unbay
    Commented May 24 at 21:42
  • so the only way these malicious actors are running sandwich by being a leader in the network ? what about rpc http api and how does that work ?
    – C.Unbay
    Commented May 24 at 21:43
  • Correct, this is a way to guarantee slippage. And the easiest way to run sandwich attacks is by being or bribing the leader in some way, since the stock validator code just process transactions as they come in. If you want to do any other ordering or examine transactions during processing, you need to have another step to inject more transactions to run the sandwich attack. In most cases, you probably need to work with the leader node to get this done.
    – Jon C
    Commented May 27 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.