Skip to main content

Get FXRP Asset Manager Address

Overview

When building on the Flare Network, it is important to avoid hardcoding contract addresses, especially for key components such as the FXRP Asset Manager. These addresses can change between Flare testnets and mainnet deployments, and relying on fixed values can lead to broken integrations.

Instead, you should dynamically fetch the FXRP Asset Manager address using the Flare Contract Registry, the trusted source for obtaining contract addresses on any Flare network.

In this guide, you will learn:

Sample code

To get the FAssets asset manager address, you can use the following smart contract that retrieves the Asset Manager Controller address from the Flare contract registry.

contracts/AssetManagerRegistry.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

// 1. Import the necessary interfaces and libraries
import {IFlareContractRegistry} from "@flarenetwork/flare-periphery-contracts/coston2/IFlareContractRegistry.sol";

import {IAssetManager} from "@flarenetwork/flare-periphery-contracts/coston2/IAssetManager.sol";
import {IAssetManagerController} from "@flarenetwork/flare-periphery-contracts/coston2/IAssetManagerController.sol";

import {ContractRegistry} from "@flarenetwork/flare-periphery-contracts/coston2/ContractRegistry.sol";

import {AssetManagerSettings} from "@flarenetwork/flare-periphery-contracts/coston2/userInterfaces/data/AssetManagerSettings.sol";

contract AssetManagerRegistry {
// 2. Define a constant TXRP_HASH that is the hash of the string "TXRP"
bytes32 private constant TXRP_HASH = keccak256(abi.encodePacked("TXRP"));

// 3. Implement a function that returns the FXRP Asset Manager address
function getFxrpAssetManager() public view returns (address) {
// 4. Use the ContractRegistry library to get the AssetManagerController
IAssetManagerController assetManagerController = ContractRegistry
.getAssetManagerController();

// 5. Get all the asset managers from the AssetManagerController
IAssetManager[] memory assetManagers = assetManagerController
.getAssetManagers();

// 6. Iterate over the asset managers and return the FXRP Asset Manager address
for (uint256 i = 0; i < assetManagers.length; i++) {
IAssetManager assetManager = IAssetManager(assetManagers[i]);

// 7. Get the settings of the asset manager
AssetManagerSettings.Data memory settings = assetManager
.getSettings();

// Calculate the hash of the pool token suffix
bytes32 poolTokenSuffixHash = keccak256(abi.encodePacked(poolTokenSuffix));

//8. return the address of the asset manager that has the pool token suffix "TXRP"
if (poolTokenSuffixHash == TXRP_HASH) {
return address(assetManager);
}
}
// If no asset manager is found, return the zero address
return address(0);
}
}

Contract Breakdown

  1. Imports the necessary interfaces and libraries from the Flare Periphery Contracts package.
  2. Defines a constant TXRP_HASH that is the hash of the string TXRP. This is used to identify the FXRP asset manager.
  3. Implements a function getFxrpAssetManager that returns the FXRP Asset Manager address.
  4. Uses the ContractRegistry library from the Flare Periphery Contracts package to get the Asset Manager Controller address from the Flare contract registry.
  5. Uses the IAssetManagerController interface from the Flare Periphery Contracts package to get all the asset managers from the Asset Manager Controller.
  6. Iterates over the asset managers and returns the FXRP Asset Manager address.
  7. Gets the settings of the asset manager.
  8. Checks if the pool token suffix is TXRP and if true returns the FXRP Asset Manager address.

You can find out this sample code in the Flare starter kit.

Reusable Library

To reuse this code to get the FXRP Asset Manager address in other smart contracts, you can wrap it in a reusable library. This is useful if you want to use the FXRP Asset Manager address in multiple smart contracts and avoid repeating the same code.

Next Steps

In this guide, you learned how to get the FXRP Asset Manager address for the FAssets system by interacting with the AssetManager contract using the IAssetManager interface.

You should not hardcode the FXRP Asset Manager address in your smart contracts.

To continue your FAssets development journey, you can: