4

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?

1 Answer 1

3

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

6
  • Here: const L1 = (await ethers.getContractFactory('L1L2Contract')).connect(l1Signer) instead i do this: const L1=await ethers.getContractFactory("L1L2Contract') from which provider will it be deployed? the hardhat one?
    – amit
    Mar 6, 2023 at 4:31
  • Look at this code :gist.github.com/ameeetgaikwad/4ca2fca502cfbf34d651527057ab9bb0 in the app.js file at line 34 i've deployed the function using signer which i got from the provider and i have used contractFactory as you can see in the deploy.js file, so here it is getting deployed using the metamask provider right?. If I used getContractFactory would It had deployed using the hardhat provider?
    – amit
    Mar 6, 2023 at 4:34
  • 1
    l1'll be deployed by provider which was assigned into l1Signer. It's L1Provider. getContractFactory this came from HardhatEthersHelpers, and it receives 2 arguments, the first one is Contract name, the second one is signer and it optional argument. If you don't pass it, it's auto assigned as provider when you run command. So as I see your concept, if you use ethers.getContractFactory('ERC', signer) and ethers.ContractFactory(....). they're the same
    – CT95
    Mar 6, 2023 at 4:52
  • 1
    I'm not familiar with reactjs, but you can simply check by log from here: factory.signer.provider
    – CT95
    Mar 6, 2023 at 5:03
  • 1
    >If I don't use the 2nd argument signer then what? As I know, it should be the first signer from ethers.getSigner()
    – CT95
    Mar 6, 2023 at 5:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.