Bridging USDCx

Current work is underway to abstract this entire flow via Circle's Bridge Kit SDK.

Intro

USDCx on Stacks opens up stablecoin liquidity into its decentralized ecosystem via Circle's xReserve protocol. This enables asset transfers from Ethereum and enhances Stacks' DeFi offerings. Users access Stacks' DeFi, maintaining stable assets, increasing liquidity, and providing a reliable option for transactions and investments.

tl;dr

  • Bridging USDC onto Stacks enables stablecoin liquidity across the Stacks DeFi ecosystem.

  • Deposits are initiated on Ethereum and automatically minted on Stacks, while withdrawals are initiated on Stacks and settled on Ethereum.

  • The bridging process is powered by Circle’s xReserve and the Stacks attestation service.

Steps

Deposit

  1. Setup USDC and xReserve's Solidity contract ABIs

  2. Setup wallet and public clients to communicate with the Ethereum network

  3. Check native ETH and USDC balances on a user's wallet.

  4. Prepare deposit params before initiating deposit request.

  5. Approve xReserve as a spender of your USDC.

  6. Execute deposit to the remote chain Stacks.

Key Tools To Use

  • viem - A Typescript-first library that interfaces with Ethereum.

  • stacks.js - A js library that helps developers build Stacks apps by handling transactions, wallet authentication, and smart contract interactions.


Complete Code

If you want to jump straight to the full implementation, the complete working code used in this guide is shown below.

Deposit

This script bridges USDC from Ethereum Sepolia testnet to Stacks testnet by first approving the xReserve contract to spend USDC, then calling depositToRemote to initiate the cross-chain transfer. It encodes the Stacks recipient address into the bytes32 format required by the Ethereum contract and submits both transactions to the Sepolia network. The Stacks attestation service will receive this event and mint the equivalent amount to the specified Stacks address.


Walkthrough (Deposit)

Before beginning, make sure you:

1

Configure environment variables

In the project root, create a .env file and set your Ethereum Sepolia wallet private key by replacing <YOUR_PRIVATE_KEY>. If you're working on the frontend with a browser wallet extension, this step won't be needed.

2

Import dependencies and define config constants

In this step, you’ll import the required dependencies and define the script’s configuration constants.

Create a file for the main script and import the below dependencies.

Configuration constants define the RPC endpoint, private key, contract addresses for the xReserve bridge and USDC token, and transfer parameters including the Stacks recipient address and deposit amount.

You can examine the USDC, xReserve, and the other contracts related to USDCx on Stacks here.

Replace YOUR_STACKS_TESTNET_ADDRESS with the wallet that should receive minted USDCx on Stacks testnet.

3

Setup contract ABIs

This adds xReserve and ERC-20 ABI fragments which tell Viem how to format and send the contract calls when the script runs.

4

Setup wallet and public clients

Creates a wallet account from the private key and initializes both a wallet client (for signing transactions) and a public client (for reading blockchain data) connected to the Sepolia testnet.

5

Check native ETH balance

Checks the wallet's native ETH balance to ensure there's enough to pay for gas fees, displaying it in both wei and ETH, and throws an error if the balance is zero.

6

Prepare deposit params

Prepares the deposit parameters by converting USDC amounts to the correct decimal format (6 decimals), encoding the Stacks recipient address into bytes32 format, and setting empty hookData for the cross-chain transaction.

Stacks addresses need to be reformatted and encoded to bytes32 on the Ethereum side. Special helper methods are needed for this encoding.

7

Check native USDC token balance

Queries the wallet's USDC balance from the ERC20 contract, displays it in both raw units and formatted USDC, and throws an error if the balance is insufficient for the deposit amount.

8

Approve xReserve and execute deposit

Executes two sequential transactions: first approves the xReserve contract to spend the specified USDC amount and waits for confirmation, then calls the depositToRemote function to initiate the cross-chain bridge transfer to the Stacks recipient.

After some time, Stacks attestation service should receive the request and mint the equivalent value in USDCx on Stacks.

These are example transactions on testnet:

Last updated

Was this helpful?