Solidity – How is Code Working Without provider when Communicating with Blockchain?

alchemyethers.jshardhatsolidityweb3-providers

Here is the smart contract code:

// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";

interface Bucket {
    function drop(address erc20, uint amount) external;
}

contract ERCWinner {
    constructor() {}

    address AmitToken = 0x2527D2e6d78915632b408f364c94EC4F6B3729a7;
    address bucket = 0x873289a1aD6Cf024B927bd13bd183B264d274c68;
    uint x = 1;
    ERC20 token = ERC20(AmitToken);

    function drop(uint _amount) external {
        token.approve(bucket, 10);
        Bucket(bucket).drop(AmitToken, _amount);
    }

    function abc(uint amt) external returns (uint) {
        x = amt;
        return x;
    }
}

Here is the deploy.js code :

const hre = require("hardhat");
const contract = require("../artifacts/contracts/ERCWinner.sol/ERCWinner.json");

require("dotenv").config();

async function main() {
  const Erc = await hre.ethers.getContractFactory("ERCWinner");
  const erc = await Erc.deploy();

  await erc.deployed();

  console.log("address", erc.address);

  const x = (await erc.abc(2)).toString();

  console.log("sasasaaaaaaaa", x);
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Here is the hardhat config:

require("@nomicfoundation/hardhat-toolbox");
require("dotenv").config();

/** @type import('hardhat/config').HardhatUserConfig */
module.exports = {
  solidity: "0.8.19",
  networks: {
    goerli: {
      url: process.env.GOERLI_URL,
      accounts: [process.env.PRIVATE_KEY],
    },
  },
};

Now in the deploy I'm calling the abc function without using provider, how is this possible?

Best Answer

Acutally, when you running the script by hardhat command, it's automatic assigned by a provider into your erc instance contract. You can check it inside erc.provider

For example

npx hardhat run scripts/deploy.js

It auto assign a default provider for you is hardhat. You can simple check by access into console

npx hardhat console
ethers.provider

If you want to run your script in goerli, it should be

npx hardhat run scripts/deploy.js --network goerli

Additional info, if you want to access 2 different networks in one script, let's try this

const l1Provider = new ethers.providers.JsonRpcProvider(
    'url_1',
  )
  const l2Provider = new ethers.providers.JsonRpcProvider(
    'url_2',
  )
  const signer = new ethers.Wallet('wallet_private_key')
  // signer = a wallet connect to a Provider
  const l1Signer = signer.connect(l1Provider)
  const l2Signer = signer.connect(l2Provider) 

  const L1 = (await ethers.getContractFactory('L1L2Contract')).connect(l1Signer)

  const L2 = (await ethers.getContractFactory('L1L2Contract')).connect(l2Signer)

  // deplloy
  const l1 = await L1.deploy()
  await l1.deployed()
  const l2 = await L2.deploy()
  await l2.deployed()

  // call function
  await l1.abc()
  await l2.abc()

Hope it help

Related Topic