5 minute guide to deploying smart contracts with Truffle and Ropsten

A recommended & quick setup for DApp development with boilerplate

Nicole Zhu
Coinmonks
Published in
3 min readAug 25, 2018

--

You have various setup options for deploying, migrating, and accessing smart contracts. Depending on the level of control and visibility you want into the EVM (Ethereum Virtual Machine), you can choose from using an online IDE like Remix, to running a full Ethereum mining node via Geth.

Of course, Truffle makes compiling & deploying smart contracts markedly easier, while still giving you visibility and control.

1 Minute Shortcut: fork this boilerplate template

Here’s a framework depicting both common setups and what we’ll tackle today: the path in blue.

Checkout Blockchain Courses and learn Solidity

This tutorial covers

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

Make sure you already have

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!

HelloWorld.deployed().then(function(instance){return instance.sayHello()});Also, Read

Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News

Also, Read

--

--