Skip to content

Deploy Smart Contracts With MetaMask

One of the problems is during deployment of the smart contracts, we use Ganache Accounts

In order to transfer Ether from an Account in Ganache to an account in MetaMask, we have to start a transaction. The easiest way to do this is to use the truffle console to transfer ether from one of the networks defined in the truffle-config.js file to another account. In your project root in a terminal enter:

truffle console --network development

Then a new truffle console should pop up. You should be able to list the accounts by simply typing in "accounts":

These are the same accounts as in Ganache. You are connected to your node via RPC. The node is Ganache. You can send off transactions using the private keys behind these accounts. Ganache will sign them.

We have to send a transaction from these accounts to MetaMask. Copy the account in MetaMask:

Type in:

web3.eth.sendTransaction({from: accounts[0], to:"PASTE_ACCOUNT_FROM_METAMASK", value: web3.utils.toWei("1","ether")})

don't forget the quotes around the account! It should return a transaction object:

And your account in MetaMask should have now 1 Ether, if connected to the right network. Connect MetaMask to Ganache first:

Hit Save.

Add HDWalletProvider and the Mnemonic to Truffle and modify truffle-config.js

The first step is to add the HDWalletProvider to truffle. On the command line type in:

npm install --save @truffle/hdwallet-provider

The next step is to add the hdwallet provider and the mnemonic from MetaMask to the truffle-config.js in a secure way. The best suited place for the mnemonic would be the .env file, which should never be shared!

Let's start with the HDWalletProvider. Open the truffle-config.js file and add these parts:

const path = require("path");
require('dotenv').config({path: './.env'});
const HDWalletProvider = require("@truffle/hdwallet-provider");
const MetaMaskAccountIndex = 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,
    network_id: "*",
    host: "127.0.0.1"
    },
    ganache_local: {
    provider: function() {
        return new HDWalletProvider(process.env.MNEMONIC, "http://127.0.0.1:7545", MetaMaskAccountIndex )
    },
    network_id: 5777
    }
},
compilers: {
    solc: {
    version: "0.6.1",
    }
}
};

And add the Mnemonic from MetaMask to the .env file:

Copy the Mnemonic and add it to the env-file:

Then run the migrations again with the right network and see how your smart contracts are deployed:

truffle migrate --network ganache_local

It should come up as these migrations:

Use the KycContract from your DApp using MetaMask

This was the groundwork. Next up is to actually white list an account. We could use another account in MetaMask to whitelist it.

Copy one of your accounts in MetaMask (other than your account#1) to whitelist:

Now, paste this account into the account-field in your new HTML UI:

But before sending off the transaction, make sure you switch back to Account #1 (the account that created the smart contract from truffle migrate):

You should see a popup to confirm the transaction and then an alert box, that tells you that your account is now whitelisted:

Account #2 is now whitelisted. But how to purchase Tokens?


Last update: March 28, 2022