Write a Steemit Web App: Part 6 - Follow/Unfollow Somebody

(Previous Post: Part 5)

Up to this point, this series has concentrated on reading data from the API. Now it's time to start writing back to the blockchain!

In Part 2, we retrieved the list of followers for an account. What if we wanted to follow (or unfollow) somebody? How can that be done programmatically?

Follows are saved in the blockchain as Custom JSON data. For example, in Block 13770966, we find this in txid 243c092653d8bd6e9098d653dbed8f2d7784f7d1:

["follow",{"follower":"euni","following":"kebi4all","what":["blog"]}]


Here, the account @euni had followed the account @kebi4all. (And in case it wasn't obvious, the record of your follows and unfollows are permanently stored in the blockchain for people to query until the end of time)

Broadcast Custom JSON

Let's suppose that you want to add a "follow" button next to an account in your web app. When that button is clicked, the event handler must execute a call to steem.broadcast.customJson() in order to announce the follow request to the network for inclusion in the blockchain:

let followReq = ["follow"]
followReq.push({follower: "you", following: "someone_else", what: ["blog"]})

const customJson = JSON.stringify(followReq)

steem.broadcast.customJsonAsync("5K...wif_here...", [], ["you"], "follow", customJson)
  .then(console.log)
  .catch(console.log)


The documentation for this function call can be found here:

steem.broadcast.customJson(wif, requiredAuths, requiredPostingAuths, id, json, function(err, result) {
  console.log(err, result);
});


The first argument is the WIF for posting, otherwise known as your Private Posting Key (to find it, go to your Wallet in Steemit.com => Permissions tab => click "Show Private Key" for the Posting key). This is something that your user will need to provide to your app, but treat it like a password - it should never be transmitted to nor stored on your server unless you absolutely need it while they are offline, and probably should be deleted after the browser session closes.

The second argument is just an empty array.

The third argument should have the account name as the only member of an array (i.e., your web app's user).

The fourth argument is a string simply containing "follow".

The fifth argument is the custom JSON string, as constructed above. The "what" property in the payload is an array that contains a single string: "blog" to follow the person, "ignore" to mute the person, and an empty string ("") to unfollow the person.

Note: If you are using node-style callbacks, then your callback function would be the sixth argument. Otherwise, if you use promises, then add Async to the function name and use .then() and .catch() in place of the callback function.

Throttling

Just a little warning: an account cannot broadcast to the blockchain more than once every three seconds. So, if you're trying to build a script to follow a massive amount of people, then you will need to throttle the broadcasts to wait three seconds between each.

javascriptlogo.png

(Next Post: Part 7)

H2
H3
H4
3 columns
2 columns
1 column
4 Comments