5 minute guide to deploying smart contracts with Truffle and Ropsten
A recommended & quick setup for DApp development with boilerplate
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
Checkout Blockchain Courses and learn Solidity
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!
HelloWorld.deployed().then(function(instance){return instance.sayHello()});Also, Read
Join Coinmonks Telegram Channel and Youtube Channel get daily Crypto News
Also, Read
- Crypto Telegram Signals | Crypto Trading Bot
- Copy Trading | Crypto Tax Software
- Grid Trading | Crypto Hardware Wallet
- Crypto Exchange | Crypto Apps in India
- Best Crypto APIs for Developers
- Best Crypto Lending Platforms
- An ultimate guide to Leveraged Token
- 7 Best Ways to buy Dogecoin | ZebPay Review
- Best Futures Trading Signals | Liquid Exchange Review
- Crypto Trading Signals for Huobi | Swapzone Review
- Best Crypto Trading Bots | Buy Solana | MatrixPort Review
- Coldcard Review | BOXtradEX Review | A Guide to Uniswap
- Coinbase Review| Deribit Review |FTX Review