Code Pack - flashloanbsc

Flash Loan Smart Contract BNB Chain with PancakeswapV3 and V2 Swaps

Successfully execute flash loan arbitrage finds using an advanced smart contract built with Hardhat

Technologies
solidity
typescript
Themes
arbitrage
crypto
trading
flashloans
Skill Level
advanced
Last updated
2023-08-22

What's Inside

As seen on YouTube:

https://youtu.be/7O16zieRTEU.

If you are looking for a smart contract that can be executed on the BNB Chain (aka Binance Smart Chain) successfully using the two most liquid traded exchanges (Pancakeswap V3 and Pancakeswap V2), then this will be the perfect code package for you.

This is not for newbies to programming or solidity. Developing smart contracts can feel highly challenging, particularly for folk new to the blockchain ecosystem so some experience is advised.

Uses

-
Gain full access to proprietary code for developing smart contracts on BNB Chain
-
Fork the mainnet to test flash loan arbitrage opportunities
-
Deploy your smart contract to mainnet (as shown below in walkthrough video)
-
Perform swaps on any mixture of Pancakeswap V3 and Pancakeswap V2 dynamically

Requirements

-
Must have some knowledge of Typescript and Solidity
-
Must understand how to debug Solidity smart contracts as shown

START

What you need to get running

Below is the basic software you will need to get started:

Node(developed with version: 18.11.0)
ts-node(developed with version: 10.9.1)
Yarn(developed with version: 1.22.19)

Step 1

Download package

Click the download button at the top of this page. Then save the project on your desktop of project folder. Going forward, we will assume a project name as 'myproject'.
However, you may of course name the project folder whatever makes sense for your purpose.

Step 2

Add environment variables

Change directory into your project folder and add a .env file:

cd myproject
touch .env

Add the below to your .env file, remember to replace the private key with your own, prefixed with “0x”:

MAINNET_PROVIDER_URL=https://bsc-dataseed.binance.org
TESTNET_PROVIDER_URL=https://data-seed-prebsc-1-s1.binance.org:8545
PRIVATE_KEY=ENTER_YOUR_PRIVATE_KEY_PREFIXED_WITH_0x

Step 3

Install Packages

Within the myproject (or whatever you have named your project folder) install the EXACT packages as shown:

~/myproject

yarn --exact
npx hardhat compile

Step 4

Test flashloan smart contract

Due to the configurations in the hardhat.config.ts file, your test will fork the mainnet and provide a thorough test for a basic swap:

~/myproject

npx hardhat test
You should see something like the below:

Make sure to watch the video above which will talk on this and how the smart contract works in more detail.

Step 5

Deploy to mainnet

Once you are satisfied with your smart contract and tested any changes you make fully, you can submit this to the blockchain using:

~/myproject

npx hardhat run scripts/deployFlash.ts --network mainnet

It will be important to ensure your private key is valid and in full working order when performing this step.

CONGRATULATIONS

Your smart contract is complete

Well done! Working with smart contracts and swaps on the BNB chain can be very challenging. You have successfully proven you can now take part in this using Flashloans which is super advanced stuff.

One note of caution: MEV bots look for smart contract transactions which call Flashloans to frontrun them. If you have not seen the code package on submarine sends, it is recommended to do so for your awareness.

See you in the next one.