We all know that dreadful feeling when we see a new token launched and we want to buy it straight away, only for the UI (user interface) of Uniswap to be buggy, slow and unresponsive, causing you to miss out on launch prices.
Luckily, Uniswap is just a UI designed to help newcomers trade easily without knowing what actually happens in the background. Therefore, nothing stops us from cutting the middleman, a.k.a. https://uniswap.org/ which is exactly what we are here to demonstrate today.
In this guide we will go over two important methods to increase the chances your transaction goes through: Avoiding the slow UI of Uniswap and other DEXes, and increasing the slippage beyond Uniswap’s limit of 49%.
- An ERC20 wallet (e.g Metamask).
- 1 or more ERC20 Token addresses.
- The Uniswap Router address of your network
90% of the transactions of users on anticipated listings fail due to slippage.
The issue is mainly because Uniswap and other DEX platforms limit the slippage to 49%.
In order to understand slippage and how to adjust it manually, we have to go over a few basic terms first.
- Slippage — Transactions in the blockchain are not immediately approved and executed. Slippage refers to the price change of a token from the time a transaction is submitted to its execution. The transaction reverts when the difference exceeds the maximum slippage
If the price of $SHIBA is 10$, and I want to sell 1 $SHIBA for no less than 5$, I can set the slippage to 50%. If the price of $SHIBA goes below 5$ by the time our transaction is confirmed, the transaction reverts (i.e cancelled) but we still have to pay the gas fees for the transaction.
In volatile listings (e.g. new token launches), slippage errors are very common and can cause money losses due to gas fees.
- Router — Most DEX platforms (over all networks) interact with a smart contract behind the scenes called `Router`. Almost all implementations of the Router contracts are clones of the Uniswap Router. Thanks to this unified interface, this tutorial applies to nearly all networks.
Part 1 — Cutting the Middleman
Step 0: Token Decimals
If you already know about decimals, feel free to skip to the next step.
Smart contracts include a variable called `decimals` that represents the smallest unit of a token. In order to trade tokens yourself you must know how to use them.
How does it work?
In a contract with 9 decimal points, 1 token will be represented as 1,000,000,000 (1 * 10⁹), half a token would be 500,000,000 and so on.
The smallest unit of the token will be 0.000000001 tokens.
In general, for a contract with n decimals points, you would represent a single token as (1 * 10^n).
Why do I need it?
Simply put, if you want to trade tokens yourself, without a middleman, you have to know the decimals of the tokens you trade. Otherwise, you might not be able to differentiate 1,000 tokens from just 1.
How do I get my token’s decimals?
Go to https://etherscan.io/token/<your token address> and enter the contract address of your token. Etherscan will then show you the decimals on the top right section of the page. Here’s an example of this page for the USDT token (at address https://etherscan.io/token/0xdac17f958d2ee523a2206206994597c13d831ec7):
Step 1: Give the Router an Allowance
This step is only required if you are selling tokens. If you are buying tokens with ETH, you can skip to the next step.
ERC20 tokens have an allowance feature that allows certain addresses to spend tokens of other addresses on their behalf. In order to sell tokens using the Uniswap protocol, you need to give the Router an allowance of at least the amount of tokens you want to sell. Go to https://etherscan.io/ and enter the contract address of the token you want to sell.
Go to the `Contract` tab and then the `Write Contract` tab (you can use the tutorial from the previous step).
Writing to the contract changes the contract’s state and will require a fee. For that reason you need to connect your wallet.
After connecting, you want to use the approve function.
It receives an address and an amount of tokens.
The address should be the Router address, and the amount of tokens should be at least the amount of tokens you want to sell (do not forget the decimals).
- The Uniswap frontend always gives this number when you click the “Approve” button before selling for the first time. This number is the maximum value of the uint256 type.
Using such a large value can be useful in order to prevent having to call approve multiple times
- If you already sold the token once on Uniswap, it has already called allowance with the value above so you can avoid that step as well.
Step 2: Use the Router to Buy Token
Go to https://etherscan.io/ and enter the Router address and go to the `Contract` tab.
Before we start trading tokens it’s important to understand the different functions we can use.
To buy tokens with ETH you have 3 functions you can choose from.
We will cover the parameters required to buy tokens and what each of them means.
For the full official documentation of Uniswap, visit Router02 | Uniswap.
In every function call we have to represent ETH using WETH — an ERC20 contract (with 18 decimals) that is used to represent ETH in functions that deal with native currency.
The function spends as little ETH as possible to buy a fixed amount of tokens. The amount of ETH spent can vary based on the current price. The amount sent to the function is the maximum amount you are willing to spend (this represents the slippage you are familiar with from the UI).
In the example below we want to buy 400 USDT tokens and we are willing to spend up to 1 ETH.
- swapETHForExactTokens - This is the maximum amount of ETH we are willing to spend for this transaction. Notice how we represent 1 ETH as (1 * 10⁸).
- amountOut - This is the amount of tokens we want to buy. Make sure this number matches the decimals. In our example we want to buy 400 USDT, so we used the value 400 * 10⁶).
- path - An array of ERC20 Token addresses the transaction has to follow. The first one should be the input token and the last one is the output token. In this example our input is the WETH address and the output is the USDT address.
- to - The address to receive the output tokens (usually your Metamask wallet).
- deadline - A unix timestamp after which the transaction reverts. You can use a website like https://www.unixtimestamp.com/ to get the timestamp of a one minute into the future to make sure your transaction is reverted if more than a minute has passed and it wasn’t approved yet
The function buys as many tokens as possible for a fixed amount of ETH. The amount of tokens bought can vary based on the current price. The function also receives a minimum amount of tokens you are willing to receive (this represents the slippage you are familiar with from the UI).
The parameters are very similar to the parameters of swapETHForExactTokens
The function is identical to swapExactEthForTokens with the single exception that it should be used if the token purchased includes fees on transfer (such as burn fees, reflection fees etc).
Part 2 — Adjusting the Slippage
The Uniswap UI only allows a slippage of 49%. This is reflected in the amountOutMin parameter of the swapExactETHForTokens, and the swapETHForExactTokens parameter of the swapETHForExactTokens function.
By setting 0 in the amountOutMin (or a high value in the swapETHForExactTokens, depending on the function you chose), you can reach 100% slippage, which essentially means you will accept any price change, and your transaction will never fail because of slippage.
Please note that slippage is mainly used to avoid rapid price changes and unfavourable trades that may be caused by front runners and bots, so make sure you understand the consequences of ignoring the slippage mechanism with the method described here.
Useful Contract Addresses
Finding the addresses of the native token (WETH for Ethereum, WBNB for BSC etc) and the Router contract is out of the scope of this guide, but can be easily found on Google.
For your convenience, here are some of the values for popular chains and DEXes:
UniswapV2 — 0x7a250d5630b4cf539739df2c5dacb4c659f2488d
WETH — 0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2
PancakeSwapV2 — 0x10ED43C718714eb63d5aA57B78B54704E256024E
WBNB — 0xbb4cdb9cbd36b01bd1cbaebf2de08d9173bc095c
In this tutorial you learned how to directly interact with the API of the DEX, instead of relying on the slow website to update.
You also learned about what slippage is, how to go beyond the 49% limit, and even ignore it altogether.
Finally, you learned about the consequences and downsides of these powerful methods.
We would also like to mention a few tools that can be used as an alternative to the above approach on specific blockchains, like Bogged Finance for BSC, Poocoin, Dextools and others. The issue with these solutions is that they don’t support all blockchains and all exchanges, and they are not always free. This is why we believe that understanding how these features work and being able to use them directly will always be a powerful tool.
About Solid Group
Solid Group is a blockchain consulting and auditing service provider founded by cybersecurity experts with a great passion for the cryptocurrency world. We are known for our exceptional out of the box thinking, experience, and our credibility among the community. Throughout our work, our team was able to discover many high severity issues & vulnerabilities. We work with leading companies in the field, helping them increase their resilience through tailored services and solutions.
Solid Group provides ALL IN ONE ICO SOLUTION -
- audited token generator ( Generate your own token with NO CODING KNOWLEDGE)
- sniper bot protection tool
- Smart contract auditing service