Skip to content

Instantly share code, notes, and snippets.

@bthaile
Created February 23, 2024 21:24
Show Gist options
  • Save bthaile/6519fe81b278357d9907ed691384b1da to your computer and use it in GitHub Desktop.
Save bthaile/6519fe81b278357d9907ed691384b1da to your computer and use it in GitHub Desktop.
GetGreeting
// Import ethers from the ethers.js library
const { ethers } = require('ethers');
// Define the contract ABI
const contractABI = [
"function hello() public view returns (string memory)"
];
// Define the contract address
const contractAddress = "0x3e6559ef912754b7cb75eaf7f265bdedebb6a437";
// Connect to the Ethereum network
// This example uses the default provider from ethers.js, which connects to the Ethereum mainnet.
// For a testnet or custom RPC, use ethers.getDefaultProvider('networkName') or new ethers.providers.JsonRpcProvider(url)
const provider = new ethers.providers.Web3Provider(window?.ethereum);
// Create a new contract instance
const contract = new ethers.Contract(contractAddress, contractABI, provider);
// Call the hello function of the contract
async function getGreeting() {
const greeting = await contract.hello();
console.log(greeting);
}
// Execute the function
getGreeting();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment