1

I'm trying to create an ERC20 token using truffle and upgradable openzeppelin contracts. The problem is that the token balance is zero when I add the contract address into the Metamask extension. When I use remix, Everything is Ok and I can see the correct balance of the deployed contract in the Metamask. But I don't know where is the problem when I use it in localhost using Truffle and Ganache.

Token.sol:

pragma solidity ^0.8.2;

import "@openzeppelin/contracts-upgradeable/token/ERC20/ERC20Upgradeable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/extensions/ERC20SnapshotUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/security/PausableUpgradeable.sol";
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";

contract Token is
  Initializable,
  ERC20Upgradeable,
  ERC20SnapshotUpgradeable,
  OwnableUpgradeable,
  PausableUpgradeable
{
  /// @custom:oz-upgrades-unsafe-allow constructor
  constructor() initializer {}

  function initialize() public initializer {
    __ERC20_init("Token", "TKN");
    __ERC20Snapshot_init();
    __Ownable_init();
    __Pausable_init();

    _mint(msg.sender, 10000000000 * 18**decimals());
  }

  function snapshot() public onlyOwner {
    _snapshot();
  }

  function pause() public onlyOwner {
    _pause();
  }

  function unpause() public onlyOwner {
    _unpause();
  }

  function _beforeTokenTransfer(
    address from,
    address to,
    uint256 amount
  )
    internal
    override(ERC20Upgradeable, ERC20SnapshotUpgradeable)
    whenNotPaused
  {
    super._beforeTokenTransfer(from, to, amount);
  }
}

truffle-config.js:

networks: {
    development: {
      host: "localhost",
      port: 7545,
      network_id: "*",
    },
    //  test: {
    //    host: "127.0.0.1",
    //    port: 7545,
    //    network_id: "*"
    //  }
  },

Metamask network config: Metamask config

TKN token balance: enter image description here

Video: https://youtu.be/HHWsnlJxZ7Y

Milad Jafari
  • 1,135
  • 1
  • 11
  • 28

2 Answers2

2

This code might help you i, since i have used tuffle and ganache to connect metamask locally. make sure you are using truffle hdwallet provider HERE

const path = require("path");
require("dotenv").config({path: "./.env"});
const HDWalletProvider = require("@truffle/hdwallet-provider");
const AccountIndex = 0;

module.exports = {
  // See <http://truffleframework.com/docs/advanced/configuration>
  // to customize your Truffle configuration!
  contracts_build_directory: path.join(__dirname, "client/src/contracts"),
  networks: {
    development: {
      port: 7545,
      host: "127.0.0.1",
      network_id: 5777
    },
    ganache_local: {
      provider: function() {
        return new HDWalletProvider(process.env.MNEMONIC, "http://127.0.0.1:7545", AccountIndex)
      },
      network_id: 5777
    },
    goerli_infura: {
      provider: function() {
        return new HDWalletProvider(process.env.MNEMONIC, "https://goerli.infura.io/v3/ffa60321cbbd4cfda3352014c556e3c3", AccountIndex)
      },
      network_id: 5
    },
    ropsten_infura: {
      provider: function() {
        return new HDWalletProvider(process.env.MNEMONIC, "https://ropsten.infura.io/v3/ffa60321cbbd4cfda3352014c556e3c3", AccountIndex)
      },
      network_id: 3
    }
  },
  compilers: {
    solc: {
      version: "0.6.1"
    }
  }
};
Tanjin Alam
  • 1,728
  • 13
  • 15
2

You must import the ganache accounts[0] to your Metamask.

When you are deploying with Remix and Metamask, deployer is your Metamask connected account, and when you are deploying initialize function being called and msg.sender is your connected account and 10000000000 token mint and transfer to that account and you can see it on your Metamask

But, when you are deploying with Truffle to your local ganache, you must get account 0 private key from ganache and import to Metamask to see that token in your wallet. If you are not provided mnemonic when running ganache, deployer(accounts[0]) in ganache every time is a newly generated address.

see this link: https://docs.nethereum.com/en/latest/ethereum-and-clients/ganache-cli/ check these flags -a accounts -m mnemonic

Amir Habibzadeh
  • 437
  • 2
  • 11
  • 1
    Hey @amir-habibzadeh , thanks for your response. I have done all these steps. But the issue was not solved. I uploaded a 30-second video. Please watch this video: [https://youtu.be/HHWsnlJxZ7Y](https://youtu.be/HHWsnlJxZ7Y) (Video link was added to the question). Thank you so much. – Milad Jafari Feb 15 '22 at 11:57