As seen on YouTube: https://youtu.be/B9v66YdyNMI.
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.
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
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.
Download package
Install packages
~
cd myproject
Then install the exact same packages using yarn (you can use npm instead if you prefer)
~/myproject
yarn --exact
Environment variables
~/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)
Ensure code is working locally
~/myproject
npx hardhat clean
~/myproject
npx hardhat compile
~/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.
Execute code on the blockchain
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.
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.
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.
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
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 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.
This shows that the funds have arrived in the Submarine Contract address:
~/myproject
npx hardhat run --network goerli scripts/step5.ts
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
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
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.