How to Switch Witness Votes in Ruby

in #radiator6 years ago (edited)

Sometimes I will simultaneously unapprove one witness and approve another in the same transaction. It's more of an OCD thing than a necessity. It's slightly more efficient because the two operations happen in the same transaction. Not only that, but it tells a little story if someone goes and looks. It says, "I'm removing my vote for your witness and replacing it with this person over here."


To use this Radiator script, you'll need to install ruby, which I have documented on more than one occasion. If you need help, I recommend looking at the other artciles on /f/ruby. Once you have that sorted, come back here.

First, make a project folder:

$ mkdir radiator
$ cd radiator

Create a file named Gemfile containing:

source 'https://rubygems.org'
gem 'radiator'
gem 'highline'
gem 'awesome_print'

Then run the command:

$ bundle install

Create a file named switch_witness.rb containing:

require 'rubygems'
require 'bundler/setup'

Bundler.require

if ARGV.empty?
  puts "Usage: ruby #{__FILE__} <unapprove> <approve>"
  exit
end

unapprove, approve = ARGV
cli = HighLine.new
account_name = cli.ask 'Account: '
wif = cli.ask 'Active WIF: ' { |q| q.echo = '*' }

chain_options = {
  chain: :steem,
  url: 'https://api.steemit.com'
}

begin
  tx = Radiator::Transaction.new(chain_options.merge(wif: wif))
rescue RuntimeError => e
  if e.message == 'Invalid version'
    puts "Invalid WIF."
  else
    puts e.message
  end

  exit
end

unapprove_witness_vote = {
  type: :account_witness_vote,
  account: account_name,
  witness: unapprove,
  approve: false
}

approve_witness_vote = {
  type: :account_witness_vote,
  account: account_name,
  witness: approve,
  approve: true
}

tx.operations << unapprove_witness_vote
tx.operations << approve_witness_vote

response = tx.process(true)

if !!response.error
  error = Radiator::ErrorParser.new(response)
  puts error.error_message
else
  ap response.result
end

Then run it:

$ ruby switch_witness.rb gingerninja utopian-io

The above example would remove a vote for gingerninja and apply a vote for utopian-io:




By the way, I'm very pleased that the blockchain allows me to unapprove/approve in the same transaction even though I have already voted for 30 witnesses. I half expected this script to fail because the transaction contains an approval vote and I've already approved 30 slots. But instead, it correctly handles this situation.

To the Steemit, Inc. Blockchain Engineers: Well Done!!

Sort:  

Okay this is programming but I don't know the language, is it python?
The feature you just explained is wonderful but how did you come about it, it might help with future developments

The awesome Radiator gem @inertia made is written in Ruby, as is the sample code above.

Mate, can I just say, I completely understand the big picture and concept but when it comes to the actual detail - I'm at about 2 fifths of fuck all. I probably need to study up a bit more on this??

@inertia :) you created radiator gem ? for steemit ?
I also wanted to create a ruby gem for steemit :) guess you already done the job. probably i will use yours :) will try it very soon.

The Radiator gem is pretty sweet @bilal-haider I've been using it for another project at @steemsmarter to pull in all kinds of data from the blockchain and it's really well coded and not hard at all to get working.

I will try it for sure :) another friend already told me about it.. .. Thanks for sharing anyways.

Hello @inertia, I have a command suggestion for you. ;-) I'm sure my friend @robrigo would not mind, he has moved on and voted for me as witness.

ruby switch_witness.rb agoric.systems helo

Have a great day!

the only thing i understood in the post was that you liked UNO. but for that, you get my upvote. just checking in on you bud :)

one of my fav childhood memories. Don't forget to say "uno" :P

oh nice! I'm gonna have to check that radiator gem out

Hello informer,,, thanks for your kindness information post.
Upvoted

can we install it on laptop ?

I would love to make a animation about this give me your thoughts. I'm THE ONLY one animator on DTUBE please check me out and give your thoughts about creating a animation from this article! Thanks.

well done! Thank you for providing Ruby insights. Our team at @steemsmarter is using Ruby as well. Make sure and follow our main dev @thescubageek for more Ruby love.

I will try it for sure another friend already told me about it Thanks for sharing anyways,,,,