EOS developer - first contract - HelloWorld
EOS Smart contract HelloWorld
- Platform: MacOS
- Editor: CLion
Set the environment
- create a folder on your hard drive called eos for example.
- follow the instructions to deploy your local EOS blockchain in your "eos" folder : https://infinitexlabs.com/first-steps-in-eos-blockchain-development/
- Open CLion -> File -> New Project... and open your "eos" folder
- 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 :
- CMakelists.txt
- HelloWorld.abi
- 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)
. 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 !