DeveloperBreeze

Blockchain Development Programming Tutorials, Guides & Best Practices

Explore 30+ expertly crafted blockchain development tutorials, components, and code examples. Stay productive and build faster with proven implementation strategies and design patterns from DeveloperBreeze.

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

Tutorial October 24, 2024

Deploying smart contracts is a more advanced topic, but here’s an example of how you can use Ethers.js with Infura to deploy a smart contract:

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();