You can now issue your own token on the Steem blockchain thanks to the Steem Smart Contracts!

in smartcontracts •  3 months ago

Hi there,

A few months ago, I read this post (link) written by @biophil, for those who have never heard of him, he invented the first token that can be issued on the Steem blockchain, POCKET, this was revolutionary for the Steem blockchain in a sense where you could turn a blockchain that is content creation driven into a smart and decentralized database. At this moment I thought, damn, why not push this idea further, why not create a smart contract platform on Steem! and that's how this story started...

A few days after this discovery I was already playing with a proof of concept and a few weeks after I launched the first working version on utopian (link). Today, I have improved it considerably and to be honest with you, I'm quite proud of what it became!

The project is called Steem Smart Contracts (SSC), it is what I like to call, the Steem sidechain. The all purpose of that is to be able to deploy smart contracts on the Steem blockchain and be able to interact with them. This sidechain is actually a blockchain coupled to a database but I'll talk about it more in a few lines.

How does it work?

Well, basically what you need is a Steem account and that's it, so if you're reading this, you might already be eligible to use this tool! To interact with this sidechain you need to either post a comment, post a custom_json or transfer Steem/SBD to someone (and use the memo). You only need to post a JSON that follows a specific pattern (more on that later) and the sidechain will catch it and run the smart contract code accordingly. Your Steem account is like your sidechain account as your permissions to run a smart contract will be based on it. (if I deploy a smart contract and says that only I can interact with it, then only I will be able to interact with it with my Steem account (unless other Steem accounts have authorities on my account but at the end my account will show up on the Steem blockchain)

Fine... but how do you retrieve these data? where are they?

The sidechain runs actually on a node, it's a nodejs (javascript) application, this application manage the blockchain (a JSON made of blocks) and a database (mongodb like). Both entities live in RAM to get the best performance as possible. The node also hosts a JSON RPC server, this is the key to retrieve data from the blockchain but most importantly from the database.
Anyone can decide to run a node as it is actually just a Steem blockchain reader, it is connected to a Steem full RPC node and read blocks after blocks to build the sidechain (and the database) so if you decide to deploy a smart contract and tomorrow you decide to stop using it, well, this won't be possible as anyone that setup a node is going to be able to rebuild the full sidechain just by replaying the Steem blockchain.

What's the all purpose of that though?

Well, you may think, what's the purpose of all of that and I will answer you, what's the all purpose of the smart contracts? With smart contracts you can make the Steem dapps even more decentralized! Why not host @utopian-io on it? Why not decentralize even more the @steemmonsters? And also, why not issue tokens on this sidechain? damn yes, why not! To what you will reply, but wait? aren't you talking about the SMTs? hmmm. not really, the SMTs are going to be tokens that will be based on the proof of brain consensus, they are Steem alikes and they will follow the same characteristics (in term of algorithm) but with a Smart Contract you can create whatever token you want, that will follow the characteristics you want to implement.

ok... you got me... how do I play with it now?

Easy peasy, I have a running node, I would say it's a test node for now, but it's the only running one...

I have deployed two smart contracts on it via Steem comments:

  • the first one is called "account": that's the first contract that you'll need to use if you want a smart contract to be able to find you in the sidechain (I would say that it's the only requirement to use the sidechain, the sidechain is totally independent from the Steem blockchain and doesn't know anything about it so for example if you want to issue tokens to an account then you need to make sure that this account exists on the Steem blockchain, so that's the workaround to help with that)
    I deployed this contract via this post just by sending a json formatted text, I basically said, I want the contract "account" to be deployed with the following code:

YWN0aW9ucy5jcmVhdGVTU0MgPSBmdW5jdGlvbiAocGF5bG9hZCkgewogIGRiLmNyZWF0ZVRhYmxlKCdhY2NvdW50cycpOwp9CgovLyByZWdpc3RlciBhbiBhY2NvdW50IGhlbHBzIG90aGVyIGNvbnRyYWN0cyB0byBrbm93IAovLyBpZiBhbiBhY2NvdW50IGV4aXN0cyBvbiB0aGUgU3RlZW0gYmxvY2tjaGFpbgphY3Rpb25zLnJlZ2lzdGVyID0gZnVuY3Rpb24gKHBheWxvYWQpIHsKICBsZXQgYWNjb3VudHMgPSBkYi5nZXRUYWJsZSgnYWNjb3VudHMnKTsKCiAgbGV0IGFjY291bnQgPSBhY2NvdW50cy5maW5kT25lKHsgJ2lkJzogc2VuZGVyIH0pOwoKICBpZiAoYWNjb3VudCA9PT0gbnVsbCkgewogICAgY29uc3QgbmV3QWNjb3VudCA9IHsKICAgICAgJ2lkJzogc2VuZGVyCiAgICB9OwoKICAgIGFjY291bnRzLmluc2VydChuZXdBY2NvdW50KTsKICB9IAp9

Wait a minute... what kind of code is that? oh yes, I forgot, to avoid parsing issues, to deploy the javascript code of your contract you need to send it as a base64 encoded string, don't worry, that's not magic, simply go to https://www.base64decode.org/ and paste the code there and you'll see how magic could be worse!

I'll do it for you this time as I'm already seeing this dude in the back sighing...

actions.createSSC = function (payload) {
  db.createTable('accounts');
}

// register an account helps other contracts to know 
// if an account exists on the Steem blockchain
actions.register = function (payload) {
  let accounts = db.getTable('accounts');

  let account = accounts.findOne({ 'id': sender });

  if (account === null) {
    const newAccount = {
      'id': sender
    };

    accounts.insert(newAccount);
  } 
}

Hmm, do I need to explain it? that's pretty basic, when the contract is deployed the action "createSSC" is automatically called (and it's called only once during the lifetime of a contract so be careful with it as it's the only place where you can initialize the storage for your smart contract). Then you can see that there is a "register" action, it basically adds the sender of the transaction to the "accounts" table. (so now another smart contract could call the "accounts" table of the contract "account" to verify if an account is valid. And that's it for this contract.

  • The second contract is a bit more complex but not that much, it's called "token" and it's a port of the EOS token contract... yup, why not? I deployed it thanks to this Steem post, this time I'll let you decode the code!

Basically this contract allows anyone to create a token with a specific and unique symbol, with the precision information and the maximum supply for it. It allows then the issuer of this token to issue tokens to himself or to other accounts (accounts that are registered), finally, it allows owners of the tokens to transfer them to someone else, I told you it's basic though powerful as the person that created the token, once the token issued, has no control over them.

You wanna try these contracts out? that's pretty easy, you can reply to this post by pasting the following JSON:

{
    "id": "ssc-b8923f70-c1f7-497f-961j",
    "json": {
        "contractName": "account",
        "contractAction": "register",
        "contractPayload": {}
    }
}

This is going to register your Steem account to the sidechain id "ssc-b8923f70-c1f7-497f-961j"

Then I can either send you some of my tokens called PKTC (POKET CASH) (one million max supply, that's a killer deal as I will give you 1000 PKTC which is worth nothing but there are only 1,000,000 of them :))

Or you can create your own token as easy as posting this:

{
    "id": "ssc-b8923f70-c1f7-497f-961j",
    "json": {
        "contractName": "token",
        "contractAction": "create",
        "contractPayload": {
            "symbol": "BTC",
            "precision": 8,
            "maxSupply": 27000000
        }
    }
}

Yes, why not issue Bitcoins on Steem? :) (the precision determines how many decimals your token supports, for example Steem/SBD will be 3

Once you're done with that (it may take a few seconds/minutes as your operation needs to be included in a Steem block and then parsed by the sidechain, but you know, Steem is quite fast), open you wonderful terminal aka tool for geek and paste this CURL command, it will basically show you the details of all token hosted on the sidechain:

curl -XPOST -H "Content-type: application/json" -d '{
    "jsonrpc": "2.0",
    "method": "findInTable",
    "params": {
        "contract": "token",
        "table": "tokens",
        "query": {
        }
    },
    "id": 1
}' 'http://ec2-54-147-134-162.compute-1.amazonaws.com:5000/contracts'

the result of the command as of now:

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "issuer": "harpagon",
        "symbol": "PKTC",
        "precision": 0,
        "maxSupply": 1000000,
        "supply": 1000,
        "meta": {
            "revision": 1,
            "created": 1536013042481,
            "version": 0,
            "updated": 1536013234396
        },
        "$loki": 1
    }
}

