0

I'm using truffle and I want to use USDC token contract to send token to a address but it shows me this error when I run npx truffle migrate

Error: Cannot create instance of ERC20Mock; no code at address 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48

although the address is correct and contract has code in mainnet
here is how I use it

const Dex = artifacts.require("Dex");
const ERC20Mock = artifacts.require("ERC20Mock");

const USDC_MAINNET = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const UNLOCKED_ACCOUNT = "<THE_ACCOUNT>";

module.exports = async function (deployer, networks, accounts) {
  const usdc = await ERC20Mock.at(USDC_MAINNET);
  console.log(accounts);
  // Create Dex Contract with 10 ether from the deployer account
  await deployer.deploy(Dex, {
    from: accounts[0],
    value: "10000000000000000000",
  });

  const dex = await Dex.deployed();

  // Transfer USDC from unlocked account to Dex Contract
  await usdc.transfer(dex.address, 10000000000, {
    from: UNLOCKED_ACCOUNT,
  });

  // Transfer USDC from unlocked account to user account
  await usdc.transfer(accounts[1], 10000000000, {
    from: UNLOCKED_ACCOUNT,
  });
};

update:

Here is ERC20Mock solidity code
pragma solidity 0.6.6;

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

contract ERC20Mock is ERC20 {
  constructor() public ERC20('USDC Coin', 'USDC') {}
}

and here is the new code

const Dex = artifacts.require("Dex");
const ERC20Mock = artifacts.require("ERC20Mock");

const USDC_MAINNET = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48";
const UNLOCKED_ACCOUNT = "0x6262998ced04146fa42253a5c0af90ca02dfd2a3";

module.exports = async function (deployer, networks, accounts) {
  console.log(accounts);
  // Create Dex Contract with 10 ether from the deployer account
  await deployer.deploy(Dex, {
    from: accounts[0],
    value: "10000000000000000000",
  });
  await deployer.deploy(ERC20Mock, {
    from: accounts[0],
    value: "10000000000000000000",
  });

  const usdc = await ERC20Mock.deployed();
  const dex = await Dex.deployed();

  // Transfer USDC from unlocked account to Dex Contract
  await usdc.transfer(dex.address, 10000000000, {
    from: UNLOCKED_ACCOUNT,
  });

  // Transfer USDC from unlocked account to user account
  await usdc.transfer(accounts[1], 10000000000, {
    from: UNLOCKED_ACCOUNT,
  });
};

Here is the error I got

Error:  *** Deployment Failed ***

"ERC20Mock" hit a require or revert statement somewhere in its constructor. Try:
   * Verifying that your constructor params satisfy all require conditions.
   * Adding reason strings to your require statements.

1 Answer 1

0

The USDC contract is on Mainnet. But most probably, you are trying to deploy it on local network. Instead, copy code of USDC and deploy on you local network similar to what you are doing now. And use deployed address of USDC from local network for you use

14
  • it shows me "ERC20Mock" hit a require or revert statement somewhere in its constructor. Try: * Verifying that your constructor params satisfy all require conditions. * Adding reason strings to your require statements.
    – Yaya
    Jul 28, 2022 at 12:51
  • I added ERC20MMock code in the question
    – Yaya
    Jul 28, 2022 at 12:51
  • I tried to deploy the contract exactly like I deployed the DEX
    – Yaya
    Jul 28, 2022 at 12:52
  • @YahyaParvar can you try this await deployer.deploy(Dex, { from: accounts[0] }); Jul 28, 2022 at 13:12
  • I tried that and I got the error
    – Yaya
    Jul 28, 2022 at 13:19

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.