Steem Smart Contracts, the sidechain that brings the power of Smart Contracts to the Steem blockchain

4 comments

harpagon
58
5 months agoBusy4 min read

Repository

https://github.com/harpagon210/steemsmartcontracts

Steem Smart Contracts

What is it?

Steem Smart Contracts is a sidechain powered by Steem. It allows you to perform actions on a decentralized database via the power of Smart Contracts. A sidechain is a blockchain that runs on the side of another blockchain, the "main chain". This sidechain can serve a totally different purpose, in the case of Steem Smart Contracts, the purpose is to be able to run Smart Contracts. A Smart Contract is a piece of code publicly visible on the blockchain that you can run from the Steem blockchain.

How does it work?

This is actually pretty easy. You just need a Steem account and that's it. To interact with the Smart Contracts, you simply post a message on the Steem blockchain (formatted in a specific way) and the message will then be caught by the sidechain and processed.

Sidechain specifications

  • run on node.js
  • database layer powered by LokiJS
  • Smart Contracts developed in Javascript
  • Smart Contracts run in a sandboxed Javascript Virtual Machine called VM2
  • a block on the sidechain is produced only if transactions are being parsed in a Steem block

How do I create a Smart Contract? How do I interact with it?

I created a wiki on GitHub, I think this is the best place to show you how all of this works:

Example of a Steem Smart Contract:

actions.create = function (payload) {
  db.createTable('users');
}

actions.addUser = function (payload) {
  const { username } = payload;
  if (username && typeof username === 'string') {

    const users = db.getTable('users');

    let user = users.findOne({ 'id': sender });

    if (user === null) {
      const newUser = {
        'id': sender,
        'username': username,
        'verified': false,
      };

      users.insert(newUser);
    }
  }
}

actions.updateUser = function (payload) {
  const { username } = payload;
  if (username && typeof username === 'string') {

    const users = db.getTable('users');
    let user = users.findOne({ 'id': sender });
    if (user) {
      user.username = username;
      users.update(user);
    }
  }
}

actions.removeUser = function (payload) {
  if (sender !== owner) return;

  const { userId } = payload;

  if (userId && typeof userId === 'string') {
    const users = db.getTable('users');
    let user = users.findOne({ 'id': userId });
    if (user)
      users.remove(user);
  }
}

As you can see this is easy to understand if you are familiar with Javascript but let me break the code down for you:

actions.create is the action that will be called when you will deploy a Smart Contract and this is the only place where you can, for example, initialize the tables that you Smart Contract will use, db.createTable('users'); will then initialize a table called 'users'

actions.addUser as you can imagine, this action will add a user to our Smart Contract, payload is a JSON object that contains the parameters sent by the sender during the transaction. db.getTable('users'); allow us to retrieve a table to then be able to interact with it. We can then perform a query on this table, a query "MongoDB Like" users.findOne({ 'id': sender });. We finally insert our user into the 'users' table via the method users.insert(newUser);

actions.updateUser will give the possibility to the user to update his username, we first retrieve the user via users.findOne({ 'id': sender });, we update his username and then update the 'users' table via users.update(user);

actions.removeUserwill allow only the contract owner to remove user from his Smart Contract, it is simply done by checking if sender is equal to owner, both are global variables available in your actions. After retrieving the user we can remove it from the table by using users.remove(user);

That's it for this contract but you can do much more, call tables from other Smart Contracts, execute other Smart Contracts from your Smart Contract, emit events, etc... more info on the wiki

Roadmap

This project is in its early stage, therefore, I consider it more as a Proof Of Concept for now as important features are still missing:

  • implement the P2P layer
  • implement the Consensus layer (PoS? DPoS? other?)

How to contribute?

Feel free to PR the project and contact me on Discord (Harpagon#4087)

GitHub Account

Paste here the full url to your GitHub account. e.g. https://github.com/harpagon210

Comments

Sort byBest