Company Contract
Overview
This page will guide you to creating and developing a simple contract using Wire CDT and C++ language. While prior knowledge of C++ is not necessary for this tutorial, we encourage you to familiarize yourself with the basics of C++ to enhance your understanding.
Prerequisites
- Before proceeding forward, ensure that you have completed Getting Started section and that you have followed Getting Started Documentation Diagram.
- This page assumes you are familiar with Smart Contract Basics.
Step-by-Step Guide to Creating a Company Contract Smart Contract
1. Set Up Your Contract Workspace
Navigate to a directory of your choice and create a new subdirectory for your contract, along with its content include/
and src/
.
mkdir company-contract && cd company-contract && mkdir include src company && touch include/company.hpp src/company.cpp
Open company-contract
in your preferred text editor to begin coding.
2. Write the Contract Code
In C++, splitting class declarations and definitions into separate header (.hpp) and source (.cpp) files is a common practice that promotes better code organization, readability, and compile time-efficiency. This approach helps segregating interface from implementation; as well hiding the implementation details from other parts of the program and exposing only what is necessary. For the purposes of this and future tutorials, we will stick to that approach.
2.1. Define contract interfaces in company.hpp
- The
sysio/sysio.hpp
header file contains necessary classes and built-in utility functions for contract development.
#pragma once
#include <sysio/sysio.hpp>
#include <string>
using namespace sysio;
2.1. Define the employees
contract class
You can use either the simplified or the long syntax for defining contracts. In this tutorial we will be using primarily the short syntax for better readability. We would also define two actions upsertemp()
and getallemp()
.
#pragma once
#include <sysio/sysio.hpp>
#include <string>
using namespace sysio;
// Using short syntax for structs
CONTRACT company : public contract {
public:
using contract::contract;
// Action to upsert (insert or update) an employee record
ACTION upsertemp(name user, const std::string& name, const std::string& email, const std::string& status);
// Action to retrieve and print all employee records
ACTION getallemp();
}
Long syntax
// Using long syntax for structs
class [[sysio::contract("employees")]] employees : public contract {
public:
using contract::contract; // Inherits the base contract constructor.
employees(name receiver, name code, datastream<const char*> ds) : contract(receiver, code, ds) {}
};
The short syntax in C++ contracts, such as those used in the Wire blockchain framework, is possible due to the language’s support for preprocessor's directive like #define
. This directive is a preprocessor command that creates macros, serving as symbolic names or aliases for code fragments. Before compilation, the preprocessor scans the source code and replaces these macros with their defined sequences. Essentially, #define
allows developers to define constants, create function-like macros, or insert code snippets, improving readability and maintainability by eliminating repetitive code segments.
#define CONTRACT class [[sysio::contract]]
2.2. Define the Employee Struct and Multi-Index Table
Within the employees contract class, let's add a struct named employee
to represent the table that holds individual employee records. We will define it with several fields:
• user
: The name of the employee, which will serve as the primary key for the table. It uniquely identifies each employee record.
• name
: A string that stores the full name of the employee.