How to Create Your Own Smart Contract Dapp Using Solidity and Truffle Framework

How to Create Your Own Smart Contract Dapp Using Solidity and Truffle Framework
Photo by Nenad Novaković / Unsplash

Introduction

In this article, we will be discussing how to create a decentralized application (DApp) on the Ethereum blockchain using the Solidity programming language and the Truffle framework. We will be creating a simple smart contract that stores data on the blockchain.

Before we get started, there are a few things that you should know. First, blockchain is a distributed database that allows for secure, transparent, and tamper-proof storage of data. Second, Solidity is a programming language used for developing smart contracts on the Ethereum blockchain. Finally, Truffle is a development environment, testing framework, and asset pipeline for Ethereum that allows for easy development of smart contracts.

Now that we have that out of the way, let's get started!

Creating the Smart Contract

The first thing we need to do is create the smart contract that will be deployed on the Ethereum blockchain. We will be using the Solidity programming language to do this.

Create a new file called contracts/MyContract.sol and open it in your text editor. The first thing we need to do is specify the version of Solidity that we are using. We will be using version 0.4.21.

pragma solidity ^0.4.21;

Next, we need to specify the contract name. We will call our contract MyContract.

contract MyContract { }

Now, we need to add a state variable to our contract. A state variable is a variable that is stored on the blockchain. We will call our state variable myData.

contract MyContract { string public myData; }

Next, we need to write a function that will allow us to set the value of our state variable. We will call this function setData. This function will take an argument of type string and will return a value of type bool. The bool type is used to represent a boolean value (true or false).

contract MyContract { 
string public myData; 

function setData(string _data) 
public returns (bool) { 
myData = _data; return true; } 
}

Now, we need to write a function that will allow us to get the value of our state variable. We will call this function getData. This function will return a value of type string.

contract MyContract { 
string public myData; 

function setData(string _data) 
public returns (bool) { 
myData = _data; return true; } 

function getData() 
public returns (string) { 
return myData; } 
}

Now, our smart contract is complete!

Compiling the Smart Contract

Now that our smart contract is complete, we need to compile it. We will be using the Truffle framework to do this.

First, we need to install the Truffle framework. We can do this using the npm package manager. npm is a package manager for JavaScript.

npm install -g truffle

Next, we need to initialize a Truffle project. We can do this using the "truffle init" command.

truffle init

This will create a new directory called "truffle" with the following structure:

truffle/ |- contracts/ |- migrations/ |- test/ |- truffle-config.js

Next, we need to compile our smart contract. We can do this using the truffle compile command.

truffle compile

This will create a new file called build/contracts/MyContract.json. This file contains the compiled bytecode and abi (Application Binary Interface) of our smart contract. The bytecode is the code that is deployed on the blockchain. The abi is a specification that allows for the interaction with our smart contract.

Deploying the Smart Contract

Now that our smart contract is compiled, we need to deploy it to the Ethereum blockchain. We will be using the Truffle framework to do this.

First, we need to create a migration. A migration is a JavaScript file that contains the instructions for deploying a smart contract. Create a new file called migrations/2_deploy_my_contract.js and open it in your text editor.

The first thing we need to do is require the truffle-hdwallet-provider package. This package allows us to use an HDWallet provider. An HDWallet provider is a service that provides us with a mnemonic phrase and an Infura API key. These are used to generate a wallet that we can use to deploy our smart contract.

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

Next, we need to specify the mnemonic phrase and the Infura API key. These can be generated here: https://infura.io/dashboard.

var mnemonic = "YOUR MNEMONIC HERE"; var infuraApiKey = "YOUR INFURA API KEY HERE";

Next, we need to specify the network that we want to deploy our smart contract to. We will deploy our smart contract to the Rinkeby testnet.

var network = "rinkeby";

Now, we need to specify the contract that we want to deploy. We will deploy our MyContract contract.

var contract = "MyContract";

Next, we need to specify the contract's bytecode and abi. These can be found in the build/contracts/MyContract.json file.

var bytecode = "0x..."; var abi = [{...}];

