Module: Cosgrove::Utils
Instance Method Summary
collapse
-
#api(chain) ⇒ Object
-
#chain_options(chain) ⇒ Object
-
#core_asset(chain = :hive) ⇒ Object
-
#cycle_api_at ⇒ Object
-
#cycle_stream_at ⇒ Object
-
#debt_asset(chain = :hive) ⇒ Object
-
#find_author(options) ⇒ Object
-
#find_author_name_permlink(slug, chain = :hive) ⇒ Object
-
#find_comment(options) ⇒ Object
-
#find_comment_by_slug(slug, chain = :hive) ⇒ Object
-
#find_transfer(options) ⇒ Object
-
#follow_api(chain) ⇒ Object
-
#head_block_number(chain) ⇒ Object
-
#hive_engine(method, params = {}, rpc) ⇒ Object
-
#hive_engine_blockchain(method, params = {}) {|result| ... } ⇒ Object
-
#hive_engine_contracts(method, params = {}) {|result| ... } ⇒ Object
-
#hive_engine_shutdown ⇒ Object
-
#last_irreversible_block_num(chain = :hive) ⇒ Object
-
#new_tx(chain) ⇒ Object
-
#parse_slug(slug) ⇒ Object
-
#ping_api(chain) ⇒ Object
-
#properties(chain) ⇒ Object
-
#reset_api ⇒ Object
-
#reset_stream ⇒ Object
-
#steem_engine(method, params = {}, rpc) ⇒ Object
-
#steem_engine_blockchain(method, params = {}) {|result| ... } ⇒ Object
-
#steem_engine_contracts(method, params = {}) {|result| ... } ⇒ Object
-
#steem_engine_shutdown ⇒ Object
-
#stream(chain) ⇒ Object
-
#to_rep(raw) ⇒ Object
Methods included from Config
#channel_disable_comment_voting, #channel_upvote_weight, #cosgrove_allow_pm_commands, #cosgrove_client_id, #cosgrove_disable_comment_voting, #cosgrove_operators, #cosgrove_secure, #cosgrove_token, #cosgrove_upvote_weight, #discord_channels, #discord_fancy_log, #discord_log_mode, #hive_account, #hive_api_failover_urls, #hive_api_url, #hive_engine_api_url, #hive_posting_wif, #meeseeker_url, #steem_account, #steem_api_failover_urls, #steem_api_url, #steem_engine_api_url, #steem_posting_wif, #test_api_failover_urls, #test_api_url, #test_posting_wif
Instance Method Details
#api(chain) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
|
# File 'lib/cosgrove/utils.rb', line 46
def api(chain)
reset_api if cycle_api_at.nil? || cycle_api_at < 15.minutes.ago
@cycle_api_at ||= Time.now
case chain
when :steem then @steem_api ||= Radiator::Api.new(chain_options(chain))
when :hive then @hive_api ||= Radiator::Api.new(chain_options(chain))
when :test then @test_api ||= Radiator::Api.new(chain_options(chain))
end
end
|
#chain_options(chain) ⇒ Object
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
# File 'lib/cosgrove/utils.rb', line 23
def chain_options(chain)
case chain
when :steem
{
chain: :steem,
url: steem_api_url,
failover_urls: steem_api_failover_urls.any? ? steem_api_failover_urls : nil
}
when :hive
{
chain: :steem, url: hive_api_url,
failover_urls: hive_api_failover_urls.any? ? hive_api_failover_urls : nil
}
when :test
{
chain: :test,
url: test_api_url,
failover_urls: test_api_failover_urls
}
end
end
|
#core_asset(chain = :hive) ⇒ Object
404
405
406
407
408
409
410
411
412
413
|
# File 'lib/cosgrove/utils.rb', line 404
def core_asset(chain = :hive)
chain ||= :hive
chain = chain.to_s.downcase.to_sym
case chain
when :steem then 'STEEM'
when :hive then 'HIVE'
else; 'TESTS'
end
end
|
#cycle_api_at ⇒ Object
5
6
7
|
# File 'lib/cosgrove/utils.rb', line 5
def cycle_api_at
@cycle_api_at if defined? @cycle_api_at
end
|
#cycle_stream_at ⇒ Object
170
171
172
|
# File 'lib/cosgrove/utils.rb', line 170
def cycle_stream_at
@cycle_stream_at if defined? @cycle_stream_at
end
|
#debt_asset(chain = :hive) ⇒ Object
415
416
417
418
419
420
421
422
423
424
|
# File 'lib/cosgrove/utils.rb', line 415
def debt_asset(chain = :hive)
chain ||= :hive
chain = chain.to_s.downcase.to_sym
case chain
when :steem then 'SBD'
when :hive then 'HBD'
else; 'TBD'
end
end
|
#find_author(options) ⇒ Object
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
# File 'lib/cosgrove/utils.rb', line 323
def find_author(options)
chain = options[:chain] || :steem
chain = chain.to_s.downcase.to_sym
author_name = options[:author_name]
author = case chain
when :hive then HiveSQL::Account.where(name: author_name).first
end
if author.nil?
author = api(chain).get_accounts([author_name]) do |accounts, errors|
accounts.first
end
end
author
end
|
#find_author_name_permlink(slug, chain = :hive) ⇒ Object
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
|
# File 'lib/cosgrove/utils.rb', line 237
def find_author_name_permlink(slug, chain = :hive)
chain = chain.to_s.downcase.to_sym
op, author_name = slug.split(':')
author_name, offset = author_name.split(/[\+-]/)
author = find_account(author_name, nil, chain)
offset = offset.to_i
posts = if op == 'latest'
case chain
when :hive then HiveSQL::Comment.where(depth: 0, author: author.name).order(created: :desc)
end
elsif op == 'first'
case chain
when :hive then HiveSQL::Comment.where(depth: 0, author: author.name).order(created: :asc)
end
else
[]
end
if posts.any? && !!(post = posts[offset.to_i.abs])
return [author_name, post.permlink]
end
[]
end
|
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
|
# File 'lib/cosgrove/utils.rb', line 273
def (options)
chain = options[:chain] || :steem
chain = chain.to_s.downcase.to_sym
author_name = options[:author_name]
permlink = options[:permlink]
parent_permlink = options[:parent_permlink]
post = if chain == :hive
posts = posts.where(permlink: permlink) if !!permlink
posts = posts.where(parent_permlink: parent_permlink) if !!parent_permlink
posts.first
elsif chain == :hive
posts = HiveSQL::Comment.where(depth: 0, author: author_name)
posts = posts.where(permlink: permlink) if !!permlink
posts = posts.where(parent_permlink: parent_permlink) if !!parent_permlink
posts.first
end
if post.nil?
post = case chain
when :steem
posts = posts.where(permlink: permlink) if !!permlink
posts = posts.where(parent_permlink: parent_permlink) if !!parent_permlink
posts.first
when :hive
posts = HiveSQL::Comment.where(author: author_name)
posts = posts.where(permlink: permlink) if !!permlink
posts = posts.where(parent_permlink: parent_permlink) if !!parent_permlink
posts.first
end
end
if post.nil? && !!author_name && !!permlink
api(chain).get_content(author_name, permlink) do |content, errors|
unless content.author.empty?
post = content
end
end
end
post
end
|
266
267
268
269
270
271
|
# File 'lib/cosgrove/utils.rb', line 266
def (slug, chain = :hive)
chain ||= :hive
chain = chain.to_s.downcase.to_sym
author_name, permlink = parse_slug slug
(chain: chain, author_name: author_name, permlink: permlink)
end
|
#find_transfer(options) ⇒ Object
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
|
# File 'lib/cosgrove/utils.rb', line 342
def find_transfer(options)
chain = options[:chain]
from = options[:from]
to = options[:to]
memo_key = options[:memo].to_s.strip
op = case chain
when :hive
transfers = HiveSQL::Tx::Transfer.
where(from: from, to: hive_account).
where("memo LIKE ?", "%#{memo_key}%")
if transfers.any?
transfers.last
else
HiveSQL::Tx::Transfer.
where(from: from).
where(to: to).
where("memo LIKE ?", "%#{memo_key}%").last
end
end
if op.nil?
api(chain).get_account_history(hive_account, -1, 10000) do |history, error|
if !!error
ap error
return "Try again later."
end
op = history.map do |trx|
e = trx.last.op
type = e.first
next unless type == 'transfer'
o = e.last
next unless o.from == from
next unless o.to == hive_account
next unless o.memo =~ /.*#{memo_key}.*/
o
end.compact.last
end
end
op
end
|
#follow_api(chain) ⇒ Object
58
59
60
61
62
63
64
65
66
67
68
|
# File 'lib/cosgrove/utils.rb', line 58
def follow_api(chain)
reset_api if cycle_api_at.nil? || cycle_api_at < 15.minutes.ago
@cycle_api_at ||= Time.now
case chain
when :steem then @steem_follow_api ||= Radiator::FollowApi.new(chain_options(chain))
when :hive then @hive_follow_api ||= Radiator::FollowApi.new(chain_options(chain))
when :test then @test_follow_api ||= Radiator::FollowApi.new(chain_options(chain))
end
end
|
#head_block_number(chain) ⇒ Object
196
197
198
|
# File 'lib/cosgrove/utils.rb', line 196
def head_block_number(chain)
properties(chain)['head_block_number']
end
|
#hive_engine(method, params = {}, rpc) ⇒ Object
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/cosgrove/utils.rb', line 140
def hive_engine(method, params = {}, rpc)
begin
if params.respond_to?(:empty?) && params.empty?
rpc.send(method)
else
rpc.send(method, params)
end
rescue => e
hive_engine_shutdown
raise e
end
end
|
#hive_engine_blockchain(method, params = {}) {|result| ... } ⇒ Object
154
155
156
157
158
159
160
|
# File 'lib/cosgrove/utils.rb', line 154
def hive_engine_blockchain(method, params = {}, &block)
@hive_engine_blockchain ||= Radiator::SSC::Blockchain.new(root_url: hive_engine_api_url)
result = hive_engine(method, params, @hive_engine_blockchain)
yield result if !!block
return result
end
|
#hive_engine_contracts(method, params = {}) {|result| ... } ⇒ Object
162
163
164
165
166
167
168
|
# File 'lib/cosgrove/utils.rb', line 162
def hive_engine_contracts(method, params = {}, &block)
@hive_engine_contracts ||= Radiator::SSC::Contracts.new(root_url: hive_engine_api_url)
result = hive_engine(method, params, @hive_engine_contracts)
yield result if !!block
return result
end
|
#hive_engine_shutdown ⇒ Object
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'lib/cosgrove/utils.rb', line 120
def hive_engine_shutdown
problem = false
begin
@hive_engine_blockchain.shutdown if !!@hive_engine_blockchain
rescue => e
puts "Unable to shut down hive engine blockchain rpc: #{e}"
problem = true
end
begin
@hive_engine_contracts.shutdown if !!@hive_engine_contracts
rescue => e
puts "Unable to shut down hive engine contracts rpc: #{e}"
problem = true
end
!problem
end
|
#last_irreversible_block_num(chain = :hive) ⇒ Object
200
201
202
|
# File 'lib/cosgrove/utils.rb', line 200
def last_irreversible_block_num(chain = :hive)
properties(chain)['last_irreversible_block_num']
end
|
#new_tx(chain) ⇒ Object
204
205
206
207
208
209
210
|
# File 'lib/cosgrove/utils.rb', line 204
def new_tx(chain)
case chain
when :steem then Radiator::Transaction.new(chain_options(chain).merge(wif: steem_posting_wif))
when :hive then Radiator::Transaction.new(chain_options(chain).merge(wif: hive_posting_wif))
when :test then Radiator::Transaction.new(chain_options(chain).merge(wif: test_posting_wif))
end
end
|
#parse_slug(slug) ⇒ Object
223
224
225
226
227
228
229
230
231
232
233
234
235
|
# File 'lib/cosgrove/utils.rb', line 223
def parse_slug(slug)
case slug
when /^latest:/ then find_author_name_permlink(slug)
when /^first:/ then find_author_name_permlink(slug)
else
slug = slug.split('@').last
author_name = slug.split('/')[0]
permlink = slug.split('/')[1..-1].join('/')
permlink = permlink.split('#')[0]
[author_name, permlink]
end
end
|
#ping_api(chain) ⇒ Object
15
16
17
18
19
20
21
|
# File 'lib/cosgrove/utils.rb', line 15
def ping_api(chain)
api(chain).get_config do |config, errors|
reset_api if !!errors || config.keys.none?
end
true
end
|
#properties(chain) ⇒ Object
192
193
194
|
# File 'lib/cosgrove/utils.rb', line 192
def properties(chain)
api(chain).get_dynamic_global_properties.result
end
|
#reset_api ⇒ Object
9
10
11
12
13
|
# File 'lib/cosgrove/utils.rb', line 9
def reset_api
@steem_api = @hive_api = @test_api = nil
@steem_follow_api = @hive_follow_api = @test_follow_api = nil
@cycle_api_at = nil
end
|
#reset_stream ⇒ Object
174
175
176
177
178
|
# File 'lib/cosgrove/utils.rb', line 174
def reset_stream
@steem_stream = @hive_stream = @test_stream = nil
@steem_follow_stream = @hive_follow_stream = @test_follow_stream = nil
@cycle_stream_at = nil
end
|
#steem_engine(method, params = {}, rpc) ⇒ Object
90
91
92
93
94
95
96
97
98
99
100
101
102
|
# File 'lib/cosgrove/utils.rb', line 90
def steem_engine(method, params = {}, rpc)
begin
if params.respond_to?(:empty?) && params.empty?
rpc.send(method)
else
rpc.send(method, params)
end
rescue => e
steem_engine_shutdown
raise e
end
end
|
#steem_engine_blockchain(method, params = {}) {|result| ... } ⇒ Object
104
105
106
107
108
109
110
|
# File 'lib/cosgrove/utils.rb', line 104
def steem_engine_blockchain(method, params = {}, &block)
@steem_engine_blockchain ||= Radiator::SSC::Blockchain.new(root_url: steem_engine_api_url)
result = steem_engine(method, params, @steem_engine_blockchain)
yield result if !!block
return result
end
|
#steem_engine_contracts(method, params = {}) {|result| ... } ⇒ Object
112
113
114
115
116
117
118
|
# File 'lib/cosgrove/utils.rb', line 112
def steem_engine_contracts(method, params = {}, &block)
@steem_engine_contracts ||= Radiator::SSC::Contracts.new(root_url: steem_engine_api_url)
result = steem_engine(method, params, @steem_engine_contracts)
yield result if !!block
return result
end
|
#steem_engine_shutdown ⇒ Object
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
|
# File 'lib/cosgrove/utils.rb', line 70
def steem_engine_shutdown
problem = false
begin
@steem_engine_blockchain.shutdown if !!@steem_engine_blockchain
rescue => e
puts "Unable to shut down steem engine blockchain rpc: #{e}"
problem = true
end
begin
@steem_engine_contracts.shutdown if !!@steem_engine_contracts
rescue => e
puts "Unable to shut down steem engine contracts rpc: #{e}"
problem = true
end
!problem
end
|
#stream(chain) ⇒ Object
180
181
182
183
184
185
186
187
188
189
190
|
# File 'lib/cosgrove/utils.rb', line 180
def stream(chain)
reset_stream if cycle_stream_at.nil? || cycle_stream_at < 15.minutes.ago
@cycle_stream_at ||= Time.now
case chain
when :steem then @steem_stream ||= Radiator::Stream.new(chain_options(chain))
when :hive then @hive_stream ||= Radiator::Stream.new(chain_options(chain))
when :test then @test_stream ||= Radiator::Stream.new(chain_options(chain))
end
end
|
#to_rep(raw) ⇒ Object
212
213
214
215
216
217
218
219
220
221
|
# File 'lib/cosgrove/utils.rb', line 212
def to_rep(raw)
raw = raw.to_i
neg = raw < 0
level = Math.log10(raw.abs)
level = [level - 9, 0].max
level = (neg ? -1 : 1) * level
level = (level * 9) + 25
'%.2f' % level
end
|