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

in utopian-io •  10 months ago

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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Thank you for your contribution. Though you have beautifully explained about the project, it would be nice to know what is the main purpose of using it, why should people use sidechain instead of normal steem blockchain.

Your contribution has been evaluated according to Utopian policies and guidelines, as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, click here.


Need help? Write a ticket on https://support.utopian.io/.
Chat with us on Discord.
[utopian-moderator]

·

thanks for the review, this post was assuming that people know what is a sidechain and what are smart contracts, I guess that was a wrong assumption.

Hey @harpagon
Thanks for contributing on Utopian.
We’re already looking forward to your next contribution!

Want to chat? Join us on Discord https://discord.gg/h52nFrV.

Vote for Utopian Witness!

Congratulations @harpagon! You have completed some achievement on Steemit and have been rewarded with new badge(s) :

Award for the number of upvotes received

Click on the badge to view your Board of Honor.
If you no longer want to receive notifications, reply to this comment with the word STOP

To support your work, I also upvoted your post!

Do not miss the last post from @steemitboard!


Participate in the SteemitBoard World Cup Contest!
Collect World Cup badges and win free SBD
Support the Gold Sponsors of the contest: @good-karma and @lukestokes


Do you like SteemitBoard's project? Then Vote for its witness and get one more award!