Finally, we need to write the instructions for deploying our smart contract. We will use the truffle-hdwallet-provider to connect to the Rinkeby network and the web3 package to interact with our smart contract.

module.exports = 
function(deployer, network, accounts) { 

deployer.then(function() { 
return new HDWalletProvider(mnemonic, "https://" + network + ".infura.io/" + infuraApiKey) }) .then(function(provider) { 
var web3 = new Web3(provider); 
return web3.eth.contract(abi).new({ 
from: accounts[0], 
data: bytecode, 
gas: 4712388 }) }) 
.then(function(instance) { 
console.log("Contract deployed to: " + instance.address); 
});
};

Now, our migration is complete!

We can now deploy our smart contract to the Rinkeby testnet. We can do this using the truffle migrate --network rinkeby command.

truffle migrate --network rinkeby

This will deploy our smart contract to the Rinkeby network.

Testing the Smart Contract

Now that our smart contract is deployed, we need to test it. We will be using the Truffle framework to do this.

Create a new file called test/MyContract.js and open it in your text editor. The first thing we need to do is require the truffle-hdwallet-provider package. This package allows us to use an HDWallet provider.

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

Next, we need to specify the mnemonic phrase and the Infura API key. These can be generated here: https://infura.io/dashboard.

var mnemonic = "YOUR MNEMONIC HERE"; 
var infuraApiKey = "YOUR INFURA API KEY HERE";

Next, we need to specify the network that we want to test our smart contract on. We will test our smart contract on the Rinkeby testnet.

var network = "rinkeby";

Now, we need to specify the contract that we want to test. We will test our "MyContract" contract.

var contract = "MyContract";

Next, we need to specify the contract's abi. This can be found in the "build/contracts/MyContract.json" file.

var abi = [{...}];

Finally, we need to write the tests for our smart contract. We will use the "truffle-hdwallet-provider" to connect to the Rinkeby network and the "web3" package to interact with our smart contract.

var HDWalletProvider = require("truffle-hdwallet-provider"); 
var mnemonic = "YOUR MNEMONIC HERE"; 
var infuraApiKey = "YOUR INFURA API KEY HERE"; 
var network = "rinkeby"; 
var contract = "MyContract"; 
var abi = [{...}]; var web3 =
new Web3(new HDWalletProvider(mnemonic, "https://" + network + ".infura.io/" + infuraApiKey)); var myContract = web3.eth.contract(abi).at("0x..."); 
it("should set the data", 

function(done) { 
myContract.setData("Hello, world!", { 
from: web3.eth.accounts[0] }, 
function(err, result) { 
if(err) { 
return done(err); } 

myContract.getData({ 
from: web3.eth.accounts[0] }, 
function(err, result) { 
if(err) { 
return done(err); } assert.equal(result, "Hello, world!"); 
done(); 
}); 
}); 
});

In this test, we are first setting the value of our state variable to Hello, world!. Then, we are retrieving the value of our state variable and asserting that it is equal to Hello, world!.

We can now run our tests using the truffle test command.

truffle test

This will run our tests and print the results to the console.

Interacting with the Smart Contract

Now that our smart contract is deployed and tested, we need to interact with it. We will be using the Truffle framework to do this.

First, we need to install the Truffle console. The Truffle console is a REPL (Read-Eval-Print-Loop) that allows us to interact with our smart contract. We can do this using the npm package manager.

npm install -g truffle-console

Next, we need to start the Truffle console. We can do this using the "truffle console" command.

truffle console

This will start the Truffle console.

Now, we need to connect to the network that our smart contract is deployed on. We can do this using the truffle console --network rinkeby command.

truffle console --network rinkeby

This will connect to the Rinkeby network.

Now, we need to load our smart contract. We can do this using the truffle console command.

truffle(rinkeby)> contract = [MyContract.at](<http://mycontract.at/>)("0x...")

This will load our smart contract.

Now, we can interact with our smart contract!

Conclusion

In this article, we have discussed how to create a decentralized application (DApp) on the Ethereum blockchain using the Solidity programming language and the Truffle framework. We have created a simple smart contract that stores data on the blockchain. We have also compiled, deployed, tested, and interacted with our smart contract.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe