Deploying and Interacting with Smart Contracts

In this lesson, you'll learn how to deploy a smart contract you've written to a test network, making it live and accessible. We'll explore interacting with your deployed contract using a web3 wallet like MetaMask, allowing you to execute its functions and see it in action.

Learning Objectives

  • Deploy a compiled smart contract to a testnet using tools like Remix IDE or Hardhat.
  • Understand the concept of gas and how it relates to transaction costs on the blockchain.
  • Interact with a deployed smart contract using a web3 wallet (MetaMask).
  • Call functions (read and write) of a deployed smart contract.

Lesson Content

Introduction to Deployment & Test Networks

Deploying a smart contract means putting your code on the blockchain, making it accessible to anyone. However, deploying directly to the main Ethereum network can be expensive. That's where test networks (like Ropsten, Goerli, Sepolia, and Holesky) come in. These are simulated Ethereum networks that allow you to test your contracts without using real Ether. They function the same as the mainnet but use test Ether, which you can obtain for free from faucets. Before deploying anything you need to create a wallet using MetaMask or another Web3 compatible wallet.

Quick Check: What is the primary purpose of a test network?

Gas and Transaction Fees

Every action on the blockchain (including deploying and interacting with smart contracts) costs gas. Gas is a unit of measurement for the computational effort required to execute a transaction. You pay for gas using Ether. The amount of gas required depends on the complexity of your contract and the operations being performed. The price of gas fluctuates based on network congestion. Understanding gas is crucial for managing your transaction costs. Before deploying any contract, make sure you've selected a test network, and you have some test ETH in your account from faucets, which provide you with fake coins for testing.

Quick Check: What is gas used for?

Deploying with Remix IDE (Simplified Approach)

Remix IDE is a web-based IDE that simplifies the process of compiling and deploying smart contracts.

  1. Open Remix: Go to remix.ethereum.org.
  2. Create a New File: In the 'File Explorer' panel, create a new Solidity file (e.g., MyContract.sol).
  3. Write Your Contract: Paste the following simple contract:
    ```solidity
    // SPDX-License-Identifier: MIT
    pragma solidity ^0.8.0;

    contract SimpleStorage {
    uint256 public storedData;

    function set(uint256 x) public {
        storedData = x;
    }
    
    function get() public view returns (uint256) {
        return storedData;
    }
    

    }
    `` 4. **Compile the Contract:** In the 'Solidity Compiler' panel, click 'Compile MyContract.sol'. Ensure the compiler version matches your pragma statement. 5. **Deploy the Contract:** In the 'Deploy & Run Transactions' panel: * Select the 'Injected Provider - MetaMask' environment. * Connect your MetaMask wallet. Ensure you've selected a test network (e.g., Goerli). Make sure you have enough test ETH in your testnet account. * Click 'Deploy'. * MetaMask will prompt you to confirm the transaction. Review the gas fees and click 'Confirm'. 6. **Interact with the Contract:** Once the transaction is confirmed, the deployed contract will appear in the 'Deployed Contracts' section. Expand it to see the functions (set,get). You can now call these functions using the UI. Forset, enter a number and click thesetbutton to send a transaction. Forget, click thegetbutton and you will receive your output without a transaction because it is aview` function.

Note: If deploying in other environments you will need to add a private key and RPC Endpoint in MetaMask to connect your wallet.

Quick Check: Which tool simplifies the process of compiling and deploying smart contracts?

Interacting with Your Contract Using MetaMask

MetaMask acts as your gateway to interacting with deployed smart contracts. After deploying the contract in Remix, you can directly interact with it through the Remix UI (as shown above). MetaMask manages your private keys and allows you to sign transactions. For more advanced interactions (e.g., building a frontend), you'll use Web3.js or Ethers.js libraries to connect to the blockchain and call contract functions. However, we're focusing on the foundational concepts in this lesson. The Remix IDE is enough for beginners to deploy and interact.

Quick Check: Which of the following is NOT a test network?

Deployment with Hardhat (Advanced approach)

Hardhat is a development environment for Ethereum, providing a development workflow. It helps you manage your smart contracts from start to finish.

  1. Set up your environment: Create a project directory, then navigate into it and run npm install --save-dev hardhat.
  2. Create a Hardhat project: Run npx hardhat and follow the setup instructions. The basic set up will create a sample contract and deployment script.
  3. Write your Contract: The sample project has a sample Greeter.sol contract. You can create your own or deploy the SimpleStorage.sol contract above.
  4. Compile the contract: Run npx hardhat compile to compile your contract.
  5. Write a Deployment script: Hardhat has a deploy.js file, in which you can paste the contract deployment logic. You'll need to import your contract's artifacts and deploy it using a signer provided by Hardhat.
  6. Deploy to a testnet: Configure your Hardhat settings in hardhat.config.js to use your testnet (e.g. Goerli). Run npx hardhat run scripts/deploy.js --network goerli to deploy your contract to the testnet.
  7. Verify on Etherscan: Once deployed, you can verify your contract on etherscan.io, which makes your contract's source code visible.

Quick Check: What function is used to interact with a smart contract on a testnet?

Progress
0%