Skip to content

Commit f8cff49

Browse files
authored
feat(docs): swap USDT0 to FXRP using Uniswap V3 guide (#976)
1 parent 8a98868 commit f8cff49

File tree

4 files changed

+560
-0
lines changed

4 files changed

+560
-0
lines changed

docs/fassets/developer-guides.mdx

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,21 @@ Guides for [redeeming FAssets](/fassets/redemption) back to underlying assets.
9696
]}
9797
/>
9898

99+
## FXRP Token Interactions
100+
101+
Guides for interacting with FXRP token.
102+
103+
<DocCardList
104+
items={[
105+
{
106+
type: "link",
107+
label: "Swap USDT0 to FXRP",
108+
href: "/fassets/developer-guides/usdt0-fxrp-swap",
109+
docId: "fassets/developer-guides/usdt0-fxrp-swap",
110+
},
111+
]}
112+
/>
113+
99114
## Agent Information
100115

101116
Guides for reading and working with agent data.
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
---
2+
title: Swap USDT0 to FXRP
3+
tags: [intermediate, fassets]
4+
slug: usdt0-fxrp-swap
5+
description: Swap USDT0 to FXRP using Uniswap V3 router
6+
keywords: [fassets, flare-network]
7+
---
8+
9+
import CodeBlock from "@theme/CodeBlock";
10+
import UniswapV3Wrapper from "!!raw-loader!/examples/developer-hub-solidity/UniswapV3Wrapper.sol";
11+
import UniswapV3WrapperScript from "!!raw-loader!/examples/developer-hub-javascript/uniswapV3Wrapper.ts";
12+
import Remix from "@site/src/components/remix";
13+
14+
## Overview
15+
16+
In this guide, you will learn how to swap USDT0 to FXRP using the Uniswap V3 router (SparkDEX).
17+
FXRP is the ERC-20 representation of XRP used by [FAssets](/fassets/overview).
18+
19+
## Prerequisites
20+
21+
Before starting, make sure you have:
22+
23+
- Basic understanding of the [FAssets system](/fassets/overview).
24+
- [Flare Network Periphery Contracts](https://www.npmjs.com/package/@flarenetwork/flare-periphery-contracts) package installed.
25+
- Familiarity with [Uniswap V3 swaps](https://docs.uniswap.org/contracts/v3/guides/swaps/single-swaps).
26+
27+
## Required Addresses on Flare Mainnet
28+
29+
- V3 SwapRouter (SparkDEX): [0x8a1E35F5c98C4E85B36B7B253222eE17773b2781](https://flarescan.com/address/0x8a1E35F5c98C4E85B36B7B253222eE17773b2781/contract/14/code).
30+
- USDT0: [0xe7cd86e13AC4309349F30B3435a9d337750fC82D](https://flarescan.com/address/0xe7cd86e13AC4309349F30B3435a9d337750fC82D).
31+
- FXRP: [resolved dynamically from the Asset Manager](/fassets/developer-guides/fassets-fxrp-address).
32+
33+
## Solidity Uniswap V3 Wrapper Contract
34+
35+
This Solidity contract wraps the Uniswap V3 router, providing safe token transfers, pool checks, and event logging for debugging.
36+
37+
<CodeBlock language="solidity" title="contracts/fassets/UniswapV3Wrapper.sol">
38+
{UniswapV3Wrapper}
39+
</CodeBlock>
40+
41+
{/* prettier-ignore */}
42+
<Remix fileName="UniswapV3Wrapper.sol">Open in Remix</Remix>
43+
<br></br>
44+
45+
### Code Explanation
46+
47+
1. Define the Uniswap V3 router function for swaps, which allows interaction with the Uniswap V3 protocol.
48+
It comes from the [Uniswap V3 periphery](https://github.com/Uniswap/v3-periphery).
49+
50+
2. Use the Uniswap V3 factory interface to locate a specific pool for a token pair and the fee tier.
51+
52+
3. Define the Uniswap V3 pool interface that provides liquidity and token details for a given pool.
53+
54+
4. Create the `UniswapV3Wrapper` contract, which acts as a wrapper around Uniswap V3 to simplify swaps and add safety checks.
55+
56+
5. Use the existing Uniswap V3 SwapRouter on Flare (SparkDEX) as the main entry point for executing swaps.
57+
58+
6. Store the Uniswap V3 factory in the contract for looking up pools.
59+
60+
7. Define events that log important actions such as swaps executed, pools checked, and tokens approved.
61+
62+
8. Create a constructor that initializes the swap router and factory.
63+
64+
9. Check if the pool exists and has liquidity, preventing the swaps execution in empty or non-existent pools.
65+
66+
10. Create a swap exact input single function that executes a single-hop swap through the Uniswap V3 router protocol.
67+
68+
10.1. Check if pool exists, ensuring a valid pool address is returned.
69+
70+
10.2. Check if the pool has liquidity, requiring the available liquidity is greater than zero.
71+
72+
10.3. Transfer tokens from the user to this contract, using SafeERC20 for secure transfers.
73+
74+
10.4. Approve the router to spend tokens using the `SafeERC20` library, allowing the router to perform the swap.
75+
76+
10.5. Prepare swap parameters, filling the `ExactInputSingleParams` struct with all necessary details.
77+
78+
10.6. Execute the swap, calling the `swapRouter.exactInputSingle` function to perform the transaction.
79+
80+
10.7. Emit the swap executed event, logging details about the user, tokens, and amounts.
81+
82+
## Execute the Swap with Hardhat
83+
84+
To execute a swap, you need to deploy the wrapper contract and call `swapExactInputSingle` with the swap parameters.
85+
86+
<CodeBlock
87+
language="typescript"
88+
title="scripts/fassets/swapExactInputSingle.ts"
89+
>
90+
{UniswapV3WrapperScript}
91+
</CodeBlock>
92+
93+
### Code Breakdown
94+
95+
1. Import contract artifacts, including `IAssetManager`, `UniswapV3Wrapper`, and `ERC20`, which provide access to the deployed contracts.
96+
97+
2. Define Flare Uniswap V3 addresses (SparkDEX), specifying the SwapRouter address used for executing swaps.
98+
99+
3. Set the USDT0 token address on Flare Mainnet, which represents the stablecoin used as the input asset for the swap.
100+
101+
4. Set the pool fee tier to 500 (0.05%), which defines the fee level of the pool to use on the Uniswap V3 router (SparkDEX).
102+
103+
5. Define swap parameters.
104+
105+
6. Define the function `deployAndVerifyContract()` that deploys the UniswapV3Wrapper contract and verifies it on the blockchain explorer.
106+
107+
7. Create the `setupAndInitializeTokens()` function that prepares accounts and ERC20 contracts.
108+
109+
8. Define the `verifyPoolAndLiquidity()` function which ensures the USDT0/FXRP pool exists and is usable.
110+
111+
9. Create the `approveUsdt0ForSwap()` function that approves the wrapper contract to spend USDT0 on behalf of the deployer.
112+
113+
10. Create the `executeSparkDexSwap()` function that executes the swap on SparkDEX using the Uniswap V3 wrapper.
114+
115+
11. Create the `checkFinalBalances()` function that verifies the swap results.
116+
117+
12. Create the `main()` function that orchestrates the entire flow of the swap.
118+
119+
### Run the Script
120+
121+
To run the script, you need to execute the following command:
122+
123+
```bash
124+
yarn hardhat run scripts/fassets/uniswapV3Wrapper.ts --network flare
125+
```
126+
127+
:::info
128+
This script is included in the [Flare Hardhat Starter Kit](/network/guides/hardhat-foundry-starter-kit).
129+
:::
130+
131+
## Summary
132+
133+
In this guide, you learned how to swap USDT0 to FXRP using the Uniswap V3 router (SparkDEX).
134+
135+
:::tip
136+
137+
To continue your FAssets development journey, you can:
138+
139+
- Learn how to [mint FXRP](/fassets/developer-guides/fassets-mint).
140+
- Understand how to [redeem FXRP](/fassets/developer-guides/fassets-redeem).
141+
- Explore [FAssets system settings](/fassets/operational-parameters).
142+
143+
:::

0 commit comments

Comments
 (0)