Deploying Contracts

Different ways to deploy your contracts to Mantle Network

2023/12/04 Thirdweb released a report about recent security vulnerabilities in the commonly used open-source library for web3 smart contracts. All developers who have previously deployed contracts are advised to ensure that the contract is secure, see details here.

Using Remix IDE

Remix‘s web-based IDE supports writing, testing, debugging, and deploying smart contracts written in Solidity, Yul, and other popular contract development languages.

  1. Start by opening Remix in your browser. Create a new .sol file under the "Contracts" folder and start writing your code.

The JSON file containing the bytecode generated upon compiling your contract can be found in the "build-info" directory.

Upon successful deployment, you'll be able to see the transaction and contract info in the terminal at the bottom, like so:

Using Hardhat

Hardhat is a smart contract development environment that comes with tools that can be used to compile, debug, and deploying Solidity smart contracts and dApps on Ethereum and other EVM-compatible chains like Mantle Network.

  1. Start by creating a new Hardhat project and configuring the hardhat.config.js file to add Mantle Network's settings to it.

import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import * as dotenv from 'dotenv';

dotenv.config();

const config: HardhatUserConfig = {
    solidity: "0.8.19", // solidity version
    defaultNetwork: "mantleTest", // chosen by default when network isn't specified while running Hardhat
    networks: {
        mantle: {
        url: "https://rpc.mantle.xyz", //mainnet
        accounts: [process.env.ACCOUNT_PRIVATE_KEY ?? ''],
        },
        mantleTest: {
        url: "https://rpc.testnet.mantle.xyz", // testnet
        accounts: [process.env.ACCOUNT_PRIVATE_KEY ?? '']
        }
    },
    etherscan: {
        apiKey: process.env.API_KEY,
    },
};

export default config;
  1. With your contracts ready in the "Contracts" directory within your Hardhat project, you can run the following command to compile them.

npx hardhat compile
Compiling...
Compiled 1 contract successfully
  1. Use the following command to deploy your contracts to a chosen network.

npx hardhat run --network mantleTest scripts/deploy.ts
Contract deployed to address: 0x0000000000000000000000000000000000000000

Refer to the Troubleshooting page if you run into issues while deploying your contract.

Using Foundry

Foundry is a fast, portable and modular toolkit for EVM application development written in Rust. The two main components that you'll generally be using for smart contract development are:

  • Forge: used to compile, test, and deploy contracts

  • Cast: used to make RPC calls to interact with a network

  1. Use the following command to compile a smart contract.

forge build

Sample Output

[⠊] Compiling...
[⠑] Installing solc version 0.8.21
[⠰] Successfully installed solc 0.8.21
[⠒] Compiling 22 files with 0.8.21
[⠃] Solc 0.8.21 finished in 3.19s
Compiler run successful!
  1. Upon successful compilation, use the following command to deploy a smart contract

forge create --rpc-url https://rpc.testnet.mantle.xyz --private-key <your-private-key> src/Storage.sol:Storage --legacy 

Sample Output

[⠢] Compiling...
No files changed, compilation skipped
Deployer: 0x...
Deployed to: 0x...
Transaction hash: 0x...

Using Thirdweb CLI

You can install the thirdweb CLI globally on your machine using npm by running the following command.

npm i -g @thirdweb-dev/cli

Creating a Project

  1. Start by creating a new project by running:

npx thirdweb create contract
  1. Input your preferences for the command line prompts:

    1. Give your project a name

    2. Choose your preferred framework: Hardhat or Foundry

    3. Name your smart contract

    4. Choose the type of base contract: Empty, ERC20, ERC721, or ERC1155

    5. Add any desired extensions

  2. Once created, navigate to your project’s directory and open in your preferred code editor. If you open the contracts folder, you will find your smart contract; this is your smart contract written in Solidity.

  3. The following is code for an ERC721Base contract without specified extensions. It implements all of the logic inside the ERC721Base.sol contract; which implements the ERC721A standard.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@thirdweb-dev/contracts/base/ERC721Base.sol";

contract Contract is ERC721Base {
    constructor(
        string memory _name,
        string memory _symbol,
        address _royaltyRecipient,
        uint128 _royaltyBps
    ) ERC721Base(_name, _symbol, _royaltyRecipient, _royaltyBps) {}
}

This contract inherits the functionality of ERC721Base through the following steps:

  • Importing the ERC721Base contract

  • Inheriting the contract by declaring that our contract is an ERC721Base contract

  • Implementing any required methods, such as the constructor.

  1. After modifying your contract with your desired custom logic, you're ready to deploy it to Mantle using Deploy.

You can also navigate to the thirdweb Explore page and choose from a collection of pre-built contracts to use and deploy. For more information on different contracts available on Explore, check out thirdweb’s documentation.

Deploying Contract

deploy allows you to deploy a smart contract to Mantle Network without configuring RPC URLs, exposing your private keys, writing scripts, and other additional setup such as verifying your contract.

  1. To deploy your smart contract using deploy, navigate to the root directory of your project and run:

npx thirdweb deploy

Executing this command will trigger the following actions:

  • Compiling all the contracts in the current directory.

  • Providing the option to select which contract(s) you wish to deploy.

  • Uploading your contract source code (ABI) to IPFS.

  1. When it is completed, it will open a dashboard interface to finish filling out the parameters for our contract.

    • _name: contract name

    • _symbol: symbol or "ticker"

    • _royaltyRecipient: wallet address to receive royalties from secondary sales

    • _royaltyBps: basis points (bps) that will be given to the royalty recipient for each secondary sale, e.g. 500 = 5%

  2. Select Mantle Testnet as the network if you're deploying to the testnet, or Mantle Mainnet if you're deploying to production.

  3. Manage additional settings on your contract’s dashboard as needed such as uploading NFTs, configuring permissions, and more.

For additional information on deploy, refer to thirdweb’s documentation. You can reach out thirdweb support at http://support.thirdweb.com/.

Last updated