Skip to main content

Retrieving Contract Addresses

Overview

Flare provides a registry of enshrined protocol contracts such as FtsoV2, FdcHub, and RandomNumberV2.
To ensure reliability, these contract addresses should always be retrieved dynamically via the Flare Contract Registry rather than hardcoding them. Using the registry ensures your contracts, and dApps are resistant to future upgrades and cannot be misled by off-chain or unverified sources.

Flare Contract Registry Address

The FlareContractRegistry smart contract is the only trusted source for resolving official protocol contract addresses.

The registry is deployed at the same address across all Flare networks:

ContractAddress
FlareContractRegistry0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019

Retrieval Methods

In the following code example, the RandomNumberFetcher contract retrieves the address of RandomNumberV2 using the three approaches described below.

contracts/RandomNumberFetcher.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;

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

// Demonstrates three different ways to retrieve the RandomNumberV2 contract on the Flare network.
contract RandomNumberFetcher {
// DO NOT USE IN PRODUCTION — hardcoding addresses is discouraged due to upgradeability and security risks
function getRandomNumberHardcoded()
public
view
returns (
uint256 _randomNumber,
bool _isSecureRandom,
uint256 _randomTimestamp
)
{
// Using a hardcoded address to create an interface instance (Not recommended in production)
RandomNumberV2Interface randomNumberV2 = RandomNumberV2Interface(
0x92a6E1127262106611e1e129BB64B6D8654273F7
);

return randomNumberV2.getRandomNumber();
}

// Fetches a random number by resolving the contract address via the FlareContractRegistry
function getRandomNumberViaRegistryName()
public
view
returns (
uint256 _randomNumber,
bool _isSecureRandom,
uint256 _randomTimestamp
)
{
// Instantiate the FlareContractRegistry at the known address (same across all networks)
IFlareContractRegistry flareContractRegistry = IFlareContractRegistry(
0xaD67FE66660Fb8dFE9d6b1b4240d8650e30F6019
);

// Get the address of the Random Number V2 contract
address randomNumberV2Address = flareContractRegistry
.getContractAddressByName("RandomNumberV2");

// Create an interface instance pointing to the contract address
RandomNumberV2Interface randomNumberV2 = RandomNumberV2Interface(
randomNumberV2Address
);

return randomNumberV2.getRandomNumber();
}

// Fetches the RandomNumberV2 contract using the shared ContractRegistry library
function getRandomNumberViaContractLibrary()
public
view
returns (
uint256 _randomNumber,
bool _isSecureRandom,
uint256 _randomTimestamp
)
{
// Get the RandomNumberV2 contract interface using the ContractRegistry library
RandomNumberV2Interface randomNumberV2 = ContractRegistry
.getRandomNumberV2();

return randomNumberV2.getRandomNumber();
}
}

🔴 Hardcoded Address

The getRandomNumberHardcoded function below uses a hardcoded address to create an interface instance. Not recommended in production.

warning
  • Never hardcode contract addresses in production.
  • Avoid using contract addresses obtained from unofficial sources like DMs, social media, or third-party websites.
  • Instead of passing contract addresses directly into constructors, retrieve them dynamically, as shown below.

Flare Contract Registry Smart Contract

The Flare Contract Registry smart contract exposes getContractAddressByName or getContractAddressByHash to retrieve the address of the contract.

In the code example above, the getRandomNumberViaRegistryName function uses the getContractAddressByName method to retrieve the address of RandomNumberV2.

Contract Registry Library

The Flare periphery package package includes a helper utility ContractRegistry. This library provides shorthand functions for accessing common protocol contracts, e.g., ContractRegistry.getRandomNumberV2().

In the example, the getRandomNumberViaContractLibrary function demonstrates this approach.


If the contract you are looking for does not have a shorthand method in

the ContractRegistry library, you can fall back to using getContractAddressByName or call getAllContracts to list all registered contracts. :::