DeveloperBreeze

Ethereum Network Development Tutorials, Guides & Insights

Unlock 1+ expert-curated ethereum network tutorials, real-world code snippets, and modern dev strategies. From fundamentals to advanced topics, boost your ethereum network skills on DeveloperBreeze.

Sending Transactions and Interacting with Smart Contracts Using Infura and Ethers.js

Tutorial October 24, 2024

const ethers = require('ethers');

// Replace with your Infura Project ID
const infuraProvider = new ethers.JsonRpcProvider('https://mainnet.infura.io/v3/YOUR_INFURA_PROJECT_ID');

// Replace with your private key
const privateKey = 'YOUR_PRIVATE_KEY';

// Create a wallet instance and connect it to Infura
const wallet = new ethers.Wallet(privateKey, infuraProvider);

// Contract bytecode and ABI
const bytecode = '0xYourContractBytecode';
const abi = [
    // Your contract ABI here
];

async function deployContract() {
    try {
        // Create a ContractFactory to deploy the contract
        const factory = new ethers.ContractFactory(abi, bytecode, wallet);

        // Deploy the contract
        const contract = await factory.deploy();

        // Wait for the contract to be mined
        console.log('Contract deployed at address:', contract.address);
        await contract.deployTransaction.wait();
    } catch (error) {
        console.error('Error deploying contract:', error);
    }
}

// Call the function to deploy the contract
deployContract();
  • Bytecode and ABI: The contract bytecode is the compiled contract, and the ABI defines the contract’s interface. You need both to deploy the contract.
  • The contract will be deployed using your Infura provider and wallet, and once mined, it will return the deployed contract address.