Skip to main content

Read FAssets Settings (Node)

Overview

In this guide, you will build a TypeScript script that connects to the Flare Testnet Coston2 and:

This guide is a perfect first step for developers working with FAssets.

Prerequisites

Project Setup

Create Project Directory

Create a new directory for your project and initialize a new npm project:

mkdir fassets-settings-ftso
cd fassets-settings-ftso
npm init -y

Install Dependencies

Install the following dependencies:

npm install --save-dev \
typescript \
typechain \
ethers \
@typechain/ethers-v6 \
@flarenetwork/flare-periphery-contract-artifacts

Configure TypeScript

Create a config file `tsconfig.json to control TypeScript behavior:

npx tsc --init

Change the tsconfig.json file so it can find the Flare generated types:

tsconfig.json
{
"compilerOptions": {
"target": "ES2020",
"module": "ESNext",
"moduleResolution": "node",
"esModuleInterop": true,
"strict": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"outDir": "./dist"
},
"include": ["scripts/**/*.ts", "typechain/**/*.ts", "typechain/**/*.d.ts"],
"exclude": ["node_modules"]
}

Update Package Configuration

For convenience, add the Typescript build and type generation commands to the scripts section of the package.json file:

"scripts": {
"build": "tsc",
"generate-types": "typechain --target ethers-v6 --out-dir typechain './node_modules/@flarenetwork/flare-periphery-contract-artifacts/coston2/artifacts/contracts/**/*.json'"
}

The build script will compile the TypeScript code.

Using the Coston2 network artifacts, the typechain generates TypeScript types from the Flare Periphery contracts, which are provided through a package containing the necessary contract artifacts.

Change the package.json file to use the module type to use ES modules and avoid issues with the import statement:

"type": "module",

Generate TypeScript Types

To generate the TypeScript types, run the following command:

npm run generate-types

It will generate the TypeScript types in the typechain directory.

Implementation

Create Script File

First, you must create a file to write the TypeScript code for this guide.

mkdir scripts
touch scripts/fassets-settings.ts

Open the scripts/fassets-settings.ts file in your favorite code editor.

Import Dependencies

Import the ethers library to interact with the blockchain:

import { ethers } from "ethers";

You need to import the Flare Contracts Registry and the FAssets asset manager contract factory types:

import { IAssetManager__factory } from "../typechain/factories/IAssetManager__factory.js";
import { IFlareContractRegistry__factory } from "../typechain/factories/IFlareContractRegistry__factory.js";

Define Constants

Define two constants:

  • COSTON2_RPC_URL: The RPC URL for the Coston2 network.
  • FLARE_CONTRACT_REGISTRY_ADDRESS: The address of the Flare Contract Registry.
const COSTON2_RPC = "https://coston-api.flare.network/ext/C/rpc";
const FLARE_CONTRACT_REGISTRY_ADDRESS =
"0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019";

Get the FAssets FXRP Asset Manager Address

You can get the FAssets FXRP Asset Manager address by calling the getAssetManagerFXRP function of the Flare Contract Registry. After that, create a new ethers provider, connect to the Coston2 network, and connect to the FAssets asset manager contract.

const provider = new ethers.JsonRpcProvider(COSTON2_RPC);

const flareContractRegistry = IFlareContractRegistry__factory.connect(
FLARE_CONTRACT_REGISTRY_ADDRESS,
provider,
);

const assetManagerAddress =
await flareContractRegistry.getContractAddressByName("AssetManagerFXRP");
const assetManager = IAssetManager__factory.connect(
assetManagerAddress,
provider,
);

Implement Settings Retrieval

Next, you must fetch the FAssets configuration settings using the getSettings function of the FAssets asset manager contract.

The last step is to get the lot size of FXRP in XRP and print it to the console.

const settings = await assetManager.getSettings();
const lotSizeFXRP =
Number(settings.lotSizeAMG) / Math.pow(10, Number(settings.assetDecimals));
console.log("Lot Size (FXRP):", lotSizeFXRP);
info

The getSettings function returns the complete asset manager settings that you can find in the FAssets Operational Parameters documentation.

Convert Lot Size to USD

To convert the lot size to USD you need to use the FTSO to get the anchor price feed of XRP/USD and convert the FXRP lot size to USD.

Import Dependencies

Import the FtsoV2Interface__factory type which is the factories for the FTSO contracts.

import { FtsoV2Interface__factory } from "../typechain/factories/FtsoV2Interface__factory.js";

Define Constants

Define the constants for the XRP/USD feed ID.

const XRP_USD_FEED_ID = "0x015852502f55534400000000000000000000000000";

Get the Price feed XRP/USD

You can get the price feed XRP/USD by calling the getFeedById function of the FtsoV2 contract.

const registry = IFlareContractRegistry__factory.connect(
FLARE_CONTRACT_REGISTRY_ADDRESS,
provider,
);
const ftsoAddress =
await flareContractRegistry.getContractAddressByName("FtsoV2");
const ftsoV2 = FtsoV2Interface__factory.connect(ftsoAddress, provider);
const priceFeed = await ftsoV2.getFeedById.staticCall(XRP_USD_FEED_ID);

Convert Lot Size to USD

Convert the lot size to USD by multiplying the lot size by the price of XRP in USD.

const xrpUsdPrice = Number(priceFeed[0]) / Math.pow(10, Number(priceFeed[1]));
const lotValueUSD = lotSizeFXRP * xrpUsdPrice;

console.log("XRP/USD Price:", xrpUsdPrice);
console.log("Lot value in USD:", lotValueUSD);
console.log("Timestamp:", priceFeed[2].toString());

Putting All Together

To put all together, you have the following code:

scripts/fassets-settings.ts
// Importing necessary modules and contract factories
import { ethers } from "ethers";
import { IAssetManager__factory } from "../typechain/factories/IAssetManager__factory.js";
import { IFlareContractRegistry__factory } from "../typechain/factories/IFlareContractRegistry__factory.js";
import { FtsoV2Interface__factory } from "../typechain/factories/FtsoV2Interface__factory.js";

// Constants for RPC endpoint and contract addresses
const COSTON2_RPC = "https://coston2-api.flare.network/ext/C/rpc"; // RPC URL for the Coston2 network
const FLARE_CONTRACT_REGISTRY_ADDRESS =
"0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019"; // Address of the Flare Contract Registry
const XRP_USD_FEED_ID = "0x015852502f55534400000000000000000000000000"; // Feed ID for XRP/USD price https://dev.flare.network/ftso/scaling/anchor-feeds

async function getSettings() {
// Create a provider for interacting with the blockchain
const provider = new ethers.JsonRpcProvider(COSTON2_RPC);

// Connect to the Flare Contract Registry
const flareContractRegistry = IFlareContractRegistry__factory.connect(
FLARE_CONTRACT_REGISTRY_ADDRESS,
provider,
);

// Get the address of the FXRP Asset Manager
const assetManagerAddress =
await flareContractRegistry.getContractAddressByName("AssetManagerFXRP");

// Connect to the FXRP Asset Manager
const assetManager = IAssetManager__factory.connect(
assetManagerAddress,
provider,
);

// Fetch settings from the Asset Manager contract
const settings = await assetManager.getSettings();

// Calculate the lot size in FXRP (Flare XRP)
const lotSizeFXRP =
Number(settings.lotSizeAMG) / Math.pow(10, Number(settings.assetDecimals));
console.log("Lot Size (FXRP):", lotSizeFXRP);

// Fetch the address of the FtsoV2 contract from the registry
const ftsoAddress =
await flareContractRegistry.getContractAddressByName("FtsoV2");

// Connect to the FtsoV2 contract
const ftsoV2 = FtsoV2Interface__factory.connect(ftsoAddress, provider);

// Fetch the XRP/USD price feed using the feed ID
const priceFeed = await ftsoV2.getFeedById.staticCall(XRP_USD_FEED_ID);

// Calculate the XRP/USD price and the lot value in USD
const xrpUsdPrice = Number(priceFeed[0]) / Math.pow(10, Number(priceFeed[1]));
const lotValueUSD = lotSizeFXRP * xrpUsdPrice;

console.log("XRP/USD Price:", xrpUsdPrice);
console.log("Lot value in USD:", lotValueUSD);
console.log("Timestamp:", priceFeed[2].toString());
}

getSettings();

Running the Script

To execute the script, run the following commands to compile the TypeScript code and run the script from the dist directory:

npm run build
node dist/scripts/fassets-settings.js

You should see the following output:

Lot Size (FXRP): 10
XRP/USD Price: 2.843861
Lot value in USD: 28.43861
Timestamp: 1756977702

Congratulations! You have built a TypeScript script that connects to the Coston2 network and retrieves the FAsset configuration settings and the price of XRP in USD.

Exploring Additional Parameters

The FAssets asset manager exposes key parameters like collateral ratios, minting fees, and liquidation thresholds via the getSettings function. See the FAssets Operational Parameters for details.

Next Steps

To continue your FAssets development journey, you can: