Exploring Beneficiaries with Steem JS Library for Developers.

in Steem Dev2 years ago

image.png

Hello Steemians,

I hope, new developers will interest to get the knowledge of their posting process. we are going to explore how we can set the beneficiary at the time of post broadcasting using steem official API. We will perform both operations in a single transaction. means that steem comment broadcasting and beneficiary operation will perform in one go. In this post, I will explore this process using code snippets and explanations.

First thing is set up the environment. We will need to ensure that we are connected with Steemit API. add the necessary things in the below code that will be required for every post.

steem.api.setOptions({ url: 'https://api.steemit.com' });
var username = 'your username';
var privatePostingKey = 'your key';
var currentDate = new Date();
var title = 'your title';
var body = 'your content';
const uniqueIdentifier = Date.now().toString();
const permlink = `${ptitle}-${uniqueIdentifier}`;


Next step is to define the beneficiaries. Beneficiaries are individuals or accounts that will receive a portion of the post rewards. in the below code i am giving 10 percent beneficiary to shy-fox account.


// Define the beneficiary
const beneficiaries = [
  { account: 'shy-fox', weight: 1000 },
];

// Create a post with desired content
const post = {
  parent_author: '',
  parent_permlink: 'hive-144064',
  author: username,
  permlink: permlink,
  title: title,
  body: body,
  json_metadata: JSON.stringify({
    tags: ['boc-report', 'featuredreport', 'shy-fox', 'beautycreativity', 'steemit', 'report'],
    app: 'SteemX', // Use your application identifier
  }),
};


Now we will combine the comment and comment_options operations into a single transaction and broadcast it on Steemit network.



const operations = [
  ['comment', post],
  ['comment_options', commentOptions],
];

// Broadcast the transaction
async function broadcastPostWithOptions() {
  try {
    const tx = await steem.broadcast.sendAsync(
      { operations, extensions: [] },
      { posting: privatePostingKey }
    );

    showMessage('Featured Post broadcasted successfully:', tx);
  } catch (error) {
    showMessage('Error broadcasting post with options:', error);
  }
}


By following the above provided code snippets, you can set beneficiaries and post content in a single transaction on Steemit.


Please cast witness vote to bangla.Witness or set proxy to rme.

_
Vote bangla.witness as witness
Set Proxy rme as proxy

Special Thanks.

Cc: @rme
Cc: @hungry-griffin
Cc: @steemcurator01
Cc: @steemchiller

Thanks for giving your precious time to my post.
Follow | Upvote | Share | Comments
Follow me on Instagram.
Instagram

🆃🅷🅰️🅽🅺 🆈🅾️🆄

Coin Marketplace

STEEM 0.04
TRX 0.33
JST 0.091
BTC 63026.84
ETH 1771.20
USDT 1.00
SBD 0.39