Jan 21, 2025
Swapping between stablecoins is usually straightforward. However, when you're new to a particular stablecoin, it can be confusing to know which tools or platforms to use.
We've created a simple swap tool for SBC that eliminates gas fees through a Pimlico paymaster, making it easy to convert between USDC and SBC in either direction without worrying about fees.
This stableswap tool is available on the Base blockchain. It's open source and available at the GitHub repo.
See the demo
After connecting your wallet to the Base blockchain, you'll see your available USDC and SBC balances.
Enter your desired USDC amount to see the minimum SBC you'll receive in return. Click the switch arrow to swap from SBC to USDC instead.
When you click Swap
, you'll need to sign two messages: first, a permit allowing your smart account to access your wallet, and second, a compressed message that performs the following actions:
Ask the user to
permit
the smart account to “spend” its source token from the user’s walletTransfer the exact amount from the user wallet to the smart account
Approve the Uniswap V3 Router to spend that exact amount from the smart account
Encode the swap details as required by Uniswap, which includes sending the received destination token back to the user wallet
Once the transaction completes, you'll receive a notification with a blockchain explorer link showing your executed swap.
Know the tech
For our stable swap solution, we chose Pimlico due to its developer-friendly SDK and simple implementation process. The swaps are executed through Uniswap V3.
Create a Smart Account
First, you need to create a smart account owned by the user's wallet. You can do this using the toSimpleSmartAccount
converter function.
Create a Smart Account Client
Next, we create a Smart Account Client — a wrapper object that handles all the functions needed for working with user operations.
pimlicoClient
is the RPC endpoint from Pimlico that enables both bundler and paymaster functionality.
Build the User Operation
Then, we want to build the swap functionality into a user operation. Technically, this translates to:
Ask the user to sign a
permit
so the smart account can “spend” its source tokenCalling the source ERC20 token’s
transferFrom
function to transfer the exact amount from the user wallet to the smart accountCalling the
approve
function on the Uniswap V3 Router to spend that exact amount from the smart accountEncode the swap details as required by Uniswap, which includes sending the received destination token back to the user wallet
We use encodeFunctionData
from viem to package an array of instructions like this.
Finally, we call sendTransaction
from our created smartAccountClient
and receive a hash, which we can then look up in Base’s blockchain scanner.
And that’s how easy it is to swap between ERC20 tokens without paying any gas.
See the code
The code for the Gasless Swap is available in Github here.
Next steps
If you have thoughts, improvements, or would like to contribute to the project, please let us know in Telegram.