Running Perp-maker for Profits & Fun: Part 2
Tags: web3, typescript, tutorial, aws
Published: 2022-06-08 13:46:15

This is the second article in our two-part series on how to run perp-maker, a market-making template for [Perpetual Protocol v2](https://perp.com/?utm_source=perp_devto&utm_medium=blog_post).

In [part one](https://dev.to/perpprotocol/running-perp-maker-for-profits-and-fun-part-1-5597), we covered the basics of Perpetual Protocol, the risks and rewards of running the bot, and how to run it locally. It's recommended to go through [part one](https://dev.to/perpprotocol/running-perp-maker-for-profits-and-fun-part-1-5597) before reading part two.

In the following, we'll guide you through how to run the bot on AWS Lambda with npm, preparation to go mainnet, as well as further modification suggestions. If you're an experienced engineer, you should have no problem setting up the bot via the readme on [our repo](https://github.com/perpetual-protocol/perp-maker).

Let's get started!

# Run on AWS Lambda with npm

If you've gone through the steps in part one, you should be able to run perp-maker locally. Now, let's try to deploy it to AWS Lambda.

> If you're not familiar with it, the main selling point of AWS Lambda is that, instead of renting a dedicated server on AWS to run your program 24/7. You only pay the times when your application runs on a shared server, which is more suitable for our use case as the bot only runs from time to time based on your settings. You can watch [this video to learn more about AWS Lambda (or serverless in general) in 100 seconds](https://www.youtube.com/watch?v=W_VV2Fx32_Y&ab_channel=Fireship).

As the first step, you need to register an account on AWS and install AWS CLI on your machine. Check the installation instructions for each OS [here](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html).

Once AWS CLI is installed, you need to [configure your AWS profile](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-quickstart.html) by running the following command in your Terminal (macOS) or Command Prompt (Windows):

`aws configure`

Then you need to type your access key and secret. You can create them from the [Identity and Access Management page](https://us-east-1.console.aws.amazon.com/iamv2/home?region=us-east-1).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1kiiprjbmve25axidy8q.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju7a0mp4m84qei8hammc.png)

> Depending on your settings, sometimes you need to create a new user on AWS to generate a key. To do so, you need to click Users on the Identity and Access Management page and then create a new user with permission to manage Lambda.

**Following this, you need to type the AWS region you use in the .env file in your perp-maker folder.** For instance, add AWS_REGION=ap-southest-1 to the file if you choose a server located in Singapore.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l0f0b6jfl7a1aw8s4w2p.png)

For other variables in the .env file, please check part 1 of this series or [perp-maker repo](https://github.com/perpetual-protocol/perp-maker) to learn more.
Additionally, for Windows users who have run it locally per [part one](https://dev.to/perpprotocol/running-perp-maker-for-profits-and-fun-part-1-5597)'s instructions, you need to remove the two lines you added in the index.ts file.

`import dotenv from "dotenv";
dotenv.config();`

By now we're all set! Enter the following commands in your terminal to deploy the bot to AWS:
`npm run build
npm run sls:deploy`

You should be able to see the following responses if everything goes well:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eh0cdkre66icmk4j51q0.png)

Once your bot is running, you can see the real-time logs by going to the perp-maker-main Lambda function on AWS and clicking the "View logs in CloudWatch" under the Monitor tab.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hv1gqjshujp3ssk5s0mp.png)

Another way to validate if the template runs successfully is to check the target markets in the pool section on the UI. For testnet (Optimism Kovan), please visit [testnet.perp.exchange](https://testnet.perp.exchange/); As for mainnet (Optimism), visit [app.perp.com](https://perp.com/?utm_source=perp_devto&utm_medium=blog_post).
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xk8809gst565r8s1l2ff.png)

# Prepare for Mainnet

Ready for mainnet? Here's a checklist:

- You need to get some USDC on Optimism. You can use [TransferTo.xyz](https://transferto.xyz/swap) to bridge your USDC from another chain to Optimism. If you only have USDC on a centralized exchange like Binance or FTX and you only want to test a small amount out, you can use [LayerSwap](https://www.layerswap.io/) to transfer directly to Optimism; as for greater amounts like over 5,000 USDC, you can withdraw to BNB Chain or Avalanche's C-chain and then use TransferTo to go to Optimism (requires a small amount of BNB or AVAX to pay for gas).
- You need to get ETH on Optimism to pay for the gas fees. Much like USDC, you can use [TransferTo](https://transferto.xyz/swap) or [LayerSwap](https://www.layerswap.io/) (for CEX) depending on how much USDC or ETH you want to transfer. In general, each transaction on Perp v2 costs around 0.0001 ETH, so even 0.1 ETH can be used for a long time.
- You need to change the endpoint in the .env file to the mainnet one.
- You need to uncomment the two lines for process.env in the index.ts file that we added in [part one](https://dev.to/perpprotocol/running-perp-maker-for-profits-and-fun-part-1-5597) (// process.env["STAGE"] = "production" and // process.env["NETWORK"] = "optimism").


# Further Modification Suggestions
As mentioned at the beginning of this article, this bot is just a template to market-make on Perp v2. Given the nature of an open-sourced software, anyone can add functionalities to this bot. Here are some directions that you can explore:

- **Provide single-sided liquidity**
- Currently, the bot only supports a fixed range above and below the market price. Another way to market-make on Perp v2 is to only provide liquidity for a single side, i.e., only above or below the current market price. One major benefit of providing single-sided liquidity is that when the price moves in the range and then subsequently moves out of the range, the maker can collect fees without any impermanent loss. You can already provide single-sided liquidity on [the UI](https://app.perp.com/?utm_source=perp_devto&utm_medium=blog_post) with the simple mode, or "Easy LP" as we call it.
- **Close the newly created position gradually**
- With the current design, the bot will adjust your liquidity when the price is out of the LIQUIDITY_ADJUST_THRESHOLD. When doing that, it'll realize the impermanent loss by closing 100% of your taker position. This approach should be fine for small positions. However, if your position size is quite large, it's better to close your position gradually to reduce the slippage incurred in each trade.
- **Utilize the multi-collateral feature**
- One of the selling points of Perp v2 is [multi-collateral support](https://blog.perp.fi/introducing-multi-collateral-on-perpetual-protocol-fe1f4fa6fb90), meaning that users can deposit ETH, USDC, FRAX, (and more in the future) as the collateral for their positions. The current template only supports USDC as the collateral. One major benefit of using ETH as the collateral is that your collateral value will increase as the price of ETH increases, which is handy if you're providing liquidity to the ETH market as you're acting as the short side during the bull market. In other words, in net USD terms, you're collecting transaction fees without liquidation risk.

That's the end of our two-part series on how to run [perp-maker](https://github.com/perpetual-protocol/perp-maker)!

We'll release another tutorial on how to run [perp-curie-arbitrageur](https://github.com/perpetual-protocol/perp-curie-arbitrageur) very soon! In the meantime, feel free to join our Discord where we have a [#coding-chat channel](https://discord.com/invite/M7y2VFqwCb) for questions you encounter when building on top of Perp v2.


# Helpful Resources

- A third-party site to track your fees and APR: https://www.seedle.finance/
- Maker simulator for Perp v2: https://defi-lab.xyz/perpetualsimulator
- API for Perp v2: https://thegraph.com/hosted-service/subgraph/perpetual-protocol/perpetual-v2-optimism?query=Get%20Trader
- Contract addresses on Perp v2: https://metadata.perp.exchange/v2/optimism.json
- Perp v2 documentation: https://v2docs.perp.fi/