As you can see, 1000 token were issued but to whom?

This other command will show you the balances of the accounts:

curl -XPOST -H "Content-type: application/json" -d '{
    "jsonrpc": "2.0",
    "method": "findInTable",
    "params": {
        "contract": "token",
        "table": "balances",
        "query": {
        }
    },
    "id": 1
}' 'http://ec2-54-147-134-162.compute-1.amazonaws.com:5000/contracts'
{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "account": "harpagon",
        "symbol": "PKTC",
        "balance": 1000,
        "meta": {
            "revision": 0,
            "created": 1536013234396,
            "version": 0
        },
        "$loki": 1
    }
}

Indeed I issued myself 1000 PKTC... shame on me!

Ok guys, that's it for today, you can find the GitHub repo here, help me to make this tool even better: https://github.com/harpagon210/steemsmartcontracts

The link to the wiki here if you want more details on how it works: https://github.com/harpagon210/steemsmartcontracts/wiki

You can contact me on discord if you wanna chat a little bit Harpagon#4087

Alright, that's your turn to create the smart contract of your dream!

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:  

Quite ingenious project! I definitely see it having a ton of potential. However, I think that most "normal" users would have a rather hard time interacting with it. Some kind of abstraction layer would probably come in handy, allowing the average Joe to also make use of it. Otherwise it risk being a developers-only project.

I'm definitely going to follow the progress of this :-)

·

I agree, I don't think "normal" users will use it as it is now but that shouldn't be too difficult to develop an UI that will take care of building the appropriate JSON and post it on the Steem blockchain. However, I think that this tool is still kind of developer oriented (like all smart contract platforms) and I think that someone that develops a smart contract on this platform should also provide the appropriate UI (that will best fit his app). I would say this tool, right now, is the lowest layer of the dapp (it's like the ETH or EOS layer), then other tools need to come on on top of it to make it even funnier to use :)

You have a minor misspelling in the following sentence:

that's the first contract that you'll need to use if you want a smart contract to be able to find you in the sidechain (I would say that it's the only requirement to use the sidechain, the sidechain is totally independant from the Steem blockchain and doesn't know anything about it so for example if you want to issue tokens to an account then you need to make sure that this account exists on the Steem blockchain, so that's the workaround to help with that).
It should be independent instead of independant.

Congratulations @harpagon! You have completed the following 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

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

Congratulations! Your post has been selected as a daily Steemit truffle! It is listed on rank 3 of all contributions awarded today. You can find the TOP DAILY TRUFFLE PICKS HERE.

I upvoted your contribution because to my mind your post is at least 20 SBD worth and should receive 304 votes. It's now up to the lovely Steemit community to make this come true.

I am TrufflePig, an Artificial Intelligence Bot that helps minnows and content curators using Machine Learning. If you are curious how I select content, you can find an explanation here!

Have a nice day and sincerely yours,
trufflepig
TrufflePig

{
"id": "ssc-b8923f70-c1f7-497f-961d",
"json": {
"contractName": "account",
"contractAction": "register",
"contractPayload": {}
}
}

Very cool! I have been thinking along these lines for a while. But what are the incentives for anyone to run a node? If there is only one node, how can we trust it? How do you enforce consensus between nodes?

Don't get me wrong, that stuff is amazing! I too was amazed at biophil's Pocket (see here: https://steemit.com/steemdev/@tiagopaixao/smart-contracts-on-steem)

I have a couple of ideas I would like to run by you. I'll try to find you on discord.

·

As of now there is no incentives nor consensus... I would say it's like trusting a Steem full node... but that's for sure two pieces that I would like to implement, however, to be honest I haven't yet found the best approach for that! we can talk about it on discord ;) (discord id: Harpagon#4087)

·
·

some type of masternode programming?

Amazing job. I still think people will be more interested in EOS. Most of the smart contracts IMHO would be related to already STEEM related projects like SMTs. It's always nice to see additional teams bringing value to the network and not depending on Steemit,Inc to take care of everything.

I think most developers would appreciate this addition. Best of Luck!

·

Indeed people outside of the Steem community will be for sure more interested by the other smart contract platforms, this tool was mostly built for the Steem dapps environment.

Thanks!

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

Award for the number of comments 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

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

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

Award for the number of comments 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

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