Smart Contract Interaction Example
This is a short example on how Ether.js can be used to interact with Smart Contracts.
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);
}
}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();Last updated