No Sandwich, Please! - Popular DeFi Attack Strategy Analysis by@anton-dzyatkovskii

No Sandwich, Please! - Popular DeFi Attack Strategy Analysis

Fraud within the DeFi space continues to grow, leaving more users deceived. CipherTrace study says losses from criminal attacks have fallen by 57% in 2020 to $1.9 billion. Front-running attacks are a type of front-running technique that’s popular in DeFi. Due to its decentralized nature, DeFi has zero regulations from national governments, exposing ignorant traders to predatory practices and behaviors such as front-runner, pump & dump, and wash trading. The transparency of the blockchain, together with the latency in executing orders, makes front running easy.
image
Anton Dzyatkovskii Hacker Noon profile picture

Anton Dzyatkovskii

CEO and co-founder of Platinum Software Development Company. Blockchain enthusiast, blogger.

A recent CipherTrace study paints a sad picture: even though crypto users’ losses from criminal attacks have dramatically fallen by 57% in 2020 to $1.9 billion (compared to $4.5 billion in 2019), fraud within the DeFi space continues to grow, leaving more users deceived.

“Thefts from hacks against centralized exchanges continue to decrease as these financial institutions mature and adopt stronger security measures. Regulation and enforcement are reducing centralized fraud schemes, which are pushing criminals to exploit decentralized finance services. DeFi platforms enjoy many exemptions from traditional regulatory enforcement regimes that centralized exchanges, money service businesses and banks face... This makes them ideal venues for moving and laundering money,”

- Dave Jevans, CipherTrace’s Chief Executive Officer.

DEX users should be aware of such vicious practices as decentralized exchanges do not give up asset custody to a third party. Due to its decentralized nature, DeFi has zero regulations from national governments, exposing ignorant traders to predatory practices and behaviors such as front-running, pump & dump, and wash trading.

What is Front-running?

image

Sandwich attacks are a type of front-running technique that’s popular in DeFi. Let’s take a closer look at this strategy together with the authors of High Frequency Trading on decentralized cn-chain exchanges.

As mentioned above, sandwiching is a variation of front-running. To make it a sandwich, the predatory trader finds a pending victim transaction in the blockchain P2P network and then tries to surround the transaction by placing one order just before the transaction (front-running) and one order just after it (back-running). The strategy is based on the idea of manipulating the price of an asset as a result of buying/selling the asset. 

The transparency of blockchain, together with the latency in executing orders, makes front-running easy, and it dramatically influences the security of traders’ assets.

All blockchain transactions are openly observed in the mempool. As soon as the predatory trader notices a potential victim’s pending transaction of asset X traded for asset Y, they front-run the victim by buying asset Y. What happens is: the predator knows that the victim trade will increase the price of the asset; the attacker plans to buy the Y asset for a lower price, letting the victim buy at a higher price and then selling the asset again at a higher price afterward.

image

Sandwich attack dissected

The idea seems simple and is easily achieved. And even though there is data that suggests professional predators make around $4,000 daily on sandwiching, this technique is not always what it seems to be. Let’s have a closer look and analyze what makes a sandwich attack.

Automated Market Maker (AMM)

This is a predefined pricing algorithm that automatically performs price discovery and market-making based on the assets in the liquidity pools. The AMM allows liquidity providers to watch and follow the market, then set the bid and ask prices. Liquidity takers, in their turn, trade against the AMM.

Price Slippage 

This is the change in the price of an asset during a trading transaction. Price slippage is predicted and expected based on the volume of the asset traded and the liquidity available. The more assets being traded, the higher the expected slippage will be. The expected slippage is counted before the trade.

Unexpected price slippage is the increase or decrease in price that occurs during the transaction process for some unknown or unpredicted reason. 

Expected Execution Price

The expected price is counted based on the AMM algorithm and the X/Y state. It’s the price the liquidity taker is counting on when starting the trade.

Execution Price

The time needed to execute the trade may dramatically change the expected execution price and the state of the AMM market X/Y.

Unexpected Price Slippage

The difference between the execution price and the expected execution price.

