Automated DeFi Exchange Using Puppeteer and MetaMask | by Amit Siva Levi | Feb, 2022

Automate cryptocurrency trade utilizing Puppeteer

Photograph by Luke White on Unsplash

As chances are you’ll know, the DeFi platforms enable individuals to trade their cryptocurrencies quick and simply with out the necessity to create an account and full an ID verification course of.

Lately, I’ve been learning the choice of constructing income by abusing the costs hole between exchanges like Binance and Coinbase to DeFi exchanges. I’m planning to disclose the analysis in my subsequent publications.

Anyway, this bot might be helpful in case you are operating a enterprise, accepting cryptocurrencies as a cost technique, and wish to trade it for an additional coin on a scheduled job.

I usually write bots in puppeteer, which appears to be a more sensible choice for me, however in fact, you would use Playwright as properly, as they’re each simply wrappers of the CDP protocol.

The CDP protocol permits us to achieve the benefit of a completely managed browser, so that is an apparent alternative.

Combine it up with the MetaMask extension, and you’ll trade cash in a completely automated means.

To make our life a lot simpler, we are going to use a bundle referred to as dappeteer, an energetic fork maintained by chainsafe and revealed on GitHub.

Let’s begin by making a brand new listing, initializing the venture, and putting in dependencies.

Now open up index.js utilizing Visible Studio Code or a textual content editor.

Import the packages we put in earlier

Create a sleep operate so we will delay our actions when the web site shouldn’t be prepared but (generally it’s wanted even in the event you use the waitFor features):

Inside the primary operate, let’s launch a Puppeteer browser and add the Metamask extension

Don’t neglect to vary “already blah start blah since blah maintain blah skirt blah any blah” to your pockets seed.

Since this information will automate the trade on, let’s add the Avalanche community to Metamask:

Go to and seek for a coin:

On this case, we used the search operate within the web site to seek for BNB and clicked on the factor containing the BNB deal with of their CSS class.

Now let’s select to commerce BNB for USDC:

Since this time, the search operate solely returns one coin, we will click on on the one factor that has “token-item-” of their CSS with out mentioning the precise deal with.

Select the quantity to trade (1 BNB):

And for the ultimate transfer, join Metamask to Traderjoexyz and approve the transaction:

And That’s it!

Full Code:

With a small piece of code, you may simply automate every little thing, particularly in relation to cryptocurrencies.

You may take this code for example of how a primary buying and selling bot can trade two cash with out fee limits, sign-in required, and with out KYC compliance. Think about how highly effective it might be in the event you change that bot to commerce a foreign money based mostly on a deep studying mannequin or just discover a hole between the costs on 2 Defi exchanges so you would simply purchase right here and promote there.

As I wrote earlier, Sooner or later, I’m going to launch complete analysis about making a margin from Defi exchanges and, in fact, an automatic working bot for the proof of idea.

More Posts