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:
1
npm install -g truffle
Copied!

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:
1
mkdir truffleProject
Copied!
Change into the new directory:
1
cd truffleProject
Copied!

4. Install Dotenv

Install the dotenv package to allow you to use a .env file to store private environment variables on your local machine.
1
npm install dotenv
Copied!
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:
1
truffle init
Copied!
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:
1
npm install @truffle/hdwallet-provider
Copied!
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.
1
INFURA_API_URL = "https://ropsten.infura.io/v3/<Your-Project-ID>"
2
MNEMONIC = "<Your-MetaMask-Secret-Recovery-Phrase>"
Copied!
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.
1
pragma solidity >=0.5.8;
2
3
contract Demo {
4
event Echo(string message);
5
6
function echo(string calldata message) external {
7
emit Echo(message);
8
}
9
}
Copied!

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.
1
require('dotenv').config();
2
const HDWalletProvider = require('@truffle/hdwallet-provider');
3
const { INFURA_API_URL, MNEMONIC } = process.env;
4
5
module.exports = {
6
networks: {
7
development: {
8
host: "127.0.0.1",
9
port: 8545,
10
network_id: "*"
11
},
12
ropsten: {
13
provider: () => new HDWalletProvider(MNEMONIC, INFURA_API_URL),
14
network_id: 3,
15
gas: 5500000
16
}
17
}
18
};
Copied!
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:
1
truffle compile
Copied!
If successfull, you'll see output similar to:
1
Compiling your contracts...
2
===========================
3
> Compiling ./contracts/Demo.sol
4
> Compiling ./contracts/Migrations.sol
5
> Artifacts written to /Users/user/truffleProject/build/contracts
6
> Compiled successfully using:
7
- solc: 0.5.16+commit.9c3226ce.Emscripten.clang
Copied!
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:
1
const Demo_Contract = artifacts.require("Demo");
2
3
module.exports = function(deployer) {
4
deployer.deploy(Demo_Contract);
5
};
Copied!

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:
1
truffle migrate --network ropsten
Copied!
If successful you'll see a response similar to the following:
1
...
2
Starting migrations...
3
======================
4
> Network name: 'ropsten'
5
> Network id: 3
6
> Block gas limit: 8000000 (0x7a1200)
7
8
9
1_initial_migration.js
10
======================
11
12
Deploying 'Migrations'
13
----------------------
14
> transaction hash: 0xe4b047069653f62a1bb4e2ad841dad19cb12a8dbdfd5336dbb7a7d3fa098e4c9
15
> Blocks: 2 Seconds: 45
16
> contract address: 0x957199EDfaFb47812f3352c7C8a2B1682e1fE440
17
> block number: 12036476
18
> block timestamp: 1646286532
19
> account: 0x9cE564c7d09f88E7d8233Cdd3A4d7AC42aBFf3aC
20
> balance: 0.099516883324242631
21
> gas used: 193243 (0x2f2db)
22
> gas price: 2.500047483 gwei
23
> value sent: 0 ETH
24
> total cost: 0.000483116675757369 ETH
25
26
> Saving migration to chain.
27
> Saving artifacts
28
-------------------------------------
29
> Total cost: 0.000483116675757369 ETH
30
31
32
2_deploy_contract.js
33
====================
34
35
Deploying 'Demo'
36
----------------
37
> transaction hash: 0x22d191dfb29d0e2a8f6705d9bb620641d19a1fbd39dbfed7e7ae27e1eb5ef417
38
> Blocks: 1 Seconds: 13
39
> contract address: 0x4Ee0845a9eBfB04Bcc225b70Dc78c4789C61c64E
40
> block number: 12036480
41
> block timestamp: 1646286597
42
> account: 0x9cE564c7d09f88E7d8233Cdd3A4d7AC42aBFf3aC
43
> balance: 0.099093977563867095
44
> gas used: 123421 (0x1e21d)
45
> gas price: 2.500046998 gwei
46
> value sent: 0 ETH
47
> total cost: 0.000308558300540158 ETH
48
49
> Saving migration to chain.
50
> Saving artifacts
51
-------------------------------------
52
> Total cost: 0.000308558300540158 ETH
53
54
Summary
55
=======
56
> Total deployments: 2
57
> Final cost: 0.000791674976297527 ETH
Copied!
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.