How to Build and Deploy a Smart Contract on the Fantom Blockchain

As the multi-chain ecosystem grows, blockchains like Fantom offer high throughput, low transaction costs, and instant finality—critical features for developers who want major scalability and cost-efficiency upgrades on what most layer-1 blockchains offer. Fantom is EVM compatible, so dApps and existing developer infrastructure can easily be ported and used on this increasingly popular chain.

Chainlink Price Feeds are available on Fantom mainnet and can be used to build applications on Fantom that require decentralized, tamper-proof data inputs. In this technical tutorial, we’ll walk you through how to build and deploy a smart contract on the Fantom blockchain using price data from Chainlink oracles.

What Is Fantom?

Fantom is a high-throughput blockchain with low transaction costs and instant finality. Due to its EVM compatibility, it’s used by many Ethereum developers as a way to scale their dApps. Ethereum developers can easily deploy their existing contracts with standard Ethereum developer tools while using a faster blockchain, increasing the variety of the types of dApps they are able to build.

Building With Fantom

Building and deploying contracts on Fantom is just as easy as doing so on Ethereum or other EVM-compatible chains. The first step is to choose a development framework that allows you to launch your contracts on-chain. In this tutorial we’ll be using Remix, but feel free to use any other Solidity-based tool, such as Hardhat, Brownie, or Truffle.

Building the Contract

The first thing we’ll need to do is to import the necessary Chainlink contract for price feeds, `AggregatorV3Interface.sol`, which contains the interface for retrieving data from existing pre-aggregated decentralized price feeds. To use this interface, we need to know where the price feeds are located—this detail can be found in the Chainlink Fantom Feeds documentation. We’re using the address for the FTM/USD feed, so we can simply initialize the price feed interface with that address as its only parameter when the contract is constructed, like so: `priceFeed = AggregatorV3Interface(0xe04676B9A9A2973BCb0D1478b5E1E9098BBB7f3D);`

// SPDX-License-Identifier: MIT
pragma solidity 0.8;

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

contract FantomLinkFeeds {

    AggregatorV3Interface internal priceFeed;

    /**
    * Network: Fantom Testnet
    * Aggregator: FTM/USD
    * Address: 0xe04676B9A9A2973BCb0D1478b5E1E9098BBB7f3D
    * URL: https://docs.chain.link/docs/fantom-price-feeds/
    */
    constructor() {
        priceFeed = AggregatorV3Interface(0xe04676B9A9A2973BCb0D1478b5E1E9098BBB7f3D);
    }

    /**
    * Returns the latest price
    */
    function getLatestPrice() public view returns (int) {
    (
        uint80 roundID, 
        int price,
        uint startedAt,
        uint timeStamp,
        uint80 answeredInRound
    ) = priceFeed.latestRoundData();
        return price;
    }
}

Once initialized, we can get the latest price data from the aggregator interface by calling its function, `latestRoundData()`, as seen in `getLatestPriceData()`. This returns multiple points of information about the feed, but it’s only the price we care about, so we return just that. Since the function is not modifying anything and is just reading data from the aggregator interface, it’s defined as a view function, which fortunately does not require gas.

Deploying the Contract

Deploying this code is simple and, thanks to Fantom’s EVM compatibility, there’s very little change required from the standard Ethereum deployment path. Start by compiling the contract in Remix under the Compiler tab—simply click “Compile FantomLinkFeeds.sol”. Then, proceed to the Deploy tab, set the environment to Injected Web3 (MetaMask), and configure your MetaMask for Fantom’s testnet. To do this, simply add these settings into your MetaMask networks as a “Custom RPC”.

Select Custom RPC on MetaMask
Select Custom RPC on MetaMask

Network Name: Fantom testnet

New RPC URL: https://rpc.testnet.fantom.network/

ChainID: 0xfa2

Symbol: FTM

Custom RPC Set Up
Custom RPC Set Up

Then proceed to faucet.fantom.network to retrieve some free testnet FTM for deploying your contract. For more info on this setup process, you can view the Fantom Documentation (you can also get testnet LINK from the Chainlink Faucet).

Now that the contract has been compiled, your network is set to Fantom testnet, and your address is funded with testnet FTM, you can deploy to the network simply by selecting the `FantomLinkFeeds` contract and clicking Deploy. Your contract is now live on Fantom testnet and ready to consume real-world data using Chainlink.

Deploying to Fantom
Deploying to Fantom

Simply call the `getLatestPrice` function and you’ll see the FTM/USD response returned with eight decimals of precision, the value here being $2.66.

With a simple import, a constructor that initializes the feed, and a single function to read the price data—all without paying any gas—you have empowered your smart contract with Chainlink’s industry-standard price data.

Summary

Fantom is an attractive prospect for developers coming from the Ethereum-based ecosystem due to its combination of EVM compatibility, high speed, and low cost. When combined with Chainlink decentralized oracle networks, Fantom becomes even more powerful, with developers able to use off-chain data and events. Chainlink Price Feeds offer high-quality aggregated price data that can be used in all kinds of useful applications, such as money markets, algorithmic stablecoins, derivatives, prediction markets, and asset management platforms.

Now that you know how to build and deploy smart contracts with Fantom, you can explore the chain in more depth or work with other EVM-compatible chains, such as Polygon, xDai, or other sidechains and layer-2 solutions which use the same tooling we used in this tutorial. If you prefer Hardhat, Brownie, or Truffle, try using one of the Chainlink Starter Kits to kickstart your development journey.

Learn more about Chainlink by visiting chain.link or read the documentation at docs.chain.link. To discuss an integration, reach out to an expert.

Need Integration Support?
Talk to an expert
Faucets
Get testnet tokens
Read the Docs
Technical documentation