// Check if MetaMask is installed
if (typeof window.ethereum !== 'undefined') {
console.log('MetaMask is installed!');
}
// Request access to accounts
window.ethereum.request({ method: 'eth_requestAccounts' })
.then(accounts => {
console.log('Connected:', accounts[0]);
// Initialize Web3.js with MetaMask provider
const web3 = new Web3(window.ethereum);
// Define ABI and contract address
const abi = [...]; // Your contract's ABI
const contractAddress = '0x451B4a4DAbEDC1a8DC64200F2Fb64332082754C3'; // Your contract's address
// Get contract instance
const contract = new web3.eth.Contract(abi, contractAddress);
// Example: Call a view/pure function
contract.methods.myFunction().call({ from: accounts[0] })
.then(result => {
console.log(result); // Display result on the website
})
.catch(error => {
console.error(error);
});
// Example: Send a transaction
contract.methods.myFunction(arg1, arg2).send({ from: accounts[0], gas: 500000 })
.on('transactionHash', hash => {
console.log('Transaction Hash:', hash);
})
.on('confirmation', (confirmationNumber, receipt) => {
console.log('Confirmation Number:', confirmationNumber);
console.log('Receipt:', receipt);
})
.on('error', error => {
console.error(error);
});
})
.catch(error => {
console.error(error);
});
const Web3 = require('web3');
// Connect to Ethereum network
const web3 = new Web3('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');
// Define the contract ABI
const abi = [...]; // Your contract's ABI
// Define the contract address
const contractAddress = '0x123456789...'; // Your contract's address
// Get contract instance
const contract = new web3.eth.Contract(abi, contractAddress);
// Example: Call a view/pure function
contract.methods.myFunction().call((error, result) => {
if (!error) {
console.log(result); // Display result on the website
} else {
console.error(error);
}
});
// Example: Send a transaction
contract.methods.myFunction(arg1, arg2).send({ from: '0xYOURADDRESS', gas: 500000 })
.on('transactionHash', (hash) => {
console.log('Transaction Hash:', hash);
})
.on('confirmation', (confirmationNumber, receipt) => {
console.log('Confirmation Number:', confirmationNumber);
console.log('Receipt:', receipt);
})
.on('error', console.error);
const Web3 = require(‘web3’);
// Connect to Ethereum network
const web3 = new Web3(‘https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID’);
// Define the contract ABI
const abi = […]; // Your contract’s ABI
// Define the contract address
const contractAddress = ‘0x123456789…’; // Your contract’s address
// Get contract instance
const contract = new web3.eth.Contract(abi, contractAddress);
// Example: Call a view/pure function
contract.methods.myFunction().call((error, result) => {
if (!error) {
console.log(result); // Display result on the website
} else {
console.error(error);
}
});
// Example: Send a transaction
contract.methods.myFunction(arg1, arg2).send({ from: ‘0xYOURADDRESS’, gas: 500000 })
.on(‘transactionHash’, (hash) => {
console.log(‘Transaction Hash:’, hash);
})
.on(‘confirmation’, (confirmationNumber, receipt) => {
console.log(‘Confirmation Number:’, confirmationNumber);
console.log(‘Receipt:’, receipt);
})
.on(‘error’, console.error);