Create a Single-Chain Application Reading From a Caff Node

This guide showcases how to use Espresso Caff Nodes to enable instant token swaps inside a single rollup chain. Specifically, we create a Swap application on Rari testnet where users can swap TokenA ↔ TokenB with fast confirmation from Espresso.

The flow is the following:

  • User calls swap() on the smart contract.

  • The Sequencer orders the transaction, Batch Poster posts to the Parent Chain.

  • Espresso Caff Node immediately indexes the swap result and exposes it via API.

  • The dApp frontend queries the Caff Node to show the updated balance instantly.

Before You Begin

  • Clone the repo: https://github.com/enoldev/espresso-examples, and move to the instant-swap folder.

  • Install Foundry and ensure forge, cast, and anvil are available.

  • Have ETH on Rari testnet (for deployment and gas fees).

  • Ensure you have an Espresso Caff Node running (use the provided testnet endpoint or spin up your own).

The Instant Swap Application

We’ll implement a very simple Automated Market Maker (AMM) smart contract:

  • Users can deposit TokenA and TokenB into the pool.

  • Users can call swapAForB(amount) or swapBForA(amount).

  • The contract uses the constant product formula (x * y = k) for swaps.

Inspect the Code

In order to test the AMM contract, we will need to mock the ERC20 tokens. The following contract is a very simple MockERC20:

The AMM contract includes very simple logic for token swaps (TokenA and TokenB):

  1. We declare a minimal ERC20 interface (IERC20Minimal) with only the functions we need: transferFrom, transfer, and balanceOf. This avoids importing the full OpenZeppelin ERC20 implementation, keeping the example lightweight.

  2. The contract stores two ERC20 tokens (TokenA, TokenB) that can be swapped. reserveA and reserveB track the balances of each token held in the contract — representing the liquidity pool.

  3. The constructor sets the token addresses for TokenA and TokenB that this AMM will support.

  4. addLiquidity lets a user deposit both TokenA and TokenB into the pool. The caller must first approve the AMM contract to spend their tokens. Transfers the tokens in, updates reserves, and emits a LiquidityAdded event.

  5. swapExactAForB allows a user to swap a specific amount of TokenA for TokenB. It first requires the user to approve TokenA spending. Then it pulls amountAIn into the contract

Deploy the Smart Contracts

The script/Deploy.s.sol file contains the logic to deploy two tokens: TKA and TKB, and the AMM contract. It also sends the tokens to the deployer address (the address used to deploy the contracts).

After executing the script, the addresses of the deployer, tokens and AMM contract will be displayed in the logs. Create the environment variables.

Test the App

Now, you can use the Rari Testnet Caff Node to test the application:

  1. First, check the balance of the deployer address for each token. If the deployment script has been executed correctly, the deployer address must be funded with both tokens.

  1. Then, execute the approve(...) function in the TKA smart contract (this is necessary to comply with the ERC20 standard).

  1. Now, you will be able to call the swapExactAForB(...) function:

Last updated