Jan 21, 2025

A Gasless Stablecoin Swap Tool

A Gasless Stablecoin Swap Tool

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:

  1. Ask the user to permit the smart account to “spend” its source token from the user’s wallet

  2. Transfer the exact amount from the user wallet to the smart account

  3. Approve the Uniswap V3 Router to spend that exact amount from the smart account

  4. 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:

  1. Ask the user to sign a permit so the smart account can “spend” its source token

  2. Calling the source ERC20 token’s transferFrom function to transfer the exact amount from the user wallet to the smart account

  3. Calling the approve function on the Uniswap V3 Router to spend that exact amount from the smart account

  4. Encode 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.

const calls = [
	// 1. Ask user to sign permit
	{ 
		to: "0x...", // sender address
		data: "0x..................", // calldata for permit
	},
	// 2. Call transferFrom to transfer $ to smart account
	{ 
		to: "0x...", // sender address
		data: "0x..................", // calldata for transfer 
	},
	// 3. Call approve on Uniswap V3 Router
	{ 
		to: "0x...", // address
		data: "0x..................", // calldata for approve
	},
	// 4. Call swapExactInputSingle on Uniswap V3 Router
	{
		to: "0x...", // Uniswap V3 router address
		data: "0x..................", // calldata for swap
	}
]

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.

Join the SBC

ecosystem

Partner with us to build a global, free, and accessible payment network.

Join the SBC

ecosystem

Partner with us to build a global, free, and accessible payment network.

Join the SBC

ecosystem

Partner with us to build a global, free, and accessible payment network.

Join the SBC

ecosystem

Partner with us to build a global, free, and accessible payment network.