Jul 274 mins read

The Flaws in the MasterChef Contract and Its "Enhanced Version"

Potential Vulnerabilities in Liquidity Pool Smart Contracts

The Flaws in the MasterChef Contract and Its "Enhanced Version"

By Dr. Wu Chiachih

In the summer of 2020, the MasterChef smart contract released by SushiSwap founder Chef Nomi may have been one of the contracts most frequently redeployed during the early DeFi craze. Many DeFi projects would modify the MasterChef contract to implement their liquidity mining function. However, over the past year, several projects made mistakes while modifying Chef Nomi's "recipe" and caused irreparable losses. On October 6, 2021, we also discovered a flawed project, but fortunately, the project successfully fixed the vulnerability before causing any losses and completed the upgrade.

Dinosaur Eggs

The Liquidity Pool smart contract of the Dinosaur Eggs project, the protagonist of this incident, is also an "enhanced version" of the MasterChef contract, with the main modification being the addition of the "addtionalRate" function. This function is designed to provide additional rewards to specific NFT holders when they deposit LP tokens.

Dinosaur-Eggs Image source:


The MasterChef contract has become the foundation of many DeFi projects and its "enhanced versions" are widely used. However, we should be aware of the potential vulnerabilities and mistakes that can be made during the modification process. It is therefore essential to conduct thorough security audits and testing before deploying any smart contract on the blockchain.

Blockchain-security Image source: Expert Dr. Wu Chiachih, Blockchain Security Expert at Amber Group_

Vulnerability in Token Reward System

The maximum reward that can be obtained is 10%, conditional on the destruction of specific NFT assets before depositing LP tokens. The code snippet on lines 327-328 shows that (_amount*user.additionalRate) is added to user.additionalAmount, which is then included in reward calculations.

code snippet

The vulnerability arises in the emergencyWithdraw() function inherited from MasterChef, where user.additionalAmount is not reset, allowing users to claim rewards without having any LP tokens deposited.

emergency withdraw function

By exploiting the harvesting and emergency withdrawal functions, an attacker can repeatedly withdraw all reward tokens without having any tokens deposited.

attacker hacking into system

0x02: Exploitation

eth-brownie screenshot showing thousands of reward tokens received with only 30 LP tokens

The attack contract code above confirms the idea mentioned earlier. In the prepare() function, we intentionally created an NFT (line 36) and activated the additionalRate mechanism mentioned earlier through the additionalNft() function on line 40. Then, we deposited some LP tokens into the LiquidityPool. To gain additional rewards, in the trigger() function, we used a loop to call the emergencyWithdraw() and harvest() functions multiple times (lines 48-51).

The eth-brownie screenshot above shows that we were able to receive thousands of reward tokens (DSG) with only 30 LP tokens (DsgLP). In the same situation, if we only used harvest() without deliberately using emergencyWithdraw(), we would have received less than one DSG reward. Suppose an attacker generates a large amount of LP tokens using flash loans; they could further expand profits.

0x04: Follow-up developments

After we reported this vulnerability to the DSG team, they quickly confirmed the problem and began the repair work. After the new version of the LiquidityPool contract was deployed online, the DSG team notified users to withdraw assets from the old version and migrate to the new version while also suspending liquidity mining for the old version. Fortunately, before the migration was completed, no actual attack occurred, and the DSG team rewarded us with DSG tokens worth $10k under their bug bounty program. This reward was later donated to the Open Culture Foundation to support the development of open-source software.

Open Culture Foundation logo

Dr. Wu Chiachih - Blockchain Security Expert at Amber Group

Dr. Wu Chiachih

Dr. Wu Chiachih is a blockchain security expert at Amber Group, where he leads the group's blockchain security research and on-chain risk management platform development. Dr. Wu has extensive experience in the field and has worked as a senior security researcher at Intel and Chinese internet firm Qihoo 360. He has also co-founded the well-known blockchain security company Peckshield.

About Amber Group

Amber Group logo

Amber Group is a leading global provider of crypto financial services with business presence in Hong Kong, Taipei, Seoul, and Vancouver. Established in 2017, Amber Group serves over 1,000 institutional clients with accumulated trading over $1 trillion, and manages assets over $4 billion across 100+ electronic exchanges.

As a comprehensive crypto financial service provider, the company helps clients acquire liquidity, generate returns, and manage risks of various crypto assets with the goal of optimizing long-term value by providing investment flexibility and maximizing returns.

Learn more about Amber Group