Smart Contract Structure

In this tutorial, we will split the contract name_validator into files and compile it. To create the skeleton of your project, use eosio-init command line tool.

Note

If you have already created a contract name_name_validator and you still have a folder, to avoid conflicts, rename it or move it.

To create a skeleton of a new project name_validator, use:

$ eosio-init --path=. --project=name_validator

eosio-init interface

USAGE: eosio-init [options]

OPTIONS:

Generic Options:

-help
  • Display available options (-help-hidden for more)
-help-list
  • Display list of available options (-help-list-hidden for more)
-version
  • Display the version of this program

eosio-init: generates an eosio smart contract project

-bare
  • Produces only a skeleton smart contract without CMake support
-path
  • Directory to place the project
-project
  • Output project name

Let’s check at your project:

$ cd name_validator && tree

The structure of our project will look as follows:

├── build
├── CMakeLists.txt
├── include
│   └── name_validator.hpp
├── README.txt
├── ricardian
│   └── name_validator.contracts.md
└── src
    ├── CMakeLists.txt
    └── name_validator.cpp

Main CMakeLists project file will look as follows:

include(ExternalProject)
# if no cdt root is given use default path
if(EOSIO_CDT_ROOT STREQUAL "" OR NOT EOSIO_CDT_ROOT)
   find_package(eosio.cdt)
endif()

ExternalProject_Add(
   name_validator_project
   SOURCE_DIR ${CMAKE_SOURCE_DIR}/src
   BINARY_DIR ${CMAKE_BINARY_DIR}/name_validator
   CMAKE_ARGS -DCMAKE_TOOLCHAIN_FILE=${EOSIO_CDT_ROOT}/lib/cmake/eosio.cdt/EosioWasmToolchain.cmake
   UPDATE_COMMAND ""
   PATCH_COMMAND ""
   TEST_COMMAND ""
   INSTALL_COMMAND ""
   BUILD_ALWAYS 1
)

CMakeLists to compile the contract will look as follows:

project(name_validator)

set(EOSIO_WASM_OLD_BEHAVIOR "Off")
find_package(eosio.cdt)

add_contract( name_validator name_validator name_validator.cpp )
target_include_directories( name_validator PUBLIC ${CMAKE_SOURCE_DIR}/../include )
target_ricardian_directory( name_validator ${CMAKE_SOURCE_DIR}/../ricardian )

You can read more about CMake macros on EOSIO.

Before that, we have already created a contract:

Note

We will not discuss the details of the implementation of the contract. You can read about the implementation here.

#include <eosio/eosio.hpp>

using namespace eosio;

class [[eosio::contract]] name_validator : public contract {
  public:
      using contract::contract;

      [[eosio::action]]
      void validatename( std::string username ) {
          name user{username};
          check( !is_account(user), "account already exists" );
          check( user.length() == name_length, "account name must be 12 characters" );
      }
  private:
      uint8_t name_length = 12;
};

Let’s split it into files; first we define name_validator.hpp:

Warning

The code that was generated by eosio-init at the stage of creating the contract skeleton in files name_validator.cpp and name_validator.hpp must be deleted.

#include <eosio/eosio.hpp>

using namespace eosio;

class [[eosio::contract]] name_validator : public contract {
 public:
   using contract::contract;

   [[eosio::action]]
   void validatename( std::string username );

 private:
   uint8_t name_length = 12;
};

Now we define the implementation of our method validatename in name_validator.cpp:

#include <name_validator.hpp>

using namespace eosio;

void name_validator::validatename( std::string username ) {
   name user{username};
   check( !is_account(user), "account already exists" );
   check( user.length() == name_length, "account name must be 12 characters" );
}

To compile our contract go to the root of the name_validator directory, then:

$ cd build && cmake ..

Then, run make in build directory:

$ make

Note

Compiled files will be located in build/name_validator directory.

It will return something like:

[ 11%] Performing build step for 'name_validator_project'
Scanning dependencies of target name_validator
[ 50%] Building CXX object CMakeFiles/name_validator.dir/name_validator.obj
Warning, empty ricardian clause file
[100%] Linking CXX executable name_validator.wasm
[100%] Built target name_validator
[ 22%] No install step for 'name_validator_project'
[ 33%] No test step for 'name_validator_project'
[ 44%] Completed 'name_validator_project'
[100%] Built target name_validator_project

Note

Next time, to compile the contract after the changes, just go to build directory and run make.