class Rubybear::Chain
Examples …
To vote on a post/comment:
bears = Rubybear::Chain.new(chain: :bears, account_name: 'your account name', wif: 'your wif') bears.vote!(10000, 'author', 'post-or-comment-permlink')
To post and vote in the same transaction:
bears = Rubybear::Chain.new(chain: :bears, account_name: 'your account name', wif: 'your wif') bears.post!(title: 'title of my post', body: 'body of my post', tags: ['tag'], self_upvote: 10000)
To post and vote with declined payout:
bears = Rubybear::Chain.new(chain: :bears, account_name: 'your account name', wif: 'your wif') options = { title: 'title of my post', body: 'body of my post', tags: ['tag'], self_upvote: 10000, percent_bears_dollars: 0 } bears.post!(options)
Constants
- VALID_OPTIONS
Public Class Methods
# File lib/rubybear/chain.rb, line 51 def initialize(options = {}) options = options.dup options.each do |k, v| k = k.to_sym if VALID_OPTIONS.include?(k.to_sym) options.delete(k) send("#{k}=", v) end end @account_name ||= ENV['ACCOUNT_NAME'] @wif ||= ENV['WIF'] reset end
# File lib/rubybear/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
Private Class Methods
# File lib/rubybear/chain.rb, line 257 def self.split_slug(slug) slug = slug.split('@').last author = slug.split('/')[0] permlink = slug.split('/')[1..-1].join('/') permlink = permlink.split('#')[0] [author, permlink] end
Public Instance Methods
Returns the current base (e.g. BEARS) price in the debt asset (e.g BSD).
# File lib/rubybear/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_mcoin end end
Returns the current base (e.g. BEARS) price in the coin asset (e.g. COINS).
# File lib/rubybear/chain.rb, line 147 def base_per_mcoin total_coining_fund_bears = properties.total_coining_fund_bears.to_f total_coining_shares_mcoin = properties.total_coining_shares.to_f / 1e6 total_coining_fund_bears / total_coining_shares_mcoin end
# File lib/rubybear/chain.rb, line 140 def block_time Time.parse(properties.time + 'Z') end
Broadcast queued operations.
@param auto_reset [boolean] clears operations no matter what, even if there's an error.
# File lib/rubybear/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 = Rubybear::Transaction.new(build_options) 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 a specific account by name.
Example:
bears = Rubybear::Chain.new(chain: :bears) ned = bears.find_account('ned') coining_shares = ned.coining_shares
@param account_name [String] Name of the account to find. @return [::Hash]
# File lib/rubybear/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 a specific block by block number.
Example:
bears = Rubybear::Chain.new(chain: :bears) block = bears.find_block(12345678) transactions = block.transactions
@param block_number [Fixnum] @return [::Hash]
# File lib/rubybear/chain.rb, line 77 def find_block(block_number) api.get_blocks(block_number).first end
Find a specific comment by author and permlink or slug.
Example:
bears = Rubybear::Chain.new(chain: :bears) comment = bears.find_comment('inertia', 'kinda-spooky') # by account, permlink active_votes = comment.active_votes
… or …
comment = bears.find_comment('@inertia/kinda-spooky') # by slug
@param args [String || ::Array<String>] Slug or author, permlink of comment. @return [::Hash]
# File lib/rubybear/chain.rb, line 113 def find_comment(*args) author, permlink = Chain.parse_slug(args) api.get_content(author, permlink) do |comment, err| raise ChainError, ErrorParser.new(err) if !!err comment unless comment.id == 0 end end
List of accounts followed by account.
@param account_name String Name of the account. @return [::Array<String>]
# File lib/rubybear/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
List of accounts following account.
@param account_name String Name of the account. @return [::Array<String>]
# File lib/rubybear/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
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.
# File lib/rubybear/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
Clears out all properties and operations.
# File lib/rubybear/chain.rb, line 225 def reset reset_properties reset_operations @api = @block_api = @follow_api = nil end
Clears out queued operations.
# File lib/rubybear/chain.rb, line 220 def reset_operations @operations = [] end
Clears out queued properties.
# File lib/rubybear/chain.rb, line 215 def reset_properties @properties = nil end
Private Instance Methods
# File lib/rubybear/chain.rb, line 275 def api @api ||= Api.new(build_options) end
# File lib/rubybear/chain.rb, line 279 def block_api @block_api ||= BlockApi.new(build_options) end
# File lib/rubybear/chain.rb, line 266 def build_options { chain: chain, wif: wif, url: url, failover_urls: failover_urls } end
# File lib/rubybear/chain.rb, line 291 def default_debt_asset case chain when :bears then ChainConfig::NETWORKS_BEARS_DEBT_ASSET when :test then ChainConfig::NETWORKS_TEST_DEBT_ASSET else; raise ChainError, "Unknown chain: #{chain}" end end
# File lib/rubybear/chain.rb, line 287 def default_max_acepted_payout "1000000.000 #{default_debt_asset}" end
# File lib/rubybear/chain.rb, line 283 def follow_api @follow_api ||= FollowApi.new(build_options) end