peatio-bitgo 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 48c2c390acb4922bd602b2ab5a426552b3427ed1221f9abf704e8fb04ba4b9bc
4
+ data.tar.gz: dea150704c82b90e18f621b6db976e909cef707c95efb598438ab2eeaa9986cd
5
+ SHA512:
6
+ metadata.gz: 3dbe30292603bdc9e590de8a516e9039179a180b2f305f0daf99075ca252d0e969b09454ab30401f8aeb02c329d7138cb84cee1a3b23c8810d4c617679575de2
7
+ data.tar.gz: acb680e00ac6d62bef9388eaef7b657a6c644f3482f784361e7d2ee795ece07a2858a29cece481c741be2af988164fbf11222b2e8798363343efbfc95493b667
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,8 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.5.3
7
+ - 2.6.2
8
+ before_install: gem install bundler -v 1.16.3
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 peatio-bitgo.gemspec
6
+ gemspec
@@ -0,0 +1,112 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ peatio-bitgo (1.0.0)
5
+ activesupport (~> 5.2.3)
6
+ better-faraday (~> 1.0.5)
7
+ faraday (~> 0.15.4)
8
+ memoist (~> 0.16.0)
9
+ net-http-persistent (~> 3.0.1)
10
+ peatio (>= 0.6.3)
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ activemodel (5.2.4.1)
16
+ activesupport (= 5.2.4.1)
17
+ activesupport (5.2.4.1)
18
+ concurrent-ruby (~> 1.0, >= 1.0.2)
19
+ i18n (>= 0.7, < 2)
20
+ minitest (~> 5.1)
21
+ tzinfo (~> 1.1)
22
+ addressable (2.7.0)
23
+ public_suffix (>= 2.0.2, < 5.0)
24
+ amq-protocol (2.3.0)
25
+ amqp (1.8.0)
26
+ amq-protocol (>= 2.2.0)
27
+ eventmachine
28
+ better-faraday (1.0.8)
29
+ activesupport (>= 4.0, < 6.0)
30
+ faraday (~> 0.12)
31
+ bunny (2.14.4)
32
+ amq-protocol (~> 2.3, >= 2.3.0)
33
+ byebug (11.1.1)
34
+ clamp (1.3.1)
35
+ coderay (1.1.2)
36
+ concurrent-ruby (1.1.6)
37
+ connection_pool (2.2.2)
38
+ crack (0.4.3)
39
+ safe_yaml (~> 1.0.0)
40
+ diff-lcs (1.3)
41
+ em-websocket (0.5.1)
42
+ eventmachine (>= 0.12.9)
43
+ http_parser.rb (~> 0.6.0)
44
+ eventmachine (1.2.7)
45
+ faraday (0.15.4)
46
+ multipart-post (>= 1.2, < 3)
47
+ hashdiff (1.0.0)
48
+ http_parser.rb (0.6.0)
49
+ i18n (1.8.2)
50
+ concurrent-ruby (~> 1.0)
51
+ jwt (2.2.1)
52
+ memoist (0.16.2)
53
+ method_source (0.9.2)
54
+ minitest (5.14.0)
55
+ mocha (1.11.2)
56
+ multipart-post (2.1.1)
57
+ mysql2 (0.5.3)
58
+ net-http-persistent (3.0.1)
59
+ connection_pool (~> 2.2)
60
+ peatio (0.6.3)
61
+ activemodel (> 5.2, <= 6.0.0)
62
+ amqp
63
+ bunny
64
+ clamp
65
+ em-websocket
66
+ eventmachine
67
+ jwt
68
+ mysql2
69
+ pry (0.12.2)
70
+ coderay (~> 1.1.0)
71
+ method_source (~> 0.9.0)
72
+ pry-byebug (3.8.0)
73
+ byebug (~> 11.0)
74
+ pry (~> 0.10)
75
+ public_suffix (4.0.3)
76
+ rake (10.5.0)
77
+ rspec (3.9.0)
78
+ rspec-core (~> 3.9.0)
79
+ rspec-expectations (~> 3.9.0)
80
+ rspec-mocks (~> 3.9.0)
81
+ rspec-core (3.9.1)
82
+ rspec-support (~> 3.9.1)
83
+ rspec-expectations (3.9.0)
84
+ diff-lcs (>= 1.2.0, < 2.0)
85
+ rspec-support (~> 3.9.0)
86
+ rspec-mocks (3.9.1)
87
+ diff-lcs (>= 1.2.0, < 2.0)
88
+ rspec-support (~> 3.9.0)
89
+ rspec-support (3.9.2)
90
+ safe_yaml (1.0.5)
91
+ thread_safe (0.3.6)
92
+ tzinfo (1.2.6)
93
+ thread_safe (~> 0.1)
94
+ webmock (3.8.2)
95
+ addressable (>= 2.3.6)
96
+ crack (>= 0.3.2)
97
+ hashdiff (>= 0.4.0, < 2.0.0)
98
+
99
+ PLATFORMS
100
+ ruby
101
+
102
+ DEPENDENCIES
103
+ bundler (~> 1.16)
104
+ mocha (~> 1.8)
105
+ peatio-bitgo!
106
+ pry-byebug
107
+ rake (~> 10.0)
108
+ rspec (~> 3.0)
109
+ webmock (~> 3.5)
110
+
111
+ BUNDLED WITH
112
+ 1.17.3
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 Naichuk Maksym
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,67 @@
1
+ # Peatio::bitgo
2
+
3
+ Peatio bitgo plugin for Rubykube stack
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'peatio-bitgo'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install peatio-bitgo
20
+
21
+ ## Usage
22
+
23
+ For Peatio bitgo plugin integration you need to do the following steps:
24
+
25
+ ### Image Build.
26
+
27
+ 1. Add peatio-bitgo gem into your Gemfile.plugin
28
+ ```ruby
29
+ gem 'peatio-bitgo', '~> 0.2.0'
30
+ ```
31
+
32
+ 2. Run `bundle install` for updating Gemfile.lock
33
+
34
+ 3. Build custom Peatio [docker image with bitgo plugin](https://github.com/rubykube/peatio/blob/master/docs/plugins.md#build)
35
+
36
+ 4. Push your image using `docker push`
37
+
38
+ 5. Update your deployment to use image with peatio-bitgo gem
39
+
40
+ ### Peatio Configuration.
41
+
42
+ 1. Create bitgo Blockchain [config example](../config/blockchains.yml).
43
+ * No additional steps are needed
44
+
45
+ 2. Create bitgo Currency [config example](../config/currencies.yml).
46
+ * No additional steps are needed
47
+
48
+ 3. Create bitgo Wallets [config example](../config/wallets.yml)(deposit and hot wallets are required).
49
+ * No additional steps are needed
50
+
51
+
52
+ ## Development
53
+
54
+ Plugin development [example](https://github.com/rubykube/peatio/blob/master/docs/coins/development.md).
55
+
56
+ ## Contributing
57
+
58
+ Bug reports and pull requests are welcome on GitHub at https://github.com/rubykube/peatio-bitgo.
59
+
60
+ ## License
61
+
62
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
63
+
64
+ ## Consulting
65
+
66
+ You can contact Openware for finding certified vendors:
67
+ [Openware.com](https://www.openware.com)
@@ -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
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "peatio/bitgo"
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__)
@@ -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
File without changes
@@ -0,0 +1,140 @@
1
+ - id: ltc
2
+ name: Litecoin
3
+ blockchain_key: ltc-testnet
4
+ symbol: 'Ł'
5
+ type: coin
6
+ precision: 8
7
+ base_factor: 100_000_000
8
+ enabled: true
9
+ # Deposits with less amount are skipped during blockchain synchronization.
10
+ # We advise to set value 10 times bigger than the network fee to prevent losses.
11
+ min_deposit_amount: 0.0004488
12
+ min_collection_amount: 0.0004488
13
+ withdraw_limit_24h: 0.5
14
+ withdraw_limit_72h: 1.2
15
+ deposit_fee: 0
16
+ withdraw_fee: 0
17
+ position: 1
18
+ options: {}
19
+
20
+ - id: xrp
21
+ name: Ripple
22
+ blockchain_key: xrp-testnet
23
+ symbol: 'ꭆ'
24
+ type: coin
25
+ precision: 8
26
+ base_factor: 1_000_000
27
+ enabled: true
28
+ # Deposits with less amount are skipped during blockchain synchronization.
29
+ # We advise to set value 10 times bigger than the network fee to prevent losses.
30
+ min_deposit_amount: 0.0001
31
+ min_collection_amount: 0.0001
32
+ withdraw_limit_24h: 100
33
+ withdraw_limit_72h: 200
34
+ deposit_fee: 0
35
+ withdraw_fee: 0
36
+ position: 2
37
+ options: {}
38
+
39
+ - id: bch
40
+ name: Bitcoin Cash
41
+ blockchain_key: bch-testnet
42
+ symbol: '฿'
43
+ type: coin
44
+ precision: 8
45
+ base_factor: 100_000_000
46
+ enabled: true
47
+ # Deposits with less amount are skipped during blockchain synchronization.
48
+ # We advise to set value 10 times bigger than the network fee to prevent losses.
49
+ min_deposit_amount: 0.0000748
50
+ min_collection_amount: 0.0000748
51
+ withdraw_limit_24h: 0.1
52
+ withdraw_limit_72h: 0.2
53
+ deposit_fee: 0
54
+ withdraw_fee: 0
55
+ position: 3
56
+ options: {}
57
+
58
+ - id: btc
59
+ name: Bitcoin
60
+ blockchain_key: btc-testnet
61
+ symbol: '฿'
62
+ type: coin
63
+ precision: 8
64
+ base_factor: 100_000_000
65
+ visible: true
66
+ deposit_enabled: true
67
+ withdrawal_enabled: true
68
+ # Deposits with less amount are skipped during blockchain synchronization.
69
+ # We advise to set value 10 times bigger than the network fee to prevent losses.
70
+ min_deposit_amount: 0.0000356
71
+ min_collection_amount: 0.0000356
72
+ withdraw_limit_24h: 0.1
73
+ withdraw_limit_72h: 0.2
74
+ deposit_fee: 0
75
+ withdraw_fee: 0
76
+ position: 4
77
+ options: {}
78
+
79
+ - id: eth
80
+ name: Ethereum
81
+ blockchain_key: eth-rinkeby
82
+ symbol: 'Ξ'
83
+ type: coin
84
+ precision: 8
85
+ base_factor: 1_000_000_000_000_000_000
86
+ visible: true
87
+ deposit_enabled: true
88
+ withdrawal_enabled: true
89
+ # Deposits with less amount are skipped during blockchain synchronization.
90
+ # We advise to set value 10 times bigger than the network fee to prevent losses.
91
+ min_deposit_amount: 0.00021
92
+ min_collection_amount: 0.00021
93
+ withdraw_limit_24h: 0.2
94
+ withdraw_limit_72h: 0.5
95
+ deposit_fee: 0
96
+ withdraw_fee: 0
97
+ position: 5
98
+ options:
99
+ # ETH tx fees configurations.
100
+ #
101
+ # Maximum amount of gas you're willing to spend on a particular transaction.
102
+ gas_limit: 21_000
103
+ # Internal price that is paid for running a transaction on the Ethereum network.
104
+ gas_price: 1_000_000_000
105
+
106
+ - id: eos
107
+ name: EOS
108
+ blockchain_key: eos-testnet
109
+ symbol: 'E'
110
+ type: coin
111
+ precision: 4
112
+ base_factor: 10_000
113
+ enabled: true
114
+ # Deposits with less amount are skipped during blockchain synchronization.
115
+ # We advise to set value 10 times bigger than the network fee to prevent losses.
116
+ min_deposit_amount: 0.0001
117
+ min_collection_amount: 0.0001
118
+ withdraw_limit_24h: 100.0
119
+ withdraw_limit_72h: 500.0
120
+ deposit_fee: 0
121
+ withdraw_fee: 0
122
+ position: 6
123
+
124
+ - id: dash
125
+ name: Dash
126
+ blockchain_key: dash-testnet
127
+ symbol: 'D'
128
+ type: coin
129
+ precision: 8
130
+ base_factor: 100_000_000
131
+ enabled: true
132
+ # Deposits with less amount are skipped during blockchain synchronization.
133
+ # We advise to set value 10 times bigger than the network fee to prevent losses.
134
+ min_deposit_amount: 0.0001
135
+ min_collection_amount: 0.0001
136
+ withdraw_limit_24h: 100.0
137
+ withdraw_limit_72h: 500.0
138
+ deposit_fee: 0
139
+ withdraw_fee: 0
140
+ position: 7
@@ -0,0 +1,54 @@
1
+
2
+ - name: Ethereum Deposit Wallet
3
+ blockchain_key: eth-rinkeby
4
+ currency_id: eth
5
+ # Address where deposits will be collected to.
6
+ address: '0x2b9fBC10EbAeEc28a8Fc10069C0BC29E45eBEB9C' # IMPORTANT: Always wrap this value in quotes!
7
+ kind: deposit # Wallet kind (deposit, hot, warm, cold or fee).
8
+ max_balance: 0.0
9
+ status: active
10
+ gateway: geth # Gateway client name.
11
+ settings:
12
+ #
13
+ # Geth gateway client settings.
14
+ uri: http://127.0.0.1:8545
15
+ secret: 'changeme'
16
+ bitgo_test_net: true
17
+ bitgo_access_token: 'v2x659261647b540ee3acda5c50ae4e878we323474eea5cbff9b9615139629'
18
+ bitgo_wallet_id: '5e5388ad80334347ceb3540c741d'
19
+
20
+ - name: Ethereum Hot Wallet
21
+ blockchain_key: eth-rinkeby
22
+ currency_id: eth
23
+ # Address where deposits will be collected to.
24
+ address: '0x270704935783087a01c7a28d8f2d8f01670c8050' # IMPORTANT: Always wrap this value in quotes!
25
+ kind: hot # Wallet kind (deposit, hot, warm, cold or fee).
26
+ max_balance: 100.0
27
+ status: active
28
+ gateway: geth # Gateway client name.
29
+ settings:
30
+ #
31
+ # Geth gateway client settings.
32
+ uri: http://127.0.0.1:8545
33
+ secret: 'test'
34
+ bitgo_test_net: true
35
+ bitgo_access_token: 'v2x659261647b540ee3acda5c50ae4e878we323474eea5cbff9b9615139629'
36
+ bitgo_wallet_id: '5e5388ad80334347ceb3540c741d'
37
+
38
+ - name: Ethereum Warm Wallet
39
+ blockchain_key: eth-rinkeby
40
+ currency_id: eth
41
+ # Address where deposits will be collected to.
42
+ address: '0x2b9fBC10EbAeEc28a8Fc10069C0BC29E45eBEB9C' # IMPORTANT: Always wrap this value in quotes!
43
+ kind: warm # Wallet kind (deposit, hot, warm, cold or fee).
44
+ max_balance: 1000.0
45
+ status: active
46
+ gateway: geth # Gateway client name.
47
+ settings:
48
+ #
49
+ # Geth gateway client settings.
50
+ uri: http://127.0.0.1:8545
51
+ secret: 'test'
52
+ bitgo_test_net: true
53
+ bitgo_access_token: 'v2x659261647b540ee3acda5c50ae4e878we323474eea5cbff9b9615139629'
54
+ bitgo_wallet_id: '5e5388ad80334347ceb3540c741d'
@@ -0,0 +1,29 @@
1
+ # Integration.
2
+
3
+ For Peatio bitgo plugin integration you need to do the following steps:
4
+
5
+ ## Image Build.
6
+
7
+ 1. Add peatio-bitgo gem into your Gemfile.plugin
8
+ ```ruby
9
+ gem 'peatio-bitgo', '~> 0.1.0'
10
+ ```
11
+
12
+ 2. Run `bundle install` for updating Gemfile.lock
13
+
14
+ 3. Build custom Peatio [docker image with bitgo plugin](https://github.com/rubykube/peatio/blob/master/docs/plugins.md#build)
15
+
16
+ 4. Push your image using `docker push`
17
+
18
+ 5. Update your deployment to use image with peatio-bitgo gem
19
+
20
+ ## Peatio Configuration.
21
+
22
+ 1. Create bitgo Blockchain [config example](../config/blockchains.yml).
23
+ * No additional steps are needed
24
+
25
+ 2. Create bitgo Currency [config example](../config/currencies.yml).
26
+ * No additional steps are needed
27
+
28
+ 3. Create bitgo Wallets [config example](../config/wallets.yml)(deposit and hot wallets are required).
29
+ * No additional steps are needed
@@ -0,0 +1,16 @@
1
+ require "peatio"
2
+
3
+ module Peatio
4
+ module Bitgo
5
+ require "bigdecimal"
6
+ require "bigdecimal/util"
7
+
8
+ require "peatio/bitgo/blockchain"
9
+ require "peatio/bitgo/client"
10
+ require "peatio/bitgo/wallet"
11
+
12
+ require "peatio/bitgo/hooks"
13
+
14
+ require "peatio/bitgo/version"
15
+ end
16
+ end
@@ -0,0 +1,21 @@
1
+
2
+ module Peatio
3
+ module Bitgo
4
+ # TODO: Processing of unconfirmed transactions from mempool isn't supported now.
5
+ class Blockchain < Peatio::Blockchain::Abstract
6
+
7
+ DEFAULT_FEATURES = {case_sensitive: true, cash_addr_format: false}.freeze
8
+
9
+ def initialize(custom_features = {})
10
+ @features = DEFAULT_FEATURES.merge(custom_features).slice(*SUPPORTED_FEATURES)
11
+ @settings = {}
12
+ end
13
+
14
+ def configure(settings = {})
15
+ # Clean client state during configure.
16
+ @client = nil
17
+ @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
18
+ end
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,52 @@
1
+ require 'faraday'
2
+ require 'better-faraday'
3
+
4
+ module Peatio
5
+ module Bitgo
6
+ class Client
7
+ Error = Class.new(StandardError)
8
+ class ConnectionError < Error; end
9
+
10
+ class ResponseError < Error
11
+ def initialize(msg)
12
+ super "#{msg}"
13
+ end
14
+ end
15
+
16
+ def initialize(endpoint, access_token)
17
+ @endpoint = URI.parse(endpoint)
18
+ @access_token = access_token
19
+ end
20
+
21
+ def rest_api(verb, path, data = nil)
22
+ args = [@endpoint.to_s + path]
23
+
24
+ if data
25
+ if %i[ post put patch ].include?(verb)
26
+ args << data.compact.to_json
27
+ args << { 'Content-Type' => 'application/json' }
28
+ else
29
+ args << data.compact
30
+ args << {}
31
+ end
32
+ else
33
+ args << nil
34
+ args << {}
35
+ end
36
+
37
+ args.last['Accept'] = 'application/json'
38
+ args.last['Authorization'] = 'Bearer ' + @access_token
39
+
40
+ response = Faraday.send(verb, *args)
41
+ response.assert_success!
42
+ response = JSON.parse(response.body)
43
+ response['error'].tap { |error| raise ResponseError.new(error) if error }
44
+ response
45
+ rescue Faraday::Error => e
46
+ raise ConnectionError, e
47
+ rescue StandardError => e
48
+ raise Error, e
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,42 @@
1
+ module Peatio
2
+ module Bitgo
3
+ module Hooks
4
+ BLOCKCHAIN_VERSION_REQUIREMENT = "~> 1.0.0"
5
+ WALLET_VERSION_REQUIREMENT = "~> 1.0.0"
6
+
7
+ class << self
8
+ def check_compatibility
9
+ unless Gem::Requirement.new(BLOCKCHAIN_VERSION_REQUIREMENT)
10
+ .satisfied_by?(Gem::Version.new(Peatio::Blockchain::VERSION))
11
+ [
12
+ "Bitgo blockchain version requiremnt was not suttisfied by Peatio::Blockchain.",
13
+ "Bitgo blockchain requires #{BLOCKCHAIN_VERSION_REQUIREMENT}.",
14
+ "Peatio::Blockchain version is #{Peatio::Blockchain::VERSION}"
15
+ ].join('\n').tap { |s| Kernel.abort s }
16
+ end
17
+
18
+ unless Gem::Requirement.new(WALLET_VERSION_REQUIREMENT)
19
+ .satisfied_by?(Gem::Version.new(Peatio::Wallet::VERSION))
20
+ [
21
+ "Bitgo wallet version requiremnt was not suttisfied by Peatio::Wallet.",
22
+ "Bitgo wallet requires #{WALLET_VERSION_REQUIREMENT}.",
23
+ "Peatio::Wallet version is #{Peatio::Wallet::VERSION}"
24
+ ].join('\n').tap { |s| Kernel.abort s }
25
+ end
26
+ end
27
+
28
+ def register
29
+ Peatio::Blockchain.registry[:bitgo] = Bitgo::Blockchain.new
30
+ Peatio::Wallet.registry[:bitgo] = Bitgo::Wallet.new
31
+ end
32
+ end
33
+
34
+ if defined?(Rails::Railtie)
35
+ require "peatio/bitgo/railtie"
36
+ else
37
+ check_compatibility
38
+ register
39
+ end
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,13 @@
1
+ module Peatio
2
+ module Bitgo
3
+ class Railtie < Rails::Railtie
4
+ config.before_initialize do
5
+ Hooks.check_compatibility
6
+ end
7
+
8
+ config.after_initialize do
9
+ Hooks.register
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,5 @@
1
+ module Peatio
2
+ module Bitgo
3
+ VERSION = "1.0.0"
4
+ end
5
+ end
@@ -0,0 +1,215 @@
1
+ module Peatio
2
+ module Bitgo
3
+ class Wallet < Peatio::Wallet::Abstract
4
+
5
+ def initialize(settings = {})
6
+ @settings = settings
7
+ end
8
+
9
+ def configure(settings = {})
10
+ # Clean client state during configure.
11
+ @client = nil
12
+
13
+ @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
14
+
15
+ @wallet = @settings.fetch(:wallet) do
16
+ raise Peatio::Wallet::MissingSettingError, :wallet
17
+ end.slice(:uri, :address, :secret, :bitgo_access_token, :bitgo_wallet_id, :bitgo_test_net)
18
+
19
+ @currency = @settings.fetch(:currency) do
20
+ raise Peatio::Wallet::MissingSettingError, :currency
21
+ end.slice(:id, :base_factor, :code, :options)
22
+ end
23
+
24
+ def create_address!(options = {})
25
+ if options[:address_id].present?
26
+ response = client.rest_api(:get, "/wallet/#{bitgo_wallet_id}/address/#{options[:address_id]}")
27
+ { address: response['address'], secret: bitgo_wallet_passphrase }
28
+ else
29
+ response = client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/address")
30
+ { address: response['address'], secret: bitgo_wallet_passphrase, details: { address_id: response['id'] }}
31
+ end
32
+ rescue Bitgo::Client::Error => e
33
+ raise Peatio::Wallet::ClientError, e
34
+ end
35
+
36
+ def create_transaction!(transaction, options = {})
37
+ currency_options = @currency.fetch(:options).slice(:gas_limit, :gas_price)
38
+
39
+ if currency_options[:gas_limit].present? && currency_options[:gas_price].present?
40
+ options.merge!(currency_options)
41
+ create_eth_transaction(transaction, options)
42
+ else
43
+ amount = convert_to_base_unit(transaction.amount)
44
+
45
+ if options[:subtract_fee].to_s == 'true'
46
+ fee = build_raw_transaction(transaction)
47
+ baseFeeInfo = fee.dig('feeInfo','fee')
48
+ fee = baseFeeInfo.present? ? baseFeeInfo : fee.dig('txInfo','Fee')
49
+ amount -= fee.to_i
50
+ end
51
+
52
+ txid = client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/sendcoins", {
53
+ address: transaction.to_address.to_s,
54
+ amount: amount.to_s,
55
+ walletPassphrase: bitgo_wallet_passphrase
56
+ }.compact).fetch('txid')
57
+
58
+ transaction.hash = normalize_txid(txid)
59
+ transaction
60
+ end
61
+ rescue Bitgo::Client::Error => e
62
+ raise Peatio::Wallet::ClientError, e
63
+ end
64
+
65
+
66
+ def build_raw_transaction(transaction)
67
+ client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/tx/build", {
68
+ recipients: [{
69
+ address: transaction.to_address,
70
+ amount: convert_to_base_unit(transaction.amount).to_s
71
+ }]
72
+ }.compact)
73
+ end
74
+
75
+ def create_eth_transaction(transaction, options = {})
76
+ amount = convert_to_base_unit(transaction.amount)
77
+ amount -= options.fetch(:gas_limit).to_i * options.fetch(:gas_price).to_i if options.dig(:subtract_fee)
78
+
79
+ txid = client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/sendcoins", {
80
+ address: transaction.to_address.to_s,
81
+ amount: amount.to_s,
82
+ walletPassphrase: bitgo_wallet_passphrase,
83
+ gas: options.fetch(:gas_limit).to_i,
84
+ gasPrice: options.fetch(:gas_price).to_i
85
+ }.compact).fetch('txid')
86
+
87
+ transaction.hash = normalize_txid(txid)
88
+ transaction
89
+ end
90
+
91
+ def load_balance!
92
+ response = client.rest_api(:get, "/wallet/#{bitgo_wallet_id}")
93
+ convert_from_base_unit(response.fetch('balanceString'))
94
+ rescue Bitgo::Client::Error => e
95
+ raise Peatio::Wallet::ClientError, e
96
+ end
97
+
98
+ def trigger_webhook_event(event)
99
+ currency_id = @wallet.fetch(:bitgo_test_net).present? ? 't' + @currency.fetch(:id) : @currency.fetch(:id)
100
+ return unless currency_id == event['coin'] && @wallet.fetch(:bitgo_wallet_id) == event['wallet']
101
+
102
+ if event['type'] == 'transfer'
103
+ transactions = fetch_transfer!(event['transfer'])
104
+ return { transfers: transactions }
105
+ elsif event['address_confirmation']
106
+ # TODO Add Address confirmation
107
+ end
108
+ end
109
+
110
+ def register_webhooks!(url)
111
+ transfer_webhook(url)
112
+ address_confirmation_webhook(url)
113
+ end
114
+
115
+ def fetch_transfer!(id)
116
+ # TODO: Add Rspecs for this one
117
+ response = client.rest_api(:get, "/wallet/#{bitgo_wallet_id}/transfer/#{id}")
118
+ parse_entries(response['entries']).map do |entry|
119
+ to_address = if response.dig('coinSpecific', 'memo').present?
120
+ build_address(response.dig('coinSpecific', 'memo').first)
121
+ else
122
+ entry['address']
123
+ end
124
+ state = if response['state'] == 'unconfrimed'
125
+ 'pending'
126
+ elsif response['state'] == 'confirmed'
127
+ 'success'
128
+ end
129
+
130
+ transaction = Peatio::Transaction.new(
131
+ currency_id: @currency.fetch(:id),
132
+ amount: convert_from_base_unit(response['valueString']),
133
+ hash: response['txid'],
134
+ to_address: to_address,
135
+ block_number: response['height'],
136
+ # TODO: Add sendmany support
137
+ txout: 0,
138
+ status: state
139
+ )
140
+ transaction if transaction.valid?
141
+ end.compact
142
+ rescue Bitgo::Client::Error => e
143
+ raise Peatio::Wallet::ClientError, e
144
+ end
145
+
146
+ def transfer_webhook(url)
147
+ client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/webhooks", {
148
+ type: 'transfer',
149
+ allToken: true,
150
+ url: url,
151
+ label: "webhook for #{url}",
152
+ listenToFailureStates: false
153
+ })
154
+ end
155
+
156
+ def address_confirmation_webhook(url)
157
+ client.rest_api(:post, "/wallet/#{bitgo_wallet_id}/webhooks", {
158
+ type: 'address_confirmation_webhook',
159
+ allToken: true,
160
+ url: url,
161
+ label: "webhook for #{url}",
162
+ listenToFailureStates: false
163
+ })
164
+ end
165
+
166
+ def parse_entries(entries)
167
+ entries.map do |e|
168
+ e if e["valueString"].to_i.positive?
169
+ end.compact
170
+ end
171
+
172
+ private
173
+
174
+ def client
175
+ currency_id = @currency.fetch(:id) { raise Peatio::Wallet::MissingSettingError, :id }
176
+ uri = @wallet.fetch(:uri) { raise Peatio::Wallet::MissingSettingError, :uri }
177
+ access_token = @wallet.fetch(:bitgo_access_token) { raise Peatio::Wallet::MissingSettingError, :bitgo_access_token }
178
+
179
+ currency_code_prefix = @wallet.fetch(:bitgo_test_net) ? 't' : ''
180
+ uri = uri.gsub(/\/+\z/, '') + '/' + currency_code_prefix + currency_id
181
+ @client ||= Client.new(uri, access_token)
182
+ end
183
+
184
+ def build_address(memo)
185
+ "#{memo['address']}?memoId=#{memo['value']}"
186
+ end
187
+
188
+ def bitgo_wallet_passphrase
189
+ @wallet.fetch(:secret)
190
+ end
191
+
192
+ def bitgo_wallet_id
193
+ @wallet.fetch(:bitgo_wallet_id)
194
+ end
195
+
196
+ def normalize_txid(txid)
197
+ txid.downcase
198
+ end
199
+
200
+ def convert_from_base_unit(value)
201
+ value.to_d / @currency.fetch(:base_factor)
202
+ end
203
+
204
+ def convert_to_base_unit(value)
205
+ x = value.to_d * @currency.fetch(:base_factor)
206
+ unless (x % 1).zero?
207
+ raise Peatio::WalletClient::Error,
208
+ "Failed to convert value to base (smallest) unit because it exceeds the maximum precision: " \
209
+ "#{value.to_d} - #{x.to_d} must be equal to zero."
210
+ end
211
+ x.to_i
212
+ end
213
+ end
214
+ end
215
+ end
@@ -0,0 +1,39 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "peatio/bitgo/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "peatio-bitgo"
8
+ spec.version = Peatio::Bitgo::VERSION
9
+ spec.authors = ["Nadia Ch., Maksym N."]
10
+ spec.email = ["nchumak@heliostech.fr"]
11
+
12
+ spec.summary = %q{Gem for extending Peatio plugable system with Bitgo implementation.}
13
+ spec.description = %q{Bitgo Peatio gem which implements Peatio::Blockchain::Abstract & Peatio::Wallet::Abstract.}
14
+ spec.homepage = "https://openware.com/"
15
+ spec.license = "MIT"
16
+
17
+ # Specify which files should be added to the gem when it is released.
18
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
19
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
20
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
21
+ end
22
+ spec.bindir = "exe"
23
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
24
+ spec.require_paths = ["lib"]
25
+
26
+ spec.add_dependency "activesupport", "~> 5.2.3"
27
+ spec.add_dependency "better-faraday", "~> 1.0.5"
28
+ spec.add_dependency "faraday", "~> 0.15.4"
29
+ spec.add_dependency "memoist", "~> 0.16.0"
30
+ spec.add_dependency "peatio", ">= 0.6.3"
31
+ spec.add_dependency 'net-http-persistent', '~> 3.0.1'
32
+
33
+ spec.add_development_dependency "bundler", "~> 1.16"
34
+ spec.add_development_dependency "mocha", "~> 1.8"
35
+ spec.add_development_dependency "pry-byebug"
36
+ spec.add_development_dependency "rake", "~> 10.0"
37
+ spec.add_development_dependency "rspec", "~> 3.0"
38
+ spec.add_development_dependency "webmock", "~> 3.5"
39
+ end
metadata ADDED
@@ -0,0 +1,233 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: peatio-bitgo
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Nadia Ch., Maksym N.
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.2.3
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.2.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: better-faraday
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.5
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.5
41
+ - !ruby/object:Gem::Dependency
42
+ name: faraday
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: 0.15.4
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: 0.15.4
55
+ - !ruby/object:Gem::Dependency
56
+ name: memoist
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: 0.16.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: 0.16.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: peatio
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 0.6.3
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 0.6.3
83
+ - !ruby/object:Gem::Dependency
84
+ name: net-http-persistent
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 3.0.1
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 3.0.1
97
+ - !ruby/object:Gem::Dependency
98
+ name: bundler
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '1.16'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '1.16'
111
+ - !ruby/object:Gem::Dependency
112
+ name: mocha
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.8'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.8'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rake
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '10.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '10.0'
153
+ - !ruby/object:Gem::Dependency
154
+ name: rspec
155
+ requirement: !ruby/object:Gem::Requirement
156
+ requirements:
157
+ - - "~>"
158
+ - !ruby/object:Gem::Version
159
+ version: '3.0'
160
+ type: :development
161
+ prerelease: false
162
+ version_requirements: !ruby/object:Gem::Requirement
163
+ requirements:
164
+ - - "~>"
165
+ - !ruby/object:Gem::Version
166
+ version: '3.0'
167
+ - !ruby/object:Gem::Dependency
168
+ name: webmock
169
+ requirement: !ruby/object:Gem::Requirement
170
+ requirements:
171
+ - - "~>"
172
+ - !ruby/object:Gem::Version
173
+ version: '3.5'
174
+ type: :development
175
+ prerelease: false
176
+ version_requirements: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - "~>"
179
+ - !ruby/object:Gem::Version
180
+ version: '3.5'
181
+ description: Bitgo Peatio gem which implements Peatio::Blockchain::Abstract & Peatio::Wallet::Abstract.
182
+ email:
183
+ - nchumak@heliostech.fr
184
+ executables: []
185
+ extensions: []
186
+ extra_rdoc_files: []
187
+ files:
188
+ - ".gitignore"
189
+ - ".rspec"
190
+ - ".travis.yml"
191
+ - Gemfile
192
+ - Gemfile.lock
193
+ - LICENSE.txt
194
+ - README.md
195
+ - Rakefile
196
+ - bin/console
197
+ - bin/setup
198
+ - configs/blockchains.yml
199
+ - configs/currencies.yml
200
+ - configs/wallets.yml
201
+ - docs/integration.md
202
+ - lib/peatio/bitgo.rb
203
+ - lib/peatio/bitgo/blockchain.rb
204
+ - lib/peatio/bitgo/client.rb
205
+ - lib/peatio/bitgo/hooks.rb
206
+ - lib/peatio/bitgo/railtie.rb
207
+ - lib/peatio/bitgo/version.rb
208
+ - lib/peatio/bitgo/wallet.rb
209
+ - peatio-bitgo.gemspec
210
+ homepage: https://openware.com/
211
+ licenses:
212
+ - MIT
213
+ metadata: {}
214
+ post_install_message:
215
+ rdoc_options: []
216
+ require_paths:
217
+ - lib
218
+ required_ruby_version: !ruby/object:Gem::Requirement
219
+ requirements:
220
+ - - ">="
221
+ - !ruby/object:Gem::Version
222
+ version: '0'
223
+ required_rubygems_version: !ruby/object:Gem::Requirement
224
+ requirements:
225
+ - - ">="
226
+ - !ruby/object:Gem::Version
227
+ version: '0'
228
+ requirements: []
229
+ rubygems_version: 3.0.3
230
+ signing_key:
231
+ specification_version: 4
232
+ summary: Gem for extending Peatio plugable system with Bitgo implementation.
233
+ test_files: []