Skip to content

Commit f3b9230

Browse files
committed
refactor(docs): add helper functions and update asset manager retrieval in FAssets swap and redeem documentation
1 parent 8c1dd07 commit f3b9230

File tree

3 files changed

+75
-64
lines changed

3 files changed

+75
-64
lines changed

docs/fassets/developer-guides/8-fassets-swap-redeem.mdx

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,15 @@ The `swapAndRedeem` is the core function that executes the swap and redemption f
8585
- Accept the number of FXRP lots intended for redemption.
8686
- Fetch the lot size from the FAssets asset manager and calculate the WCFLR needed for swap and redemption.
8787

88+
### 5. Helper Function: `calculateRedemptionAmountIn`
89+
90+
This function calculates the amount of WCFLR needed to swap to FXRP and redeem.
91+
92+
### 6. Helper Function: `_redeem`
93+
94+
- Redeem the obtained FXRP through the FAssets system.
95+
- Transfer the resulting XRP to the caller's XRPL address.
96+
8897
## Execute the Swap and Redeem
8998

9099
To execute the swap and redeem process, you need to deploy the smart contract instance and call the `swapAndRedeem` function.
@@ -96,11 +105,10 @@ To execute the swap and redeem process, you need to deploy the smart contract in
96105
### Code Breakdown
97106

98107
- **1. Dependencies and Constants**
99-
- `ASSET_MANAGER_ADDRESS`: The address of the FAssets Asset Manager contract.
100108
- `LOTS_TO_REDEEM`: The number of FAsset lots to redeem (typically set to 1).
101109
- `UNDERLYING_ADDRESS`: The XRPL address that will receive the redeemed assets.
102110
- `SWAP_ROUTER_ADDRESS`: The address of the Uniswap V2-compatible swap router.
103-
- `SWAP_PATH`: An array of token addresses defining the swap path from WCFLR to FXRP.
111+
- `WC2FLR`: Wrapped CFLR token address.
104112

105113
- **2. Deploy and Verify**
106114
- Deploys the `SwapAndRedeem` contract and verifies it using [Flare Hardhat Starter Kit](/network/guides/hardhat-foundry-starter-kit).

examples/developer-hub-javascript/fassetsSwapAndRedeem.ts

Lines changed: 52 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -4,68 +4,70 @@ import { run } from "hardhat";
44
import { SwapAndRedeemInstance } from "../../typechain-types";
55
import { ERC20Instance } from "../../typechain-types/@openzeppelin/contracts/token/ERC20/ERC20";
66

7-
// AssetManager address on Songbird Testnet Coston network
8-
const ASSET_MANAGER_ADDRESS = "0x56728e46908fB6FcC5BCD2cc0c0F9BB91C3e4D34";
7+
import { getAssetManagerFXRP } from "../utils/getters";
8+
9+
// yarn hardhat run scripts/fassets/swapAndRedeem.ts --network coston2
10+
911
const LOTS_TO_REDEEM = 1;
1012
const UNDERLYING_ADDRESS = "rSHYuiEvsYsKR8uUHhBTuGP5zjRcGt4nm";
11-
// BlazeSwap router address on Songbird Testnet Coston network
12-
const SWAP_ROUTER_ADDRESS = "0xf0D01450C037DB2903CF5Ff638Dd1e2e6B0EEDF4";
13-
const SWAP_PATH = [
14-
"0x767b25A658E8FC8ab6eBbd52043495dB61b4ea91", // WCFLR
15-
"0x36be8f2e1CC3339Cf6702CEfA69626271C36E2fd", // FXRP
16-
];
13+
14+
// BlazeSwap router address on Flare Testnet Coston2 network
15+
const SWAP_ROUTER_ADDRESS = "0x8D29b61C41CF318d15d031BE2928F79630e068e6";
16+
const WC2FLR = "0xC67DCE33D7A8efA5FfEB961899C73fe01bCe9273";
17+
18+
const SwapAndRedeem = artifacts.require("SwapAndRedeem");
1719

