Steem Smart Contracts: interaction via cutom json and transfer operations

in #utopian-io2 years ago

Repository

https://github.com/harpagon210/steemsmartcontracts

Related commits:

What feature(s) did you add?

1) replacement of the interaction via comment by custom_json

Before this commit, to interact with the sidechain you needed to post a message on the Steem blockchain that was following a specific pattern, this has been deprecated and you now have two ways to interact with the Steem Smart Contracts sidechain:

a) via custom_json

Send a custom_json operation to the Steem blockchain with the following parameters:

id: "ssc"
json:

{

"contractName": "NAME OF THE CONTRACT",

"contractAction": "ACTION OF THE CONTRACT TO PERFORM",

"contractPayload": { OBJECT THAT WILL BE PASSED TO THE CONTRACT ACTION }

}

b) via transfer

Transfer funds with the following json as memo:

{

"id": "ssc",

"json": {

"contractName": "NAME OF THE CONTRACT",

"contractAction": "ACTION OF THE CONTRACT TO PERFORM",

"contractPayload": { OBJECT  THAT  WILL  BE  PASSED  TO  THE  CONTRACT  ACTION }

}

}

How did you implement it?

Basically before this commit I was scanning the "comment" operations and then I was parsing the messages, now I am scanning the "transfer" operations as well as the "custom_json" operations.
link to code

if (operation[0] ===  'custom_json'  || operation[0] ===  'transfer') 
{ 
... 
}

I also had to update the parsing method as the data are now sent as a JSON (either via the JSON item of the custom_json operation or via the memo of the transfer)
link to code

if (operation[0] === 'custom_json') {

id = operation[1].id; // eslint-disable-line prefer-destructuring

sender = operation[1].required_posting_auths[0]; // eslint-disable-line

sscTransaction = JSON.parse(operation[1].json); // eslint-disable-line

} else if (operation[0] === 'transfer') {

sender = operation[1].from;

recipient = operation[1].to;

amount = operation[1].amount; // eslint-disable-line prefer-destructuring

const transferParams = JSON.parse(operation[1].memo);

id = transferParams.id; // eslint-disable-line prefer-destructuring

sscTransaction = transferParams.json; // eslint-disable-line prefer-destructuring

}

Example of a custom_json operation
Example of a transfer operation

2) implementation of the Steem transfer operation

The implementation of the transfer operation is a major feature that has been added with this update, indeed, by allowing the usage of this operation, the Steem Smart Contracts sidechains is now able to interact with the Steem blockchain at the "currency" level. Basically you can now send Steem or SBD to a Steem Smart Contract which means that these contracts can now have actions based on amount of currencies sent to the contract (hence allow users and owners of Smart Contracts to sell/buy digital assets).

Basically in a Smart Contract, two variables are now accessible if the transaction was made via a Steem transfer operation:

  • recipient: this variable holds the Steem username of the "to" of the Steem transfer operation
  • amountSTEEMSBD: this variable holds the amount sent via the Steem transfer operation, for instance you could find "10.000 STEEM" or "3.003 SBD"

These two variables have been added to the payload that is passed to the action of a contract:
link to code

contractPayload.recipient = recipient;
contractPayload.amountSTEEMSBD = amount;

GitHub Account

https://github.com/harpagon210

Sort:  

Thank you for your contribution. You are disallowing the console on this line
https://github.com/harpagon210/steemsmartcontracts/commit/35393c51be239d45eadb37ab259a1a6902f04214#diff-8e89c85f3650e78ec257a307fc4212f9R115, but I could not find any configuration of the same? Am I missing something?

Also in some places you are hardcoding some of the values like ssc, transfer,start block etc, can this be moved in a config file and then read it from there, because for example in the future if it changes you have to change everywhere wherever you are using it.

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 @codingdefined !

I disallowed the console because I figured out that it was logging too much unnecessary data... I should try to work on something that could allow/disallow the console debugging (an entry in the config file?)

I'll try to work on the hardcoded data to make everything a bit more flexible.

Thanks for the inputs, appreciate!

Thank you for your review, @codingdefined!

So far this week you've reviewed 12 contributions. Keep up the good work!

Hi @harpagon! We are @steem-ua, a new Steem dApp, computing UserAuthority for all accounts on Steem. We are currently in test modus upvoting quality Utopian-io contributions! Nice work!

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!

This is awesome, way better than SMTs. I want to figure out how to make eBooks as custom JSONs and have an automatic ebook store that sells the books on Steem for STEEM/SBD.

I'm not the most fancy techie out there, is there any more info that breaks things down for me? Thanks dude, you're awesome!

The main problem I see is that the database of the sidechain is public meaning that everyone can read its content. You will need to store your ebooks in an encrypted manner (or a download link) and will need, somehow (with a bot?), to send the decryption key to the buyer.