Class: Radiator::Chain

Inherits:
Object
  • Object
show all
Defined in:
lib/radiator/chain.rb

Overview

Examples …

To vote on a post/comment:

steem = Steem.new(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 = Steem.new(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 = Steem.new(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_steem_dollars: 0
}

steem.post!(options)

Direct Known Subclasses

Golos, Steem

Constant Summary collapse

VALID_OPTIONS =
%w(
  chain account_name wif
).map(&:to_sym)

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Chain

Returns a new instance of Chain.

Raises:



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/radiator/chain.rb', line 34

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']
  
  raise ChainError, "Required option: chain" if @chain.nil?
  raise ChainError, "Required option: account_name, wif" if @account_name.nil? || @wif.nil?
  
  reset
end

Instance Method Details

#broadcast!(auto_reset = false) ⇒ Object

Broadcast queued operations.

Parameters:

  • auto_reset (boolean) (defaults to: false)

    clears operations no matter what, even if there’s an error.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/radiator/chain.rb', line 61

def broadcast!(auto_reset = false)
  begin
    transaction = Radiator::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

#claim_reward_balance(options) ⇒ Object

Create a claim_reward_balance operation.

Examples:

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
steem.claim_reward_balance(reward_sbd: '100.000 SBD')
steem.broadcast!

Parameters:

  • options (Hash)

    options

Options Hash (options):

  • :reward_steem (String)

    The amount of STEEM to claim, like: ‘100.000 STEEM`

  • :reward_sbd (String)

    The amount of SBD to claim, like: ‘100.000 SBD`

  • :reward_vests (String)

    The amount of VESTS to claim, like: ‘100.000000 VESTS`



260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/radiator/chain.rb', line 260

def claim_reward_balance(options)
  reward_steem = options[:reward_steem] || '0.000 STEEM'
  reward_sbd = options[:reward_sbd] || '0.000 SBD'
  reward_vests = options[:reward_vests] || '0.000000 VESTS'
  
  @operations << {
    type: :claim_reward_balance,
    account: ,
    reward_steem: reward_steem,
    reward_sbd: reward_sbd,
    reward_vests: reward_vests
  }
  
  self
end

#claim_reward_balance!(permlink) ⇒ Object

Create a claim_reward_balance operation and broadcasts it right away.

Examples:

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
steem.claim_reward_balance!(reward_sbd: '100.000 SBD')


284
# File 'lib/radiator/chain.rb', line 284

def claim_reward_balance!(permlink); claim_reward_balance(permlink).broadcast!(true); end

#delete_comment(permlink) ⇒ Object

Create a delete_comment operation.

Examples:

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
steem.delete_comment('permlink')
steem.broadcast!

Parameters:

  • permlink


228
229
230
231
232
233
234
235
236
# File 'lib/radiator/chain.rb', line 228

def delete_comment(permlink)
  @operations << {
    type: :delete_comment,
    author: ,
    permlink: permlink
  }
  
  self
end

#delete_comment!(permlink) ⇒ Object

Create a delete_comment operation and broadcasts it right away.

Examples:

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
steem.delete_comment!('permlink')

See Also:



246
# File 'lib/radiator/chain.rb', line 246

def delete_comment!(permlink); delete_comment(permlink).broadcast!(true); end

#post(options = {}) ⇒ Object

Creates a post operation.

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
options = {
  title: 'This is my fancy post title.',
  body: 'This is my fancy post body.',
  tags: %w(thess are my fancy tags)
}
steem.post(options)
steem.broadcast!

Parameters:

  • options (Hash) (defaults to: {})

    options

Options Hash (options):

  • :title (String)

    Title of the post.

  • :body (String)

    Body of the post.

  • :tags (Array<String>)

    Tags of the post.

  • :permlink (String) — default: automatic

    Permlink of the post, defaults to formatted title.

  • :parent_permlink (String) — default: automatic

    Parent permlink of the post, defaults to first tag.

  • :parent_author (String) — default: optional

    Parent author of the post (only used if reply).

  • :max_accepted_payout (String) — default: 1000000.000 SBD

    Maximum accepted payout, set to ‘0.000 SBD’ to deline payout

  • :percent_steem_dollars (Integer) — default: 5000

    Percent STEEM Dollars is used to set 50/50 or 100% STEEM Power

  • :allow_votes (Integer) — default: true

    Allow votes for this post.

  • :allow_curation_rewards (Integer) — default: true

    Allow curation rewards for this post.

Raises:



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/radiator/chain.rb', line 151

def post(options = {})
  tags = [options[:tags] || []].flatten
  title = options[:title].to_s
  permlink = options[:permlink] || title.downcase.gsub(/[^a-z0-9\-]+/, '-')
  parent_permlink = options[:parent_permlink] || tags[0]
  
  raise ChainError, 'At least one tag is required or set the parent_permlink directy.' if parent_permlink.nil?
  
  body = options[:body]
  parent_author = options[:parent_author] || ''
  max_accepted_payout = options[:max_accepted_payout] || default_max_acepted_payout
  percent_steem_dollars = options[:percent_steem_dollars]
  allow_votes = options[:allow_votes] || true
  allow_curation_rewards = options[:allow_curation_rewards] || true
  self_vote = options[:self_vote]
  
  tags.insert(0, parent_permlink)
  tags = tags.compact.uniq
  
   = {
    app: Radiator::AGENT_ID
  }
  [:tags] = tags if tags.any?
  
  @operations << {
    type: :comment,
    parent_permlink: parent_permlink,
    author: ,
    permlink: permlink,
    title: title,
    body: body,
    json_metadata: .to_json,
    parent_author: parent_author
  }
  
  if (!!max_accepted_payout &&
      max_accepted_payout != default_max_acepted_payout
    ) || !!percent_steem_dollars || !allow_votes || !allow_curation_rewards
    @operations << {
      type: :comment_options,
      author: ,
      permlink: permlink,
      max_accepted_payout: max_accepted_payout,
      percent_steem_dollars: percent_steem_dollars,
      allow_votes: allow_votes,
      allow_curation_rewards: allow_curation_rewards,
      extensions: []
    }
  end
  
  vote(self_vote, , permlink) if !!self_vote
  
  self
end

#post!(options = {}) ⇒ Object

Create a vote operation and broadcasts it right away.

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
options = {
  title: 'This is my fancy post title.',
  body: 'This is my fancy post body.',
  tags: %w(thess are my fancy tags)
}
steem.post!(options)

See Also:



217
# File 'lib/radiator/chain.rb', line 217

def post!(options = {}); post(options).broadcast!(true); end

#resetObject

Clears out queued operations.



54
55
56
# File 'lib/radiator/chain.rb', line 54

def reset
  @operations = []
end

#transfer(options = {}) ⇒ Object

Create a transfer operation.

steem = Steem.new(account_name: 'your account name', wif: 'your active wif')
steem.transfer(amount: '1.000 SBD', to: 'account name', memo: 'this is a memo')
steem.broadcast!

Parameters:

  • options (Hash) (defaults to: {})

    options

Options Hash (options):

  • :amount (String)

    The amount to transfer, like: ‘100.000 STEEM`

  • :to (String)

    The account receiving the transfer.

  • :memo (String) — default: ''

    The memo for the transfer.



296
297
298
299
300
# File 'lib/radiator/chain.rb', line 296

def transfer(options = {})
  @operations << options.merge(type: :transfer, from: )
  
  self
end

#transfer!(options = {}) ⇒ Object

Create a transfer operation and broadcasts it right away.

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
steem.transfer!(amount: '1.000 SBD', to: 'account name', memo: 'this is a memo')

See Also:



308
# File 'lib/radiator/chain.rb', line 308

def transfer!(options = {}); transfer(options).broadcast!(true); end

#vote(weight, *args) ⇒ Object

Create a vote operation.

Examples:

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
steem.vote(10000, 'author', 'permlink')
steem.broadcast!

… or …

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
steem.vote(10000, '@author/permlink')
steem.broadcast!

Parameters:

  • weight (Integer)

    value between -10000 and 10000.

  • args (author, permlink || slug)

    pass either author and permlink or string containing both like ‘@author/permlink`.



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/radiator/chain.rb', line 96

def vote(weight, *args)
  author, permlink = if args.size == 1
    author, permlink = parse_slug(args[0])
  else
    author, permlink = args
  end
  
  @operations << {
    type: :vote,
    voter: ,
    author: author,
    permlink: permlink,
    weight: weight
  }
  
  self
end

#vote!(weight, *args) ⇒ Object

Create a vote operation and broadcasts it right away.

Examples:

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
steem.vote!(10000, 'author', 'permlink')

… or …

steem = Steem.new(account_name: 'your account name', wif: 'your wif')
steem.vote!(10000, '@author/permlink')

See Also:



127
# File 'lib/radiator/chain.rb', line 127

def vote!(weight, *args); vote(weight, *args).broadcast!(true); end