How to Switch Witness Votes in Ruby

View this thread on: d.buzz | hive.blog | peakd.com | ecency.com
·@inertia·
0.000 HBD
How to Switch Witness Votes in Ruby
<div class="pull-right">
  <blockquote>
    <img src="https://i.imgur.com/yvrNsIo.png" />
  </blockquote>
</div>

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](https://steemit.com/steem/@inertia/radiator-steem-ruby-api-client) 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](https://chainbb.com/f/ruby).  Once you have that sorted, come back here.

First, make a project folder:

```bash
$ mkdir radiator
$ cd radiator
```

Create a file named `Gemfile` containing:

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

Then run the command:

```bash
$ bundle install
```

Create a file named `switch_witness.rb` containing:

```ruby
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:

```bash
$ ruby switch_witness.rb gingerninja utopian-io
```

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

<center>
  [![](https://i.imgur.com/GZg5TRP.png)](https://steemd.com/tx/9782b50013fe3feb5722cadcb5cc4feccf3ba92e)
</center>

---

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*!!*<div class="chainbb-footer"><hr><em><small><a href="https://chainbb.com/radiator/@inertia/how-to-switch-witness-votes-in-ruby">Originally posted</a> in the <a href="https://chainbb.com/f/ruby">/f/ruby</a> forum on <a href="https://chainbb.com">chainBB.com</a> (<a href="https://chainbb.com/chainbb/@jesta/chainbb-frequently-asked-questions-faq">learn more</a>).</small></em></div>
👍 , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,