Deploy a contract using Truffle
In this tutorial, you'll create a simple smart contract and use Truffle to compile and then deploy the smart contract on the Ropsten network.

Prerequisites

You can use MetaMask or similar to create an Ethereum wallet for testing purposes.

Steps

1. Install Truffle

Truffle is a smart contract development tool and testing framework for blockchain networks.
Install Truffle globally using the Node.js package manager:
npm install -g truffle

2. Fund your Ethereum account

Use a faucet to load testnet ETH on your Ethereum account for the Ropsten network.
If using a network other than Ropsten, ensure you update your environment file with the network name.

3. Create a project directory

Create a new directory for your project. This can be done from the command line:
mkdir truffleProject
Change into the new directory:
cd truffleProject

4. Install Dotenv

Install the dotenv package to allow you to use a .env file to store private environment variables on your local machine.
npm install dotenv
In this example we'll use the .env file to store your wallet's mnemonic, and the Infura API URL.

5. Create a Truffle project

We'll create a bare Truffle project which generates the required directory structure to test and deploy contracts:
truffle init
Truffle creates the following directory structure for your project:

6. Install hdwallet-provider

hdwallet-provider is a separate package that can sign transactions for addresses derived from a 12 or 24 word mnemonic. By default, the hdwallet-provider uses the first address that's generated from the mnemonic however, this behavior is configurable.
Refer to the Truffle hdwallet-provider repository for more usage instructions.
Infura does not manage your private keys, meaning it cannot sign transactions on your behalf.
Run the following command to install hdwallet-provider:
npm install @truffle/hdwallet-provider
You can also use the Truffle Dashboard to allow MetaMask to sign your transactions.

7. Create the .env file

Create a .env file in your project directory to store the project and Ethereum account details.
In the example the file incudes your MetaMask secret recovery phrase. Refer to the MetaMask instructions on how to reveal a secret recovery phrase.
INFURA_API_URL = "https://ropsten.infura.io/v3/<Your-Project-ID>"
MNEMONIC = "<Your-MetaMask-Secret-Recovery-Phrase>"
Ensure you replace the following values in the .env file:
  • <Project-ID> with the Project ID of the Ethereum project.
  • <Your-MetaMask-Secret-Recovery-Phrase> with the mnemonic of your MetaMask wallet. This phrase is used by the Truffle hdwallet-provider to sign transactions.
Never disclose your secret recovery phrase. Anyone with your recovery phrase can steal any assets held in your wallet.

8. Create a smart contract

Using an editor, create a smart contract in the contracts/ directory. In this example, we'll create a basic contract called Demo.sol.
pragma solidity >=0.5.8;
contract Demo {
event Echo(string message);
function echo(string calldata message) external {
emit Echo(message);
}
}

9. Configure the Truffle settings

Configure the truffle-config.js file to use the HDWalletProvider and include the required configuration to deploy to the network (Ropsten in this example). You can find the truffle-config.js file in your project's root directory.
require('dotenv').config();
const HDWalletProvider = require('@truffle/hdwallet-provider');
const { INFURA_API_URL, MNEMONIC } = process.env;
module.exports = {
networks: {
development: {
host: "127.0.0.1",
port: 8545,
network_id: "*"
},
ropsten: {
provider: () => new HDWalletProvider(MNEMONIC, INFURA_API_URL),
network_id: 3,
gas: 5500000
}
}
};
Refer to the Truffle documentation for more information about configuring the Truffle settings.

10. Compile the smart contract

To compile the smart contract, navigate to the project's root directory and run the following:
truffle compile
If successfull, you'll see output similar to:
Compiling your contracts...
===========================
> Compiling ./contracts/Demo.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /Users/user/truffleProject/build/contracts
> Compiled successfully using:
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
Artifacts of your compilation are placed in the build/contracts/ directory, relative to your project root. Refer to the Truffle documentation for more information about the compilation process.

11. Create the deployment script

Scripts to deploy smart contracts are located in the migrations/ directory and are numbered. In the migrations/ directory, create a file called 2_deploy_contract.js.
Truffle requires a migration contract in order to use the migrations feature. The Migration.sol and associated deployment file (1_initial_migration.js) are created automatically when creating your Truffle project.
Add the following code to the 2_deploy_contract.jsfile:
const Demo_Contract = artifacts.require("Demo");
module.exports = function(deployer) {
deployer.deploy(Demo_Contract);
};

12. Deploy the smart contract

You need to run the migration command to deploy the smart contract to the Ethereum network.
To deploy the contract, run the following command:
truffle migrate --network ropsten
If successful you'll see a response similar to the following:
...
Starting migrations...
======================
> Network name: 'ropsten'
> Network id: 3
> Block gas limit: 8000000 (0x7a1200)
1_initial_migration.js
======================
Deploying 'Migrations'
----------------------
> transaction hash: 0xe4b047069653f62a1bb4e2ad841dad19cb12a8dbdfd5336dbb7a7d3fa098e4c9
> Blocks: 2 Seconds: 45
> contract address: 0x957199EDfaFb47812f3352c7C8a2B1682e1fE440
> block number: 12036476
> block timestamp: 1646286532
> account: 0x9cE564c7d09f88E7d8233Cdd3A4d7AC42aBFf3aC
> balance: 0.099516883324242631
> gas used: 193243 (0x2f2db)
> gas price: 2.500047483 gwei
> value sent: 0 ETH
> total cost: 0.000483116675757369 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.000483116675757369 ETH
2_deploy_contract.js
====================
Deploying 'Demo'
----------------
> transaction hash: 0x22d191dfb29d0e2a8f6705d9bb620641d19a1fbd39dbfed7e7ae27e1eb5ef417
> Blocks: 1 Seconds: 13
> contract address: 0x4Ee0845a9eBfB04Bcc225b70Dc78c4789C61c64E
> block number: 12036480
> block timestamp: 1646286597
> account: 0x9cE564c7d09f88E7d8233Cdd3A4d7AC42aBFf3aC
> balance: 0.099093977563867095
> gas used: 123421 (0x1e21d)
> gas price: 2.500046998 gwei
> value sent: 0 ETH
> total cost: 0.000308558300540158 ETH
> Saving migration to chain.
> Saving artifacts
-------------------------------------
> Total cost: 0.000308558300540158 ETH
Summary
=======
> Total deployments: 2
> Final cost: 0.000791674976297527 ETH
Use a block explorer like Etherscan to view the transaction details. For example, use the transaction hash or contract address to view the details.

Next steps

You can now make calls to the deployed contract. A contract call is a transaction that will consume gas on the public Ethereum network.