1820
// 2. Deploy and verify the `SwapAndRedeem` smart contract
1921
async function deployAndVerifyContract() {
20-
const SwapAndRedeem = artifacts.require("SwapAndRedeem");
21-
const args = [SWAP_ROUTER_ADDRESS, ASSET_MANAGER_ADDRESS, SWAP_PATH];
22-
const swapAndRedeem: SwapAndRedeemInstance = await SwapAndRedeem.new(...args);
22+
const assetManager = await getAssetManagerFXRP();
23+
const fassetAddress = await assetManager.fAsset();
24+
const swapPath = [WC2FLR, fassetAddress];
25+
26+
const args = [SWAP_ROUTER_ADDRESS, swapPath];
27+
const swapAndRedeem: SwapAndRedeemInstance = await SwapAndRedeem.new(...args);
2328

24-
const fassetsSwapAndRedeemAddress = await swapAndRedeem.address;
29+
const fassetsSwapAndRedeemAddress = await swapAndRedeem.address;
2530

26-
try {
27-
await run("verify:verify", {
28-
address: fassetsSwapAndRedeemAddress,
29-
constructorArguments: args,
30-
});
31-
} catch (e) {
32-
console.log(e);
33-
}
31+
try {
32+
await run("verify:verify", {
33+
address: fassetsSwapAndRedeemAddress,
34+
constructorArguments: args,
35+
});
36+
} catch (e: any) {
37+
console.log(e);
38+
}
3439

35-
console.log("FAssetsSwapAndRedeem deployed to:", fassetsSwapAndRedeemAddress);
40+
console.log("FAssetsSwapAndRedeem deployed to:", fassetsSwapAndRedeemAddress);
3641

37-
return swapAndRedeem;
42+
return swapAndRedeem;
3843
}
3944

4045
async function main() {
41-
// 2. Deploy and verify the `SwapAndRedeem` smart contract
42-
const swapAndRedeem: SwapAndRedeemInstance = await deployAndVerifyContract();
43-
44-
// 3. Calculate Required Amounts
45-
const swapAndRedeemAddress = await swapAndRedeem.address;
46-
const amounts =
47-
await swapAndRedeem.calculateRedemptionAmountIn(LOTS_TO_REDEEM);
48-
const amountIn = amounts.amountIn;
49-
const amountOut = amounts.amountOut;
50-
console.log("Amount of tokens out (FXRP): ", amountOut.toString());
51-
console.log("Amount of tokens in (WCFLR): ", amountIn.toString());
52-
53-
// 4. Approve spending WCFLR tokens
54-
const ERC20 = artifacts.require("ERC20");
55-
const wcflr: ERC20Instance = await ERC20.at(SWAP_PATH[0]);
56-
57-
const approveTx = await wcflr.approve(swapAndRedeemAddress, amountOut);
58-
console.log("Approve transaction: ", approveTx);
59-
60-
// 5. Swap WCFLR for FXRP and redeem to underlying XRP token on XRP Ledger
61-
const swapResult = await swapAndRedeemAddress.swapAndRedeem(
62-
LOTS_TO_REDEEM,
63-
UNDERLYING_ADDRESS,
64-
);
65-
console.log("Swap and redeem transaction: ", swapResult);
46+
// 2. Deploy and verify the `SwapAndRedeem` smart contract
47+
const swapAndRedeem: SwapAndRedeemInstance = await deployAndVerifyContract();
48+
49+
// 3. Calculate Required Amounts
50+
const swapAndRedeemAddress = await swapAndRedeem.address;
51+
const amounts = await swapAndRedeem.calculateRedemptionAmountIn(LOTS_TO_REDEEM);
52+
const amountIn = amounts.amountIn;
53+
const amountOut = amounts.amountOut;
54+
console.log("Amount of tokens out (FXRP): ", amountOut.toString());
55+
console.log("Amount of tokens in (WCFLR): ", amountIn.toString());
56+
57+
// 4. Approve spending WCFLR tokens
58+
// Get WCFLR token
59+
const ERC20 = artifacts.require("ERC20");
60+
const wcflr: ERC20Instance = await ERC20.at(WC2FLR);
61+
62+
const approveTx = await wcflr.approve(swapAndRedeemAddress, amountOut);
63+
console.log("Approve transaction: ", approveTx);
64+
65+
// 5. Swap WCFLR for FXRP and redeem to underlying XRP token on XRP Ledger
66+
const swapResult = await swapAndRedeem.swapAndRedeem(LOTS_TO_REDEEM, UNDERLYING_ADDRESS);
67+
console.log("Swap and redeem transaction: ", swapResult);
6668
}
6769

