Class: Radiator::Chain
- Inherits:
-
Object
- Object
- Radiator::Chain
- Defined in:
- lib/radiator/chain.rb
Overview
Examples …
To vote on a post/comment:
steem = Radiator::Chain.new(chain: :steem, account_name: 'your account name', wif: 'your wif')
steem.vote!(10000, 'author', 'post-or-comment-permlink')
To post and vote in the same transaction:
steem = Radiator::Chain.new(chain: :steem, account_name: 'your account name', wif: 'your wif')
steem.post!(title: 'title of my post', body: 'body of my post', tags: ['tag'], self_upvote: 10000)
To post and vote with declined payout:
steem = Radiator::Chain.new(chain: :steem, account_name: 'your account name', wif: 'your wif')
= {
title: 'title of my post',
body: 'body of my post',
tags: ['tag'],
self_upvote: 10000,
percent_hbd: 0
}
steem.post!()
Constant Summary collapse
- VALID_OPTIONS =
%w( chain account_name wif url failover_urls ).map(&:to_sym)
Class Method Summary collapse
Instance Method Summary collapse
-
#base_per_debt ⇒ Object
Returns the current base (e.g. STEEM) price in the debt asset (e.g SBD).
-
#base_per_mvest ⇒ Object
Returns the current base (e.g. STEEM) price in the vest asset (e.g. VESTS).
- #block_time ⇒ Object
-
#broadcast!(auto_reset = false) ⇒ Object
Broadcast queued operations.
-
#find_account(account_name) ⇒ ::Hash
Find a specific account by name.
-
#find_block(block_number) ⇒ ::Hash
Find a specific block by block number.
-
#find_comment(*args) ⇒ ::Hash
Find a specific comment by author and permlink or slug.
-
#followed_by(account_name) ⇒ ::Array<String>
List of accounts followed by account.
-
#following(account_name) ⇒ ::Array<String>
List of accounts following account.
-
#initialize(options = {}) ⇒ Chain
constructor
A new instance of Chain.
-
#properties ⇒ Object
Current dynamic global properties, cached for 3 seconds.
-
#reset ⇒ Object
Clears out all properties and operations.
-
#reset_operations ⇒ Object
Clears out queued operations.
-
#reset_properties ⇒ Object
Clears out queued properties.
Methods included from Mixins::ActsAsWallet
#claim_reward_balance, #claim_reward_balance!, #transfer, #transfer!
Methods included from Mixins::ActsAsVoter
Methods included from Mixins::ActsAsPoster
#delete_comment, #delete_comment!, #post, #post!
Constructor Details
#initialize(options = {}) ⇒ Chain
Returns a new instance of Chain.
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/radiator/chain.rb', line 51 def initialize( = {}) = .dup .each do |k, v| k = k.to_sym if VALID_OPTIONS.include?(k.to_sym) .delete(k) send("#{k}=", v) end end @account_name ||= ENV['ACCOUNT_NAME'] @wif ||= ENV['WIF'] reset end |
Class Method Details
.parse_slug(*args) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/radiator/chain.rb', line 38 def self.parse_slug(*args) args = [args].flatten if args.size == 1 case args[0] when String then split_slug(args[0]) when ::Hash then [args[0]['author'], args[0]['permlink']] end else args end end |
Instance Method Details
#base_per_debt ⇒ Object
Returns the current base (e.g. STEEM) price in the debt asset (e.g SBD).
156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/radiator/chain.rb', line 156 def base_per_debt api.get_feed_history do |feed_history| current_median_history = feed_history.current_median_history base = current_median_history.base base = base.split(' ').first.to_f quote = current_median_history.quote quote = quote.split(' ').first.to_f (base / quote) * base_per_mvest end end |
#base_per_mvest ⇒ Object
Returns the current base (e.g. STEEM) price in the vest asset (e.g. VESTS).
147 148 149 150 151 152 |
# File 'lib/radiator/chain.rb', line 147 def base_per_mvest total_vesting_fund_steem = properties.total_vesting_fund_steem.to_f total_vesting_shares_mvest = properties.total_vesting_shares.to_f / 1e6 total_vesting_fund_steem / total_vesting_shares_mvest end |
#block_time ⇒ Object
140 141 142 |
# File 'lib/radiator/chain.rb', line 140 def block_time Time.parse(properties.time + 'Z') end |
#broadcast!(auto_reset = false) ⇒ Object
Broadcast queued operations.
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 |
# File 'lib/radiator/chain.rb', line 235 def broadcast!(auto_reset = false) raise ChainError, "Required option: chain" if @chain.nil? raise ChainError, "Required option: account_name, wif" if @account_name.nil? || @wif.nil? begin transaction = Radiator::Transaction.new() transaction.operations = @operations response = transaction.process(true) rescue => e reset if auto_reset raise e end if !!response.result reset response else reset if auto_reset ErrorParser.new(response) end end |
#find_account(account_name) ⇒ ::Hash
91 92 93 94 95 96 97 |
# File 'lib/radiator/chain.rb', line 91 def find_account(account_name) api.get_accounts([account_name]) do |accounts, err| raise ChainError, ErrorParser.new(err) if !!err accounts[0] end end |
#find_block(block_number) ⇒ ::Hash
77 78 79 |
# File 'lib/radiator/chain.rb', line 77 def find_block(block_number) api.get_blocks(block_number).first end |
#find_comment(*args) ⇒ ::Hash
113 114 115 116 117 118 119 120 121 |
# File 'lib/radiator/chain.rb', line 113 def find_comment(*args) , permlink = Chain.parse_slug(args) api.get_content(, permlink) do |comment, err| raise ChainError, ErrorParser.new(err) if !!err comment unless comment.id == 0 end end |
#followed_by(account_name) ⇒ ::Array<String>
List of accounts followed by account.
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'lib/radiator/chain.rb', line 172 def followed_by(account_name) return [] if account_name.nil? followers = [] count = -1 until count == followers.size count = followers.size follow_api.get_followers(account: account_name, start: followers.last, type: 'blog', limit: 1000) do |follows, err| raise ChainError, ErrorParser.new(err) if !!err followers += follows.map(&:follower) followers = followers.uniq end end followers end |
#following(account_name) ⇒ ::Array<String>
List of accounts following account.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/radiator/chain.rb', line 195 def following(account_name) return [] if account_name.nil? following = [] count = -1 until count == following.size count = following.size follow_api.get_following(account: account_name, start: following.last, type: 'blog', limit: 100) do |follows, err| raise ChainError, ErrorParser.new(err) if !!err following += follows.map(&:following) following = following.uniq end end following end |
#properties ⇒ Object
Current dynamic global properties, cached for 3 seconds. This is useful for reading properties without worrying about actually fetching it over rpc more than needed.
126 127 128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/radiator/chain.rb', line 126 def properties @properties ||= nil if !!@properties && Time.now.utc - Time.parse(@properties.time + 'Z') > 3 @properties = nil end return @properties if !!@properties api.get_dynamic_global_properties do |properties| @properties = properties end end |
#reset ⇒ Object
Clears out all properties and operations.
225 226 227 228 229 230 |
# File 'lib/radiator/chain.rb', line 225 def reset reset_properties reset_operations @api = @block_api = @follow_api = nil end |
#reset_operations ⇒ Object
Clears out queued operations.
220 221 222 |
# File 'lib/radiator/chain.rb', line 220 def reset_operations @operations = [] end |
#reset_properties ⇒ Object
Clears out queued properties.
215 216 217 |
# File 'lib/radiator/chain.rb', line 215 def reset_properties @properties = nil end |