Unexpected Slippage Rate

The unexpected slippage over the expected price.

For example, a liquidity taker wants to trade 1X for 20Y, with a price of 0.05 Y units. The transaction takes some time, and when it is finally executed, the price may have already changed - now it is 0.1 Y unit. At this price, the liquidity taker can only afford 10Y for 1X. The unexpected slippage is 0.05 (0.1 − 0.05). On the opposite side, if the execution price decreases to 0.25 Y units, the liquidity taker can now buy 40 Y for 1 X. The unexpected slippage here is negative −0.15 (-0.25 - 0.1).

Now let’s look at two scenarios:

1. Liquidity Taker Attacks Taker: In this event, the liquidity taker tries to attack the liquidity taker who has a pending AMM DEX transaction on the blockchain P2P network. Seeing the transaction waiting to be approved, the predator emits two subsequent transactions (front-running and back-running) to benefit from the trader’s transaction. Now there are three pending transactions connected by one liquidity pool and asset pair. The miner has to choose which transaction will be approved first. This is where the predator can influence this decision by practically bribing the miner - paying a higher or lower transaction fee amount.

image

Credit: https://arxiv.org/abs/2009.14021

2. Liquidity Provider Attacks Taker: In this event, the liquidity provider tries to attack a liquidity taker. Everything begins the same: the attacker sees a pending transaction on the P2P network and then performs three transactions:

Remove Liquidity: front-running (increases victim’s slippage by reducing the market liquidity of the asset) 

Add Liquidity: back-running (restores pool liquidity to the before-the-attack amount) 

Transact Y for X: back-running (restores asset balance of X to the before-the-attack state) 

In this attack, the adversary withdraws all their assets from the liquidity pool before the victim’s transaction executes. In doing so, the predator foregoes the commission fees for the victim’s transaction. Liquidity providers receive commission fees in proportion to the liquidity they provide to the AMM DEX market.

image

Credit: https://arxiv.org/abs/2009.14021

Is it worth the effort?

All seems logical and simple. But the ultimate question is: are sandwich attacks always profitable? 

Trading as many tokens as you can afford would be the most logical way to earn the most profit unless the DEX does not require you to pay a fee. For example, Uniswap takes a 0.3% fee for every transaction and the attacker has to commit at least two transactions. Also, let’s not forget about the Gas necessary to pay for each transaction, especially if you are front-running and have to pay more. All this leads us to the following conclusion: beyond the point where the fees and commissions are higher than the victim’s trade amount, the predator does not make any profit.

The idea of the sandwich attack is not new. The concept and possible effects on all the market members have been discussed since the idea of decentralized finance came up. Automated Market Maker (AMM) seems to be the best decision for decentralized trading, yet leaving a lot of loopholes for malicious attacks. As DeFi attracts more and more people, the protection of inexperienced traders should become the number one priority for blockchain professionals. Yet, the very concept of decentralization implies that there can never be an authorized third party guaranteeing safety, security, and reparations to the casualties.  

The creator of Ethereum, Vitalik Buterin, talked about a possible solution back in 2018:

“If the market maker seems to earn profits from the implied spread from the difference between the virtual quantities, these profits could be allocated after the fact to users who seem to have bought at unfair prices. For example, if the price over some period goes from P1 to P2, but at times in between either exceeds P2 or goes below P1, then anyone who bought at that price would be able to send another transaction after the fact to claim some additional funds, to the extent that the market maker has funds available.”

So far, DeFi is extremely vulnerable to price manipulations and various dubious strategies, both within one block and the entire industry. 

So, am I Safe, Ever?

No, you are not.

Besides the fact that the decentralized idea of blockchain means that there is no one to protect you and guarantee your safety, except the system solution itself, traders should understand that DeFi today is in its embryonic stage with lots of imperfections and flaws.

So why do people take the risk and still use DEXs for trading? For the same reason, they all come to crypto and blockchain - easy large profits. Huge potential costs, lack of security, and ever-changing rules are all downsides users have to accept, if they want to be part of this up-and-coming industry and make a fortune.  

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.