68-
main().catch((error) => {
69-
console.error(error);
70-
process.exitCode = 1;
70+
main().catch(error => {
71+
console.error(error);
72+
process.exitCode = 1;
7173
});

examples/developer-hub-solidity/FAssetsSwapAndRedeem.sol

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ pragma solidity ^0.8.25;
44
// 1. Required Imports
55
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
66

7-
import {IAssetManager} from "@flarenetwork/flare-periphery-contracts/coston/IAssetManager.sol";
8-
import {AssetManagerSettings} from "@flarenetwork/flare-periphery-contracts/coston/userInterfaces/data/AssetManagerSettings.sol";
7+
import {ContractRegistry} from "@flarenetwork/flare-periphery-contracts/coston2/ContractRegistry.sol";
8+
import {IAssetManager} from "@flarenetwork/flare-periphery-contracts/coston2/IAssetManager.sol";
9+
import {AssetManagerSettings} from "@flarenetwork/flare-periphery-contracts/coston2/data/AssetManagerSettings.sol";
910

1011
// 2. Interfaces
1112

@@ -33,26 +34,23 @@ contract SwapAndRedeem {
3334

3435
// Uniswap V2 Router interface to communicate with BlazeSwap
3536
ISwapRouter public immutable router;
36-
// FAssets asset manager interface
37-
IAssetManager public immutable assetManager;
3837
// FAssets token (FXRP)
3938
IERC20 public immutable token;
4039

4140
// Path to swap WCFLR for FXRP
4241
address[] public swapPath;
4342

44-
constructor(
45-
address _router,
46-
address _assetManager,
47-
address[] memory _swapPath
48-
) {
43+
constructor(address _router, address[] memory _swapPath) {
4944
router = ISwapRouter(_router);
50-
assetManager = IAssetManager(_assetManager);
5145
swapPath = _swapPath;
5246

5347
token = IERC20(_swapPath[0]);
5448
}
5549

50+
function getAssetManager() public view returns (IAssetManager) {
51+
return ContractRegistry.getAssetManagerFXRP();
52+
}
53+
5654
// 4. Main Function: `swapAndRedeem`
5755

5856
// Swap WCFLR for FXRP and redeem FAssets
@@ -138,7 +136,8 @@ contract SwapAndRedeem {
138136
function calculateRedemptionAmountIn(
139137
uint256 _lots
140138
) public view returns (uint256 amountOut, uint256 amountIn) {
141-
AssetManagerSettings.Data memory settings = assetManager.getSettings();
139+
AssetManagerSettings.Data memory settings = getAssetManager()
140+
.getSettings();
142141
uint256 lotSizeAMG = settings.lotSizeAMG;
143142

144143
// Calculate the amount of WCFLR needed to swap to FXRP
@@ -150,6 +149,8 @@ contract SwapAndRedeem {
150149
return (amounts[0], amounts[1]);
151150
}
152151

152+
// 6. Helper Function: `_redeem`
153+
153154
// Redeem FAssets from FXRP to the redeemer's underlying XRPL address
154155
// @param _lots: number of lots to redeem
155156
// @param _redeemerUnderlyingAddressString: redeemer underlying address string (XRP address)
@@ -159,7 +160,7 @@ contract SwapAndRedeem {
159160
string memory _redeemerUnderlyingAddressString
160161
) internal returns (uint256) {
161162
return
162-
assetManager.redeem(
163+
getAssetManager().redeem(
163164
_lots,
164165
_redeemerUnderlyingAddressString,
165166
// The account that is allowed to execute redemption default (besides redeemer and agent).

0 commit comments

Comments
 (0)