Before you go, check out these stories!

0
Hackernoon logoThe Git Way to Deploy a Solidity Contract on Ropsten With Truffle by@Bassaganas

The Git Way to Deploy a Solidity Contract on Ropsten With Truffle

Author profile picture

@BassaganasJordi Bassagañas

Hi there! How are you today? I write about all things web and more.

Hey dapp developers! Today I am excited because I am posting the step-by-step process that allowed me to deploy my first Ethereum smart contract on the Ropsten testnet.

If you're an intermediate dapp learner planning to build more Solidity applications in 2020 this is an awesome starting point to clone or fork.

It's Git-based, meaning you just go take a cup of coffee and browse the commit history, or log, which is available at programarivm/solidity-hello-world.

The git log will give you a clear understanding of what needs to be done specifically.

$ git log --oneline
ab27b5b (HEAD -> master, origin/master) README is added
2b0e05a truffle migrate --network ropsten
aae6249 truffle-config.js is updated
8f1ef2e npm install --save dotenv
6e99bcc 2_deploy_contracts.js is added
6b6d358 HelloWorld.sol is added
144bf28 truffle init
31aea18 npm install @truffle/hdwallet-provider
173d4cd .gitignore is added
560a1ed npm init

Are you ready? Please browse the commits one by one and try to do the exact same thing on your side.

Let's recap, your work starts with 560a1ed:

$ npm init

And ends with 2b0e05a:

$ truffle migrate --network ropsten

If all this makes little sense to you, don't worry and take it easy.

Probably you still have to get familiar with a little bit of programming tools and also grasp some basics of Ethereum first -- I'd recommend you look at the "You may also be interested in" section below.

Be that as it may let me say two important things to be considered when deploying your first smart contract.

The Simplest Contract You Can Probably Think Of

It's usually a good idea to start off by doing something really really simple.

The Solidity contract itself is added to the Git repository with commit

6b6d358
and basically is intended to output the
Hello world!
string.

// SPDX-License-Identifier: MIT

pragma solidity 0.5.16;

contract HelloWorld {
  function output() public pure returns (string memory) {
    return "Hello world!";
  }
}

That's it!

About Environment Variables

But hang on a minute, remember cryptocurrency wallets are secret.

As you can see in commit

8f1ef2e
, we're using
dotenv
.
.env
files are a great mechanism to store stuff like secrets, passwords, or third-party identifiers that you don't want to share with anyone else.

On the other hand, Infura is the web3 provider allowing us to access the Ethereum network.

So you must add an

.env
file in your project root directory in order to be able to deploy your smart contract to Ropsten.

MNENOMIC=your mnemonic goes here
INFURA_KEY=your_key_goes_here

It is crucial to add the

.env
file to
.gitignore
when working with environment variables along with Git so they won't get pushed to the repo -- and therefore shared with the world.

This is how the

.gitignore
file looks like in this very first contract:

/node_modules
.env

The Infura project's identifier and the wallet mnemonic are provided to Truffle by updating

truffle-config.js
accordingly in commit
aae6249
.

Compile and Migrate

Finally with everything set up, the smart contract is deployed to the Ropsten test network with commit

2b0e05a
.

$ truffle migrate --network ropsten

Compiling your contracts...
===========================
> Compiling ./contracts/HelloWorld.sol
> Compiling ./contracts/Migrations.sol
> Artifacts written to /home/standard/projects/solidity-hello-world/build/contracts
> Compiled successfully using:
   - solc: 0.5.16+commit.9c3226ce.Emscripten.clang



Migrations dry-run (simulation)
===============================
> Network name:    'ropsten-fork'
> Network id:      3
> Block gas limit: 7951692 (0x79554c)


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > block number:        8926499
   > block timestamp:     1603355450
   > account:             0x8863ae48646c493efF8cd54f9Ffb8Be89669E62A
   > balance:             1.992118114
   > gas used:            176943 (0x2b32f)
   > gas price:           2 gwei
   > value sent:          0 ETH
   > total cost:          0.000353886 ETH

   -------------------------------------
   > Total cost:         0.000353886 ETH


2_deploy_contracts.js
=====================

   Deploying 'HelloWorld'
   ----------------------
   > block number:        8926501
   > block timestamp:     1603355468
   > account:             0x8863ae48646c493efF8cd54f9Ffb8Be89669E62A
   > balance:             1.991833852
   > gas used:            114793 (0x1c069)
   > gas price:           2 gwei
   > value sent:          0 ETH
   > total cost:          0.000229586 ETH

   -------------------------------------
   > Total cost:         0.000229586 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0.000583472 ETH





Starting migrations...
======================
> Network name:    'ropsten'
> Network id:      3
> Block gas limit: 7959456 (0x7973a0)


1_initial_migration.js
======================

   Deploying 'Migrations'
   ----------------------
   > transaction hash:    0x1b171b9092089a9c2193de42f02b2b126f3e653fd45bb51a4daff8e89cceb4c0
   > Blocks: 1            Seconds: 4
   > contract address:    0x9Ebdda3450DE009a2c9670276bF8DC2bc90B8156
   > block number:        8926500
   > block timestamp:     1603355470
   > account:             0x8863ae48646c493efF8cd54f9Ffb8Be89669E62A
   > balance:             1.98863314
   > gas used:            191943 (0x2edc7)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00383886 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00383886 ETH


2_deploy_contracts.js
=====================

   Deploying 'HelloWorld'
   ----------------------
   > transaction hash:    0x95bef79088bf48466e171d30cde908182a788a062da4b5d83156a5b69bb2391b
   > Blocks: 0            Seconds: 13
   > contract address:    0x38898AeE932ceA2f748A6F5B50aaea3235E845EE
   > block number:        8926502
   > block timestamp:     1603355486
   > account:             0x8863ae48646c493efF8cd54f9Ffb8Be89669E62A
   > balance:             1.98549052
   > gas used:            114793 (0x1c069)
   > gas price:           20 gwei
   > value sent:          0 ETH
   > total cost:          0.00229586 ETH


   > Saving migration to chain.
   > Saving artifacts
   -------------------------------------
   > Total cost:          0.00229586 ETH


Summary
=======
> Total deployments:   2
> Final cost:          0.00613472 ETH

Congratulations! Did you enjoy browsing the Git log?

I hope the Git way of doing things is also helpful to you when it comes to learning and doing new things. Please let me know in the comments below.

Don't miss out on the next post about interacting with the contract through a Node.js application.

You may also be interested in...

Tags

Join Hacker Noon

Create your free account to unlock your custom reading experience.