🔮
Ethereum
  • General
    • What is Ethereum
      • Ethereum & Bitcoin General Comparison
      • Singleton State
      • The Ethereum Virtual Machine (EVM)
      • Opcodes (operation codes) EVM
      • Ethereum Client
      • Forks
      • Gas (wei)
        • EIP-1559
        • Table Conversion (wei)
      • Proof of Stake (PoS)
      • Proof of Authority (PoA)
      • The Beacon Chain
      • Networks
        • Ethereum mainnet
        • Goerli
      • Account-based model & UTXO-based model
      • Externally Owned Account (EOA)
      • Node Clients
        • Geth
        • Nethermind
      • Contract Account
      • Smart Contract Upgradeability
      • Ultrasound Money
      • Merkle Trees
        • Patricia Merkle Tree
      • Tries
        • State Trie
        • Storage Trie
        • Transactions Trie
        • Receipts Trie
      • Transactions
        • Ethereum Transaction Architecture
      • World State
        • Chain of States
        • Chain of Blocks
        • Stack of Transactions / Mempool
      • Contract Creation
      • Message Call Transaction
      • P2P Network
      • Web3.js
      • Ether.js
        • Smart Contract Interaction Example
      • Web3.js vs Ether.js
      • Node Providers
      • ENS (Ethereum Name Service)
      • Web3 dapp
      • Escrow
      • Multi-signature
      • ERC-20 tokens
        • Send ERC20s to Contracts
      • NFTs
        • ERC-721 and ERC-1155
      • Solidity
        • State Variables
        • Data Location
        • Numbers
        • Modifiers
        • View & Pure Modifiers
        • Data Types
          • Modifiers
          • Modifiers (Functions)
          • Address & Address Payable
        • Hardhat
        • Payable Functions
        • Receive Function
        • Fallback Function
        • Global Variables
        • Self Destruct
        • Create2 Function
        • Revert function
        • Require function
        • Assert Function
        • Calldata
        • Interface
        • Mapping
        • Array
        • Struct
        • Inheritance
          • Virtual & Overwrite
          • Multiple inheritance
          • Hierarchical Inheritance
        • Events
          • Indexed (keyword)
          • LOG0 - LOG4
        • Multi-signature Example
        • Smart Contracts
          • Context
      • Application Binary Interface (ABI )
  • Extras
    • Terminology
      • Bytecode
      • Keccak-256
      • Turing complete
Powered by GitBook
On this page
  1. General
  2. What is Ethereum
  3. Ether.js

Smart Contract Interaction Example

This is a short example on how Ether.js can be used to interact with Smart Contracts.

Here is a simple example of a smart contract to send or deposit ether to an address

pragma solidity ^0.8.0;

contract EtherDeposit {
    address payable public owner;
    
    constructor() {
        owner = payable(msg.sender);
    }
    
    function deposit(address payable recipient) public payable {
        require(msg.sender == owner, "Only the owner can deposit ether.");
        recipient.transfer(msg.value);
    }
}

In this contract, the owner can deposit ether to any recipient address by calling the deposit function and specifying the recipient address as a parameter. The require statement ensures that only the owner can deposit ether.

Here is an example of how the ABI can be used with Ether.js to execute the deposit function:

const { ethers } = require('ethers');

const abi = [/* insert ABI here */];
const contractAddress = '/* insert contract address here */';

const provider = new ethers.providers.JsonRpcProvider('/* insert provider URL here */');
const signer = provider.getSigner();
const contract = new ethers.Contract(contractAddress, abi, signer);

const recipientAddress = '/* insert recipient address here */';
const etherAmount = ethers.utils.parseEther('1'); // 1 ether

const depositTx = await contract.deposit(recipientAddress, { value: etherAmount });
await depositTx.wait();

In this example, we first create an Ether.js provider and signer to connect to an Ethereum node. We then create an instance of the contract using the contract's address and ABI. Finally, we call the deposit function on the contract instance, passing in the recipient address and ether amount as parameters. The ethers.utils.parseEther function is used to convert the ether amount to wei. We then wait for the transaction to be mined using the wait function.

ABI Example:

[
  { "inputs": [], "stateMutability": "nonpayable", "type": "constructor" },
  {
    "inputs": [
      {
        "internalType": "address payable",
        "name": "recipient",
        "type": "address"
      }
    ],
    "name": "deposit",
    "outputs": [],
    "stateMutability": "payable",
    "type": "function"
  },
  {
    "inputs": [],
    "name": "owner",
    "outputs": [
      {
        "internalType": "address payable",
        "name": "",
        "type": "address"
      }
    ],
    "stateMutability": "view",
    "type": "function"
  }
]
PreviousEther.jsNextWeb3.js vs Ether.js

Last updated 2 years ago