Interacting With Fiefdom Smart Contracts

Interacting with smart contracts is a fundamental aspect of developing decentralized applications (DApps) on the Fiefdom network. This guide provides an overview of the methods and tools you can use to interact with deployed smart contracts, enabling you to create dynamic and responsive applications within the Fiefdom ecosystem.

Preparing for Interaction

Before interacting with smart contracts on Fiefdom, ensure you have:

  • Deployed a smart contract on the Fiefdom network or have the address of an existing contract you wish to interact with.

  • Access to a Fiefdom-compatible wallet, such as MetaMask, configured with the Fiefdom network and funded with testnet FIEF for transactions.

  • Chosen a development tool or library, like Web3.js, Ethers.js, or a frontend framework integrated with these libraries, for interacting with the blockchain.

Web3.js and Ethers.js Libraries

Web3.js and Ethers.js are JavaScript libraries that enable interaction with the Ethereum blockchain and are fully compatible with Fiefdom due to its EVM-compatibility. These libraries can be used in Node.js applications or integrated into web applications to interact with smart contracts.

  • Web3.js: A collection of modules that contain specific functionalities for the ethereum ecosystem.

  • Ethers.js: A compact library that aims to be a complete and compact library for interacting with the Ethereum Blockchain and its ecosystem.

Interacting from a Script

To interact with a smart contract using a script, follow these steps:

  1. Initialize Your Web3 or Ethers Provider: Connect to the Fiefdom network using the RPC URL provided by Fiefdom.

    • For Web3.js:

      const Web3 = require('web3');
      const web3 = new Web3('');
    • For Ethers.js:

      const { ethers } = require('ethers');
      const provider = new ethers.providers.JsonRpcProvider('');
  2. Create a Contract Instance: Use the ABI (Application Binary Interface) and the address of your deployed contract to create an instance.

    • For Web3.js:

      const contract = new web3.eth.Contract(abi, contractAddress);
    • For Ethers.js:

      const contract = new ethers.Contract(contractAddress, abi, provider);
  3. Call Contract Methods: Use the contract instance to call methods. You can call view methods directly, but for state-changing methods, you'll need to send a transaction.

    • For view methods in Web3.js:

    • For state-changing methods in Web3.js, you'll need to sign the transaction:

      contract.methods.yourMethodName().send({ from: senderAddress }).then(console.log);
    • For Ethers.js, interacting is similar, but you'll use a signer for transactions:

      // For view methods
      // For state-changing methods
      const signer = provider.getSigner();
      const contractWithSigner = contract.connect(signer);

Interacting through a Web Interface

Integrating Web3.js or Ethers.js into a web application allows users to interact with smart contracts through a user-friendly interface. Frameworks like React or Vue can be used to create dynamic DApp interfaces that communicate with smart contracts on Fiefdom.

  • Connect Wallet: Use libraries like @web3-react/core or ethers' built-in wallet connection functionalities to connect users' wallets to your DApp.

  • Read and Display Data: Call view functions from your contract to display data in your DApp.

  • Send Transactions: Create forms and buttons that allow users to send transactions to your smart contract, modifying the blockchain state.

Last updated