This tutorial covers

Configure Truffle Framework to the Ropsten test network Use it to deploy (or migrate) a contract instance Access the deployed instance and interact with it (via Truffle console)

Make sure you already have

Save your wallet mnemonic from Metamask

Get some test ether

Get a Ropsten API key from Infura

Detailed Walkthrough

1. Set up Truffle

npm install -g truffle

Make an empty repository, cd into it, then

truffle init

Install HDWalletProvider

npm install --save truffle-hdwallet-provider

2. Create your contract

In ./contracts create a new contract called HelloWorld.sol with the following code:

pragma solidity ^0.4.23; contract HelloWorld {

function sayHello() public pure returns(string){

return(“hello world”);

}

}

3. Deploy your contract

In ./migrations , create a deployment script specifically named 2_deploy_contracts.js with the following code:

var HelloWorld = artifacts.require(“HelloWorld”); module.exports = function(deployer) {

deployer.deploy(HelloWorld, “hello”);

// Additional contracts can be deployed here

};

4. Configure Ropsten network and the provider

In truffle.js , add the following snippet inside module.exports:

Make sure to replace mnemonic and API_KEY with your own.

Security note: remember to .gitignore the file containing your wallet mnemonic!

Now deploy (or migrate) your contract to Ropsten as follows. By default, Truffle only deploys to the local developer network.

truffle deploy --network ropsten

You should see a console log as follows:

Running migration: 1_initial_migration.js

Deploying Migrations…

… 0xd01dd7...

Migrations: 0xf741...

Saving successful migration to network…

… 0x78ed...

Saving artifacts…

Running migration: 2_deploy_contracts.js

Deploying HelloWorld…

… 0x0aa9...

HelloWorld: [SAVE THIS ADDRESS!!]

Saving successful migration to network…

… 0xee95...

Saving artifacts…

Tip: Make sure to save your contract address for future reference. If you lose it, proceed to Etherscan to examine your wallet address transactions.

5. Access your deployed contract

Set up your Truffle console to Ropsten network:

truffle console --network ropsten

Access your deployed contract instance via:

HelloWorld.deployed().then(function(instance){return instance });

Or you can retrieve the instance by its public address via:

web3.eth.contract(HelloWorld.abi, contractAddress)

where HelloWorld.abi is the locally compile abi, and contractAddress is your publicly deployed contract instance.

6. Finally, invoke contract function and say hello!