Rescuing an NFT

Lauri Peltonen
Coinmonks
4 min readNov 7, 2021

--

Story of a rescue mission. Photo by Egor Myznik on Unsplash

As some of you know, I do Ethereum development/consulting as a freelancer on my free time. I’ve been around long enough (4 years soon) that I’ve seen lots of weird s*it in the cryptocurrency space. But hey, that’s also one of the reasons I love it!

So, a user had seen my rather vague answer on Ethereum StackExchange about how to use MEV (Miner/Maximum Extracted Value), and approached me privately about it. He had a very similar problem.

The problem

The user (well, from now on: the client) had received an NFT as payment for some work. Not being very crypto-savvy, this client had accepted the NFT in a form of a wallet’s private key: the NFT was in a wallet, and he was given the private key to the wallet.

Now, this approach has one main issue: whoever gave that private key to the client also knows the private key. Rule number uno in the crypto space: NEVER SHARE YOUR PRIVATE KEY WITH ANYONE. Unfortunately, this approach also had another issue: the private key was known to some pirate bots.

Dangerous pirate stealing our Eth. Photo by Markus Spiske on Unsplash

So if the private key was known to bots, why didn’t they simply empty the wallet? That’s a very valid question, and I don’t have the answer to that — my best guess is that the bots simply weren’t smart enough to recognize that the wallet had a valuable NFT in it. Also, I have no idea why or how the bots got the private key.

What the bots were smart enough for was that whenever the wallet received any Ethers, they stole it. Therefore it was impossible to transfer the NFT out by regular means — whenever the client tried to send Eth for the gas fees, the bots stole it.

Flashbots to the rescue

So if we can’t send in Ethers to rescue the NFT, what can we do? If only we could bundle two transactions together in one atomic bundle: supplying Ethers to the wallet and transferring the NFT out. Unfortunately the basic Ethereum transaction system doesn’t support such functionality.

MEV to the rescue!

MEV allows one to privately bribe the blockchain’s miners to bundle multiple transactions together (among other things), in a single atomic bundle: either all of its transaction succeed, or none. The bundle’s transactions are also promised to be included in a block next to each other, so no bot can steal the Eth between the transactions.

However, utilizing MEV is not very straightforward. Luckily, there’s an awesome project called Flashbots which has tools that help you utilize MEV.

Flashbots are awesome

First tries of utilizing Flashbots

The Flashbots project has inspired a community member to create a website for recovering assets in a user-friendly manner. Unfortunately the website has two issues:

  1. It doesn’t support NFTs (ERC-721)
  2. It uses an outdated transaction reward model (pre-EIP-1559)

My first try was to start modifying the website to do what we needed, but it turned out to be a bit bigger project than I initially anticipated. After an hour or two of fast coding, I gave up on that approach.

At that point, the client had found a good blog post about a very similar issue. I had read the post a few weeks earlier, but had forgotten all about it. The post has a link to the source code used, and that looked much easier to edit. Also, the client had a tech-savvy friend, with whom they could use the script at their end, so no fancy website was needed. I forked the repository and got to work, again.

We’re saved, hurray

After some small trial-and-errors, the script worked. Hurray!

Celebratory cookies for everyone. Photo by Vyshnavi Bisani on Unsplash

Feel free to use the repository for your own purposes, as you wish. I’ve tried to add comments and make it a bit more understandable.

I hope you found this post helpful. If you have any questions, feel free to contact via Telegram at http://t .me/Lauri_P

Join Coinmonks Telegram Channel and Youtube Channel learn about crypto trading and investing

Also Read

--

--