JavaScript Solutions, Competitive programming in JavaScript, MCQ in JS

Tuesday, 6 March 2018

Hello World in Solidity Programming

Hello Friends,

After the completion of our installation lets start with creating Hello World program in Solidity.

To start solidity we need to run first truffle command to initialize our basic app structure.

$ truffle init

This will create our basic app structure.

I am using Visual Studio Code as IDE, you can use any IDE.

Now let's go through the folder structure.

  • contracts:
    • contracts is the main folder where we will write our all contracts.
  • migrations:
    • migration is used to configure migration and deployment.
  • test:
    • test is folder used to write test cases for our smart contracts
  • truffle-config.js:
    • if you want to customize your truffle configuration then you can edit this file.
  • truffle.js :
    • its also used to configure truffle for development or production etc.

Create the first file in contracts folder with name HelloWorld.sol file
Write down following code  :

pragma solidity ^0.4.13;
contract HelloWorld {
function sayHello() public returns (string) {
return ("hello World");

Now let's create a new file in migration folder for deployment.

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

Lets edit  our truffle file :

module.exports = {
networks: {
development: {
host: "localhost",
port: 8545,
network_id: "*" // Match any network id

Now we have written our first Hello world program, to run it we need to run our testrpc server:

just run following command in terminal:

$ testrpc

Now open a new tab to compile our app, run following command to compile our app.

$ truffle compile

if there isn't any errors or warnings then it will compile properly and return to the terminal.

then run following command:

$ truffle compile

usersolidityHelloWorld webwerks$ truffle migrate
Using network 'development'.

Running migration: 1_initial_migration.js
Deploying Migrations...
... 0xea12dd3b0517asdf37e4cb3fcc64basdf61230a59378d68995asdf988cacd0d2c601e5364687
Migrations: 0xea35asdf5bff5e555c9asdf76f5a22008asdfd57a3f898d20f65
Deploying HelloWorld...
... 0xb649d108b056bd929dbeasdfc5fa87599379896d681294f6881f6eb2265f3bc0d211
HelloWorld: 0x8e5afbafe5b411d568f7767516c3aff0ccfde562
Saving successful migration to network...
... 0x77ccddddbe064bcecasdf53271965f135eac424741243515d6bb4c25f7c9470c54aca8a9
Saving artifacts...
Running migration: 2_deploy_contracts.js
Replacing Migrations...
... 0x5a711d3296b18f8c8040ed1a8e6a5ad700aaef6234112c0cd501aed066c5734218f14cc
Migrations: 0x4119bd0f8676625asd412329ca0b4bbdf763ae673865c3
Replacing HelloWorld...
... 0x10bb38b7eee7e234be872abcfeaasd1ga72350d288066c0829db4ed0212fd5bfef8dc330
HelloWorld: 0x9f11755a4ec076de0c8368e18ead4247570f38cb
Saving successful migration to network...
... 0xbc358f6d9b4aeb99952fbf8bf31324asdfvtde5996d21224e2f33f66ab18abc1e38780fed0
Saving artifacts...
user: solidityHelloWorld webwerks$

So lets run our program with truffle console.

user: solidityHelloWorld webwerks$ truffle console
truffle(development)> var hw
truffle(development)> HelloWorld.deployed().then(function(deployed){hw=deployed})
truffle(development)> hw.SayHello().call();
'Hello World'

 Cheers, Finally we have run our first Hello World program with solidity Programming.

Please comment if you get some errors or issues.

Thanks .....

No comments:

Post a Comment