Executing a Trade

This section demonstrates how to use a quote to construct and execute a trade on the Storyhunt V3 protocol.

Introduction

This section demonstrates how to use a quote to construct and execute a trade on the Storyhunt V3 protocol. The example involves trading between two ERC20 tokens, WIP and USDC, and allows configuring the input token, output token, input amount, and fee level.

The process includes:

  1. Constructing a route from pool information

  2. Building an unchecked trade

  3. Executing the trade

At the end, you’ll be able to create and execute a trade programmatically between any two ERC20 tokens.

Constructing a Route from Pool Information

First, gather metadata from the relevant pool contract. This includes constant information about the pool and its current state:

async function getPoolInfo() {
    const [fee, liquidity, slot0] = await Promise.all([
        poolContract.fee(),
        poolContract.liquidity(),
        poolContract.slot0(),
    ]);

    return {
        fee,
        liquidity,
        sqrtPriceX96: slot0[0],
        tick: slot0[1],
    };
}

Key values:

  • fee: The fee taken from every swap in the pool, expressed as 1 per million (e.g., a value of 500 means 0.05%).

  • liquidity: The current liquidity available for trades at the current price.

  • sqrtPriceX96: The current price of the pool.

  • tick: The tick corresponding to the current pool price.

Next, construct a Pool instance:

Creating a Route

Using the Pool instance, construct a Route to represent a trade path:

The Route object specifies that the input token will be traded for the output token over the specified pool.

Building an Unchecked Trade

Obtain a quote for the trade and construct an unchecked trade:

Executing the Trade

Approve the SwapRouter contract to spend the input token:

Set the swap options, including slippage tolerance and deadline:

Use the SwapRouter to get the call parameters for the trade:

Construct and send the transaction:

Next Steps

With the ability to execute trades, you can now explore routing through multiple pools for optimal pricing and fees. Expand your implementation to include advanced routing features for more efficient swaps.