Basic Service (C++)
Here is a basic service definition. Place example.cpp and CMakeLists.txt in an empty folder.
example.cpp
#include <psibase/psibase.hpp>
// The service
struct ExampleService
{
// Add two numbers
int32_t add(int32_t a, int32_t b) { return a + b; }
// Multiply two numbers
int32_t multiply(int32_t a, int32_t b) { return a * b; }
};
// Reflect the service's methods. This enables
// PSIBASE_DISPATCH and other mechanisms to operate.
PSIO_REFLECT(ExampleService, //
method(add, a, b),
method(multiply, a, b))
// Allow users to invoke reflected methods inside transactions.
// Also allows other services to invoke these methods.
PSIBASE_DISPATCH(ExampleService)
CMakeLists.txt
# All cmake projects need these
cmake_minimum_required(VERSION 3.16)
project(example)
# Generate compile_commands.json to aid vscode and other editors
set(CMAKE_EXPORT_COMPILE_COMMANDS on)
# Libraries for building services and tests
find_package(psibase REQUIRED)
# Build example.wasm service
add_executable(example example.cpp)
target_link_libraries(example Psibase::service)
# Create a package
psibase_package(
NAME Example
VERSION 1.0.0
DESCRIPTION "An example service"
# The account that the service will run on
SERVICE example
# The CMake target that builds the service
TARGET example
)
Building
This will create a psibase package, Example.psi:
mkdir build
cd build
cmake `psidk-cmake-args` ..
make -j $(nproc)
Deploying the service
This, when run on a local test chain, will:
- Create the
exampleaccount, if it doesn't already exist. The account will be owned by therootaccount. - Deploy the
example.wasmservice on that service.
psibase install ./Example.psi