crypto-service 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: caeb3d4bf826a92cf335e649c50fb7abadd7b6175c021f438641781f070f3e05
4
- data.tar.gz: 9b5a35056f03f7701f8482cd28b2a55edbb0d587548192859bcfd2c0b5cc7c49
3
+ metadata.gz: ed5769c8ed79cd6a12e10aabd5035776ea6ef5cdcc2f32812e65e86a1464ab1d
4
+ data.tar.gz: 9e547251610c7f066564c4af4d7ff87cb2b5c1a0e0a3ec90ee9bdad3aee464ea
5
5
  SHA512:
6
- metadata.gz: 91558907b161b5cde84533d8df178cf2c20e31d570e029045a7eea9cf85a80868b39d7f0f51f4d9fdb03f712209affe94fc6f296c97b6db609ee18ca0fb7aea4
7
- data.tar.gz: 4024d5c5eaa55c595a46f0404d778eabdcaa20cd27eba183c9b996f40f6053def3d78be7e41cf4bbafb0f3fab4fcb67a45b3100acba536a5ae398dc93cec4c18
6
+ metadata.gz: 1a06fc3a72160c2ce570e91725fcf8b3d749088a13b322b55e630a70a6ba40c2cc075f003f9dbe6e1ca29445782227b89f0a3b7902a55abdc296c915a9eae0f7
7
+ data.tar.gz: c3c7dc4526dc42383a968db113aad034aaf7da46e310e0436e6c4ed00a893ef7e897ac87a49b6999f5ffa130295f6ac8abe6986821f9d87e8f742421f93f9beb
data/lib/crypto/base.rb CHANGED
@@ -3,14 +3,70 @@
3
3
  require 'uri'
4
4
  require 'net/http'
5
5
  require 'json'
6
+ require 'coinex'
7
+ require 'request'
6
8
 
7
9
  module Crypto
8
10
  class Base
9
11
  class << self
10
- attr_accessor :app_info, :path_prefix
12
+ attr_accessor :app_info, :path_prefix, :development
13
+
14
+ def calculate_win_or_lost(avg_price:, exchange:, settings:)
15
+ percentages = percentage(exchange: exchange, settings: settings)
16
+ profit = percentages.first
17
+ lost = percentages[1]
18
+
19
+ win_price = avg_price.to_f + ((avg_price.to_f * profit.to_f) / 100)
20
+ lost_price = avg_price.to_f - ((avg_price.to_f * lost.to_f) / 100)
21
+
22
+ [win_price, lost_price]
23
+ end
24
+
25
+ def binance?(exchange)
26
+ exchange == 'Binance'
27
+ end
28
+
29
+ def percentage(exchange:, settings:)
30
+ if binance?(exchange)
31
+ profit = settings[:perc_profit_exchange]
32
+ loss = settings[:perc_lost_exchange]
33
+ else
34
+ profit = settings[:perc_profit_all]
35
+ loss = settings[:perc_lost_all]
36
+ end
37
+
38
+ [profit, loss]
39
+ end
40
+
41
+ def correct(result)
42
+ { 'status' => :success, 'code' => 200, 'payload' => result }
43
+ end
44
+
45
+ def error(result)
46
+ { 'status' => :failed, 'code' => result['code'], 'message' => result['message'] }
47
+ end
48
+
49
+ def coinex_keys(settings)
50
+ Crypto::Exchanges::Cx.access_id(settings[:access_id])
51
+ Crypto::Exchanges::Cx.secret_key(settings[:secret_key])
52
+ end
53
+
54
+ def response_correct?(result)
55
+ (result['code']).zero?
56
+ end
57
+
58
+ def calculate_signature(params)
59
+ data_signature = Coinex::Signature.calculate(params: params)
60
+
61
+ params[:tonce] = Crypto.development ? '1672669323219' : data_signature[:tonce]
62
+ params[:signature] = data_signature[:signature]
63
+
64
+ params
65
+ end
11
66
  end
12
67
 
13
68
  self.app_info = ::ENV['APP_INFO'] || "#{::Crypto::NAME} V#{::Crypto.version}"
14
69
  self.path_prefix = 'https://www.binance.com/bapi/composite/v1/public/promo/cmc/cryptocurrency'
70
+ self.development = false
15
71
  end
