create account

Steem Smart Contracts: interaction via cutom json and transfer operations by harpagon

View this thread on: hive.blogpeakd.comecency.com
· @harpagon ·
$21.12
Steem Smart Contracts: interaction via cutom json and transfer operations
#### Repository
https://github.com/harpagon210/steemsmartcontracts

Related commits: 
- [replacement of the interaction via comment by custom_json](https://github.com/harpagon210/steemsmartcontracts/commit/35393c51be239d45eadb37ab259a1a6902f04214)
- [implementation of the Steem transfer operation](https://github.com/harpagon210/steemsmartcontracts/commit/39d842806eaf137dfc45a39b485f2d5b62f36255)

## 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: 
```js
{

"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:

```js
{

"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](https://github.com/harpagon210/steemsmartcontracts/blob/master/libs/SteemStreamer.js#L81)
```js
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](https://github.com/harpagon210/steemsmartcontracts/blob/master/libs/SteemStreamer.js#L91)
```js
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](https://steemd.com/tx/e8e9bb4f1af59cac459c502650ff806e5d0691cc)
[Example of a transfer operation](https://steemd.com/tx/d78fc59a9ae902cb95aa966c0e5f2db41c88b0d8)

### 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](https://github.com/harpagon210/steemsmartcontracts/blob/master/libs/SteemStreamer.js#L125)
```js
contractPayload.recipient = recipient;
contractPayload.amountSTEEMSBD = amount;
```

#### GitHub Account
https://github.com/harpagon210
πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 53 others
properties (23)
authorharpagon
permlinksteem-smart-contracts-interaction-via-cutom-json-and-transfer-operations
categoryutopian-io
json_metadata{"tags":["utopian-io","development"],"app":"steem-plus-app"}
created2018-08-19 21:40:51
last_update2018-08-19 21:40:51
depth0
children7
last_payout2018-08-26 21:40:51
cashout_time1969-12-31 23:59:59
total_payout_value15.794 HBD
curator_payout_value5.330 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length4,168
author_reputation20,287,642,226,146
root_title"Steem Smart Contracts: interaction via cutom json and transfer operations"
beneficiaries
0.
accountsteemplus-pay
weight100
1.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id68,736,888
net_rshares16,593,174,125,752
author_curate_reward""
vote details (117)
@codingdefined · (edited)
$5.67
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](https://join.utopian.io/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](https://review.utopian.io/result/3/2332222).

---- 
Need help? Write a ticket on https://support.utopian.io/. 
Chat with us on [Discord](https://discord.gg/uTyJkNm). 
[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , ,
properties (23)
authorcodingdefined
permlinkre-harpagon-steem-smart-contracts-interaction-via-cutom-json-and-transfer-operations-20180821t043851780z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"links":["https://github.com/harpagon210/steemsmartcontracts/commit/35393c51be239d45eadb37ab259a1a6902f04214#diff-8e89c85f3650e78ec257a307fc4212f9R115","https://join.utopian.io/guidelines","https://review.utopian.io/result/3/2332222","https://support.utopian.io/","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"app":"steemit/0.1"}
created2018-08-21 04:38:51
last_update2018-08-21 04:41:15
depth1
children2
last_payout2018-08-28 04:38:51
cashout_time1969-12-31 23:59:59
total_payout_value4.276 HBD
curator_payout_value1.389 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,053
author_reputation528,100,925,972,008
root_title"Steem Smart Contracts: interaction via cutom json and transfer operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,873,865
net_rshares4,178,578,109,450
author_curate_reward""
vote details (8)
@harpagon ·
$0.04
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!
πŸ‘  
properties (23)
authorharpagon
permlinkre-codingdefined-re-harpagon-steem-smart-contracts-interaction-via-cutom-json-and-transfer-operations-20180821t131656534z
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.6","format":"markdown","tags":["utopian-io"],"users":["codingdefined"],"links":["/@codingdefined"],"image":[]}
created2018-08-21 13:16:57
last_update2018-08-21 13:16:57
depth2
children0
last_payout2018-08-28 13:16:57
cashout_time1969-12-31 23:59:59
total_payout_value0.030 HBD
curator_payout_value0.009 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length364
author_reputation20,287,642,226,146
root_title"Steem Smart Contracts: interaction via cutom json and transfer operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,912,732
net_rshares30,128,836,814
author_curate_reward""
vote details (1)
@utopian-io ·
$0.02
Thank you for your review, @codingdefined!

So far this week you've reviewed 12 contributions. Keep up the good work!
πŸ‘  ,
properties (23)
authorutopian-io
permlinkre-re-harpagon-steem-smart-contracts-interaction-via-cutom-json-and-transfer-operations-20180821t043851780z-20180824t051009z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-08-24 05:10:09
last_update2018-08-24 05:10:09
depth2
children0
last_payout2018-08-31 05:10:09
cashout_time1969-12-31 23:59:59
total_payout_value0.018 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length117
author_reputation152,955,367,999,756
root_title"Steem Smart Contracts: interaction via cutom json and transfer operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id69,192,836
net_rshares16,328,992,909
author_curate_reward""
vote details (2)
@hobo.media ·
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!
properties (22)
authorhobo.media
permlinkre-harpagon-steem-smart-contracts-interaction-via-cutom-json-and-transfer-operations-20181203t214144999z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"app":"steemit/0.1"}
created2018-12-03 21:41:45
last_update2018-12-03 21:41:45
depth1
children1
last_payout2018-12-10 21:41:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length300
author_reputation21,121,859,045,871
root_title"Steem Smart Contracts: interaction via cutom json and transfer operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id76,314,068
net_rshares0
@harpagon ·
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.
πŸ‘  , , ,
properties (23)
authorharpagon
permlinkre-hobomedia-re-harpagon-steem-smart-contracts-interaction-via-cutom-json-and-transfer-operations-20181204t152650919z
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.6","format":"markdown","tags":["utopian-io"],"users":[],"links":[],"image":[]}
created2018-12-04 15:26:54
last_update2018-12-04 15:26:54
depth2
children0
last_payout2018-12-11 15:26:54
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length272
author_reputation20,287,642,226,146
root_title"Steem Smart Contracts: interaction via cutom json and transfer operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id76,350,707
net_rshares2,657,684,190
author_curate_reward""
vote details (4)
@steem-ua ·
$0.02
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!
πŸ‘  
properties (23)
authorsteem-ua
permlinkre-steem-smart-contracts-interaction-via-cutom-json-and-transfer-operations-20180821t050749z
categoryutopian-io
json_metadata"{"app": "beem/0.19.54"}"
created2018-08-21 05:07:48
last_update2018-08-21 05:07:48
depth1
children0
last_payout2018-08-28 05:07:48
cashout_time1969-12-31 23:59:59
total_payout_value0.016 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length185
author_reputation23,214,230,978,060
root_title"Steem Smart Contracts: interaction via cutom json and transfer operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,876,141
net_rshares15,620,559,499
author_curate_reward""
vote details (1)
@utopian-io ·
$0.02
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.**

<a href='https://v2.steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
πŸ‘  
properties (23)
authorutopian-io
permlinkre-steem-smart-contracts-interaction-via-cutom-json-and-transfer-operations-20180821t145534z
categoryutopian-io
json_metadata"{"app": "beem/0.19.42"}"
created2018-08-21 14:55:36
last_update2018-08-21 14:55:36
depth1
children0
last_payout2018-08-28 14:55:36
cashout_time1969-12-31 23:59:59
total_payout_value0.016 HBD
curator_payout_value0.004 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length301
author_reputation152,955,367,999,756
root_title"Steem Smart Contracts: interaction via cutom json and transfer operations"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id68,922,110
net_rshares15,382,682,959
author_curate_reward""
vote details (1)