Hive Developers logo

Hive Developer Portal

Vote On Content

Create a weighted up or down vote on a comment/post.

Full, runnable src of Vote On Content can be downloaded as part of: tutorials/ruby (or download just this tutorial: devportal-master-tutorials-ruby-17_vote_on_content.zip).

Also see:

Sections

  1. Making the api call - broadcasting the operation
    1. Example api call - make the call in code
    2. Example api call using script - using our tutorial script
    3. Example Output - output from a successful call
    4. Example Error - error output from a unsuccessful call
  2. Vote Fields - understanding the result
  3. To Run - Running the example.

Making the api call

vote_on_content.rb

To broadcast the operation, we can use a Radiator::Transaction instance:

tx = Radiator::Transaction.new


tx.process(true)

Passing true to Radiator::Transaction#process will broadcast the operations queued in the operations array of the transaction.

Example api call

If we want to vote, for example:

tx.operations << {
  type: :vote,
  voter: voter,
  author: author,
  permlink: permlink,
  weight: weight
}

Example api call using script

And to do the same with our tutorial script:

ruby vote_on_content.rb https://hive.blog/@inertia/kinda-spooky

Example Output

From the example we get the following output from our script:

{
  "jsonrpc": "2.0",
  "result": {
    "id": "244a67bf1e64f05fb2ab52a0652a8edd30c5d273",
    "block_num": 27035223,
    "trx_num": 15,
    "expired": false
  },
  "id": 3
}

The response we get after broadcasting the transaction gives us the transaction id (244a67b...), block number (27035223), and the transaction number of that block (15).

Note, this script accepts accepts an optional percentage (defaulting 100.0 %). To set the vote to 50.0 %:

ruby vote_on_content.rb https://hive.blog/@inertia/kinda-spooky 50

Example Error

If an invalid vote weight is given (e.g.: 101 %), we will get back an error:

{
  "jsonrpc": "2.0",
  "error": {
    "code": -32000,
    "message": "Assert Exception:abs(weight) <= HIVE_100_PERCENT: Weight is not a HIVE percentage",
    "data": {
      "code": 10,
      "name": "assert_exception",
      "message": "Assert Exception",
      "stack": [
        {
          "context": {
            "level": "error",
            "file": "hive_operations.cpp",
            "line": 179,
            "method": "validate",
            "hostname": "",
            "timestamp": "2018-10-22T16:09:50"
          },
          "format": "abs(weight) <= HIVE_100_PERCENT: Weight is not a HIVE percentage",
          "data": {
          }
        },
        {
          "context": {
            "level": "warn",
            "file": "database.cpp",
            "line": 3491,
            "method": "_apply_transaction",
            "hostname": "",
            "timestamp": "2018-10-22T16:09:50"
          },
          "format": "",
          "data": {
            "trx": {
              "ref_block_num": 34171,
              "ref_block_prefix": 1240848906,
              "expiration": "2018-10-22T16:19:48",
              "operations": [
                {
                  "type": "vote_operation",
                  "value": {
                    "voter": "social",
                    "author": "inertia",
                    "permlink": "kinda-spooky",
                    "weight": 10100
                  }
                }
              ],
              "extensions": [

              ],
              "signatures": [
                "1c50556b312dd71446621fc3b509da3f5596ab20e8846edd7e55ce5fb13f51742c77d1ab021afa43e039ed2655f28beb1859924ddc6db1087742f3e63e4bc2502b"
              ]
            }
          }
        },
        {
          "context": {
            "level": "warn",
            "file": "database.cpp",
            "line": 817,
            "method": "push_transaction",
            "hostname": "",
            "timestamp": "2018-10-22T16:09:50"
          },
          "format": "",
          "data": {
            "trx": {
              "ref_block_num": 34171,
              "ref_block_prefix": 1240848906,
              "expiration": "2018-10-22T16:19:48",
              "operations": [
                {
                  "type": "vote_operation",
                  "value": {
                    "voter": "social",
                    "author": "inertia",
                    "permlink": "kinda-spooky",
                    "weight": 10100
                  }
                }
              ],
              "extensions": [

              ],
              "signatures": [
                "1c50556b312dd71446621fc3b509da3f5596ab20e8846edd7e55ce5fb13f51742c77d1ab021afa43e039ed2655f28beb1859924ddc6db1087742f3e63e4bc2502b"
              ]
            }
          }
        }
      ]
    }
  },
  "id": 3
}

This indicates that the operation was not included in the blockchain because it was given an invalid weight argument.

Vote Fields

Broadcasting a vote operation will require the following fields:

Final code:

require 'rubygems'
require 'bundler/setup'

Bundler.require

url, weight = ARGV
slug = url.split('@').last
author = slug.split('/')[0]
permlink = slug.split('/')[1..-1].join('/')
voter = 'social'
posting_wif = '5JrvPrQeBBvCRdjv29iDvkwn3EQYZ9jqfAHzrCyUvfbEbRkrYFC'
weight = ((weight || '100.0').to_f * 100).to_i
options = {
  url: 'https://testnet.openhive.network',
  wif: posting_wif
}

tx = Radiator::Transaction.new(options)

tx.operations << {
  type: :vote,
  voter: voter,
  author: author,
  permlink: permlink,
  weight: weight
}

puts JSON.pretty_generate tx.process(true)

To Run

First, set up your workstation using the steps provided in Getting Started. Then you can create and execute the script (or clone from this repository) with the following arguments:

You can launch a local testnet, with port 8090 mapped locally to the docker container:

docker run -d -p 8090:8090 inertia/tintoy:latest

For details on running a local testnet, see: Setting Up a Testnet

git clone https://gitlab.syncad.com/hive/devportal.git
cd devportal/tutorials/ruby/17_vote_on_content
bundle install
ruby vote_on_content.rb <url> [weight]