The root cause of the exploit was that during the swapExactTokenForToken
attacker inputed the tokenIn
and tokenOut
as the same tokens. The main purpose of this function is to deposits MONO(*token that plays crucial role during the exploit) tokens into the pool.
Let’s move forward into the swapIn function. The main thing to concentrate here, is prices. And where do the prices are calculated? Right, in the getAmountOut.
Following the getAmountOut function, we discovered that the _getNewPrice
function is used to calculate the tokenInPrice
and the tokenOutPrice
.
When tokenInPrice
is calculated, one of the param. the flag “TxType.SELL” is passed
When tokenOutPrice
is calculated, one of the param. the flag “TxType.BUY” is passed
The _getNewPrice
is calculated both for tokenIn
as well as for tokenOut
. Because the four variables in the price calculation formula are the same, the incoming and exiting tokens are the same. We can now assume that tokenOutPrice
will be greater than tokenInPrice
.
So, let’s move forward in the swapIn
function. Because tokenIn
and tokenOut
are the same, the _updateTokenInfo
function will be called 2 times.
Remember! Above we have stated that tokenOutPrice
will be higher than tokenInPrice
! What does it mean? Correct! During the second _updateTokenInfo
the price will be increased.
So, after the price is updated to the higher value, attacker call swapTokenForExactToken
, compare to the function that we start the attack from (swapExactTokenForToken
), it does the stuff vice-versa, swap MONO for other tokens in the pool.
Eventually, to get the price of swapping, the swapOut
is called. Here we could see the final results and the reason why the MONO token was “pumped” recently. It causes the amountIn to become very small, it means “we take less MONO token to receive enough value of other assets”.
The hacker exploited a vulnerability in the MonoX protocol, where the same token was used for both the tokenIn and tokenOut functions. By utilizing the same token in the swapTokenFORExactToken function, the price update for tokenOut bypassed the price update for tokenIn. This caused the price of the MONO token to rise sharply, allowing the hacker to use it to acquire the majority of assets within the protocol.
This educational article is based on the SlowMist writeUp