Metamask: Deploy contract using second address in wallet using hardhat

Here is an article on deploying a contract using the second address in your Metamask Wallet with Hardhat:

Deploying a Contract Using the Second Address in Your Metamask Wallet With Hardhat

When developing smart contracts, it is essential to keep track of multiple addresses and their associated funds. One Common Scenario Involves Having Multiple Wallets, Each Storing Different Accounts or Assets. A contract created with hardhat.

Understanding Hardhat Config

Before diving into deploying contracts, crucial to understand the configuration file used by hardhat for development and production environment. For Local Development, you can use the following Configuration:

`js

module.exports = {

// Your Network Provider (e.g., Infua, EthersGorP)

Network: {

Provider: ‘

Network_id: 1,

},

// Your Wallet Provider (Metamask)

Walletprovider: {

privatekey: ‘‘,

mnemonic: ‘‘,

Gasprice: 20 * Math.Pow (10, 18),

},

// Your Project’s EntryPoint

Entries: [

{from: ‘0xyouraccountadddress’, to: ‘0xcontraddaddress’, args: [‘arg1’, ‘arg2’]},

],

};

`

In the example above:

  • Network: the provider and network details.

  • Walletprovider: Your Metamask Wallet Configuration, Including Private Key, Mnemonic, and Gas Price.

  • Entries: an array of contract deployment configurations.

Deploying Contracts Using Hardhat

To deploy a contract This file will specify the private key and other metadata.

Create a new file named .env inside your project Root:

`bash

Metamask_private_key = your_metascan_private_key

`

Alternatively, you can use hardhat’s built-in configuration feature to define an environment file with a single entry for each wallet.

`js

module.exports = {

// Your Network Provider (e.g., Infua, EthersGorP)

Network: ‘Development’,

// Metamask Wallets

Enviles: {

‘Metamask1’: {

Privatekey: ‘‘,

mnemonic: ‘‘,

Gasprice: 20 * Math.pow (10, 18),

},

‘Metamask2’: {

Privatekey: ‘‘,

mnemonic: ‘‘,

Gasprice: 20 * Math.Pow (10, 18),

},

},

};

`

Now, create a new file named hardhat.config.js with the following configuration:

`js

Const {Ethers} = Require (‘Ethers’);

consta (‘./ Enviles’);

module.exports = {

// Your Project’s EntryPoint

Entries: [

{from: ‘metamask1’, to: ‘0xcontraddress_1’, args: [‘arg1’, ‘arg2’]},

{from: ‘metamask2’, to: ‘0xcontraddress_2’, args: [‘arg3’, ‘arg4’]},

],

};

`

Deploying Contracts

With the Enviles configuration in place, you can now use hardhat to deploy contracts. In your main.js file or any other entry point:

`JavaScript

Const {Ethers} = Require (‘Ethers’);

const {Enviles} = require (‘./ hardhat.config’);

Async Function Main () {

// Import the contract abi and bytecode

Const Contractti = Await Import (‘./ Path/to/contract/abi.json’);

Const contractbytecode = Await import (‘./ path/to/bytecode.json’ contract;

// Get the Accounts from the Environment Files

Const {Metamask1, Metamask2} = Enviles;

// Deploy the Contract Using Hardhat’s Deploy Function`

Const Deployer = New Ethers.providers.jsonrpcprovider (

{

Gasprice: 20 * Math.pow (10, 18),

}

);

Const accounts = Await Deployer.

ethereum difference between transaction


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *