Code Pack - submarinesends

Send Private Transactions with Submarine EVM Smart Contracts using Hardhat

Beat front runners on EVM based blockchains by sending transactions discretely A crypto snipers dream

Technologies
solidity
typescript
Themes
frontrunning
crypto
trading
flashloans
arbitrage
Skill Level
advanced
Last updated
2023-02-13

What's Inside

As seen on YouTube: https://youtu.be/B9v66YdyNMI.

Welcome to Submarine Sends on steroids.

If you are looking to perform a discrete trade on the blockchain and avoid being front run by bots, this code package will likely be extremely useful for you.


What is Front Running?

If you are using or developing with the Ethereum or any other EVM based blockchain, you have likely either heard of front running or been a victim of a front running attack yourself. If you are new to this concept, you can learn about it here at the Crypto Wizards YouTube channel: https://youtu.be/uElOqz-Htos


What are Submarine Sends?

So how can we avoid being front run? Submarine Sends. Full credit for the idea goes to libsubmarine. Following on from the above video, you can learn a lot more about this custom submarine send code and how it works here: https://youtu.be/B9v66YdyNMI.

A submarine send is essentially where you essentially do the following:

1 - create a blockchain account that looks like any regular blockchain account

2 - send funds to that blockchain account (appearing like any other token transfer). This part is crucial. It means you have commit a transaction with value to an address that no bot will likely care about. The address created is done in a special kind of way which will talk more about in the content of this code package.

3 - you then reveal your transaction later on to a given smart contract that performs the token swap (or whatever transaction you like) for you. In this code pack example, we focus on building a smart contract that executes a swap on any UniswapV2 compatible decentralised exchange. This transaction carries zero value and data that the bots aren’t familiar with. Information can be encoded in really clever ways as we talk about in the video. By now, it is too late for any bot to front run you and your transaction goes through undetected - or detected too late.

It is worth noting, all transactions on the blockchain are public, so you should encrypt any private information first off-chain. At some point, all data becomes known.

This code package is both an excellent way to get into blockchain development as it is also a highly valuable tool. Our Submarine Contract will perform a few special things such as:

1 - Being a unique Submarine contract address created predictably from some input code (more on that in the video)

2 - A self-destruct method so that the transaction will give you a reduction on gas fees (keeping transaction costs lower). Note that self-destruct has been deprecated but still works as intended in this code package.

3 - It will perform a DEX swap (you could alter this to do a flash loan, arbitrage trade or whatever) at the current price on the point of execution without having to care about slippage issues etc.

This is truly a unique piece of code and have learnt so much about advanced topics working with it. You will no doubt, experience the same joy and excitement with this code if you are also into blockchain or solidity programming.

Enjoy!

Uses

-
Run and test code on localhost in addition to the live Ethereum Goerli testnet
-
Perform DEX swaps directly in Solidity
-
Create Factory, Reveal and Submarine smart contracts which can all interact together
-
Use Typescript and ethers to create and query blockchain transactions
-
Put a super strong safeguard in place against bots and front runners
-
Have thew ability to 'snipe' crypto tokens at launch undetected
-
Create contracts that create other contracts (Factory Contracts)

Requirements

-
You need to be familiar with cryptocurrency and solidity to a basic degree to make use of this code. You must also be able to program with Typescript
-
Experience with hardhat is advantageous but not required
-
You must understand the concept of what we are achieving before writing any code. That said, you can follow the steps in the code package blindly and it should all still work
-
Do not download or use this package unless you understand the risks in trading crypto currency or the costs of cryptocurrency transactions

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

Install packages

In your terminal, cd into your project.

~

cd myproject

Then install the exact same packages using yarn (you can use npm instead if you prefer)

~/myproject

yarn --exact

Step 3

Environment variables

In your terminal, cd into your project

~/myproject

touch .env

Now add the following to your .env file. Keep the TOKEN_SWAP_ADDRESS exactly the same as below. This is the USDC address and is the test token we will be swapping (as widely available on testnet).

ACCOUNT_ADDRESS = "ENTER YOUR ACCOUNT ADDRESS"
PRIVATE_KEY = "ENTER YOUR PRIVATE KEY WITH 0X"
PROVIDER_URL = "ENTER YOUR INFURA OR ALCHEMY GOERLI PROVIDER"

