EOS developer - first contract - HelloWorld

in #helloworld6 years ago (edited)

EOS Smart contract HelloWorld

  • Platform: MacOS
  • Editor: CLion

Set the environment

  1. create a folder on your hard drive called eos for example.
  2. follow the instructions to deploy your local EOS blockchain in your "eos" folder : https://infinitexlabs.com/first-steps-in-eos-blockchain-development/
  3. Open CLion -> File -> New Project... and open your "eos" folder
  4. On the left colon, open "contracts " and create a new folder called HelloWorld

Build the HelloWorld contract

In your HelloWorld folder you need to create 3 files :

  1. CMakelists.txt
  2. HelloWorld.abi
  3. HelloWorld.cpp

CMakelists.txt

  file(GLOB ABI_FILES "*.abi")
  configure_file("${ABI_FILES}" "${CMAKE_CURRENT_BINARY_DIR}" COPYONLY)
  
  add_wast_executable(TARGET HelloWorld
  INCLUDE_FOLDERS "${STANDARD_INCLUDE_FOLDERS}"
  LIBRARIES libc libc++ eosiolib
  DESTINATION_FOLDER ${CMAKE_CURRENT_BINARY_DIR}
  )

HelloWorld.abi

   empty

HelloWorld.cpp

 #include <eosiolib/eosio.hpp>
 #include <eosiolib/print.hpp>

 using namespace eosio;

 class HelloWorld : public eosio::contract {
      public:
      using contract::contract;

       /// @abi action
      void hello(const name user){
           print("Hello, ", name{user});
      }
 };

 EOSIO_ABI(HelloWorld, (hello))

BUILD the smart contract

Add HelloWorld to the eos/contracts/CMakelists.txt.
You will see a list of all the contracts and you can add yours at the end:

   add_subdirectory(HelloWorld)

Screen Shot 2018-09-17 at 11.20.57.png

. Then click the "reload the changes" that appears on top of the page

. And build all the contracts : Menu -> Run -> Build


DEPLOY THE CONTRACT

Make sure your EOS blockchain is running locally in one of your terminal window

Go to your ~Documents/eos/build directory and write :

  sudo make install

Make sure there are no error before you continue.
Navigate to your HelloWorld folder from the command line: cd ~/Documents/eos/contracts/HelloWorld

To compile we can use the eosiocpp command, followed by -o (meaning it should compile) HelloWorld.wasp (the input) HelloWorld.cpp (the output)

 eosiocpp -o HelloWorld.wasp HelloWorld.cpp

Finally we need to generate the abi file using a similar command :

 eosiocpp -g HelloWorld.abi HelloWorld.cpp

Et voila !
The first HelloWorld contract done !

Coin Marketplace

STEEM 0.30
TRX 0.12
JST 0.033
BTC 64344.02
ETH 3142.36
USDT 1.00
SBD 4.01