In the previous SensorDApp article, we explained how to save data in Telos smart contracts. In this article we will introduce how to declare the actions of the application.
You can see actions as the way to interact with the application. They are what we would call functions in a standard contract. They also can be public or private as a normal function.
But in Telos smart contracts there is an extra element, an action has to be declared in the ABI to be visible to the rest of users. But don’t worry, if we declare an action on the ABI it won’t mean that everyone can access it. We can restrict its use to certain accounts, using the whitelist we declared on the last article.
So let’s start by defining what actions are we going to need for our application:
- NewSensor: a user can create a new sensor and become its owner.
- DeleteSensor: the sensor owner can delete a specific sensor.
- Multiupload: an allowed user (the owner or someone present in the withelist) can make data insertions on a specific sensor.
- GrantUser: we can whitelist a user to make data insertions on our sensors.
- RevokeUser: we revoke a user permissions to make data insertions on our sensors.
You can see these actions implemented in the following code fragment:
1. /// @abi action
2. ACTION grantuser( const name& user);
3. /// @abi action
4. ACTION revokeuser( const name& user);
5. /// @abi action
6. ACTION newsensor(const name& owner, const uint64_t& id, const
7. std::string& location, const std::vector<std::string>&labels);
8. /// @abi action
9. ACTION deletesensor( const name& owner, const uint64_t id);
10. /// @abi action
11. ACTION multiupload( const name& owner, const uint64_t& id,
12. const uint64_t& timestamp, const std::vector<double>& values)
If you remember, we need to tell the compiler, something to identify table structures on compiling time. For actions, we need to follow the same pattern. We have to specify that the following lines are actions with “/// @abi action” as we can see on the previous snippet.
Below each action commented with “/// @abi action”, the actions previously described were declared as void type function. This is because smart contract actions cannot return any value. An action is a change to the Blockchain, so, all actions are like “set” methods in Java. They do not return any data they only change the state.
This “state” of the Blockchain is what is commonly known in Telos as the RAM, as it works like the RAM of a computer, as you can access it quickly. The inconvinience is that you have to rent a piece of this memory for your account if you want to store any data.
With this lines of code, the full skeleton of our contract has been built and our contract header has been finished. Now the only thing left is to implement the logic of the actions. But this will be done in the sensordapp.cpp that we will leave for the next article (see below).
Putting logic into action
The third part of SensorDApp tutorial continues diving deep into the smart contract development.
We hope that you liked it and so far, and more or less everything has been understood correctly. If not, remember that you can connect directly with us and the community of creators and developers of DApps for Telos at the Telos Dapp Development telegram group.
This tutorial has been created by members of The Teloscope team.
You can support us by including theteloscope among the list of BPs that deserve your vote.