REVEAL_ADDRESS=(Leave blank for now)

FACTORY_ADDRESS=(Leave blank for now)

TOKEN_SWAP_ADDRESS=0x07865c6e87b9f70255377e024ace6630c1eaa37f

UNIQUE_INT=0 (Leave 0 for now)

SUBMARINE_ADDRESS=(Leave blank for now)

Step 4

Ensure code is working locally

Test Hardhat on localhost.
Start by removing any previously saved artifacts:

~/myproject

npx hardhat clean
Compile solidity code

~/myproject

npx hardhat compile
Test the code

~/myproject

npx hardhat test

You should see green ticks against each test. The code for this is saved in the ‘test.ts’ folder.

This code is very useful to understand how to interact with the solidity smart contracts. It is strongly recommended that you read the code in the test.ts folder to guage what is happening, although you can skip that as the code is broken down into further steps when doing this on the actual blockchain.

Step 5

Execute code on the blockchain

Send Funds

Ensure you have sent your Metamask some Goerli from the Goerli testnet faucet. You can use any testnet, but this code has been built with Goerli tokens and contracts in mind. Your Metamask wallet should look like the below and be connected to the test network, Goerli. Make sure your provider connects to the Goerli network and is updated in your .env file.

Run Steps

For this to work, your ‘.env’ environment variables with provider url must be setup per the steps above.

In the Scripts folder, you will notice there are 7 steps. This is to help understand and see the code for each step in running such a Submarine type send in real-life. It will make things much easier to follow and understand.


Step 1

To run each step, just type the following with the step number into your command terminal:

~/myproject

npx hardhat run --network goerli scripts/step1.ts

Wait a minute or so and you will now have a Reveal Contract address and a Factory Contract address. Add these to the empty space in your .env file. For example:

REVEAL_ADDRESS=0x6666739c5348574A899D768EadDE3Dcc1a42fA08
FACTORY_ADDRESS=0x508956F5843f90cc14AE00DC09395E9723721353

Your addresses might look different to the above. That is totally ok. As long as they are there.


Step 2

Running Step 2 gives you the Submarine contract address (predicted) and creates the contract. Check the transaction on the Goerli Etherscan to ensure it completes before moving to step 3. Equally, remember to update the predicted Submarine Contract address in your .env file as shown below. Do not forget this.

~/myproject

npx hardhat run --network goerli scripts/step2.ts

Update .env example after running the above (manually paste in your Submarine contract address):

SUBMARINE_ADDRESS=0x091eE557493f3e3175061041843301148AB4135d

Step 3

This that the actual contract address for the Submarine contract matches the predicted one. This is a crucial check as it ensures all inputs into your contract and off-chain calls sync up nicely.

~/myproject

npx hardhat run --network goerli scripts/step3.ts

Step 4

Step 4 simply sends Ether to your Submarine contract address

~/myproject

npx hardhat run --network goerli scripts/step4.ts

Again, check the transaction on the Goerli Etherscan before moving on to the next step.


Step 5

This shows that the funds have arrived in the Submarine Contract address:

~/myproject

npx hardhat run --network goerli scripts/step5.ts

Step 6

Executing this will reveal your transaction to the Reveal Contract which calls the Submarine Contract. Once the swap takes place on the Submarine contract, it will self-destruct (yes, just like in Mission Impossible).

~/myproject

npx hardhat run --network goerli scripts/step6.ts

Step 7

Congratulations, once your transaction above goes through, you can then check your account balance for Ether and also the USDC testnet token (our SWAP_TOKEN). If you decide to change your token, just update the decimals from 6 on row 39 of the step7 file to 18 for example (or whatever the number of decimals are for the token you are swapping for).

This is because USDC uses 6 decimals. Failing to do this will not be severe, it just means the formatting of the amount in your wallet will be wrong.

You can also add the USDC token address to your Metamask on Goerli testnet. The amount will show in there.

~/myproject

npx hardhat run --network goerli scripts/step7.ts

CONGRATULATIONS

Finished

You can now edit the code and repeat all steps to make sure it is working for your unique requirements. If creating new Submarine contracts from the existing Factory address, update the UNIQUE_NUM in the environment variables folder. This will ensure your Submarine Contract addresses are unique.