The blockchain ensures that value moves securely and trustlessly, but it’s not very user friendly to interact with. That’s why crypto protocols build frontends – websites that help you seamlessly interact with the on-chain contracts. But what if a frontend website goes down, is compromised, or blocks your address? Knowing how to use DeFi without a frontend is critical to protecting your assets in an emergency.
Smart contracts are (broadly) permissionless. You don’t have to interact with them via approved portals like an official website or anything like that. However, it can be annoying to interact with them without the help of the frontend. As an example, Twitter has an API (Application Programming Interface). The API lets you tweet, delete, and much more, without using the Twitter web or mobile apps. That’s how services like TweetHunter and Hypefury work.
But, how annoying would it be to use Twitter if there wasn’t a frontend app to interact with, and everything had to be done via the Twitter API? Many users found themselves a in similar situation recently, when protocols began blocking addresses associated with Tornado Cash from accessing their frontends. Lots of people got caught in the crossfire. These users were unable to withdraw their funds or make DEX swaps unless they did so directly on the blockchain.
Let’s dive into how to use DeFi without a frontend, so you can be prepared if the need arises.
How To Use DeFi Via Etherscan
The simplest way to not use a frontend is to interact with the contract via Etherscan. This is perfectly fine for simple functions or less complex interactions. To start with, you’re going to need to find the contract on Etherscan. We’ll use the USDC token contract for this quick example. Once you find the contract, go to the “Contract” page, then go to the tab marked “Write Contract”. For some contracts, like USDC, this will also include a “Write as Proxy” tab – if that is present, you need to click on that.
Now, you can see the token’s functions. If you hit the “Connect to Web3” button, you can connect your wallet and call functions directly from here. For USDC, the approve function can be used to revoke a malicious approval, or grant approval to transfer your tokens!
That was a very simple example of how to interact with a contract without a frontend. Now, let’s get into some common DeFi actions.
How to Swap Tokens On Etherscan
This set of instructions will be applicable, perhaps with some small modifications, to Uniswap V2, Sushiswap, Trader Joe, or any other Uniswap V2 fork DEX. If you need to swap via Uniswap V3 without a frontend, may God be with you.
First, find the Router contract. You’ll need this address to feed to your approval transaction, as well as to interact with the router itself. The Uniswap V2 router is here.
Before you can interact with the router to swap, you need to approve it to transfer your input currency. If you’ve already approved, you can skip to the next step.
Remember, approval transactions for a token have to be sent to that token’s contract. For example, we’ll approve the router to transfer USDC. Set “spender” as the UniV2 router address, and “value” as your desired amount. USDC has 6 decimals – meaning 1 USDC is represented by 1,000,000 units at the contract level. Other tokens may have 18 decimals. You can see what decimal your token uses by going to its “Read Contract” tab on Etherscan.
Pro Tip: hitting the “+” button lets you add zeroes without worrying about counting. You can also verify that you put in the right number of zeroes by going to the Etherscan unit converter. When you’re satisfied, hit “Write”, approve the transaction in your wallet, and let it complete.
Go to the router’s “Write Contract” tab. Your exact needs may vary, but you’re probably looking for “
swapExactTokensForTokens” or “
swapExactTokensForETH“, which, as the name implies, will swap an exact amount of the input token for however much output token the pool conditions dictate, where the output token is either an ERC-20 token or ETH.
Let’s presume you want to swap USDC to DAI. We’ll use the
swapExactTokensForTokens function for this. The
amountIn parameter will be the amount you want to swap. For 1000 USDC, this will be 1000000000. Remember to use the zeroes tool if need be.
amountOut parameter is the minimum amount out you are willing to accept. If this is not met, the swap will revert. If you want to turbo math the exact amount that’s probably something to write a script for, otherwise just use a scaling factor. There’s a 0.3% fee on most UniV2 clones, and you should tolerate a little slippage also, say 0.5%, for a total of 0.8% allowable slip. This means we should tolerate no less than 99.2% of our input value. Run that through a calculator and you’ll get 992 DAI for 1000 USDC. Shocker.
Now, don’t forget to add the 18 decimals for DAI. Use the zero tool. Our swap setup should now look like this:
Next up – the path parameter. This allows you to feed a path of pools to swap through. Not every token pairing has a direct path. Ex. if I want to swap from exotic token A to exotic token B, there’s probably not liquidity directly for that pair – but there may be ExoticA/ETH and ExoticB/ETH pairs, and you can swap A -> ETH -> B. I will not discuss trying to calculate and route a swap through multiple pools here, it’s out of scope. If you need to do this for whatever reason, check the docs.
For our example, there’s a direct pair, and we just need to feed a list of 2 addresses – starting with USDC, and ending with DAI. Use square brackets to enclose the list. Like so.
The to parameter should be your address which you want to receive the funds. I’ll let you fill that in. Finally, the deadline parameter is the time at which this swap is no longer valid. That protects you against someone forcing the swap to execute later under unfavorable conditions. We’ll set that as the current time, in Unix Epoch seconds, plus a bit of time for the block to process. Grab the current epoch time off of this handy site, and add a few minutes to it. Maybe 180 seconds, up to you.
All set up! Hit “Write”, and enjoy your new tokens!
How To Add or Remove Liquidity On Etherscan
Adding/removing liquidity works very similarly to swapping, except we need a little more info about the pool. Please see the docs to make sure you don’t slip up on some nuance.
Adding liquidity to the pool has to be done at the prevailing pool price. EG. if I have a pool of A and B, and the ratio of A:B in the reserves is 5:1, I should add liquidity at a 5:1 ratio also. If I want to put in 50 A, I should put in 10 B. Failure to do so will slip the price, and open the pool up to arbitrage which will lose some of your funds.
You can determine this price by looking at the pool reserves directly. BowTiedDevil has a very detailed writeup of the pool reserve math on his Substack, which you should consult if you want to get the absolute best price.
Short of that, grab a price off your favorite price feed, or in the case of our USDC/DAI example, we will assume they’ll be trading at about 1:1 parity. This can be a dangerous assumption in the wild!
Let’s say I want to add 1000 USDC and 1000 DAI of liquidity to the pool. The token addresses will be tokenA and tokenB respectively. Those quantities (padded by the appropriate zeroes) will be the amountADesired and amountBDesired. I will accept 0.5% slippage in either direction, so I’ll set my amountAMin and amountBMin to 995 and 995 (again zeroes padded). Set my address as the to parameter, and set deadline as we did in the swap tutorial above. Voila!
How to Use DeFi Via Script
The most powerful way to use DeFi without a frontend is by running pieces of code, or scripts. Interacting via script is more complicated, and we’ll discuss it in detail in another article. Here’s a few high level points.
If you don’t know how to code, you’ll struggle to work with scripts. I’ll say that up front. You don’t have to be a pro by any stretch, but you should have at least some grasp of how computer programming works. If your brain just doesn’t work that way, get someone you trust to write the scripts for you.
Warning: If you don’t know exactly what a piece of code does, don’t run it. There are a lot of people out there scamming unsavvy users by tricking them into blindly running scripts. Safety first!
Programmatically interacting with the blockchain via a script is how power users execute their transactions. Developers use it to deploy contracts. Blackhat hackers use it to execute their attacks. Protocols use it to automate their operations. The primary power of scripts is the ability to automatically run calculations and avoid tedious manual input work. Plus, it’s satisfying to smash the enter key like a hacker from the movies.
At a high level, to interact with a blockchain, you need to have the following elements:
- The contract ABI (instructions to interact with it)
- The contract address
- A way to generate and sign transactions.
- A node or RPC provider
You can either get the ABI from Etherscan, or you can generate it yourself by compiling the contract’s source code. We’ll get the address off Etherscan or from the protocol docs. You can grab an RPC provider from Ankr. That leaves us with preparing and signing the transaction – that’s where the script comes in.
One critical thing to keep in mind: do not use the private key from any of your main wallets for running scripts. The script has to have access to a private key to sign the transactions, and unless you know exactly what you’re doing that key is likely to be exposed in plain text. As you should know, exposed keys are vulnerable if your computer is compromised in any way. (Learn how to respond if your keys are compromised)
Finally, you need the actual script. We’ll discuss that in a later article!
A Word Of Warning
It’s important to do your due diligence when using DeFi without a frontend. Frontends have a lot of safety rails built into them to help you avoid simple mistakes. Entering raw transaction parameters and interacting directly with the contracts is a more risky proposition. It’s all too easy to enter the wrong value, or paste the wrong address, and poof goes your money.
Make sure you verify all your parameters before approving the transaction!
It’s also important to understand the architecture. UniswapV2 pairs use a pattern that requires you to send the money to them before calling to swap or mint. The router takes care of this for you, but if you didn’t know to use the router and tried interacting directly with the pair, you would lose your tokens!
Ask around and get help if you don’t understand something. Better that, than making a silly mistake and putting your funds somewhere unrecoverable – or giving them to someone else.
As long as you’re cautious and understand what you’re doing, however, it’s perfectly safe to interact directly. I recommend all users try a simple transaction like revoking an old unneeded approval. It’s simple to do, and hard to mess up!