hive_sql 1.0.0.pre.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (68) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +58 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE +41 -0
  5. data/Rakefile +538 -0
  6. data/bin/hive_sql +23 -0
  7. data/hive_sql.gemspec +41 -0
  8. data/lib/hive_sql.rb +101 -0
  9. data/lib/hive_sql/models/account.rb +119 -0
  10. data/lib/hive_sql/models/block.rb +18 -0
  11. data/lib/hive_sql/models/comment.rb +142 -0
  12. data/lib/hive_sql/models/community.rb +78 -0
  13. data/lib/hive_sql/models/community_role.rb +37 -0
  14. data/lib/hive_sql/models/community_subscriber.rb +23 -0
  15. data/lib/hive_sql/models/connection.rb +18 -0
  16. data/lib/hive_sql/models/dynamic_global_properties.rb +45 -0
  17. data/lib/hive_sql/models/follower.rb +17 -0
  18. data/lib/hive_sql/models/reblog.rb +20 -0
  19. data/lib/hive_sql/models/sql_base.rb +28 -0
  20. data/lib/hive_sql/models/tag.rb +15 -0
  21. data/lib/hive_sql/models/token.rb +27 -0
  22. data/lib/hive_sql/models/transaction.rb +76 -0
  23. data/lib/hive_sql/models/tx/account_create.rb +26 -0
  24. data/lib/hive_sql/models/tx/account_recover.rb +20 -0
  25. data/lib/hive_sql/models/tx/account_update.rb +22 -0
  26. data/lib/hive_sql/models/tx/account_witness_proxy.rb +31 -0
  27. data/lib/hive_sql/models/tx/account_witness_vote.rb +22 -0
  28. data/lib/hive_sql/models/tx/claim_reward_balance.rb +21 -0
  29. data/lib/hive_sql/models/tx/comment.rb +24 -0
  30. data/lib/hive_sql/models/tx/comments_option.rb +23 -0
  31. data/lib/hive_sql/models/tx/convert.rb +20 -0
  32. data/lib/hive_sql/models/tx/custom.rb +25 -0
  33. data/lib/hive_sql/models/tx/custom/community.rb +32 -0
  34. data/lib/hive_sql/models/tx/custom/follow.rb +48 -0
  35. data/lib/hive_sql/models/tx/custom/reblog.rb +22 -0
  36. data/lib/hive_sql/models/tx/custom/witness.rb +11 -0
  37. data/lib/hive_sql/models/tx/delegate_vesting_share.rb +20 -0
  38. data/lib/hive_sql/models/tx/delete_comment.rb +19 -0
  39. data/lib/hive_sql/models/tx/escrow_approve.rb +23 -0
  40. data/lib/hive_sql/models/tx/escrow_dispute.rb +22 -0
  41. data/lib/hive_sql/models/tx/escrow_release.rb +25 -0
  42. data/lib/hive_sql/models/tx/escrow_transfer.rb +28 -0
  43. data/lib/hive_sql/models/tx/feed.rb +20 -0
  44. data/lib/hive_sql/models/tx/limit_order.rb +26 -0
  45. data/lib/hive_sql/models/tx/pow.rb +19 -0
  46. data/lib/hive_sql/models/tx/transfer.rb +24 -0
  47. data/lib/hive_sql/models/tx/update_proposal_vote.rb +31 -0
  48. data/lib/hive_sql/models/tx/vote.rb +21 -0
  49. data/lib/hive_sql/models/tx/withdraw.rb +19 -0
  50. data/lib/hive_sql/models/tx/withdraw_vesting_route.rb +21 -0
  51. data/lib/hive_sql/models/tx/witness_update.rb +24 -0
  52. data/lib/hive_sql/models/vo/author_reward.rb +22 -0
  53. data/lib/hive_sql/models/vo/comment_benefactor_reward.rb +23 -0
  54. data/lib/hive_sql/models/vo/curation_reward.rb +21 -0
  55. data/lib/hive_sql/models/vo/fill_convert_request.rb +21 -0
  56. data/lib/hive_sql/models/vo/fill_order.rb +23 -0
  57. data/lib/hive_sql/models/vo/fill_transfer_from_savings.rb +23 -0
  58. data/lib/hive_sql/models/vo/fill_vesting_withdraw.rb +21 -0
  59. data/lib/hive_sql/models/vo/interest.rb +19 -0
  60. data/lib/hive_sql/models/vo/liquidity_reward.rb +19 -0
  61. data/lib/hive_sql/models/vo/producer_reward.rb +19 -0
  62. data/lib/hive_sql/models/vo/proposal_pay.rb +20 -0
  63. data/lib/hive_sql/models/vo/return_vesting_delegation.rb +20 -0
  64. data/lib/hive_sql/models/vo/shutdown_witness.rb +18 -0
  65. data/lib/hive_sql/models/vo/sps_fund.rb +18 -0
  66. data/lib/hive_sql/models/witness.rb +48 -0
  67. data/lib/hive_sql/version.rb +3 -0
  68. metadata +413 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 46ad6e598c60e582ea5129bbb84c4f29a37fa452f7dc14ba3248b871288d47e5
