Steem.Js for dummies #6: Broadcasting several operationssteemCreated with Sketch.

in #utopian-io9 years ago

image.png

In this example, we will try the broadcast.send function to perform two simultaneous operations : post a comment (actually it's a post, not a comment, but it is regarded as the same), and comment_options to set beneficiaries.

I will take a top-down approach this time and will give you the relevant code from my beneficiaries code on @steem-plus:

var operations = [
        ['comment',
            {
                parent_author: '',
                parent_permlink: tags[0],
                author: author,
                permlink: permlink,
                title: title,
                body: body,
                json_metadata : JSON.stringify({
                tags: tags,
                app: 'steemplus'
                })
            }
        ],
        ['comment_options', {
            author: author,
            permlink: permlink,
            max_accepted_payout: '100000.000 SBD',
            percent_steem_dollars: sbdpercent,
            allow_votes: true,
            allow_curation_rewards: true,
            extensions: [
                [0, {
                    beneficiaries: beneficiaries
                }]
            ]
        }]
    ];
    steem.broadcast.send(
        { operations: operations, extensions: [] },
        { posting: wif },
        function(e, r) {
       //TODO : Do something here
});

Now, let's break it into pieces. Ready?
As you can see in the code, we will broadcast two operations:

Comment



In the first object of our array of operations, you will need to fill the following parameters:

'comment',
            {
                parent_author: '',
                parent_permlink: tags[0],
                author: author,
                permlink: permlink,
                title: title,
                body: body,
                json_metadata : JSON.stringify({
                tags: tags,
                app: 'steemplus'
                })
            }
  • parent_author : Who authored the post or comment you are replying to? Here we are not posting a comment but a blog, this field is thus null
  • parent_permlink : Permlink of the parent if you are sending a comment, for an original blog, it corresponds to the first tag.
  • author : really, you're going to make me write this? Put the name of the author there =p
  • permlink : create the permlink from the title and put it in here, be careful, no space allowed.
  • title and body : well...
  • json_metadata is an object containing additional information on the operation. Here, I ve only put the tags and the name of the app broadcasting.

You could broadcast this comment already, but let's make it better and associate it with the second operation:

CommentOptions



Here is what it looks like:

comment_options', {
            author: author,
            permlink: permlink,
            max_accepted_payout: '100000.000 SBD',
            percent_steem_dollars: sbdpercent,
            allow_votes: true,
            allow_curation_rewards: true,
            extensions: [
                [0, {
                    beneficiaries: beneficiaries
                }]
            ]
        }

In this part of the we have to redundant variables : author and permlink, so just input the same in here.
As for the rest:

  • max_accepted_payout is self-explanatory, put something big unless you want to decline part of the rewards
  • percent_steem : 10000 (100.00%) means 50/50 SBD/SP post, 0 means 100% SP post.
  • allow_votes: people can vote on your post, wouldn't really make sense to put false, right?
  • allow_curation_rewards: allow voters to get curation rewards
  • beneficiaries are under extensions. To create the object, follow my lead:
beneficiaries.push({
        account: 'steem-plus',
        weight: 100*STEEM_PLUS_FEED
    });

If STEEM_PLUS_FEED=5;, we will have a weight of 500 (5.00%). If that is the only beneficiary, by default, the author account will get the remaining 95% of the rewards. It has to be noted that all beneficiaries rewards are fully paid in SP regardless of percent_steem.

Finally send the broadcast



Now that everything is ready, you just need to send the broadcast:

 steem.broadcast.send(
        { operations: operations, extensions: [] },
        { posting: wif },
        function(e, r) {
       //TODO : Do something here
});

The only thing you need to do here is adding the PRIVATE posting WIF. Now, it should work ;)

Hope this helps!

@stoodkev



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

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

Award for the total payout received

Click on any badge to view your own Board of Honor on SteemitBoard.
For more information about SteemitBoard, click here

If you no longer want to receive notifications, reply to this comment with the word STOP

By upvoting this notification, you can help all Steemit users. Learn how here!

Thank you for helping aspiring steemdevs get started with ease!...

Hey @stoodkev I am @utopian-io. I have just upvoted you at 7% Power!

Achievements

  • WOW WOW WOW People loved what you did here. GREAT JOB!
  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!
  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • I introduced a competition factor. My vote is based also on how competitive the category used is.

Human Curation

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.076
BTC 61973.24
ETH 1625.90
USDT 1.00
SBD 0.40