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 \
viem \
@flarenetwork/flare-wagmi-periphery-package

Configure TypeScript

Create a tsconfig.json file:

npx tsc --init

Update tsconfig.json as follows:

tsconfig.json
{
"compilerOptions": {
"rootDir": "./scripts",
"outDir": "./dist",
"module": "esnext",
"moduleResolution": "node",
"target": "esnext",
"lib": ["dom", "dom.iterable", "esnext"],
"types": ["node"],
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true
},
"include": ["scripts/**/*.ts"],
"exclude": ["node_modules"]
}

Update Package Configuration

Change package.json to use ES modules and add a build script:

"type": "module",
"scripts": {
"build": "tsc"
}

Implementation

Create Script File

mkdir scripts
touch scripts/fassets-settings.ts

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

Import Dependencies

Import viem to interact with the blockchain and the coston2 namespace from the @flarenetwork/flare-wagmi-periphery-package, which provides all typed contract ABIs for the Coston2 network:

import { createPublicClient, http } from "viem";
import { flareTestnet } from "viem/chains";
import { coston2 } from "@flarenetwork/flare-wagmi-periphery-package";

Define Constants

const FLARE_CONTRACT_REGISTRY_ADDRESS =
"0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019" as const;
const XRP_USD_FEED_ID = "0x015852502f55534400000000000000000000000000" as const;

Create a Client

Create a viem public client connected to Coston2:

const client = createPublicClient({
chain: flareTestnet,
transport: http(),
});

Get the FAssets FXRP Asset Manager Address

Resolve the FXRP Asset Manager address from the Flare Contract Registry:

const assetManagerAddress = await client.readContract({
address: FLARE_CONTRACT_REGISTRY_ADDRESS,
abi: coston2.iFlareContractRegistryAbi,
functionName: "getContractAddressByName",
args: ["AssetManagerFXRP"],
});

Implement Settings Retrieval

Fetch the FAssets configuration settings using the getSettings function and calculate the FXRP lot size:

const settings = await client.readContract({
address: assetManagerAddress,
abi: coston2.iAssetManagerAbi,
functionName: "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, fetch the XRP/USD anchor price feed from the FTSO.

Get the FtsoV2 Address

const ftsoAddress = await client.readContract({
address: FLARE_CONTRACT_REGISTRY_ADDRESS,
abi: coston2.iFlareContractRegistryAbi,
functionName: "getContractAddressByName",
args: ["FtsoV2"],
});

Get the Price Feed

getFeedById is a payable function, so use simulateContract to call it without sending a transaction:

const {
result: [_value, _decimals, _timestamp],
} = await client.simulateContract({
address: ftsoAddress,
abi: coston2.ftsoV2InterfaceAbi,
functionName: "getFeedById",
args: [XRP_USD_FEED_ID],
value: 0n,
});

Convert Lot Size to USD

const xrpUsdPrice = Number(_value) / Math.pow(10, Number(_decimals));
const lotValueUSD = lotSizeFXRP * xrpUsdPrice;

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

Putting All Together

scripts/fassets-settings.ts
import { createPublicClient, http } from "viem";
import { flareTestnet } from "viem/chains";
import { coston2 } from "@flarenetwork/flare-wagmi-periphery-package";

const FLARE_CONTRACT_REGISTRY_ADDRESS =
"0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019" as const;
const XRP_USD_FEED_ID = "0x015852502f55534400000000000000000000000000" as const;

const client = createPublicClient({
chain: flareTestnet,
transport: http(),
});

const assetManagerAddress = await client.readContract({
address: FLARE_CONTRACT_REGISTRY_ADDRESS,
abi: coston2.iFlareContractRegistryAbi,
functionName: "getContractAddressByName",
args: ["AssetManagerFXRP"],
});

const settings = await client.readContract({
address: assetManagerAddress,
abi: coston2.iAssetManagerAbi,
functionName: "getSettings",
});

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

const ftsoAddress = await client.readContract({
address: FLARE_CONTRACT_REGISTRY_ADDRESS,
abi: coston2.iFlareContractRegistryAbi,
functionName: "getContractAddressByName",
args: ["FtsoV2"],
});

const {
result: [_value, _decimals, _timestamp],
} = await client.simulateContract({
address: ftsoAddress,
abi: coston2.ftsoV2InterfaceAbi,
functionName: "getFeedById",
args: [XRP_USD_FEED_ID],
value: 0n,
});

const xrpUsdPrice = Number(_value) / Math.pow(10, Number(_decimals));
const lotValueUSD = lotSizeFXRP * xrpUsdPrice;

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

Running the Script

npm run build
node dist/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

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.

Summary

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.

What's next

To continue your FAssets development journey, you can: