istox 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: f9bb943887045454013cf21a0afc9779a7939daf858a9ff170f65bf9d45eeed2
4
+ data.tar.gz: a0450d2ea9c5239db6cf524228bfdbdedeb1895be6f4f3e2047991bb7f4b8c4f
5
+ SHA512:
6
+ metadata.gz: b5e4acca703008269366b34728f58eee52eb0237060ebcae323c8c4499ef8f87be4d2f42f2b5672d86009d988f97b7203bf053f7950070476b7bfcbc652282b1
7
+ data.tar.gz: 51a3cd590c0f26b6fa0d928fe6d764155bad5caca6770d666d6b9bae824c09f110918c3335d645391f4595b3553d453645c85336587b799421eb484a20b815d2
data/.gitignore ADDED
@@ -0,0 +1 @@
1
+ .rspec_status
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at siongleng@ich-group.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in istox.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,191 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ istox (0.1.0)
5
+ bunny (>= 2.12.0)
6
+ gruf
7
+ paranoia (~> 2.2)
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ actioncable (5.2.2)
13
+ actionpack (= 5.2.2)
14
+ nio4r (~> 2.0)
15
+ websocket-driver (>= 0.6.1)
16
+ actionmailer (5.2.2)
17
+ actionpack (= 5.2.2)
18
+ actionview (= 5.2.2)
19
+ activejob (= 5.2.2)
20
+ mail (~> 2.5, >= 2.5.4)
21
+ rails-dom-testing (~> 2.0)
22
+ actionpack (5.2.2)
23
+ actionview (= 5.2.2)
24
+ activesupport (= 5.2.2)
25
+ rack (~> 2.0)
26
+ rack-test (>= 0.6.3)
27
+ rails-dom-testing (~> 2.0)
28
+ rails-html-sanitizer (~> 1.0, >= 1.0.2)
29
+ actionview (5.2.2)
30
+ activesupport (= 5.2.2)
31
+ builder (~> 3.1)
32
+ erubi (~> 1.4)
33
+ rails-dom-testing (~> 2.0)
34
+ rails-html-sanitizer (~> 1.0, >= 1.0.3)
35
+ activejob (5.2.2)
36
+ activesupport (= 5.2.2)
37
+ globalid (>= 0.3.6)
38
+ activemodel (5.2.2)
39
+ activesupport (= 5.2.2)
40
+ activerecord (5.2.2)
41
+ activemodel (= 5.2.2)
42
+ activesupport (= 5.2.2)
43
+ arel (>= 9.0)
44
+ activestorage (5.2.2)
45
+ actionpack (= 5.2.2)
46
+ activerecord (= 5.2.2)
47
+ marcel (~> 0.3.1)
48
+ activesupport (5.2.2)
49
+ concurrent-ruby (~> 1.0, >= 1.0.2)
50
+ i18n (>= 0.7, < 2)
51
+ minitest (~> 5.1)
52
+ tzinfo (~> 1.1)
53
+ amq-protocol (2.3.0)
54
+ arel (9.0.0)
55
+ builder (3.2.3)
56
+ bullet (5.7.6)
57
+ activesupport (>= 3.0.0)
58
+ uniform_notifier (~> 1.11.0)
59
+ bunny (2.14.1)
60
+ amq-protocol (~> 2.3, >= 2.3.0)
61
+ concurrent-ruby (1.1.4)
62
+ crass (1.0.4)
63
+ database_cleaner (1.6.2)
64
+ diff-lcs (1.3)
65
+ erubi (1.8.0)
66
+ factory_bot (4.8.2)
67
+ activesupport (>= 3.0.0)
68
+ factory_bot_rails (4.8.2)
69
+ factory_bot (~> 4.8.2)
70
+ railties (>= 3.0.0)
71
+ faker (1.7.3)
72
+ i18n (~> 0.5)
73
+ globalid (0.4.2)
74
+ activesupport (>= 4.2.0)
75
+ google-protobuf (3.7.0)
76
+ googleapis-common-protos-types (1.0.3)
77
+ google-protobuf (~> 3.0)
78
+ grpc (1.19.0)
79
+ google-protobuf (~> 3.1)
80
+ googleapis-common-protos-types (~> 1.0.0)
81
+ grpc-tools (1.19.0)
82
+ gruf (2.5.2)
83
+ activesupport
84
+ concurrent-ruby
85
+ grpc (~> 1.10)
86
+ grpc-tools (~> 1.10)
87
+ slop (~> 4.6)
88
+ i18n (0.9.5)
89
+ concurrent-ruby (~> 1.0)
90
+ loofah (2.2.3)
91
+ crass (~> 1.0.2)
92
+ nokogiri (>= 1.5.9)
93
+ mail (2.7.1)
94
+ mini_mime (>= 0.1.1)
95
+ marcel (0.3.3)
96
+ mimemagic (~> 0.3.2)
97
+ method_source (0.9.2)
98
+ mimemagic (0.3.3)
99
+ mini_mime (1.0.1)
100
+ mini_portile2 (2.4.0)
101
+ minitest (5.11.3)
102
+ nio4r (2.3.1)
103
+ nokogiri (1.10.1)
104
+ mini_portile2 (~> 2.4.0)
105
+ paranoia (2.4.1)
106
+ activerecord (>= 4.0, < 5.3)
107
+ rack (2.0.6)
108
+ rack-test (1.1.0)
109
+ rack (>= 1.0, < 3)
110
+ rails (5.2.2)
111
+ actioncable (= 5.2.2)
112
+ actionmailer (= 5.2.2)
113
+ actionpack (= 5.2.2)
114
+ actionview (= 5.2.2)
115
+ activejob (= 5.2.2)
116
+ activemodel (= 5.2.2)
117
+ activerecord (= 5.2.2)
118
+ activestorage (= 5.2.2)
119
+ activesupport (= 5.2.2)
120
+ bundler (>= 1.3.0)
121
+ railties (= 5.2.2)
122
+ sprockets-rails (>= 2.0.0)
123
+ rails-dom-testing (2.0.3)
124
+ activesupport (>= 4.2.0)
125
+ nokogiri (>= 1.6)
126
+ rails-html-sanitizer (1.0.4)
127
+ loofah (~> 2.2, >= 2.2.2)
128
+ railties (5.2.2)
129
+ actionpack (= 5.2.2)
130
+ activesupport (= 5.2.2)
131
+ method_source
132
+ rake (>= 0.8.7)
133
+ thor (>= 0.19.0, < 2.0)
134
+ rake (10.5.0)
135
+ rspec (3.8.0)
136
+ rspec-core (~> 3.8.0)
137
+ rspec-expectations (~> 3.8.0)
138
+ rspec-mocks (~> 3.8.0)
139
+ rspec-core (3.8.0)
140
+ rspec-support (~> 3.8.0)
141
+ rspec-expectations (3.8.2)
142
+ diff-lcs (>= 1.2.0, < 2.0)
143
+ rspec-support (~> 3.8.0)
144
+ rspec-mocks (3.8.0)
145
+ diff-lcs (>= 1.2.0, < 2.0)
146
+ rspec-support (~> 3.8.0)
147
+ rspec-rails (3.8.2)
148
+ actionpack (>= 3.0)
149
+ activesupport (>= 3.0)
150
+ railties (>= 3.0)
151
+ rspec-core (~> 3.8.0)
152
+ rspec-expectations (~> 3.8.0)
153
+ rspec-mocks (~> 3.8.0)
154
+ rspec-support (~> 3.8.0)
155
+ rspec-support (3.8.0)
156
+ slop (4.6.2)
157
+ sprockets (3.7.2)
158
+ concurrent-ruby (~> 1.0)
159
+ rack (> 1, < 3)
160
+ sprockets-rails (3.2.1)
161
+ actionpack (>= 4.0)
162
+ activesupport (>= 4.0)
163
+ sprockets (>= 3.0.0)
164
+ sqlite3 (1.3.13)
165
+ thor (0.20.3)
166
+ thread_safe (0.3.6)
167
+ tzinfo (1.2.5)
168
+ thread_safe (~> 0.1)
169
+ uniform_notifier (1.11.0)
170
+ websocket-driver (0.7.0)
171
+ websocket-extensions (>= 0.1.0)
172
+ websocket-extensions (0.1.3)
173
+
174
+ PLATFORMS
175
+ ruby
176
+
177
+ DEPENDENCIES
178
+ bullet (~> 5.7.5)
179
+ bundler (~> 1.16)
180
+ database_cleaner (~> 1.6.0)
181
+ factory_bot_rails (~> 4.8.2)
182
+ faker (~> 1.7.3)
183
+ istox!
184
+ rails (~> 5.2.1)
185
+ rake (~> 10.0)
186
+ rspec (~> 3.0)
187
+ rspec-rails (~> 3.8.0)
188
+ sqlite3 (~> 1.3.6)
189
+
190
+ BUNDLED WITH
191
+ 1.16.6
data/README.md ADDED
@@ -0,0 +1,39 @@
1
+ # Istox
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/istox`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'istox'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install istox
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/istox. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
36
+
37
+ ## Code of Conduct
38
+
39
+ Everyone interacting in the Istox project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/istox/blob/master/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "istox"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
data/istox.gemspec ADDED
@@ -0,0 +1,49 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "istox/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "istox"
8
+ spec.version = Istox::VERSION
9
+ spec.authors = ["Siong Leng"]
10
+ spec.email = ["siongleng@ich-group.com"]
11
+
12
+ spec.summary = "istox backend shared gem"
13
+ spec.description = "istox backend shared gem"
14
+ spec.homepage = "http://www.abc.com"
15
+
16
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
17
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
18
+ # if spec.respond_to?(:metadata)
19
+ # spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
20
+ # else
21
+ # raise "RubyGems 2.0 or newer is required to protect against " \
22
+ # "public gem pushes."
23
+ # end
24
+
25
+ # Specify which files should be added to the gem when it is released.
26
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
27
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
28
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
29
+ end
30
+ spec.bindir = "exe"
31
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
32
+ spec.require_paths = ["lib"]
33
+
34
+ spec.add_dependency "bunny", ">= 2.12.0"
35
+ spec.add_dependency "gruf"
36
+ spec.add_dependency "paranoia", "~> 2.2"
37
+ spec.add_development_dependency "bundler", "~> 1.16"
38
+ spec.add_development_dependency "rake", "~> 10.0"
39
+ spec.add_development_dependency "rspec", "~> 3.0"
40
+ spec.add_development_dependency "rspec-rails", "~> 3.8.0"
41
+ spec.add_development_dependency "factory_bot_rails", "~> 4.8.2"
42
+ spec.add_development_dependency "faker", "~> 1.7.3"
43
+ spec.add_development_dependency "bullet", "~> 5.7.5"
44
+ spec.add_development_dependency "rails", "~> 5.2.1"
45
+
46
+ spec.add_development_dependency "sqlite3", "~> 1.3.6"
47
+ spec.add_development_dependency "database_cleaner", "~> 1.6.0"
48
+
49
+ end
data/lib/istox.rb ADDED
@@ -0,0 +1,22 @@
1
+ require 'active_record'
2
+ require 'paranoia'
3
+ require "istox/version"
4
+
5
+ module Istox
6
+ # Your code goes here...
7
+ class Test
8
+ def self.method1
9
+ "abc"
10
+ end
11
+ end
12
+
13
+ require "istox/helpers/publisher"
14
+ require "istox/helpers/bunny_boot"
15
+ require "istox/helpers/order_book"
16
+ require "istox/helpers/grpc_client"
17
+ require "istox/helpers/blockchain_receipt_service"
18
+ require "istox/models/blockchain_receipt"
19
+ require "istox/models/concerns/blockchain_receipt_query"
20
+ require "istox/consumers/blockchain_status_handler"
21
+ require "istox/migrations/create_blockchain_receipts"
22
+ end
@@ -0,0 +1,109 @@
1
+ module Istox
2
+ class BlockchainStatusHandler
3
+ class << self
4
+ def txn_status_changed(transactions)
5
+ transactions.each do |transaction|
6
+
7
+ if ::Istox::BlockchainReceipt.find_by_txid(transaction[:id]).nil?
8
+ puts 'Transaction not found, skipping...'
9
+ next
10
+ end
11
+
12
+ find_resource(transaction) do |receipt, resource_name, resource_id, resource_action, sid|
13
+ resource = begin
14
+ class_eval("::#{resource_name}").find(resource_id)
15
+ rescue => e
16
+ puts e.inspect
17
+ next
18
+ end
19
+ receipt_list = get_receipt_list(resource, resource_name)
20
+
21
+ next if !allowed_statuses?(transaction)
22
+ next if receipt_handled?(receipt_list)
23
+
24
+ if confirmed?(transaction)
25
+ next if resource.outcome == 'pending'
26
+ if resource.outcome == 'confirmed'
27
+ resource.handle_confirm(resource_action)
28
+ publish_to_frontend(resource, true, receipt, sid, receipt_list) do
29
+ update_receipts(receipt_list)
30
+ end
31
+ end
32
+ end
33
+
34
+ if failed?(transaction)
35
+ resource.handle_fail(resource_action)
36
+ publish_to_frontend(resource, false, receipt, sid, receipt_list) { update_receipts(receipt_list) }
37
+ end
38
+ end
39
+ end
40
+ end
41
+
42
+ private
43
+
44
+ def allowed_statuses?(transaction)
45
+ return %w[failed confirmed reverted].include?(transaction[:status])
46
+ end
47
+
48
+ def get_receipt_list(resource, resource_name)
49
+ ::Istox::BlockchainReceipt.where(
50
+ resource_name: resource_name,
51
+ resource_id: resource.id
52
+ )
53
+ end
54
+
55
+ def publish_to_frontend(model, success, receipt, sid, receipt_list)
56
+ yield if block_given?
57
+
58
+ ::Istox::Publisher.publish(
59
+ 'amq.topic',
60
+ model.class.name.gsub('::', '').underscore.to_s,
61
+ data: {
62
+ id: model.id,
63
+ extra: model.handle_extra
64
+ }
65
+ )
66
+
67
+ ::Istox::Publisher.publish(
68
+ 'amq.topic',
69
+ sid,
70
+ data: {
71
+ success: success,
72
+ purpose: receipt.activity,
73
+ txids: get_receipts_tx_ids(receipt_list)
74
+ }
75
+ )
76
+ end
77
+
78
+ def failed?(transaction)
79
+ transaction[:status] == 'failed' || transaction[:status] == 'reverted'
80
+ end
81
+
82
+ def confirmed?(transaction)
83
+ transaction[:status] == 'confirmed'
84
+ end
85
+
86
+ def update_receipts(receipt_list)
87
+ receipt_list.each do |receipt|
88
+ receipt.update(resource_handled: true)
89
+ end
90
+ end
91
+
92
+ def find_resource(transaction)
93
+ receipt = ::Istox::BlockchainReceipt.find_by_txid(transaction[:id])
94
+ yield(receipt, receipt.resource_name, receipt.resource_id, receipt.resource_action, receipt.sid)
95
+ end
96
+
97
+ def get_receipts_tx_ids(receipt_list)
98
+ tx_ids = receipt_list.map(&:txid).compact
99
+
100
+ tx_ids.join(', ')
101
+ end
102
+
103
+ def receipt_handled?(receipt_list)
104
+ receipt_list.map(&:resource_handled).none? { |item| item.nil? || item == false }
105
+ end
106
+
107
+ end
108
+ end
109
+ end
@@ -0,0 +1,26 @@
1
+ module Istox
2
+ class BlockchainReceiptService
3
+ def create!(blockchain_receipt_klass, txids, model, sid, action)
4
+
5
+ klass = class_eval("::#{blockchain_receipt_klass.name}")
6
+ unless klass <= ::Istox::BlockchainReceipt
7
+ raise RuntimeError, "#{blockchain_receipt_klass.name} does not inherit istox blockchain receipt"
8
+ end
9
+
10
+ # delete the previous existing blockchain receipts
11
+ klass.where(resource_name: model.class.name,
12
+ resource_id: model.id).destroy_all
13
+
14
+ [txids].flatten.each do |txid|
15
+ blockchain_receipt = klass.new(
16
+ sid: sid,
17
+ txid: txid,
18
+ resource_name: model.class.name,
19
+ resource_id: model.id,
20
+ resource_action: action
21
+ )
22
+ blockchain_receipt.save!
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,74 @@
1
+ require 'bunny'
2
+ module Istox
3
+ class BunnyBoot
4
+ class << self
5
+
6
+ def initialize!(amqp_config_path)
7
+ @@amqp_config_path = amqp_config_path
8
+ ::Istox::Publisher.initialize!(amqp_config_path)
9
+ connect
10
+ subscribe_to_queues
11
+ end
12
+
13
+ def connect
14
+ connection = Bunny.new(data[:connect].symbolize_keys)
15
+ connection.start
16
+ $channel = connection.create_channel
17
+ end
18
+
19
+ protected
20
+
21
+ def subscribe_to_queues
22
+ data.queues.each do |data|
23
+ config = OpenStruct.new(data[1])
24
+ klass = Object.const_get("::#{data[0]}_consumer".camelize)
25
+ exchange_type = if config.exchange.ends_with?('.fanout')
26
+ :fanout
27
+ else
28
+ :direct
29
+ end
30
+ options = {}
31
+ options[:routing_key] = config.name if exchange_type != :fanout
32
+ queue = $channel.queue(
33
+ config.name,
34
+ durable: config.durable
35
+ ).bind(exchange(config.exchange, exchange_type), options)
36
+
37
+ queue.subscribe do |delivery_info, metadata, payload|
38
+ begin
39
+ Rails.logger.info("Processing in consumer: #{klass}")
40
+ klass.new.process(
41
+ Hashie::Mash.new(JSON.parse(payload)),
42
+ metadata,
43
+ delivery_info
44
+ )
45
+ rescue Exception => e
46
+ Rails.logger.error(e)
47
+ ensure
48
+ $channel.ack(delivery_info.delivery_tag) if config.manual_ack
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ private
55
+
56
+ def read_file
57
+ YAML.safe_load(ERB.new(File.read(@@amqp_config_path)).result)
58
+ end
59
+
60
+ def data
61
+ OpenStruct.new(read_file)
62
+ end
63
+
64
+ def exchange(host_exchange, exchange_type)
65
+ case exchange_type
66
+ when :fanout
67
+ $channel.fanout(host_exchange, durable: true)
68
+ else
69
+ $channel.direct(host_exchange, durable: true)
70
+ end
71
+ end
72
+ end
73
+ end
74
+ end
@@ -0,0 +1,63 @@
1
+ module Istox
2
+ class GrpcClient
3
+ class << self
4
+
5
+ def add_host(host_type, url)
6
+ unless defined?(@@hosts)
7
+ @@hosts = {}
8
+ end
9
+
10
+ @@hosts[host_type] = url
11
+ end
12
+
13
+ def call(host_type, service, method, **keyword_args)
14
+ execute(host_type, service, method, **keyword_args)
15
+ rescue => e
16
+ # reinitiate service and try again, second chance
17
+ reinitiate_service(host_type, service)
18
+ execute(host_type, service, method, **keyword_args)
19
+ end
20
+
21
+ private
22
+
23
+ def execute(host_type, service, method, **keyword_args)
24
+ Rails.logger.info "Calling method in grpc method: #{method.inspect}, params: #{keyword_args.inspect}"
25
+ t1 = Time.now
26
+
27
+ result = get_host(host_type, service).call(method, keyword_args)
28
+ Rails.logger.info "Time taken for grpc execution: #{Time.now - t1} seconds"
29
+
30
+ result
31
+ end
32
+
33
+ def get_host(host_type, service)
34
+ unless defined?(@@services)
35
+ @@services = {}
36
+ end
37
+
38
+ grpc_client = @@services[host_type + service]
39
+
40
+ if grpc_client
41
+ return grpc_client
42
+ else
43
+ reinitiate_service(host_type, service)
44
+ return @@services[host_type + service]
45
+ end
46
+ end
47
+
48
+ def reinitiate_service(host_type, service)
49
+ unless defined?(@@services)
50
+ @@services = {}
51
+ end
52
+
53
+ host_url = @@hosts[host_type]
54
+ raise StandardError, 'Unable to find host, have you forgotten to add host to grpc client?' if !host_url
55
+
56
+ Rails.logger.info 'Reinitiating to grpc host at ' + host_url
57
+ t1 = Time.now
58
+ @@services[host_type + service] = ::Gruf::Client.new(service: service, options: { hostname: host_url })
59
+ Rails.logger.info "Time taken for reinitiating grpc host: #{Time.now - t1} seconds"
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,46 @@
1
+ module Istox
2
+ class MessageService
3
+ class << self
4
+ def send_sms(number, subject, content)
5
+ publish('sms', data: {
6
+ to: number,
7
+ subject: subject,
8
+ body: content
9
+ },
10
+ type: __method__.to_s.upcase!)
11
+ end
12
+
13
+ def send_sms_template(number, template)
14
+ publish('sms', data: {
15
+ to: number,
16
+ template: template
17
+ },
18
+ type: __method__.to_s.upcase!)
19
+ end
20
+
21
+ def send_email(email, subject, content)
22
+ publish('email', data: {
23
+ to: email,
24
+ subject: subject,
25
+ type: 'text/plain',
26
+ body: content
27
+ },
28
+ type: __method__.to_s.upcase!)
29
+ end
30
+
31
+ def send_email_template(email, template)
32
+ publish('email', data: {
33
+ to: email,
34
+ template: template
35
+ },
36
+ type: __method__.to_s.upcase!)
37
+ end
38
+
39
+ private
40
+
41
+ def publish(type, payload)
42
+ ::Istox::Publisher.publish('message', "#{type}.message", payload)
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,58 @@
1
+ module Istox
2
+ class OrderBook
3
+ class << self
4
+ def allocation(soft_cap, total_supply, investments)
5
+
6
+ # sort by token price desc, and id asc
7
+ investments = investments.sort do |a, b|
8
+ [b[:token_price], a[:id]] <=> [a[:token_price], b[:id]]
9
+ end
10
+
11
+ interests = investments
12
+ if interests.count > 0
13
+ cutoff_price = 0.0
14
+ total_bid_token = 0.0
15
+ total_allocated = 0.0
16
+ total_unallocated = 0.0
17
+ total_investment = 0.0
18
+ cutoff_price = (soft_cap / total_supply).round(4)
19
+ is_cutoff = false
20
+
21
+ interests = interests.map do |item|
22
+ bid_token = (item[:fiat_amount] / item[:token_price]).round(4)
23
+ total_bid_token += bid_token
24
+ allocated = 0.0
25
+ unallocated = bid_token
26
+ unless is_cutoff
27
+ allocated = bid_token
28
+ unallocated = 0.0
29
+ if total_allocated + bid_token >= total_supply
30
+ unallocated = total_allocated + bid_token - total_supply
31
+ allocated = bid_token - unallocated
32
+ cutoff_price = item[:token_price]
33
+ is_cutoff = true
34
+ end
35
+ total_allocated += allocated
36
+ total_unallocated += unallocated
37
+ end
38
+ { id: item[:id], fiat_amount: item[:fiat_amount], bid_price: item[:token_price], bid_token: bid_token.round(4),
39
+ allocated: allocated.round(4), unallocated: unallocated.round(4) }
40
+ end
41
+
42
+ interests = interests.map do |item|
43
+ investment = item[:allocated] * cutoff_price
44
+ total_investment += investment
45
+ item.merge!(investment: investment)
46
+ end
47
+
48
+ return { total_supply: total_supply, total_investment: total_investment.round(4),
49
+ cutoff_price: cutoff_price.round(4), total_bid_token: total_bid_token.round(4),
50
+ total_allocated: total_allocated.round(4), total_unallocated: total_unallocated.round(4),
51
+ interests: interests }
52
+ else
53
+ raise 'Registration interest is empty'
54
+ end
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,36 @@
1
+ module Istox
2
+ class Publisher
3
+ class << self
4
+
5
+ def initialize!(amqp_config_path)
6
+ @@amqp_config_path = amqp_config_path
7
+ end
8
+
9
+ def publish(exchange, routing_key, message)
10
+ Rails.logger.info 'Before publish retrieve data..'
11
+ durable = data.fetch('publish')[exchange]['durable']
12
+ Rails.logger.info "Publishing message: #{message} with exchange: #{exchange} routing key: #{routing_key}"
13
+ enqueue do
14
+ $channel
15
+ .direct(exchange, durable: durable)
16
+ .publish(message.to_json, routing_key: routing_key, durable: durable)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def enqueue
23
+ initialize_channel! unless !!$channel
24
+ yield
25
+ end
26
+
27
+ def initialize_channel!
28
+ ::Istox::BunnyBoot.connect
29
+ end
30
+
31
+ def data
32
+ YAML.safe_load(ERB.new(File.read(@@amqp_config_path)).result)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,22 @@
1
+ module Istox
2
+ class CreateBlockchainReceipts < ActiveRecord::Migration[5.2]
3
+ def initialize(table_name = nil)
4
+ @table_name = table_name
5
+ unless @table_name
6
+ @table_name = :blockchain_receipts
7
+ end
8
+ end
9
+
10
+ def change
11
+ create_table @table_name do |t|
12
+ t.string :txid
13
+ t.integer :resource_id
14
+ t.string :resource_action
15
+ t.string :resource_name
16
+ t.boolean :resource_handled
17
+ t.datetime :deleted_at, index: true
18
+ t.timestamps
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,10 @@
1
+ module Istox
2
+ class BlockchainReceipt < ActiveRecord::Base
3
+ acts_as_paranoid
4
+
5
+
6
+ def activity
7
+ I18n.t("#{resource_name}.#{resource_action}")
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,60 @@
1
+ module Istox
2
+ module BlockchainReceiptQuery
3
+ extend ActiveSupport::Concern
4
+
5
+ #overrideable, when blockchain transaction has confirmed
6
+ def handle_confirm(resource_action)
7
+ self
8
+ end
9
+
10
+ #overrideable, when blockchain transaction has failed
11
+ def handle_fail(resource_action)
12
+ self
13
+ end
14
+
15
+ #overrideable, extra data that will be published back to frontend
16
+ def handle_extra
17
+ nil
18
+ end
19
+
20
+ # outcome of the blockchain transaction, can be "pending" / "confirmed" / "failed"
21
+ def outcome
22
+ @blockchain_receipts = get_blockchain_receipts
23
+
24
+ return "confirmed" if @blockchain_receipts.empty?
25
+
26
+ txids = @blockchain_receipts.map do |blockchain_receipt|
27
+ blockchain_receipt.txid
28
+ end.compact
29
+
30
+ @blocks = get_block_transactions(txids)
31
+
32
+ return "failed" if blocks_failed?
33
+ return "pending" if blocks_pending?
34
+ return "confirmed" if blocks_confirmed?
35
+ end
36
+
37
+ private
38
+
39
+ def get_blockchain_receipts
40
+ ::Istox::BlockchainReceipt.where(resource_id: self.id, resource_name: self.class.name).all
41
+ end
42
+
43
+ def blocks_failed?
44
+ @blocks.any? { |obj| obj[:status] == 'failed' }
45
+ end
46
+
47
+ def blocks_pending?
48
+ @blocks.empty? || @blocks.any? { |obj| obj[:status] == 'pending' }
49
+ end
50
+
51
+ def blocks_confirmed?
52
+ !@blocks.any? { |obj| obj[:status] == 'pending' } && !@blocks.any? { |obj| obj[:status] == 'failed' }
53
+ end
54
+
55
+ def get_block_transactions(txids)
56
+ #to do graphql call to get results
57
+ end
58
+
59
+ end
60
+ end
@@ -0,0 +1,3 @@
1
+ module Istox
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,246 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: istox
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Siong Leng
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-03-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bunny
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 2.12.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 2.12.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: gruf
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: paranoia
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '1.16'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '1.16'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec-rails
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: 3.8.0
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: 3.8.0
111
+ - !ruby/object:Gem::Dependency
112
+ name: factory_bot_rails
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: 4.8.2
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: 4.8.2
125
+ - !ruby/object:Gem::Dependency
126
+ name: faker
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: 1.7.3
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: 1.7.3
139
+ - !ruby/object:Gem::Dependency
140
+ name: bullet
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: 5.7.5
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: 5.7.5
153
+ - !ruby/object:Gem::Dependency
154
+ name: rails
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: 5.2.1
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: 5.2.1
167
+ - !ruby/object:Gem::Dependency
168
+ name: sqlite3
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: 1.3.6
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: 1.3.6
181
+ - !ruby/object:Gem::Dependency
182
+ name: database_cleaner
183
+ requirement: !ruby/object:Gem::Requirement
184
+ requirements:
185
+ - - "~>"
186
+ - !ruby/object:Gem::Version
187
+ version: 1.6.0
188
+ type: :development
189
+ prerelease: false
190
+ version_requirements: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - "~>"
193
+ - !ruby/object:Gem::Version
194
+ version: 1.6.0
195
+ description: istox backend shared gem
196
+ email:
197
+ - siongleng@ich-group.com
198
+ executables: []
199
+ extensions: []
200
+ extra_rdoc_files: []
201
+ files:
202
+ - ".gitignore"
203
+ - CODE_OF_CONDUCT.md
204
+ - Gemfile
205
+ - Gemfile.lock
206
+ - README.md
207
+ - Rakefile
208
+ - bin/console
209
+ - bin/setup
210
+ - istox.gemspec
211
+ - lib/istox.rb
212
+ - lib/istox/consumers/blockchain_status_handler.rb
213
+ - lib/istox/helpers/blockchain_receipt_service.rb
214
+ - lib/istox/helpers/bunny_boot.rb
215
+ - lib/istox/helpers/grpc_client.rb
216
+ - lib/istox/helpers/message_service.rb
217
+ - lib/istox/helpers/order_book.rb
218
+ - lib/istox/helpers/publisher.rb
219
+ - lib/istox/migrations/create_blockchain_receipts.rb
220
+ - lib/istox/models/blockchain_receipt.rb
221
+ - lib/istox/models/concerns/blockchain_receipt_query.rb
222
+ - lib/istox/version.rb
223
+ homepage: http://www.abc.com
224
+ licenses: []
225
+ metadata: {}
226
+ post_install_message:
227
+ rdoc_options: []
228
+ require_paths:
229
+ - lib
230
+ required_ruby_version: !ruby/object:Gem::Requirement
231
+ requirements:
232
+ - - ">="
233
+ - !ruby/object:Gem::Version
234
+ version: '0'
235
+ required_rubygems_version: !ruby/object:Gem::Requirement
236
+ requirements:
237
+ - - ">="
238
+ - !ruby/object:Gem::Version
239
+ version: '0'
240
+ requirements: []
241
+ rubyforge_project:
242
+ rubygems_version: 2.7.7
243
+ signing_key:
244
+ specification_version: 4
245
+ summary: istox backend shared gem
246
+ test_files: []