Deploy a Greeter contract on Mantle Testnet

SAMBIT SARGAM EKALABYA
3 min readMay 8, 2023

--

Sure! Here’s a step-by-step guide for deploying the Greeter contract on the Mantle network using different tools:

Deploying with Hardhat

  1. Install Hardhat by running the following command in your terminal:
npm install - save-dev hardhat

2. Install the required plugins by running the following command:

npm install - save-dev @nomiclabs/hardhat-waffle ethereum-waffle chai @nomiclabs/hardhat-ethers ethers

3. Create a new Hardhat project by running the following command:

npx hardhat

This will create a new directory called `my-project` with the basic files you need to get started.

4. Open the `hardhat.config.js` file and add the following lines to the `networks` object:

mantle: {
url: "https://rpc-mainnet.maticvigil.com",
chainId: 5001,
accounts: ["PRIVATE_KEY"]
}

Replace `PRIVATE_KEY` with your own private key.

5. Create a new file called `Greeter.sol` in the `contracts` directory with the following code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Greeter {
string private greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}

6. Create a new file called `deploy.js` in the root directory with the following code:

const { ethers } = require("hardhat");
async function main() {
const Greeter = await ethers.getContractFactory("Greeter");
const greeter = await Greeter.deploy("Hello, world!");
console.log("Greeter deployed to:", greeter.address);
}
main();

7. In your terminal, run the following command to deploy the contract:

npx hardhat run - network mantle deploy.js

This will compile the contract, deploy it to the Mantle network, and print the address of the deployed contract to the console.

Deploying with Truffle

  1. Install Truffle by running the following command in your terminal:
npm install -g truffle

2. Create a new Truffle project by running the following command:

truffle init

This will create a new directory called `my-project` with the basic files you need to get started.

3. Open the `truffle-config.js` file and add the following lines to the `networks` object:

mantle: {
provider: () => new HDWalletProvider("PRIVATE_KEY", "https://rpc-mainnet.maticvigil.com"),
network_id: 5001,
gas: 8000000,
gasPrice: 1000000000
}

Replace `PRIVATE_KEY` with your own private key.

4. Create a new file called `Greeter.sol` in the `contracts` directory with the following code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Greeter {
string private greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public { greeting = _greeting;
}
}

5. Create a new migration file by running the following command:

truffle create migration deploy_greeter

6. Open the migration file (`migrations/deploy_greeter.js`) and add the following code:

const Greeter = artifacts.require("Greeter");
module.exports = function (deployer) {
deployer.deploy(Greeter, "Hello, world!");
};

7. In your terminal, run the following command to deploy the contract:

truffle migrate - network mantle

This will compile the contract, deploy it to the Mantle network, and print the address of the deployed contract to the console.

Deploying with Remix

1. Open the Remix IDE in your browser at https://remix.ethereum.org/.

2. Create a new file called `Greeter.sol` and add the following code:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Greeter {
string private greeting;
constructor(string memory _greeting) {
greeting = _greeting;
}
function greet() public view returns (string memory) {
return greeting;
}
function setGreeting(string memory _greeting) public {
greeting = _greeting;
}
}

3. In the Remix sidebar, click on the `Solidity Compiler` tab and compile the contract by clicking the `Compile Greeter.sol` button.

4. In the Remix sidebar, click on the `Deploy & Run Transactions` tab.

5. In the `Environment` dropdown, select `Injected Web3` and ensure that the Mantle Testnet` network is selected.

6. Click the `Deploy` button to deploy the contract.

This will open a popup window asking you to confirm the transaction. Once confirmed, the contract will be deployed to the Mantle network.

Congratulations! You have now deployed the Greeter contract on the Mantle network using Hardhat, Truffle, and Remix.

--

--