DEV Community

Cover image for WAX Guestbook Smart Contract
Ivan Montiel
Ivan Montiel

Posted on • Updated on

WAX Guestbook Smart Contract

Introduction

In this section, we will dive deeper into WAX Smart Contracts but creating a more complex contract that includes storing data on the blockchain and providing actions for accounts to call on the blockchain.

Writing WAX Smart Contracts

WAX Smart Contracts are written in C++ and typically include header filers, type definitions, and actions. The contract definition may also include persistent data, permissions, and notify hooks.

The header file will contain our action definitions and our persistent blockchain data definitions.

The source file will contain the actual code implementations for the actions we defined in the header file.

We’ve been using the term action, but what is an action? Actions define the core functionality of our smart contracts. When an action runs, events are written to the WAX blockchain.

A transaction is a list of one or more actions executed in the same block. If one of our actions calls another actions, they will show up in the same transaction.

Let’s move onto a real example to see how this all comes together.

Guestbook Example

For the rest of this chapter, we are going to create a Smart Contract called a Guestbook. Accounts will be able to call an action called sign with their name and a message that we will store on the blockchain for all to see.

As before, let’s start with creating the empty project:

cd wax
eosio-init -project guestbook
Enter fullscreen mode Exit fullscreen mode

Then open the project folder in your favorite editor. I’ll be using VSCode throughout these tutorials.

Navigate to guestbook/include/guestbook.hpp and we can start defining some actions for our contract.

To start, we can replace the file with the following:

#include <eosio/eosio.hpp>
using namespace eosio;
using namespace std;

CONTRACT guestbook : public contract {
   public:
      using contract::contract;

      ACTION sign( name nm, string memo );

   private:
      TABLE guestbook_s {
         uint64_t id;
         name user;
         string memo;
         uint64_t timestamp;

         uint64_t primary_key() const { return id; }
      };
      typedef multi_index<name("guestbook"), guestbook_s> guestbook_t;
      guestbook_t guestbook = guestbook_t(get_self(), get_self().value);
};
Enter fullscreen mode Exit fullscreen mode

The #include line tells the compile that we will be using definitions defined by eosio. Then we will use the eosio and standard namespaces within our code.

The line CONTRACT guestbook : public contract { is the start of our contract definition.

The only public action we define is ACTION sign( name nm, string memo );. This is just the header definition, we will fill in the implementation later. This is the method others will call on this contract to add their name and memo to the smart contract’s guestbook table.

The private section of the contract defines the guestbook struct (which is what the _s suffix in the name stands for). This defines the data we will store in the smart contract’s table on the blockchain. The next two lines define the table on the blockchain.

Implementing the sign method

Next, let’s implement the sign method in guestbook/src/guestbook.cpp:

#include <eosio/eosio.hpp>
#include <eosio/system.hpp>
#include <guestbook.hpp>
using namespace eosio;
using namespace std;

ACTION guestbook::sign( name nm, string memo ) {
   require_auth(nm);

   guestbook.emplace(nm, [&](auto &row) {
     row.id = guestbook.available_primary_key();
     row.user = nm;
     row.memo = memo;
     row.timestamp = current_time_point().sec_since_epoch();
   });
}
Enter fullscreen mode Exit fullscreen mode

Here we check that the caller is the same as the name given using require_auth(nm), verifying that the current caller has the required authorization to act as that name. This allows chains of authority to act on behalf of signing as other names, which gives more flexibility to the caller on how they sign the contract.

The next section guestbook.emplace is where we actually add a row to the guestbook with a unique id, the name of the user, the memo they set, and the timestamp of when it was created.

Compiling

Let’s compile our contract:

( cd guestbook/build ; cmake .. ; make )
Enter fullscreen mode Exit fullscreen mode

You should have a successfully compiled contract!

Conclusion

In this section, we covered writing and compiling a simple Smart Contract that stores data on the WAX blockchain.

In the upcoming section, we will deploy this contract to our local blockchain, interact with it, and then deploy to the WAX testnet.

Next post: Deploying the WAX Guestbook Contract Locally

E-book

Get this entire WAX tutorial as an e-book on Amazon.

Additional links

Top comments (0)