Created
May 7, 2024 11:43
-
-
Save arseneeth/57a7c4be1fb8ff80efe74b11ff8f78e9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Import ethers from the ethers library | |
import { ethers } from "ethers"; | |
// This is the address of the contract on Holesky deployment | |
const contractAddress = "0xd9029669bc74878bcb5be58c259ed0a277c5c16e"; | |
// ABI (Application Binary Interface) array defining the functions available in the contract | |
const contractABI = [ | |
{ | |
"inputs": [], | |
"name": "totalAssets", | |
"outputs": [ | |
{ | |
"internalType": "uint256", | |
"name": "total", | |
"type": "uint256" | |
} | |
], | |
"stateMutability": "view", | |
"type": "function" | |
} | |
]; | |
// Async function to query the totalAssets function from the smart contract | |
async function queryTotalAssets() { | |
try { | |
// Automatically determine the network and connect to MetaMask | |
const provider = new ethers.providers.Web3Provider(window.ethereum, "any"); | |
// Prompt user to connect their wallet | |
await provider.send("eth_requestAccounts", []); | |
// Create an instance of the contract | |
const contract = new ethers.Contract(contractAddress, contractABI, provider); | |
// Call the totalAssets function | |
const total = await contract.totalAssets(); | |
// Convert the result from wei to ether | |
const totalInEther = ethers.utils.formatEther(total); | |
console.log(`Total assets: ${totalInEther} ETH`); | |
} catch (error) { | |
console.error(error); | |
} | |
} | |
// Run the function | |
queryTotalAssets(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment