# Interacting with Smart Contracts Using ethers.js Learn how to interact with smart contracts deployed on **Status Network** from a frontend using **ethers.js**. In this tutorial, we'll walk through setting up a basic web application, deploying a smart contract on the **Status Network Testnet**, and interacting with it using **ethers.js**. --- ## Prerequisites Before you begin, ensure you have the following: - **MetaMask** installed in your browser. - **Node.js** and **npm** installed on your computer. - Basic understanding of **React.js** and how **blockchains** work. ### Set Up Status Network Testnet 1. **Add Status Network Testnet to MetaMask**: - Follow the guide [Adding Status Network to MetaMask](/general-info/add-status-network) to add the testnet network to your wallet. 2. **Obtain Test ETH**: - Use the [Status Network Testnet Faucet](/tools/testnet-faucets) to get test ETH for deploying and interacting with contracts. 3. **Bridge Assets (Optional)**: - If needed, bridge assets to the Status Network Testnet using the [Testnet Bridge](/general-info/bridge/bridging-testnet). --- ## Smart Contract Deployment on Status Network We'll use a simple smart contract called **BidBoard** for this tutorial. The contract allows advertisers to bid for space on an advertising board. ### BidBoard Smart Contract Here's the `BidBoard.sol` contract code: ```solidity // SPDX-License-Identifier: MIT pragma solidity ^0.8.0; contract BidBoard { string public currentAd; address public advertiser; uint256 public currentBid; event AdUpdated(string newAd, address newAdvertiser, uint256 newBid); constructor() { currentAd = "Welcome to Status Network!"; advertiser = msg.sender; currentBid = 0; } function updateAd(string memory newAd) public payable { require(msg.value > currentBid, "Bid must be higher than current bid"); currentAd = newAd; advertiser = msg.sender; currentBid = msg.value; emit AdUpdated(newAd, msg.sender, msg.value); } function getCurrentAd() public view returns (string memory, address, uint256) { return (currentAd, advertiser, currentBid); } } ``` > **Note:** You can use your own smart contract if you prefer. ### Deploying the Contract Follow the guide [Deploying a Smart Contract Using Remix](/tutorials/deploying-contracts/using-remix) to deploy the `BidBoard.sol` contract to the Status Network Testnet. --- ## Setting Up the Frontend Application We'll create a React.js application to interact with the deployed smart contract. ### Step 1: Create a React App Open your terminal and run: ```bash npx create-react-app bidboard-ui ``` This command creates a new React application named `bidboard-ui`. ### Step 2: Install ethers.js Navigate to the project directory and install **ethers.js**: ```bash cd bidboard-ui npm install ethers ``` --- ## Building the Main Application Component Open the project in your code editor and navigate to `src/App.js`. ### Import Required Libraries Replace the content of `App.js` with the following code: ```jsx import React, { useState, useEffect } from "react"; import { ethers } from "ethers"; import "./App.css"; const App = () => { // Contract details const contractAddress = "YOUR_CONTRACT_ADDRESS"; const abi = [/* ABI JSON CODE */]; // State variables const [currentAd, setCurrentAd] = useState(""); const [currentBid, setCurrentBid] = useState(0); const [advertiser, setAdvertiser] = useState(""); const [newAd, setNewAd] = useState(""); const [bidAmount, setBidAmount] = useState(""); const [provider, setProvider] = useState(null); const [status, setStatus] = useState(""); // Rest of the code... }; export default App; ``` > **Important:** Replace `"YOUR_CONTRACT_ADDRESS"` with the address of your deployed `BidBoard` contract. Paste the ABI of your contract in the `abi` array. ### Setting Up the Provider Add the following code inside the `App` component to set up the provider: ```jsx useEffect(() => { if (typeof window.ethereum !== "undefined") { const newProvider = new ethers.providers.Web3Provider(window.ethereum); setProvider(newProvider); } else { console.error("Please install MetaMask!"); } }, []); ``` ### Fetch Current Advertisement Data Add a function to fetch the current advertisement data: ```jsx const fetchCurrentAd = async () => { try { const contract = new ethers.Contract(contractAddress, abi, provider); const adData = await contract.getCurrentAd(); setCurrentAd(adData[0]); setAdvertiser(adData[1]); setCurrentBid(ethers.utils.formatEther(adData[2])); } catch (error) { console.error("Error fetching current ad:", error); } }; useEffect(() => { if (provider) { fetchCurrentAd(); } }, [provider]); ``` ### Submit a New Bid Add a function to submit a new bid: ```jsx const submitBid = async () => { if (!newAd || !bidAmount) { setStatus("Please enter an ad message and bid amount."); return; } try { const signer = provider.getSigner(); const contract = new ethers.Contract(contractAddress, abi, signer); const tx = await contract.updateAd(newAd, { value: ethers.utils.parseEther(bidAmount), }); setStatus("Transaction sent, waiting for confirmation..."); await tx.wait(); setStatus("Transaction confirmed!"); setNewAd(""); setBidAmount(""); fetchCurrentAd(); } catch (err) { console.error(err); setStatus("Error: " + err.message); } }; ``` ### Listen to Contract Events Add code to listen to the `AdUpdated` event: ```jsx useEffect(() => { let contract; const setupEventListener = async () => { if (provider) { contract = new ethers.Contract(contractAddress, abi, provider); contract.on("AdUpdated", (newAd, newAdvertiser, newBid) => { setCurrentAd(newAd); setAdvertiser(newAdvertiser); setCurrentBid(ethers.utils.formatEther(newBid)); }); } }; setupEventListener(); return () => { if (contract) { contract.removeAllListeners("AdUpdated"); } }; }, [provider]); ``` --- ## Creating the User Interface Update the `return` statement in the `App` component: ```jsx return (
Status: {status}
"{currentAd}"
Advertiser: {advertiser}
Current Bid: {currentBid} ETH