Steemjs Tutorial - Send SBD

in #steem-dev7 years ago

In this tutorial we will learn how to send SBD using SteemJS which is the JavaScript API for Steem blockchain. We will use steemjs and jquery to accomplish this task.

First thing we will do is setting up the html page for the input fields:


It will accept the following inputs:

  • Text box for user sending the money.
  • Password field for Private active/owner key for the user sending the money.
  • Textbox for amount to be sent.
  • Textbox for amount to be sent in SBD.
  • Button to invoke the transfer action.

Here is the html for the above fields.

    <div>
        From User
       <input type="text" id="txtFromUser" value="" style="width: 500px;" />
    </div>
    <div>
        Private Active/Owner Key
       <input type="password" id="txtActiveKey"   value="" style="width: 500px;" />
    </div>
   <div>
        To User
       <input type="text" id="txtToUser" value="" style="width: 500px;" />
    </div>
        <div>
        SBD AMOUNT
        <input type="text" id="txtSBDAmount"  style="width: 200px;" />
    </div>
    <div>
        <input id="btnSendSBD" type="button" value="Send SBD" />
    </div>
</div>

Next we import the steemjs and jquery libraries to be used for this functionality from the following CDN location:

The last step is to setup the javascript function that calls the steemjs function steem.broadcast.transfer.
This function has 4 input parameters:

  • wif --> value of password field txtActiveKey.
  • from --> Value of textbox txtFromUser.
  • to --> Value of textbox txtToUser.
  • amt --> Value of textbox txtSBDAmount. This needs to be formatted to 3 decimal places.
  • memo --> blank string in this case.

Here is the script to capture the input using jquery and call the steemjs email.

    $(document).ready(function () {

        $("#btnSendSBD").click(function () {

            var from = $("#txtFromUser").val();
            var wif = $("#txtActiveKey").val();
            var ul = document.getElementById('result');
            var amt = parseFloat($("#txtSBDAmount").val()).toFixed(3) + " SBD";
            var to = $("#txtToUser").val();
             steem.broadcast.transfer(wif, from, to, amt, "", function (sendError, sendResult) 
                {
                    console.log(sendError, sendResult);
                    if (sendError != null)
                        { 
                            console.log(sendError);
                            var li = document.createElement('li');
                            li.innerHTML = to + ' Send SBD Error';
                            ul.appendChild(li);
                        }
                    else {
                            var li = document.createElement('li');
                            li.innerHTML = from + ' sent ' + amt + ' to ' + to ;
                            ul.appendChild(li);
                        }
                }); 
          });
    });

Here is a screenshot of the html in action:

The complete source code is available at http://steemjs.blogspot.com/p/send-sbd.html

In the next tutorial I will show how to send SBD to all Voters of a post by URL.

Sort:  

This post recieved an upvote from minnowpond. If you would like to recieve upvotes from minnowpond on all your posts, simply FOLLOW @minnowpond

This post has received a 1.75 % upvote from @booster thanks to: @hackerwhacker.

Hi :)
I'm looking for a why to confirm transaction has got place with steem js library, maybe you know how to do it?

I want to create bot which do something after it receives money.

Got this error when try to post

B.AssertionError {name: "AssertionError", actual: 128, expected: 38, operator: "==", message: "Expected version 128, instead got 38", …}

Any solution?