how-to-deploy-the-calculator-smart-contract-using-eos.png

Calculator Dapp in EOS (Blockchain Applications)

03/July/2018
A smart contract is simply a set of code that allows one to connect to a blockchain. The smart contract allows us to create a Dapp. DApp is an abbreviated form for decentralized application also known as decentralized blockchain applications.Blockchain applications are disrupting all major sectors today.  Dapp is very similar to traditional web applications that use the same technology to provide services to the application or page.  One main change is that instead of an API connecting to a Database, we have a Smart Contract that connects to a blockchain. One could easily deploy Dapp since it is decentralized that is it hosted without a centralized server.

how-to-deploy-the-calculator-smart-contract-using-eos-1-blockchainexpert

After understanding the basic steps of installation of EOS and contract creation  ( (please refer the previous blogs ‘How to deploy and manipulate the contract of  “EOS Token” ?’) then we can start our own contract “calculator”.

Step 1:
First create a skeleton contract called "calculator", using the command:
$ eosiocpp -n ${contract}
Eg: $ eosiocpp -n calculator
This will create a folder called calculator. The folder will automatically create two files: 
By default, it will automatically generate code for hello contract

calculator/calculator.cpp
#include <eosiolib/eosio.hpp>
#include <eosiolib/print.hpp>
using namespace eosio;
class hello : public eosio::contract {
  public:
      using contract::contract;
      /// @abi action 
      void hi( account_name user ) {
         print( "Hello, ", name{user} );
      }
};
EOSIO_ABI( hello, (hi) )
calculator/calculator.hpp
/**
 *  @file
 *  @copyright defined in eos/LICENSE.txt
 */
#include <eosiolib/eosio.hpp>
Step 2:
Open the file “calculator.cpp”. Then start editing the “calculator.cpp”.
Write a contract for a calculator.
#include <eosiolib/eosio.hpp>
using namespace eosio;
class calc : public eosio::contract {
  public:
  using contract::contract;
  /// @abi action
  void add( uint64_t a,uint64_t b ) {
  uint64_t s=a+b;
      print( "ans =", s );
  }
  void sub( uint64_t a,uint64_t b ) {
  uint64_t s=a-b;
      print( "ans = ", s );
  }
  void div( uint64_t a,uint64_t b ) {
  uint64_t s=a/b;
      print( " ans = ", s );
  }
  void multi( uint64_t a,uint64_t b ) {
  uint64_t s=a*b;
      print( "ans = ", s );
  }
};
EOSIO_ABI(calc,(add)(sub)(div)(multi))
Now the smart contact is created for the calculator. Each function is called the action. In this contract, mainly four arithmetic actions are used in the Class ‘calc’  i.e: add(), sub(), div(), multi().
The Class calc is inherited from the eosio::contract class publicly. Functions used in this contract( eg: print) are the part of eosio namespace. 
Each action calls two parameters a and b and performs a particular action when it is called.
EOSIO_ABI is a macro for generating abi for the contract.

Step 3:
In the next step, we need to create two file format .wast and .abi
Firstly compile the code to web assembly file format(.wast) using eosiocpp tool.
$ eosiocpp -o calculator.wast calculator.cpp
The above code will automatically create two separate files in the calculator folder 
‘calculator.wast’ and ‘calculator.wasm files’
Next generate an .abi file
$ eosiocpp -g calculator.abi calculator.cpp
Created: calculator.abi
Please Note: Compiler generates Warning, Ignore that.

Step 4:
In next step create an account and deploy the contract:

If you already created an account then skip this step
$ cleos create account eosio calculator 
EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4 
EOS7ijWCBmoXBi3CgtK7DJxentZZeTkeUnaSDvyro9dq7Sd1C3dC4
Step 5:
Now set the contract to the above-created account using :
 $ cleos set contract {accountname} ./../{path to calculator}  -p {accountname}
 
Reading WAST/WASM from {path to calculator}/calculator/calculator.wasm...
Using already assembled WASM...
Publishing contract...

$ cleos set contract calculator {path to calculator} -p calculator
Now the contract is set.

Step 6:
Now run the contract:
$ cleos push action {account_name} {action_name} '["user"]' -p {account_name}
Eg:
cleos push action calculator  add '["5","5"]' -p calculator
In this, the action add is pushed. It will and execute that particular action.
Output:
executed transaction: 275a63a828c9f70dc65eeec96e92d5895495dde3ee92a64a4e06c4f8e17f85c7  112 bytes  28564 us
# calculator <= calculator::add          {"a":5,"b":5}
>> ans =10
Since there is no authentication is specified in our contract, anyone can call these actions.
Eg:
cleos push action calculator  sub '["5","5"]' -p user
Output:
executed transaction: 275a63a828c9f70dc65eeec96e92d5895495dde3ee92a64a4e06c4f8e17f85c7  112 bytes  28564 us
# calculator <= calculator::sub          {"a":5,"b":5}
>> ans =0
Here, the user is an external account accessing our deployed contract.
Smart contracts are incredibly powerful at the same time complex. One can create both correct and affordable smart contracts via researching the underlying tools and architecture.Blockchain applications are rapidly picking up footing over various enterprises.