16
72
  end
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Class that buy crypto, set TP & SL and return their responses
4
+ # Parameter of order:
5
+ # Payload: Hash of Crypto::Listing.info or Crypto::Top.info
6
+ # Settings: contact settings
7
+ # Exchange: exchange where crypto is listed or buy, (Binance → 20%, Other exchange → 10% )
8
+
9
+ module Crypto
10
+ class Buy < Base
11
+ class << self
12
+ # rubocop: disable Metrics/MethodLength, Lint/MissingCopEnableDirective
13
+ def order(payload:, settings:, exchange:, count:)
14
+ coinex_keys(settings)
15
+ result = Coinex::Order.market(params: params(
16
+ symbol: payload['symbol'],
17
+ count: count
18
+ ))
19
+
20
+ Crypto::Exchanges::Cx.clean_keys
21
+
22
+ if response_correct?(result)
23
+ prices = calculate_win_or_lost(avg_price: result['data']['avg_price'], exchange: exchange, settings: settings)
24
+ divert(payload: payload, result: result, prices: prices, settings: settings)
25
+ else
26
+ error(result)
27
+ end
28
+ end
29
+
30
+ def balance(count)
31
+ params = Coinex::Signature.calculate
32
+ params[:tonce] = '1672669323219' if Crypto.development
33
+
34
+ balance = Coinex::Balance.info(params: params)
35
+ balance['data']['USDT']['available'].to_i / count.to_i
36
+ end
37
+
38
+ def params(symbol:, count:)
39
+ params = { market: "#{symbol}USDT", type: 'buy', amount: balance(count) }
40
+ calculate_signature(params)
41
+ end
42
+
43
+ def divert(payload:, result:, prices:, settings:)
44
+ operations = []
45
+ operations << result
46
+
47
+ loss = Crypto::Loss.limit(payload: payload, result: result, prices: prices, settings: settings)
48
+ operations << loss
49
+ return operations if payload['mode'] == :top
50
+
51
+ profit = Crypto::Profit.limit(payload: payload, result: result, prices: prices, settings: settings)
52
+ operations << profit
53
+
54
+ operations
55
+ end
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Exchange -> Coinex
4
+
5
+ module Crypto
6
+ module Exchanges
7
+ class Cx < Base
8
+ class << self
9
+ def access_id(access_id)
10
+ return unless ping
11
+
12
+ Coinex.access_id = access_id
13
+ end
14
+
15
+ def secret_key(secret_key)
16
+ return unless ping
17
+
18
+ Coinex.secret_key = secret_key
19
+ end
20
+
21
+ def clean_keys
22
+ return unless ping
23
+
24
+ Coinex.access_id = nil
25
+ Coinex.secret_key = nil
26
+ end
27
+
28
+ def ping
29
+ Coinex.ping == { status: 200 }
30
+ end
31
+ end
32
+ end
33
+ end
34
+ end
@@ -2,6 +2,8 @@
2
2
 
3
3
  require 'active_support/all'
4
4
 
5
+ # Class that return crypto listed in a interval of MINUTES
6
+
5
7
  module Crypto
6
8
  class Listing < Base
7
9
  MINUTES = 15
@@ -34,7 +36,7 @@ module Crypto
34
36
 
35
37
  def detail
36
38
  uri = URI("#{Crypto.path}/listings/new?limit=100&start=1")
37
- Crypto::Request.request(uri: uri, method: 'GET')
39
+ Request::Http.request(uri: uri, method: 'GET')
38
40
  end
39
41
 
40
42
  def status?(payload)
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crypto
4
+ class Loss < Stop
5
+ class << self
6
+ private
7
+
8
+ def what_price(prices)
9
+ prices[1]
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crypto
4
+ class Profit < Stop
5
+ class << self
6
+ private
7
+
8
+ def what_price(prices)
9
+ prices.first
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crypto
4
+ class Sell < Base
5
+ class << self
6
+ def order(payload:, settings:)
7
+ coinex_keys(settings)
8
+ result = Coinex::Order.market(params: params(payload))
9
+
10
+ Crypto::Exchanges::Cx.clean_keys
11
+
12
+ if response_correct?(result)
13
+ correct(result)
14
+ else
15
+ error(result)
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def params(payload)
22
+ params = { market: "#{payload['symbol']}USDT", type: 'sell', amount: payload['balance'] }
23
+ calculate_signature(params)
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Crypto
4
+ class Stop < Base
5
+ class << self
6
+ def limit(payload:, result:, prices:, settings:)
7
+ coinex_keys(settings)
8
+
9
+ result = Coinex::Order.limit(params: params(
10
+ payload: payload, result: result, prices: prices
11
+ ))
12
+
13
+ Crypto::Exchanges::Cx.clean_keys
14
+ if response_correct?(result)
15
+ correct(result)
16
+ else
17
+ error(result)
18
+ end
19
+ end
20
+
21
+ private
22
+
23
+ def params(payload:, result:, prices:)
24
+ params = { market: "#{payload['symbol']}USDT", type: 'sell', amount: result['data']['amount'],
25
+ stop_price: what_price(prices) }
26
+
27
+ calculate_signature(params)
28
+ end
29
+ end
30
+ end
31
+ end
@@ -1,5 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ # Class that return crypto after a percentage in top gainers
4
+
3
5
  module Crypto
4
6
  class Top < Base
5
7
  PERCENTAGE = 20.0
@@ -32,7 +34,7 @@ module Crypto
32
34
 
33
35
  def detail
34
36
  uri = URI("#{Crypto.path}/trending/gainers-losers?limit=100&sort_dir=desc&start=1")
35
- Crypto::Request.request(uri: uri, method: 'GET')
37
+ Request::Http.request(uri: uri, method: 'GET')
36
38
  end
37
39
 
38
40
  def status?(payload)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Crypto
4
- VERSION = '0.0.0'
4
+ VERSION = '0.0.1'
5
5
  end
data/lib/crypto.rb CHANGED
@@ -27,6 +27,14 @@ module Crypto
27
27
  def version
28
28
  VERSION
29
29
  end
30
+
31
+ def development=(boolean)
32
+ @mutex.synchronize { ::Crypto::Base.development = boolean }
33
+ end
34
+
35
+ def development
36
+ @mutex.synchronize { ::Crypto::Base.development }
37
+ end
30
38
  end
31
39
  end
32
40
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crypto-service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefano Baldazzi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-02 00:00:00.000000000 Z
11
+ date: 2023-01-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -24,6 +24,34 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 7.0.4
27
+ - !ruby/object:Gem::Dependency
28
+ name: aga-request
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.0.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.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: coinex
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.0.3
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.0.3
27
55
  - !ruby/object:Gem::Dependency
28
56
  name: zeitwerk
29
57
  requirement: !ruby/object:Gem::Requirement
@@ -162,8 +190,13 @@ files:
162
190
  - lib/crypto.rb
163
191
  - lib/crypto/autoloader.rb
164
192
  - lib/crypto/base.rb
193
+ - lib/crypto/resources/buy.rb
194
+ - lib/crypto/resources/exchanges/cx.rb
165
195
  - lib/crypto/resources/listing.rb
166
- - lib/crypto/resources/request.rb
196
+ - lib/crypto/resources/loss.rb
197
+ - lib/crypto/resources/profit.rb
198
+ - lib/crypto/resources/sell.rb
199
+ - lib/crypto/resources/stop.rb
167
200
  - lib/crypto/resources/top.rb
168
201
  - lib/crypto/version.rb
169
202
  homepage: https://github.com/Baldaz02/crypto-ruby
@@ -188,5 +221,5 @@ requirements: []
188
221
  rubygems_version: 3.3.24
189
222
  signing_key:
190
223
  specification_version: 4
191
- summary: crypto0.0.0
224
+ summary: crypto0.0.1
192
225
  test_files: []
@@ -1,31 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Crypto
4
- class Request < Base
5
- class << self
6
- def request(uri:, method:)
7
- req = get_method(name: method, uri: uri)
8
- req['Content-Type'] = 'application/json'
9
-
10
- res = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == 'https') do |http|
11
- http.request(req)
12
- end
13
-
14
- JSON.parse(res.body)
15
- end
16
-
17
- def get_method(name:, uri:)
18
- case name
19
- when 'GET'
20
- req = Net::HTTP::Get.new(uri)
21
- # when 'POST'
22
- # req = Net::HTTP::Post.new(uri)
23
- # when 'DELETE'
24
- # req = Net::HTTP::Delete.new(uri)
25
- end
26
-
27
- req
28
- end
29
- end
30
- end
31
- end