Deploying A Smart Contract Using Truffle

Follow

Deploying a Smart Contract on an Ethereum Private Network with Truffle

 

First up, you'll need to install Truffle

Install the following:

Once you have those installed, you only need one command to install Truffle: 

npm install -g truffle

To verify that Truffle is installed properly, type truffle version on a terminal. If you see an error, make sure that your npm modules are added to your path.

What is Truffle?

A development environment, testing framework and asset pipeline for blockchains using the Ethereum Virtual Machine (EVM).

With Truffle, you get:

  • Built-in smart contract compilation, linking, deployment and binary management.
  • Automated contract testing for rapid development.
  • Scriptable, extensible deployment & migrations framework.
  • Network management for deploying to any number of public & private networks.
  • Package management with EthPM & NPM, using the ERC190 standard.
  • Interactive console for direct contract communication.
  • Configurable build pipeline with support for tight integration.
  • External script runner that executes scripts within a Truffle environment.

It's designed to simplify and automate some of the more clunky aspects of developing and deploying contracts.  They have excellent documentation on every phase of the process, here.

Truffle comes with various pre-packaged smart contracts/DApps called boxes (https://truffleframework.com/boxes). The following tutorial ignores boxes in order to show how to deploy a smart contract from scratch. We recommend however to take a look at the boxes as well. They are often a good starting point for building your own smart contracts/DApps. A guide on how to initialize a project from a box can be found here: https://truffleframework.com/docs/truffle/getting-started/creating-a-project

 

Step 1: Create a truffle project 

Create a new directory and type truffle init:

mkdir greeter  
truffle init

 You should see the following output:

✔ Preparing to download 
✔ Downloading
✔ Cleaning up temporary files
✔ Setting up box

Unbox successful. Sweet!

Commands:
Compile:    truffle compile
Migrate:    truffle migrate
Test contracts: truffle test

 

Step 2: Saving and compiling the smart contract

All contracts go into the contracts directory. There is already one smart contract called Migrations.sol in that directory. This smart contract gets deployed with the other smart contracts. It keeps track of what’s already deployed to avoid re-deploying live production smart contracts (see: https://truffleframework.com/docs/truffle/getting-started/running-migrations).

 

Create a new file called Greeter.sol in the contracts directory and copy the Greeter code, below into it.

 

Run the following command on the main directory e.g. /greeter path to compile the smart contract:

truffle build

You should see the following output:

No build configuration found. Preparing to compile contracts.
Compiling ./contracts/Greeter.sol...
Compiling ./contracts/Migrations.sol...
Writing artifacts to ./build/contracts

 

Step 3: Set up migrations for the Greeter contract

Truffle uses migration scripts for deploying smart contracts. These scripts allow deployment of smart contracts but also other calls to the Blockchain. E.g. to initialize a smart contract. In this example we are just going to deploy the contract.

Create a new file called 2_greeter.js in the migrations directory. Copy the following code into the file.

var Greeter = artifacts.require("./Greeter.sol")
module.exports = function(deployer){
     deployer.deploy(Greeter);
};



Step 4: Configuring the Blockdaemon network

Truffle allows to configure&deploy to different networks. In order to deploy to a Blockdaemon network, we first need to configure it.

 

In its standard configuration Truffle can deploy using Ethereum accounts that are stored on the node. For security reasons, we advise against this. It’s better to keep the account (or more precisely the private key of the account) locally and sign the transactions before sending them to the node. This minimizes exposure of the private key and reduces the likelihood of losing or leaking it.

 

Truffle provides functionality to sign transactions locally before sending them with the HDWalletProvider. This needs to be installed separately. Please run the following command on the main directory e.g. /greeter:

npm install truffle-hdwallet-provider

Next, open the file truffle-config.js. At the top of the file insert the following line to import the HDWalletProvider:

var HDWalletProvider = require("truffle-hdwallet-provider");

Under networks we can now configure the Blockdaemon network like this: 

networks: {
      blockdaemon: {
             provider: function() { 
                    return new HDWalletProvider("[Insert private Ethereum key 
      here]", "[Insert the nodes JSON-RPC endpoint here]"); 
                       }, 
                       network_id: '*',
               }
       }

 


Each private Ethereum network is by default launched with a pre-funded account. The private key for that account is: 

D52C7CDE8D579FBBA204717AE1F6765B30CA9FF9B01A9D16F3ACB6EE36ACF233

Note: The JSON-RPC endpoint can be found on the connect page in your node dashboard     

For initial testing and development we recommend using that account as it’s already well funded. For production use we recommend to supply your own genesis file  with its own set of pre-funded accounts.

Save the file with all the changes. We are now ready to deploy the smart contract.

Step 5: Deploy the contract

Run on the /greeter path

truffle migrate --network blockdaemon

To deploy the smart contract.

This usually takes between a few seconds and a few minutes. If all went ok, you should see the following output:

Note: Your output should look similar to the one below. The values should in the output depend on the previous block and hash values.

 

Important
If you're using an HDWalletProvider, it must be Web3 1.0 enabled or your migration
will hang.


Starting migrations
======================
> Network name: 'blockdaemon' > Network id:  4323123 > Block gas limit: 100412470


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

  Deploying 'Migrations' ---------------------- >transaction hash:
0x551d167e2ea51aaee54f6e6aca156f9f32380aef2da68a902a935546a518aa1e     
>Blocks: 1    
>Seconds: 5     
>contract address: 0xab2577f568De4895dA519782D1Fb2814e8556269     
>account:    0x401ccDde01bced0D0D2Fb029b9d6E3B9Ef0823e2      >balance:     
904625697166532776746648320380374280103671755200316906558.257321181821325312 >gas used:        252694      >gas price:       20 gwei      >value sent:      0 ETH >total cost:      0.00505388 ETH 
>Saving migration to chain.     
>Saving artifacts
-------------------------------------
> Total cost:      0.00505388 ETH


2_greeter.js
============

  Deploying 'Greeter' ------------------- >transaction hash:
0x70fbe0ea3f3dee5ce95fb74a1f78352d2aa81282b0c76f6fe26e8e7e190b8a66 >Blocks: 2 Seconds: 5
>contract address: 0x88bcD2C61DFA0408d8D0BdC281627022b7bac64c >account: 0x401ccDde01bced0D0D2Fb029b9d6E3B9Ef0823e2 >balance:
904625697166532776746648320380374280103671755200316906558.249348541821325312 >gas used: 356748 >gas price: 20 gwei >value sent: 0 ETH >total cost: 0.00713496 ETH<

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

Summary
=======

> Total deployments:  2
> Final cost:       0.01218884 ETH

 

Greeter smart contract

pragma solidity ^0.5.0;
contract Greeter {
       string public greeting;
constructor() public { greeting = 'Hello'; }
function setGreeting(string memory _greeting) public { greeting = _greeting; }
function greet() view public returns (string memory) { return greeting; } }



1 out of 1 found this helpful

Comments

0 comments

Please sign in to leave a comment.