Files
hoelee be54471760
CI / Foundry project (push) Has been cancelled
Added front-end Javascript
2024-08-17 12:19:13 +08:00

72 lines
2.4 KiB
JavaScript

const { ethers } = require("ethers"); // yarn add ethers
async function connect() {
if (typeof window.ethereum !== "undefined") {
await ethereum.request({ method: "eth_requestAccounts" }).then((accounts) => {
console.log("Connected account: " + accounts);
});
}
}
async function storeNumber() {
/*
1. Get the contract address
2. Get the contract ABI
3. Confirm which function what to call
4. Node connection - MetaMask
*/
const contractAddress = "0x5F109D5Cea7BD7345EFcC1b7b7Bb7FB2693e5Fb2";
const abi = [
{
inputs: [
{ internalType: "string", name: "_name", type: "string" },
{ internalType: "uint256", name: "_favoriteNumber", type: "uint256" },
],
stateMutability: "nonpayable",
type: "function",
name: "addPerson",
},
{
inputs: [{ internalType: "uint256", name: "", type: "uint256" }],
stateMutability: "view",
type: "function",
name: "listOfPeople",
outputs: [
{ internalType: "uint256", name: "favoriteNumber", type: "uint256" },
{ internalType: "string", name: "name", type: "string" },
],
},
{
inputs: [{ internalType: "string", name: "", type: "string" }],
stateMutability: "view",
type: "function",
name: "nameToFavoriteNumber",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
},
{
inputs: [],
stateMutability: "view",
type: "function",
name: "retrieve",
outputs: [{ internalType: "uint256", name: "", type: "uint256" }],
},
{
inputs: [{ internalType: "uint256", name: "_favoriteNumber", type: "uint256" }],
stateMutability: "nonpayable",
type: "function",
name: "store",
},
];
const provider = new ethers.providers.Web3Provider(window.ethereum);
// const provider = new ethers.providers.Web3Provider("http://127.0.0.1:8545"); // Anvil
const signer = provider.getSigner(); // Get connected account
const contract = new ethers.Contract(contractAddress, abi, signer);
await contract.store(12);
}
module.exports = {
connect,
storeNumber,
retrieve,
};