Deploying a Smart Contract on the Fuel Protocol: My First Experience

Paul
6 min readMay 30, 2023

Introduction

In this article, I will share my first experience of deploying a smart contract on the Fuel protocol. Fuel is a decentralized blockchain protocol that offers high-speed and low-cost transactions. I will walk you through the process of setting up the test network, writing and deploying the smart contract, and building a frontend interface to interact with the contract.

Using the provided guide on deploying a contract on the Fuel protocol, I was able to successfully set up the test network and deploy my own smart contract. The video that assisted me in this process can be found at the following link https://youtu.be/S6Fln4SreeQ. I highly recommend watching the video for detailed instructions and explanations on setting up the test network and deploying a contract on the Fuel protocol.

Part 1: Contract Deployment

Here’s the breakdown of the source code into sections, along with descriptions for each part:

This line simply signifies the start of the contract code and serves as a declaration that we are defining a contract.

In this section, we define the storage structure for our smart contract. The counter variable is declared as an unsigned 64-bit integer (u64) and initialized with a value of 0. This variable will be used to store the counter value.

Here, we define the ABI (Application Binary Interface) for our contract. It specifies the functions that can be called externally to interact with the contract. The Counter interface includes two functions:

  1. increment(): This function is annotated with #[storage(read, write)], indicating that it both reads from and writes to the storage. It does not require any input parameters. This function will be responsible for incrementing the value of the counter variable.
  2. count() -> u64: This function is annotated with #[storage(read)], indicating that it only reads from the storage. It does not take any parameters. It retrieves the current value of the counter variable and returns it as an unsigned 64-bit integer.

In this section, we associate the Counter interface with the main contract, Contract. This allows the contract to implement the functions specified in the interface.

  • The count() function retrieves the current value of the counter variable from storage using the try_read() method. If the value is not available (e.g., if it has not been set before), we use unwrap_or(0) to default it to 0. This function serves the purpose of reading the value of the counter variable.
  • The increment() function first retrieves the current value of the counter variable from storage using the try_read() method. We add 1 to the retrieved value and store it in a variable called incremented. Finally, we write the updated value back to the counter variable using the write() method, effectively incrementing the value.

Overall, this smart contract provides the functionality to track and increment a counter value. The increment() function increases the counter by 1, while the count() function retrieves the current value of the counter. By breaking down the code into these sections, we can clearly see the storage declaration, the ABI definition, and the implementation of the contract functions.

Part 2: Building the Frontend Interface

In this section, I will provide an overview of the frontend code that I developed to accompany my smart contract. Using React, a popular JavaScript library for building user interfaces, I created a user-friendly interface that allows users to interact with my smart contract. To connect to the blockchain and handle transactions, I integrated the Fuel Wallet SDK into my frontend code.

The purpose of the frontend is to provide a seamless and intuitive experience for users. They can easily connect their wallets, view the current value of the counter stored in the smart contract, and increment the counter by invoking the appropriate function. By developing this frontend, I aimed to make it easy for users to interact with my smart contract and benefit from its functionalities.

Throughout the following sections, I will break down the different parts of the frontend code and explain how each section contributes to the overall functionality of the application.

Import Statements

In this part, I imported the necessary modules and dependencies for the frontend application. Here’s a breakdown of the imports:

  • React is imported from the React library to enable the creation of React components.
  • useEffect and useState are imported from React to use the corresponding hooks for managing side effects and state in functional components.
  • @fuel-wallet/sdk is imported to access the Fuel Wallet SDK, which provides blockchain connectivity.
  • "./App.css" is imported to include the CSS file for styling the application.
  • { CounterContractAbi__factory } is imported from the "./contracts" file. This import is responsible for generating the contract factory that will be used to interact with the smart contract.

Contract ID

In this part, I declared a constant variable CONTRACT_ID that represents the address of the deployed contract on the Fuel testnet. This address will be used to connect to the contract and interact with its functions.

App Component

Here, I defined the main functional component App(), which represents the frontend application. It uses the useState hook to define several state variables:

  • connected is a boolean state that keeps track of the connection status to the Fuel Wallet. It is initialized as false.
  • account is a string state that stores the currently selected account address. It is initially an empty string.
  • counter is a number state that holds the current value of the counter from the smart contract. It is initially set to 0.
  • loaded is a boolean state used as a flag to indicate if the application has finished loading. It is initially set to false.

useEffect

In this part, I used the useEffect hook to perform certain actions when the component mounts or when the connected state changes. Here's what it does:

  • Inside the effect function, I used setTimeout to introduce a small delay of 200 milliseconds.
  • After the delay, I call the checkConnection() function, which verifies if the Fuel Wallet is connected.
  • Once the delay is over and the connection is verified, I set the loaded state to true.
  • If the connected state changes, indicating a successful connection, I call the getCount() function to fetch the current value of the counter from the smart contract.

This ensures that the connection is checked and the counter value is fetched when the component mounts and whenever the connected state changes.

Conclusion

In conclusion, I am proud to have successfully developed the frontend interface for my smart contract project. By leveraging React and integrating the Fuel Wallet SDK, I have created a user-friendly interface that allows users to interact with the functionalities of my smart contract seamlessly.

The frontend enables users to connect their wallets, view the current counter value stored in the smart contract, and increment the counter as desired. It provides a simple and intuitive experience, making it convenient for users to engage with my smart contract.

I am excited to share my project with others and invite you to explore the code and documentation in my GitHub repository: https://github.com/PaulZhemanov/fuel-counter-contract. Feel free to provide any feedback or suggestions for improvement.

--

--