|
| 1 | +--- |
| 2 | +title: Swap and Redeem FAssets |
| 3 | +tags: [intermediate, fassets] |
| 4 | +slug: fassets-swap-redeem |
| 5 | +description: Learn how to swap and redeem FAssets |
| 6 | +keywords: [fassets, flare-network] |
| 7 | +sidebar_position: 4 |
| 8 | +--- |
| 9 | + |
| 10 | +import CodeBlock from "@theme/CodeBlock"; |
| 11 | +import Remix from "@site/src/components/remix"; |
| 12 | +import FAssetsSwapAndRedeemContract from "!!raw-loader!/examples/developer-hub-solidity/FAssetsSwapAndRedeem.sol"; |
| 13 | +import FAssetsSwapAndRedeemScript from "!!raw-loader!/examples/developer-hub-javascript/fassetsSwapAndRedeem.ts"; |
| 14 | +import RedemptionProcessPrerequisites from "./_redemption_process_prerequisites.mdx"; |
| 15 | + |
| 16 | +## Overview |
| 17 | + |
| 18 | +In this guide, you will learn how to swap a token (in this example, WCFLR) for FXRP and redeem FXRP for FAssets using the Uniswap V2 router (in this example, BlazeSwap) and the FAssets asset manager. |
| 19 | + |
| 20 | +<RedemptionProcessPrerequisites /> |
| 21 | + |
| 22 | +## Swap and Redeem Smart Contract |
| 23 | + |
| 24 | +The following Solidity contract demonstrates how to swap WCFLR for FXRP and then redeem FXRP to receive XRP on the XRPL. |
| 25 | + |
| 26 | +### Contract Code |
| 27 | + |
| 28 | +<CodeBlock language="solidity" title="contracts/fassets/SwapAndRedeem.sol"> |
| 29 | + {FAssetsSwapAndRedeemContract} |
| 30 | +</CodeBlock> |
| 31 | + |
| 32 | +This contract has two main functions: |
| 33 | + |
| 34 | +- Swaps WCFLR for FXRP using a BlazeSwap router (Uniswap V2 compatible). |
| 35 | +- Redeems FAssets (FXRP) to an XRPL address using the FAssets asset manager. |
| 36 | + |
| 37 | +### 1. Required Imports |
| 38 | + |
| 39 | +The contract uses the following dependencies: |
| 40 | + |
| 41 | +- `IERC20` OpenZeppelin standard ERC-20 interface. |
| 42 | +- `IAssetManager` interface to the FAssets Asset Manager. |
| 43 | +- `AssetManagerSettings` is used to retrieve lot size and configuration. |
| 44 | + |
| 45 | +### 2. Interfaces |
| 46 | + |
| 47 | +The `ISwapRouter` interface allows interaction with a Uniswap V2-compatible router: |
| 48 | + |
| 49 | +- `swapExactTokensForTokens`: Swaps an exact amount of input tokens. |
| 50 | +- `getAmountsIn`: Calculates how much input is needed for a given output. |
| 51 | + |
| 52 | +### 3. State Variables and Constructor |
| 53 | + |
| 54 | +Defined state variables: |
| 55 | + |
| 56 | +- `router`: Uniswap V2 router contract; in this example, it is the BlazeSwap router. |
| 57 | +- `assetManager`: Asset Manager handling FXRP redemption. |
| 58 | +- `token`: Token to swap (WCFLR in the swap path). |
| 59 | +- `swapPath`: Array of addresses defining the swap route (e.g., WCFLR → FXRP). |
| 60 | + |
| 61 | +To instantiate the contract, the constructor sets up the Uniswap router, FAssets asset manager, and swap path. |
| 62 | +It initializes the token as the first token in the swap path. |
| 63 | + |
| 64 | +### 4. Main Function: `swapAndRedeem` |
| 65 | + |
| 66 | +The `swapAndRedeem` is the core function that executes the swap and redemption flow. |
| 67 | + |
| 68 | +- **1. Validation** |
| 69 | + |
| 70 | + - Ensure the caller has sufficient WCFLR balance. |
| 71 | + - Confirm the contract has enough FXRP allowance for redemption. |
| 72 | + |
| 73 | +- **2. Transfer** |
| 74 | + |
| 75 | + - Move WCFLR from the caller to the contract. |
| 76 | + - Approve the router to spend WCFLR on behalf of the contract. |
| 77 | + |
| 78 | +- **3. Swap** |
| 79 | + |
| 80 | + - Perform the swap from WCFLR to FXRP using the specified swap path. |
| 81 | + - Apply a 10-minute deadline for the swap operation. |
| 82 | + |
| 83 | +- **4. Redemption** |
| 84 | + |
| 85 | + - Redeem the obtained FXRP through the FAssets system. |
| 86 | + - Transfer the resulting XRP to the caller's XRPL address. |
| 87 | + |
| 88 | +- **5. Helper Function: `calculateRedemptionAmountIn`** |
| 89 | + - Accept the number of FXRP lots intended for redemption. |
| 90 | + - Fetch the lot size from the FAssets asset manager and calculate the WCFLR needed for swap and redemption. |
| 91 | + |
| 92 | +## Execute the Swap and Redeem |
| 93 | + |
| 94 | +To execute the swap and redeem process, you need to deploy the smart contract instance and call the `swapAndRedeem` function. |
| 95 | + |
| 96 | +<CodeBlock language="typescript" title="scripts/fassets/swapAndRedeem.ts"> |
| 97 | + {FAssetsSwapAndRedeemScript} |
| 98 | +</CodeBlock> |
| 99 | + |
| 100 | +### Code Breakdown |
| 101 | + |
| 102 | +- **1. Dependencies and Constants** |
| 103 | + |
| 104 | + - `ASSET_MANAGER_ADDRESS`: The address of the FAssets Asset Manager contract. |
| 105 | + - `LOTS_TO_REDEEM`: The number of FAsset lots to redeem (typically set to 1). |
| 106 | + - `UNDERLYING_ADDRESS`: The XRPL address that will receive the redeemed assets. |
| 107 | + - `SWAP_ROUTER_ADDRESS`: The address of the Uniswap V2-compatible swap router. |
| 108 | + - `SWAP_PATH`: An array of token addresses defining the swap path from WCFLR to FXRP. |
| 109 | + |
| 110 | +- **2. Deploy and Verify** |
| 111 | + |
| 112 | + - Deploys the `SwapAndRedeem` contract and verifies it using [Flare Hardhat Starter Kit](/network/guides/hardhat-foundry-starter-kit). |
| 113 | + |
| 114 | +- **3. Calculate Redemption Amounts** |
| 115 | + |
| 116 | + - Calls `calculateRedemptionAmountIn` to determine the required WCFLR amount. |
| 117 | + |
| 118 | +- **4. Approve Tokens** |
| 119 | + |
| 120 | + - Uses the ERC-20 `approve` method to allow the contract to spend WCFLR. |
| 121 | + :::warning |
| 122 | + In a production environment, you should use a secure method to approve spending the tokens. |
| 123 | + ::: |
| 124 | + |
| 125 | +- **5. Execute the Swap and Redemption** |
| 126 | + - Calls `swapAndRedeem` to complete the FAssets redemption process. |
| 127 | + |
| 128 | +### Run the Script |
| 129 | + |
| 130 | +To run the script, use the [Flare Hardhat Starter Kit](/network/guides/hardhat-foundry-starter-kit) with the following command: |
| 131 | + |
| 132 | +```bash |
| 133 | +npx hardhat run scripts/fassets/swapAndRedeem.ts --network coston |
| 134 | +``` |
| 135 | + |
| 136 | +The script outputs transaction details, including swap amounts and redemption results. |
| 137 | + |
| 138 | +:::tip |
| 139 | +For an in-depth explanation of the FAssets redemption process, refer to the [FAssets Redeem Guide](/fassets/developer-guides/fassets-redeem). |
| 140 | +::: |
| 141 | + |
| 142 | +## Conclusion |
| 143 | + |
| 144 | +In this guide, you learned how to: |
| 145 | + |
| 146 | +- swap WCFLR for FXRP using Uniswap V2 compatible router (in this example, BlazeSwap); |
| 147 | +- redeem FXRP to XRP on the XRP Ledger using FAssets asset manager. |
| 148 | + |
| 149 | +The complete code is in the [Flare Hardhat Starter Kit](/network/guides/hardhat-foundry-starter-kit). |
0 commit comments