Flash Claiming NFTX Vaults
May 30th, 2022

This long tail MEV method of flash claiming NFT pools to redeem airdrops is nothing new and has been seen before with crypto punks during the meebits airdrop, and most recently with the Bored Apes for the $APE and Otherside drops.

I was able to bootstrap a bot to execute the same flash claim method for the Doodles airdrop, this article serves as a breakdown to explain how the code and execution work.

Prerequisites

Doodles is an NFT project created by some members of the Crypto Kitties team. During the airdrop period, each Doodle is eligible to claim 1 new NFT called a Dooplicator. My bot flash claimed 10 doodles from NFTX and used them to claim 10 Dooplicators at the start of the airdrop period.

What is NFTX?

NFTX is a protocol that provides quick liquidity to NFTs by allowing users to deposit their NFTs into designated vaults in return for an ERC20 token. This ERC20 vault token represents ownership over the NFTs and can be used anytime to redeem an NFT from the vault (1 vault token = 1 NFT). This provides quick liquidity because after depositing, users can swap the token on any AMM such as SushiSwap.

Bootstrapping the project

The project was bootstrapped using Foundry a day before the airdrop. Due to the time constraints, I was not able to optimize and fine-tune the contract to my liking but it got the job done in the end.

The execution flow can be summarized as the following :

  • Monitor mempool for when airdrop is live
  • Initiate a zero fee flash mint from NFTX to mint vDoodle tokens
  • Use the vDoodle tokens to redeem 10 doodles from the NFTX vault
  • Use the 10 doodles to claim the Dooplicator airdrop
  • Return all 10 doodles back to the NFTX vault to regain some vDoodle tokens
  • Use SushiSwap to buyback all lost vDoodle tokens due to redeeming fees
  • Return the full amount of minted vDoodle tokens back to NFTX
  • Calculate the ids of the Dooplicators claimed off-chain and then withdraw them from the custom contract

In order for the Flash Claim to be successful, all steps outlined above (besides the last one) need to happen all in one function call.

This project was inspired by Anish’s FlashSide contracts

Setting Up The vDoodle Flash Mint

To be able to claim doodles from the vault, our custom smart contract needs to accumulate a large sum of vDoodle tokens. Instead of taking a FlashLoan from Aave (0.09% fee) or performing a FlashSwap (AMM swap fees), I used NFTX’s Flash Mint module which is a function directly forked from MakerDao’s Dai. I was able to mint the full supply of vDoodle without needing to pay any fees.

NFTX expects the full amount of vDoodle minted/borrowed to be returned at the end of the transaction so that it can be burned. Otherwise, the execution is reverted.

Snippet from my contract that starts the flash mint
Snippet from my contract that starts the flash mint

The Flash Mint module is called through the flashLoan method and requires the receiving contract to adhere to the ERC3156 standard.

Redeeming And Claiming

NFTX has two ways to redeem doodles from the vault. The first is a through a targetted redeem and the second is a random redeem.

  • Targetted Redeem allows you to choose exactly which NFT ids you want to redeem for a fee of 0.03 vDoodle per redeem (1.03 vDoodle per doodle)

  • Random Redeem randomly redeems a doodle for you from the vault. This option has a fee of 0.02 vDoodle per redeem (1.02 vDoodle per doodle)

    Note: 1 vDoodle is equivilant to the floor price of 1 doodles NFT

I went with the Random Redeem approach as it is slightly cheaper and I was banking on being one of the first to claim the pool meaning all doodles redeemed could be used to claim a Dooplicator.

Snippet from my contract where I redeem doodles from the vault and claim the airdrop
Snippet from my contract where I redeem doodles from the vault and claim the airdrop

Front Running Protection

There is a risk of random redeeming as, without any safety measures, my contract could have redeemed 10 doodles that are not eligible for the Dooplicator airdrop if someone managed to front-run my transaction. If this happened, I would still have to pay the fee for redeeming the doodles and would have no Dooplicators to offset this cost.

This issue was solved, by adding the following require statement.

Code snippet of contract code containing front running protection
Code snippet of contract code containing front running protection

My contract will now revert if it randomly redeemed less than 8 airdrop eligible doodles (out of 10). My transaction was also submitted through FlashBots which meant that I don’t have to pay for any contract reverts and if it did revert, the transaction would be submitted again in the next block.

I was lucky to have included this check as I had to hotfix my bot due to a parameter miscalculation, this delay allowed someone to flash claim before me. However, I was still able to claim 8 Dooplicators in the end.

Returning The Loan

The final part of the execution is to transfer the doodles back to the vault to get back vDoodle tokens. The contract also had to use SushiSwap to rebuy the lost vDoodle amt due to the redeeming fees. This ensures that the full amount is returned at the end of the Flash Mint callback.

Snippet from code to return doodles to the vault and calculate buyback amount
Snippet from code to return doodles to the vault and calculate buyback amount

Full onchain breakdown of all tokens transferred during the execution

Transaction overview
Transaction overview

Withdrawing Dooplicators

With the mint amount fully returned, the execution has now been completed, and the contract now holds 8 dooplicators. All that is left to be done is to withdraw the dooplicators to an EOA.

I calculated the claimed Dooplicators tokenId’s off-chain as the Dooplicator contract implement ERC721A which makes calculating tokens that you own very gas expensive on-chain. I then sent an array of the claimed Dooplicator ids to a custom withdraw function to move which then moved all dooplicators off the contract.

Snippet of my contract's withdrawal function
Snippet of my contract's withdrawal function
Image of the 8 dooplicators claimed
Image of the 8 dooplicators claimed

Conclusion

There are countless ways that I can think of to optimize this project but given the time constraints, I am happy with how it turned out. In total, I paid ~7.21Ξ to claim 10 doodles from NFTX (+gas fees), out of the 10 only 8 were eligible for the Dooplicator airdrop. After the airdrop, the floor price of the dooplicators ranged around 3.5Ξ leaving me with a profit of ~21Ξ on paper. Not bad for a quickly bootstrapped project but could have definitely optimized to extract more value from the pool.

If you found this post interesting, you can Link with me on Twitter @MouseLessDev. And also check out my website at mouse-less.dev for my other projects.

Subscribe to 0xMouseLess
Receive the latest updates directly to your inbox.
Mint this entry as an NFT to add it to your collection.
Verification
This entry has been permanently stored onchain and signed by its creator.
More from 0xMouseLess

Skeleton

Skeleton

Skeleton