4
+ data.tar.gz: 9e99c26f8421a2aad2ac5f8ca9f8ac63d98e7348a5bfc81aaf92a258473e7cb1
5
+ SHA512:
6
+ metadata.gz: f9721dfcd1ec4d7d318c2847361d79b7446e7ae6d2053518de5cd47f62d9567bd5968293fccd5e1c642b18f24b8e6401e9eff844620860a6c6c6b567f66d45ec
7
+ data.tar.gz: 492c17a5435644d5a1352ac39eb42f4c8e1339fcf8f9440559ed4d62279a98d0c65649e92a422a27b9b1e6826e8c2ddb5c62aaa18160787ec7da753c487581c6
data/.gitignore ADDED
@@ -0,0 +1,58 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ # Used by dotenv library to load environment variables.
14
+ # .env
15
+
16
+ # Ignore Byebug command history file.
17
+ .byebug_history
18
+
19
+ ## Specific to RubyMotion:
20
+ .dat*
21
+ .repl_history
22
+ build/
23
+ *.bridgesupport
24
+ build-iPhoneOS/
25
+ build-iPhoneSimulator/
26
+
27
+ ## Specific to RubyMotion (use of CocoaPods):
28
+ #
29
+ # We recommend against adding the Pods directory to your .gitignore. However
30
+ # you should judge for yourself, the pros and cons are mentioned at:
31
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
32
+ #
33
+ # vendor/Pods/
34
+
35
+ ## Documentation cache and generated files:
36
+ /.yardoc/
37
+ /_yardoc/
38
+ /doc/
39
+ /rdoc/
40
+
41
+ ## Environment normalization:
42
+ /.bundle/
43
+ /vendor/bundle
44
+ /lib/bundler/man/
45
+
46
+ # for a library or gem, you might want to ignore these files since the code is
47
+ # intended to run in multiple environments; otherwise, check them in:
48
+ Gemfile.lock
49
+ .ruby-version
50
+ .ruby-gemset
51
+
52
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
53
+ .rvmrc
54
+
55
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
56
+ # .rubocop-https?--*
57
+
58
+ **/.DS_Store
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in hive_sql.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,41 @@
1
+ CC0 1.0 Universal (CC0 1.0)
2
+ Public Domain Dedication
3
+ https://creativecommons.org/publicdomain/zero/1.0/
4
+
5
+ This is a human-readable summary of the Legal Code:
6
+ https://creativecommons.org/publicdomain/zero/1.0/legalcode
7
+
8
+ Disclaimer
9
+
10
+ The Commons Deed is not a legal instrument. It is simply a handy reference for
11
+ understanding the CC0 Legal Code, a human-readable expression of some of its key
12
+ terms. Think of it as the user-friendly interface to the CC0 Legal Code beneath.
13
+ This Deed itself has no legal value, and its contents do not appear in CC0.
14
+ Creative Commons is not a law firm and does not provide legal services.
15
+ Distributing, displaying, or linking to this Commons Deed does not create an
16
+ attorney-client relationship.
17
+
18
+ Creative Commons has not verified the copyright status of any work to which CC0
19
+ has been applied. CC makes no warranties about any work or its copyright status
20
+ in any jurisdiction, and disclaims all liability for all uses of any work.
21
+
22
+ No Copyright
23
+
24
+ The person who associated a work with this deed has dedicated the work to the
25
+ public domain by waiving all of his or her rights to the work worldwide under
26
+ copyright law, including all related and neighboring rights, to the extent
27
+ allowed by law.
28
+
29
+ You can copy, modify, distribute and perform the work, even for commercial
30
+ purposes, all without asking permission. See Other Information below.
31
+
32
+ Other Information
33
+
34
+ * In no way are the patent or trademark rights of any person affected by CC0,
35
+ nor are the rights that other persons may have in the work or in how the work
36
+ is used, such as publicity or privacy rights.
37
+ * Unless expressly stated otherwise, the person who associated a work with this
38
+ deed makes no warranties about the work, and disclaims liability for all uses
39
+ of the work, to the fullest extent permitted by applicable law.
40
+ * When using or citing the work, you should not imply endorsement by the author
41
+ or the affirmer.
data/Rakefile ADDED
@@ -0,0 +1,538 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require 'hive_sql'
4
+ require 'awesome_print'
5
+
6
+ Rake::TestTask.new(:test) do |t|
7
+ t.libs << 'test'
8
+ t.libs << 'lib'
9
+ t.test_files = FileList['test/**/*_test.rb']
10
+ t.ruby_opts << if ENV['HELL_ENABLED']
11
+ '-W2'
12
+ else
13
+ '-W1'
14
+ end
15
+ end
16
+
17
+ task :default => :test
18
+
19
+ task :console do
20
+ exec "irb -r hive_sql -I ./lib"
21
+ end
22
+
23
+ namespace :created do
24
+ desc 'Lists accounts created grouped by creator and date.'
25
+ task :accounts, [:creator, :days_ago] do |t, args|
26
+ now = Time.now.utc
27
+ creator = args[:creator]
28
+ after_timestamp = now - ((args[:days_ago] || '30').to_i * 86400)
29
+
30
+ accounts = HiveSQL::Tx::AccountCreate.all
31
+
32
+ if !!creator || creator == '%'
33
+ unless creator == '%'
34
+ accounts = accounts.where(creator: creator)
35
+ end
36
+ elsif creator =~ /.*%.*/
37
+ accounts = accounts.where('creator LIKE ?', creator)
38
+ end
39
+
40
+ accounts = accounts.where('timestamp > ?', after_timestamp)
41
+ accounts = accounts.group('CAST(timestamp AS DATE)', :creator)
42
+ accounts = accounts.order('cast_timestamp_as_date ASC')
43
+
44
+ accounts = accounts.count
45
+ puts "# Daily creation count by #{creator.nil? ? 'all account creators' : creator} since #{after_timestamp} ..."
46
+ ap accounts
47
+ puts "# Total accounts: #{accounts.values.sum}"
48
+ end
49
+
50
+ desc 'Lists custom_json_operations grouped by id and date.'
51
+ task :custom_json, [:id, :days_ago, :min_count] do |t, args|
52
+ now = Time.now.utc
53
+ tid = args[:id]
54
+ after_timestamp = now - ((args[:days_ago] || '30').to_i * 86400)
55
+ min_count = (args[:min_count] || 1).to_i
56
+
57
+ customs = HiveSQL::Tx::Custom.all
58
+
59
+ if !!tid && tid != '%' && tid =~ /.*%.*/
60
+ customs = customs.where("tid LIKE ?", tid)
61
+ elsif !!tid && tid != '%'
62
+ customs = customs.where(tid: tid)
63
+ end
64
+
65
+ customs = customs.where('timestamp > ?', after_timestamp)
66
+ customs = customs.group('CAST(timestamp AS DATE)', :tid)
67
+ customs = customs.order('cast_timestamp_as_date ASC')
68
+
69
+ customs = customs.count
70
+
71
+ customs = customs.map do |k, v|
72
+ [k, v] if v >= min_count
73
+ end.compact.to_h
74
+
75
+ puts "# Daily creation count by #{tid.nil? ? 'all custom_json_operation' : tid} since #{after_timestamp} ..."
76
+ ap customs
77
+ puts "# Total custom_json_operation: #{customs.values.sum}"
78
+ end
79
+ end
80
+
81
+ desc 'Lists sum of transfers grouped by date, from, and to.'
82
+ task :transfers, [:minimum_amount, :symbol, :days_ago] do |t, args|
83
+ now = Time.now.utc
84
+ minimum_amount = (args[:minimum_amount] || '1000000').to_f
85
+ symbol = (args[:symbol] || 'HIVE').upcase
86
+ after_timestamp = now - ((args[:days_ago] || '30').to_i * 86400)
87
+
88
+ # Only type: transfer; ignore savings, vestings
89
+ transfers = HiveSQL::Tx::Transfer.where(type: 'transfer')
90
+ transfers = transfers.where('amount > ?', minimum_amount)
91
+ transfers = transfers.where('amount_symbol = ?', symbol)
92
+ transfers = transfers.where('timestamp > ?', after_timestamp)
93
+ transfers = transfers.group('CAST(timestamp AS DATE)', :from, :to)
94
+ transfers = transfers.order('cast_timestamp_as_date ASC')
95
+
96
+ puts "Daily transfer sum over #{'%.3f' % minimum_amount} #{symbol} since #{after_timestamp} ..."
97
+ ap transfers.sum(:amount)
98
+ end
99
+
100
+ desc 'Lists sum of powered up grouped by date, from, and to.'
101
+ task :powerup, [:minimum_amount, :symbol, :days_ago, :not_to_self] do |t, args|
102
+ now = Time.now.utc
103
+ minimum_amount = (args[:minimum_amount] || '500').to_f
104
+ symbol = (args[:symbol] || 'HIVE').upcase
105
+ after_timestamp = now - ((args[:days_ago] || '30').to_i * 86400)
106
+ not_to_self = (args[:not_to_self] || 'false') == 'true'
107
+
108
+ minimum_amount = case symbol
109
+ when 'MVESTS' then minimum_amount * 1e6 #TODO
110
+ when 'VESTS' then minimum_amount # TODO
111
+ when 'HIVE' then minimum_amount
112
+ else; raise "Unknown symbol: #{symbol}"
113
+ end
114
+
115
+ # Only type: transfer; ignore savings, vestings
116
+ transfers = HiveSQL::Tx::Transfer.where(type: 'transfer_to_vesting')
117
+ transfers = transfers.where('amount > ?', minimum_amount)
118
+ transfers = transfers.where('amount_symbol = ?', 'HIVE')
119
+ transfers = transfers.where('timestamp > ?', after_timestamp)
120
+ transfers = transfers.group('CAST(timestamp AS DATE)', :from, :to)
121
+ transfers = transfers.order('cast_timestamp_as_date ASC')
122
+
123
+ transfers = transfers.sum(:amount)
124
+
125
+ if not_to_self
126
+ transfers = transfers.map do |k, v|
127
+ [k, v] if k[1] != k[2]
128
+ end.compact.to_h
129
+ end
130
+
131
+ puts "# Daily transfer sum over #{'%.3f' % minimum_amount} #{symbol} #{not_to_self ? '' : 'not to self '}since #{after_timestamp} ..."
132
+ ap transfers
133
+ puts "# Total #{symbol}: #{transfers.values.sum}"
134
+ end
135
+
136
+ desc 'Lists sum of powered down grouped by date, from, and to.'
137
+ task :powerdown, [:minimum_amount, :symbol, :days_ago, :not_to_self] do |t, args|
138
+ now = Time.now.utc
139
+ minimum_amount = (args[:minimum_amount] || '500').to_f
140
+ symbol = (args[:symbol] || 'HIVE').upcase
141
+ after_timestamp = now - ((args[:days_ago] || '30').to_i * 86400)
142
+ not_to_self = (args[:not_to_self] || 'false') == 'true'
143
+
144
+ minimum_amount = case symbol
145
+ when 'MVESTS' then minimum_amount * 1e6 #TODO
146
+ when 'VESTS' then minimum_amount # TODO
147
+ when 'HIVE' then minimum_amount
148
+ else; raise "Unknown symbol: #{symbol}"
149
+ end
150
+
151
+ # Only type: transfer; ignore savings, vestings
152
+ transfers = HiveSQL::Tx::Transfer.where(type: 'transfer_to_vesting')
153
+ transfers = transfers.where('amount > ?', minimum_amount)
154
+ transfers = transfers.where('amount_symbol = ?', 'HIVE')
155
+ transfers = transfers.where('timestamp > ?', after_timestamp)
156
+ transfers = transfers.group('CAST(timestamp AS DATE)', :from, :to)
157
+ transfers = transfers.order('cast_timestamp_as_date ASC')
158
+
159
+ transfers = transfers.sum(:amount)
160
+
161
+ if not_to_self
162
+ transfers = transfers.map do |k, v|
163
+ [k, v] if k[1] != k[2]
164
+ end.compact.to_h
165
+ end
166
+
167
+ puts "# Daily transfer sum over #{'%.3f' % minimum_amount} #{symbol} #{not_to_self ? '' : 'not to self '}since #{after_timestamp} ..."
168
+ ap transfers
169
+ puts "# Total #{symbol}: #{transfers.values.sum}"
170
+ end
171
+
172
+ desc 'Lists apps grouped by date, app/version.'
173
+ task :apps, [:app, :days_ago] do |t, args|
174
+ now = Time.now.utc
175
+ app = args[:app]
176
+ after_timestamp = now - ((args[:days_ago] || '7').to_i * 86400)
177
+
178
+ comments = HiveSQL::Comment.normalized_json
179
+ comments = comments.app(app) if !!app
180
+ comments = comments.where('created > ?', after_timestamp)
181
+ comments = comments.group('CAST(created AS DATE)', "JSON_VALUE(json_metadata, '$.app')")
182
+ comments = comments.order('cast_created_as_date ASC')
183
+
184
+ matching = " matching \"#{app}\"" if !!app
185
+ puts "Daily app#{matching} count since #{after_timestamp} ..."
186
+ ap comments.count(:all)
187
+ end
188
+
189
+ desc 'Lists app names grouped by date, app/version.'
190
+ task :app_names, [:app, :days_ago] do |t, args|
191
+ now = Time.now.utc
192
+ app = args[:app]
193
+ after_timestamp = now - ((args[:days_ago] || '7').to_f * 86400)
194
+
195
+ comments = HiveSQL::Comment.normalized_json
196
+ comments = comments.app(app) if !!app
197
+ comments = comments.where('created > ?', after_timestamp)
198
+ comments = comments.group('CAST(created AS DATE)', "JSON_VALUE(json_metadata, '$.app')")
199
+ comments = comments.order('cast_created_as_date ASC')
200
+
201
+ matching = " matching \"#{app}\"" if !!app
202
+ puts "# Daily app#{matching} count since #{after_timestamp} ..."
203
+
204
+ app_names = {}
205
+
206
+ comments.count(:all).each do |k, v|
207
+ date, app = k
208
+ if !!app && app.include?('/')
209
+ name, version = app.split('/')
210
+ app_names[[date, name]] ||= 0.0
211
+ app_names[[date, name]] += v
212
+ end
213
+ end
214
+
215
+ ap app_names
216
+ end
217
+
218
+ desc 'Do all crosschecks of given account.'
219
+ task :crosscheck, [:account] do |t, args|
220
+ account = args[:account]
221
+
222
+ Rake::Task["crosscheck:powerdowns"].invoke(account)
223
+ Rake::Task["crosscheck:powerups"].invoke(account)
224
+ Rake::Task["crosscheck:transfers"].invoke(account)
225
+ Rake::Task["crosscheck:vesting_from"].invoke(account)
226
+ Rake::Task["crosscheck:vesting_to"].invoke(account)
227
+ end
228
+
229
+ namespace :crosscheck do
230
+ desc 'List of accounts grouped by transfer count crosschecked by memo of given account.'
231
+ task :transfers, [:account] do |t, args|
232
+ exchanges = %w(bittrex poloniex openledger blocktrades deepcrypto8 gopax
233
+ binanceexchange teambitwala changelly hitbtc-exchange korbit roomofsatoshi
234
+ shapeshiftio)
235
+ account = args[:account]
236
+
237
+ if account.nil? || account == ''
238
+ puts 'Account name required.'
239
+ exit
240
+ elsif exchanges.include? account
241
+ puts 'That procedure is not recommended.'
242
+ exit
243
+ end
244
+
245
+ all = HiveSQL::Tx::Transfer.where(type: 'transfer')
246
+ transfers = all.where.not(memo: '')
247
+ transfers = transfers.where(to: exchanges)
248
+ transfers = if account =~ /%/
249
+ table = HiveSQL::Tx::Transfer.arel_table
250
+ transfers.where(table[:from].matches(account))
251
+ else
252
+ transfers.where(from: account)
253
+ end
254
+ crosscheck_transfers = all.where(memo: transfers.select(:memo))
255
+
256
+ if transfers.none?
257
+ puts "No match."
258
+ else
259
+ from = transfers.pluck(:from).uniq.join(', ')
260
+ puts "Accounts grouped by transfer count using common memos as #{from} on common exchanges ..."
261
+ ap crosscheck_transfers.group(:from).order('count_all').count(:all)
262
+ end
263
+ end
264
+
265
+ desc 'List of accounts grouped by vesting transfers from a given account'
266
+ task :vesting_from, [:account] do |t, args|
267
+ account = args[:account]
268
+
269
+ if account.nil? || account == ''
270
+ puts 'Account name required.'
271
+ exit
272
+ end
273
+
274
+ table = HiveSQL::Tx::Transfer.arel_table
275
+ all = HiveSQL::Tx::Transfer.where(type: 'transfer_to_vesting')
276
+ transfers = all.where(table[:from].not_eq(:to))
277
+ transfers = transfers.where.not(to: '')
278
+ transfers = if account =~ /%/
279
+ table = HiveSQL::Tx::Transfer.arel_table
280
+ transfers.where(table[:from].matches(account))
281
+ else
282
+ transfers.where(from: account)
283
+ end
284
+
285
+ if transfers.none?
286
+ puts "No match."
287
+ else
288
+ from = transfers.pluck(:from).uniq.join(', ')
289
+ puts "Accounts grouped by vesting transfer count from #{from} ..."
290
+ ap transfers.group(:to).order('count_all').count(:all)
291
+ end
292
+ end
293
+
294
+ desc 'List of accounts grouped by vesting transfers to a given account'
295
+ task :vesting_to, [:account] do |t, args|
296
+ account = args[:account]
297
+
298
+ if account.nil? || account == ''
299
+ puts 'Account name required.'
300
+ exit
301
+ end
302
+
303
+ table = HiveSQL::Tx::Transfer.arel_table
304
+ all = HiveSQL::Tx::Transfer.where(type: 'transfer_to_vesting')
305
+ transfers = all.where(table[:from].not_eq(table[:to]))
306
+ transfers = transfers.where.not(to: '')
307
+ transfers = if account =~ /%/
308
+ table = HiveSQL::Tx::Transfer.arel_table
309
+ transfers.where(table[:to].matches(account))
310
+ else
311
+ transfers.where(to: account)
312
+ end
313
+
314
+ if transfers.none?
315
+ puts "No match."
316
+ else
317
+ from = transfers.pluck(:to).uniq.join(', ')
318
+ puts "Accounts grouped by vesting transfer count to #{from} ..."
319
+ ap transfers.group(:from).order('count_all').count(:all)
320
+ end
321
+ end
322
+
323
+ desc 'List of accounts grouped by powerdown sums crosschecked by given account.'
324
+ task :powerdowns, [:account] do |t, args|
325
+ account = args[:account]
326
+
327
+ if account.nil? || account == ''
328
+ puts 'Account name required.'
329
+ exit
330
+ end
331
+
332
+ table = HiveSQL::Vo::FillVestingWithdraw.arel_table
333
+ all = HiveSQL::Vo::FillVestingWithdraw.where(table[:from_account].not_eq(table[:to_account]))
334
+ powerdowns = if account =~ /%/
335
+ all.where(table[:from_account].matches(account))
336
+ else
337
+ all.where(from_account: account)
338
+ end
339
+
340
+ if powerdowns.none?
341
+ puts "No match."
342
+ else
343
+ from = powerdowns.pluck(:from_account).uniq.join(', ')
344
+ puts "Powerdowns grouped by sum from #{from} ..."
345
+ ap powerdowns.group(:to_account).
346
+ order('sum_try_parse_replace_withdrawn_vests_as_float').
347
+ sum("TRY_PARSE(REPLACE(withdrawn, ' VESTS', '') AS float)")
348
+ end
349
+ end
350
+
351
+ desc 'List of accounts grouped by powerup sums crosschecked by given account.'
352
+ task :powerups, [:account] do |t, args|
353
+ account = args[:account]
354
+
355
+ if account.nil? || account == ''
356
+ puts 'Account name required.'
357
+ exit
358
+ end
359
+
360
+ table = HiveSQL::Vo::FillVestingWithdraw.arel_table
361
+ all = HiveSQL::Vo::FillVestingWithdraw.where(table[:from_account].not_eq(table[:to_account]))
362
+ powerups = if account =~ /%/
363
+ all.where(table[:to_account].matches(account))
364
+ else
365
+ all.where(to_account: account)
366
+ end
367
+
368
+ if powerups.none?
369
+ puts "No match."
370
+ else
371
+ to = powerups.pluck(:to_account).uniq.join(', ')
372
+ puts "Powerups grouped by sum to #{to} ..."
373
+ ap powerups.group(:from_account).
374
+ order('sum_try_parse_replace_withdrawn_vests_as_float').
375
+ sum("TRY_PARSE(REPLACE(withdrawn, ' VESTS', '') AS float)")
376
+ end
377
+ end
378
+ end
379
+
380
+ namespace :rewards do
381
+ desc 'Lists author rewards grouped by date.'
382
+ task :author, [:symbol, :days_ago, :author] do |t, args|
383
+ now = Time.now.utc
384
+ symbol = (args[:symbol] || 'HBD').upcase
385
+ after_timestamp = now - ((args[:days_ago] || '7').to_i * 86400)
386
+ author = args[:author]
387
+
388
+ rewards = HiveSQL::Vo::AuthorReward
389
+ rewards = rewards.where('timestamp > ?', after_timestamp)
390
+ rewards = rewards.group('CAST(timestamp AS DATE)')
391
+ rewards = rewards.order('cast_timestamp_as_date ASC')
392
+
393
+ if !!author
394
+ if author =~ /%/
395
+ rewards = rewards.where("author LIKE ?", author)
396
+ else
397
+ rewards = rewards.where(author: author)
398
+ end
399
+
400
+ puts "Daily #{author} reward #{symbol} sum grouped by date since #{after_timestamp} ..."
401
+ else
402
+ puts "Daily reward #{symbol} sum grouped by date since #{after_timestamp} ..."
403
+ end
404
+
405
+ rewards = case symbol
406
+ when 'HBD' then rewards.sum(:sbd_payout)
407
+ when 'HIVE' then rewards.sum(:steem_payout)
408
+ when 'VESTS' then rewards.sum(:vesting_payout)
409
+ when 'MVESTS'
410
+ rewards.sum('vesting_payout / 1000000')
411
+ else; puts "Unknown symbol: #{symbol}. Symbols supported: HBD, HIVE, VESTS, MVESTS"
412
+ end
413
+
414
+ ap rewards
415
+ sum = rewards.values.sum
416
+ puts "# Total rewards: %.3f %s (average: %.3f per day)" % [sum, symbol, (sum / rewards.size)]
417
+ end
418
+
419
+ desc 'Lists curation rewards grouped by date.'
420
+ task :curation, [:symbol, :days_ago] do |t, args|
421
+ now = Time.now.utc
422
+ symbol = (args[:symbol] || 'MVESTS').upcase
423
+ after_timestamp = now - ((args[:days_ago] || '7').to_i * 86400)
424
+
425
+ rewards = HiveSQL::Vo::CurationReward
426
+ rewards = rewards.where('timestamp > ?', after_timestamp)
427
+ rewards = rewards.group('CAST(timestamp AS DATE)')
428
+ rewards = rewards.order('cast_timestamp_as_date ASC')
429
+
430
+ puts "Daily curation reward #{symbol} sum grouped by date since #{after_timestamp} ..."
431
+
432
+ case symbol
433
+ when 'VESTS'
434
+ ap rewards.sum("TRY_PARSE(REPLACE(reward, ' VESTS', '') AS float)")
435
+ when 'MVESTS'
436
+ ap rewards.sum("TRY_PARSE(REPLACE(reward, ' VESTS', '') AS float) / 1000000")
437
+ else; puts "Unknown symbol: #{symbol}. Symbols supported: VESTS, MVESTS"
438
+ end
439
+ end
440
+ end
441
+
442
+ desc 'Lists proxied grouped by month.'
443
+ task :proxied, [:days_ago] do |t, args|
444
+ now = Time.now.utc
445
+ after_timestamp = now - ((args[:days_ago] || '7').to_i * 86400)
446
+
447
+ proxied = HiveSQL::Tx::AccountWitnessProxy
448
+ proxied = proxied.where('timestamp > ?', after_timestamp)
449
+ proxied = proxied.group("FORMAT(timestamp, 'yyyy-MM')", :proxy)
450
+ proxied = proxied.order('format_timestamp_yyyy_mm ASC')
451
+
452
+ puts "Daily proxied grouped by month since #{after_timestamp} ..."
453
+
454
+ ap proxied.count(:all)
455
+ end
456
+
457
+ desc 'Claimed Rewards'
458
+ task :claimed, [:account_name, :days_ago, :symbol] do |t, args|
459
+ now = Time.now.utc
460
+ account_name = args[:account_name] || '%'
461
+ after_timestamp = now - ((args[:days_ago] || '30').to_i * 86400)
462
+ symbol = (args[:symbol] || 'vests').downcase.to_sym
463
+ claims = HiveSQL::Tx::ClaimRewardBalance.where('timestamp > ?', after_timestamp)
464
+
465
+ claims = if account_name =~ /%/
466
+ claims.where('account LIKE ?', account_name)
467
+ else
468
+ claims.where(account: account_name)
469
+ end
470
+
471
+ claims = case symbol
472
+ when :vests then claims.where("reward_vests > 0")
473
+ when :mvests then claims.where("reward_vests > 0")
474
+ when :hive then claims.where("reward_steem > 0")
475
+ when :hbd then claims.where("reward_sbd > 0")
476
+ else; raise "Unknown symbol: #{symbol}"
477
+ end
478
+
479
+ claims = claims.group("FORMAT(timestamp, 'yyyy-MM')")
480
+ claims = claims.order('format_timestamp_yyyy_mm ASC')
481
+
482
+ claims = case symbol
483
+ when :vests then claims.sum(:reward_vests)
484
+ when :mvests then claims.sum('reward_vests / 1000000')
485
+ when :hive then claims.sum(:reward_steem)
486
+ when :hbd then claims.sum(:reward_sbd)
487
+ end
488
+
489
+ puts "# Claimed rewards in #{symbol.to_s.upcase} sum grouped by month ..."
490
+
491
+ ap claims
492
+ puts "# Total claimed #{symbol}: #{claims.values.sum}"
493
+ end
494
+
495
+ task :balance, [:party_a, :party_b, :symbol] do |t, args|
496
+ party_a = args[:party_a]
497
+ party_b = args[:party_b]
498
+ symbol = args[:symbol].upcase
499
+
500
+ balance_a = HiveSQL::Tx::Transfer.where(to: party_a, from: party_b, amount_symbol: symbol).sum(:amount).to_f
501
+ balance_b = HiveSQL::Tx::Transfer.where(to: party_b, from: party_a, amount_symbol: symbol).sum(:amount).to_f
502
+
503
+ puts "#{party_a}: %.3f #{symbol}, difference: %.3f #{symbol}" % [balance_a, (balance_a - balance_b)]
504
+ puts "#{party_b}: %.3f #{symbol}, difference: %.3f #{symbol}" % [balance_b, (balance_b - balance_a)]
505
+ end
506
+
507
+ # Doesn't look like this table exists.
508
+ # desc 'List conversion HBD conversion request sums grouped by day.'
509
+ # task :convert, [:days_ago] do |t, args|
510
+ # now = Time.now.utc
511
+ # after_timestamp = now - ((args[:days_ago] || '3.5').to_f * 86400)
512
+ #
513
+ # converts = HiveSQL::Vo::FillConvertRequest
514
+ # converts = converts.where('timestamp > ?', after_timestamp)
515
+ # converts = converts.group('CAST(timestamp AS DATE)')
516
+ # converts = converts.order('cast_timestamp_as_date ASC')
517
+ #
518
+ # puts "Daily conversion requests failled sum grouped by date since #{after_timestamp} ..."
519
+ # ap converts.sum(:amount)
520
+ # end
521
+
522
+ desc 'Build a new version of the hive_sql gem.'
523
+ task :build do
524
+ exec 'gem build hive_sql.gemspec'
525
+ end
526
+
527
+ desc 'Publish the current version of the hive_sql gem.'
528
+ task :push do
529
+ exec "gem push hive_sql-#{HiveSQL::VERSION}.gem"
530
+ end
531
+
532
+ # We're not going to yank on a regular basis, but this is how it's done if you
533
+ # really want a task for that for some reason.
534
+
535
+ # desc 'Yank the current version of the hive_sql gem.'
536
+ # task :yank do
537
+ # exec "gem yank hive_sql -v #{HiveSQL::VERSION}"
538
+ # end