Introduction
This guide will build off our quoting guide and show how to use a quote to construct and execute a trade on the Ramses V3 protocol. The examples use the@kingdomdotone/v3-sdk package, Ramses’ fork of the Uniswap V3 SDK.
If you need a briefer on the SDK and to learn more about how these guides connect to the examples repository, please visit our background page!To get started with local development, also check out the local development guide.
- Constructing a route from pool information
- Constructing an unchecked trade
- Executing a trade
Included in the example application is functionality to wrap/unwrap ETH as needed to fund the example
WETH to USDC swap directly from an ETH balance.trading.ts.
Using a wallet extension
Like in the previous guide, our example uses a config file to configure the inputs used. The strucuture is similar to the quoting config, but we also have the option to select an environment:Environment.LOCAL:
Environment.WALLET_EXTENSION:
0xac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 from Foundry’s example wallets.
Consider checking out the README of the example.
If you cannot see the Tokens traded in your wallet, you possibly have to import them.
Constructing a route from pool information
To construct our trade, we will first create an model instance of aPool. We create an ethers contract like in the previous guide.
We will first extract the needed metadata from the relevant pool contract. Metadata includes both constant information about the pool as well as information about its current state stored in its first slot:
feeis the fee that is taken from every swap that is executed on the pool in 1 per million - if thefeevalue of a pool is 500,500/ 1000000(or 0.05%) of the trade amount is taken as a fee. This fee goes to the liquidity providers of the Pool.liquidityis the amount of liquidity the Pool can use for trades at the current price.sqrtPriceX96is the current Price of the pool, encoded as a ratio betweentoken0andtoken1.tickis the tick at the current price of the pool.
pool.ts.
Using this metadata along with our inputs, we will then construct a Pool:
Creating a Route
With thisPool, we can now construct a route to use in our trade. Routes represent a route over one or more pools from one Token to another. Let’s imagine we have three pools:
Route object can find this route from an array of given pools and an input and output Token.
To keep it simple for this guide, we only swap over one Pool:
Route understands that CurrentConfig.tokens.in should be traded for CurrentConfig.tokens.out over the Array of pools [pool].
Constructing an unchecked trade
Once we have constructed the route object, we now need to obtain a quote for the giveninputAmount of the example:
v3-sdk’s SwapQuoter, in contrast to the previous quoting guide, where we directly accessed the smart contact:
SwapQuoter’s quoteCallParameters function, gives us the calldata needed to make the call to the Quoter, and we then decode the returned quote:
uncheckedTrade function to create our Trade:
Executing a trade
Once we have created a trade, we can now execute this trade with our provider. First, we must give theSwapRouter approval to spend our tokens for us:
trading.ts.
We will use this function or similar implementations in most guides.
Then, we set our options that define how much time and slippage can occur in our execution as well as the address to use for our wallet:
SwapRouter class, a representation of the Ramses SwapRouter Contract, to get the associated call parameters for our trade and options:
