22title : Swap USDT0 to FXRP
33tags : [intermediate, fassets]
44slug : usdt0-fxrp-swap
5- description : Swap USDT0 to FXRP tokens using Uniswap V3 router
5+ description : Swap USDT0 to FXRP using Uniswap V3 router
66keywords : [fassets, flare-network]
77---
88
@@ -13,20 +13,22 @@ import Remix from "@site/src/components/remix";
1313
1414## Overview
1515
16- In this guide, you will learn how to swap USDT0 to FXRP tokens using the Uniswap V3 router (SparkDEX).
16+ In this guide, you will learn how to swap USDT0 to FXRP using the Uniswap V3 router (SparkDEX).
1717FXRP is the ERC-20 representation of XRP used by [ FAssets] ( /fassets/overview ) .
1818
1919## Prerequisites
2020
2121Before starting, make sure you have:
2222
2323- Basic understanding of the [ FAssets system] ( /fassets/overview ) .
24- - [ Flare Network Periphery Contracts] ( https://www.npmjs.com/package/@flarenetwork/flare-periphery-contracts ) package.
24+ - [ Flare Network Periphery Contracts] ( https://www.npmjs.com/package/@flarenetwork/flare-periphery-contracts ) package installed .
2525- Familiarity with [ Uniswap V3 swaps] ( https://docs.uniswap.org/contracts/v3/guides/swaps/single-swaps ) .
26- - Addresses on Flare Mainnet:
27- - V3 SwapRouter (SparkDEX): [ 0x8a1E35F5c98C4E85B36B7B253222eE17773b2781] ( https://flarescan.com/address/0x8a1E35F5c98C4E85B36B7B253222eE17773b2781/contract/14/code ) .
28- - USDT0: [ 0xe7cd86e13AC4309349F30B3435a9d337750fC82D] ( https://flarescan.com/address/0xe7cd86e13AC4309349F30B3435a9d337750fC82D ) .
29- - FXRP: [ resolved dynamically from the Asset Manager] ( /fassets/developer-guides/fassets-fxrp-address ) .
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 ) .
3032
3133## Solidity Uniswap V3 Wrapper Contract
3234
@@ -42,45 +44,42 @@ This Solidity contract wraps the Uniswap V3 router, providing safe token transfe
4244
4345### Code Explanation
4446
45- 1 . Defines the Uniswap V3 router function for swaps, which allows interaction with the Uniswap V3 protocol.
46-
47- 2 . ` exactInputSingle ` function used for direct swaps between two tokens in a single pool.
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 ) .
4849
49- 3 . Uniswap V3 factory interface that returns the factory address responsible for creating pools .
50+ 2 . Use the Uniswap V3 factory interface to locate a specific pool for a token pair and the fee tier .
5051
51- 4 . Uniswap V3 factory interface, used to locate a specific pool for a given token pair and fee tier .
52+ 3 . Define the Uniswap V3 pool interface that provides liquidity and token details for a given pool .
5253
53- 5 . The Uniswap V3 pool interface provides liquidity and token details for a given pool .
54+ 4 . Create the ` UniswapV3Wrapper ` contract, which acts as a wrapper around Uniswap V3 to simplify swaps and add safety checks .
5455
55- 6 . ` UniswapV3Wrapper ` contract, which acts as a wrapper around Uniswap V3 to simplify swaps and add safety checks .
56+ 5 . Use the existing Uniswap V3 SwapRouter on Flare (SparkDEX) as the main entry point for executing swaps .
5657
57- 7 . Existing Uniswap V3 SwapRouter on Flare (SparkDEX), used as the main entry point for executing swaps .
58+ 6 . Store the Uniswap V3 factory in the contract for looking up pools .
5859
59- 8 . The Uniswap V3 factory is stored in the contract for looking up pools .
60+ 7 . Define events that log important actions such as swaps executed, pools checked, and tokens approved .
6061
61- 9 . Events, which log important actions such as swaps executed, pools checked, and tokens approved .
62+ 8 . Create a constructor that initializes the swap router and factory .
6263
63- 10 . Constructor that initializes the swap router and factory, ensuring both references are immutable .
64+ 9 . Check if the pool exists and has liquidity, preventing the swaps execution in empty or non-existent pools .
6465
65- 11 . Check if the pool exists and has liquidity, preventing execution of swaps in empty or non-existent pools .
66+ 10 . Create a swap exact input single function that executes a single-hop swap through the Uniswap V3 router protocol .
6667
67- 12 . Swap exact input single function, which executes a single-hop swap through the Uniswap V3 router protocol .
68+ 10.1. Check if pool exists, ensuring a valid pool address is returned .
6869
69- 12.1 . Check if pool exists, ensuring a valid pool address is returned .
70+ 10.2 . Check if the pool has liquidity, requiring the available liquidity is greater than zero .
7071
71- 12.2. Check if the pool has liquidity, requiring that the available liquidity is greater than zero .
72+ 10.3. Transfer tokens from the user to this contract, using SafeERC20 for secure transfers .
7273
73- 12.3. Transfer tokens from user to this contract, using SafeERC20 for secure transfers .
74+ 10.4. Approve the router to spend tokens using the ` SafeERC20 ` library, allowing the router to perform the swap .
7475
75- 12.4. Approve router to spend tokens using the ` SafeERC20 ` library, allowing the router to perform the swap .
76+ 10.5. Prepare swap parameters, filling the ` ExactInputSingleParams ` struct with all necessary details .
7677
77- 12.5. Prepare swap parameters, filling the ` ExactInputSingleParams ` struct with all necessary details .
78+ 10.6. Execute the swap, calling the ` swapRouter.exactInputSingle ` function to perform the transaction .
7879
79- 12.6. Execute swap, calling ` swapRouter.exactInputSingle ` function to perform the transaction .
80+ 10.7. Emit the swap executed event, logging details about the user, tokens, and amounts .
8081
81- 12.7. Emit swap executed event, logging details about the user, tokens, and amounts.
82-
83- ## Execute the Swap
82+ ## Execute the Swap with Hardhat
8483
8584To execute a swap, you need to deploy the wrapper contract and call ` swapExactInputSingle ` with the swap parameters.
8685
@@ -93,41 +92,29 @@ To execute a swap, you need to deploy the wrapper contract and call `swapExactIn
9392
9493### Code Breakdown
9594
96- 1 . Contract artifacts are imported , including ` IAssetManager ` , ` UniswapV3Wrapper ` , and ` ERC20 ` , which provide access to the deployed contracts.
95+ 1 . Import contract artifacts , including ` IAssetManager ` , ` UniswapV3Wrapper ` , and ` ERC20 ` , which provide access to the deployed contracts .
9796
98- 2 . Flare Uniswap V3 addresses (SparkDEX) are defined , specifying the SwapRouter address used for executing swaps.
97+ 2 . Define Flare Uniswap V3 addresses (SparkDEX), specifying the SwapRouter address used for executing swaps.
9998
100- 3 . USDT0 token address on Flare Mainnet, which represents the stablecoin used as the input asset for the swap.
99+ 3 . Set the USDT0 token address on Flare Mainnet, which represents the stablecoin used as the input asset for the swap.
101100
102- 4 . Pool fee tier is set to 500 (0.05%), which defines the fee level of the pool to use on the Uniswap V3 router (SparkDEX).
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).
103102
104- 5 . Swap parameters are defined .
103+ 5 . Define swap parameters .
105104
1061056 . Define the function ` deployAndVerifyContract() ` that deploys the UniswapV3Wrapper contract and verifies it on the blockchain explorer.
107106
108- 7 . The ` main() ` function is the entry point to execute the complete swap logic.
109-
110- 8 . Deploy and verify the ` UniswapV3Wrapper ` smart contract, retrieving its deployed address for later use.
111-
112- 9 . Get the deployer account using ` web3.eth.getAccounts() ` .
113-
114- 10 . Retrieve the FXRP token address dynamically from the Asset Manager contract.
115-
116- 11 . Define USDT0 and FXRP contract instances.
117-
118- 12 . Check the deployer account's initial balances of USDT0 and FXRP.
119-
120- 13 . Verify that there is enough USDT0 to perform the swap.
107+ 7 . Create the ` setupAndInitializeTokens() ` function that prepares accounts and ERC20 contracts.
121108
122- 14 . Check the Uniswap V3 pool using the wrapper contract to ensure that the USDT0/FXRP pool exists and has liquidity .
109+ 8 . Define the ` verifyPoolAndLiquidity() ` function which ensures the USDT0/FXRP pool exists and is usable .
123110
124- 15 . Approve the USDT0 token to the wrapper contract, allowing it to spend the tokens on behalf of the deployer.
111+ 9 . Create the ` approveUsdt0ForSwap() ` function that approves the wrapper contract to spend USDT0 on behalf of the deployer.
125112
126- 16 . Execute the swap using the UniswapV3Wrapper contract's ` swapExactInputSingle ` method .
113+ 10 . Create the ` executeSparkDexSwap() ` function that executes the swap on SparkDEX using the Uniswap V3 wrapper .
127114
128- 17 . Extract the swap result by checking the new FXRP balance and calculating the difference from the initial balance .
115+ 11 . Create the ` checkFinalBalances() ` function that verifies the swap results .
129116
130- 18 . Check final balances after the swap to verify execution .
117+ 12 . Create the ` main() ` function that orchestrates the entire flow of the swap .
131118
132119### Run the Script
133120
@@ -145,11 +132,12 @@ This script is included in the [Flare Hardhat Starter Kit](/network/guides/hardh
145132
146133In this guide, you learned how to swap USDT0 to FXRP using the Uniswap V3 router (SparkDEX).
147134
148- :::tip[ What's next ]
135+ :::tip
149136
150137To continue your FAssets development journey, you can:
151138
152- - Learn how to [ mint FAssets using the executor ] ( /fassets/developer-guides/fassets-mint-executor ) .
139+ - Learn how to [ mint FXRP ] ( /fassets/developer-guides/fassets-mint ) .
153140- Understand how to [ redeem FXRP] ( /fassets/developer-guides/fassets-redeem ) .
141+ - Explore [ FAssets system settings] ( /fassets/operational-parameters ) .
154142
155143:::
0 commit comments