exchange-tron 2.6.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: ef08cdaa03c1841cc052135c00778912a49b0186f515a7d72d02cae666ea8818
4
+ data.tar.gz: e6a2ef685fafb30b60cb7012c8a951a8d6e17a24f0456e6c375140d4504c6deb
5
+ SHA512:
6
+ metadata.gz: 4a78937bced2799a62a78f07a0a27100e134c4e0e37f54aa6a22064a6131671c49ba08a7f3dff333c799deea923a918c4ab13710a659c71edad6b10af26fb5c9
7
+ data.tar.gz: b5fee08a93144b0eb6617af40ccaede353d5aa9a4b62fcbf52903caaf0e4fa85b8b19facdbcb61be412a141a6b43bf7c239e55cad846e488b3d391452e5da4f8
data/.gitattributes ADDED
@@ -0,0 +1,2 @@
1
+ # Auto detect text files and perform LF normalization
2
+ * text=auto
data/.gitignore ADDED
@@ -0,0 +1,14 @@
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
12
+
13
+ # Ignore RubyMine files
14
+ .idea
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -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.17.1
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-tron.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,164 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ exchange-tron (2.6.1)
5
+ activesupport (~> 5.2.3)
6
+ better-faraday (~> 1.0.5)
7
+ digest-sha3 (~> 1.1.0)
8
+ ed25519
9
+ faraday (~> 0.17)
10
+ ffi
11
+ memoist (~> 0.16.0)
12
+ net-http-persistent (~> 3.0.1)
13
+ peatio (>= 2.6.5)
14
+
15
+ GEM
16
+ remote: https://rubygems.org/
17
+ specs:
18
+ activemodel (5.2.6)
19
+ activesupport (= 5.2.6)
20
+ activesupport (5.2.6)
21
+ concurrent-ruby (~> 1.0, >= 1.0.2)
22
+ i18n (>= 0.7, < 2)
23
+ minitest (~> 5.1)
24
+ tzinfo (~> 1.1)
25
+ addressable (2.8.0)
26
+ public_suffix (>= 2.0.2, < 5.0)
27
+ amq-protocol (2.3.2)
28
+ amqp (1.8.0)
29
+ amq-protocol (>= 2.2.0)
30
+ eventmachine
31
+ better-faraday (1.0.8)
32
+ activesupport (>= 4.0, < 6.0)
33
+ faraday (~> 0.12)
34
+ bunny (2.19.0)
35
+ amq-protocol (~> 2.3, >= 2.3.1)
36
+ sorted_set (~> 1, >= 1.0.2)
37
+ byebug (11.1.3)
38
+ clamp (1.3.2)
39
+ coderay (1.1.3)
40
+ concurrent-ruby (1.1.9)
41
+ connection_pool (2.2.5)
42
+ cookiejar (0.3.3)
43
+ crack (0.4.5)
44
+ rexml
45
+ daemons (1.4.1)
46
+ diff-lcs (1.4.4)
47
+ digest-sha3 (1.1.0)
48
+ ed25519 (1.2.4)
49
+ em-http-request (1.1.7)
50
+ addressable (>= 2.3.4)
51
+ cookiejar (!= 0.3.1)
52
+ em-socksify (>= 0.3)
53
+ eventmachine (>= 1.0.3)
54
+ http_parser.rb (>= 0.6.0)
55
+ em-socksify (0.3.2)
56
+ eventmachine (>= 1.0.0.beta.4)
57
+ em-synchrony (1.0.6)
58
+ eventmachine (>= 1.0.0.beta.1)
59
+ em-websocket (0.5.3)
60
+ eventmachine (>= 0.12.9)
61
+ http_parser.rb (~> 0)
62
+ eventmachine (1.2.7)
63
+ faraday (0.17.4)
64
+ multipart-post (>= 1.2, < 3)
65
+ faraday_middleware (0.13.1)
66
+ faraday (>= 0.7.4, < 1.0)
67
+ faye (1.4.0)
68
+ cookiejar (>= 0.3.0)
69
+ em-http-request (>= 1.1.6)
70
+ eventmachine (>= 0.12.0)
71
+ faye-websocket (>= 0.11.0)
72
+ multi_json (>= 1.0.0)
73
+ rack (>= 1.0.0)
74
+ websocket-driver (>= 0.5.1)
75
+ faye-websocket (0.11.1)
76
+ eventmachine (>= 0.12.0)
77
+ websocket-driver (>= 0.5.1)
78
+ ffi (1.15.4)
79
+ hashdiff (1.0.1)
80
+ http_parser.rb (0.8.0)
81
+ i18n (1.8.11)
82
+ concurrent-ruby (~> 1.0)
83
+ jwt (2.3.0)
84
+ memoist (0.16.2)
85
+ method_source (1.0.0)
86
+ minitest (5.14.4)
87
+ mocha (1.13.0)
88
+ multi_json (1.15.0)
89
+ multipart-post (2.1.1)
90
+ mysql2 (0.5.3)
91
+ net-http-persistent (3.0.1)
92
+ connection_pool (~> 2.2)
93
+ peatio (2.6.5)
94
+ activemodel (> 5.2, <= 6.0.0)
95
+ amqp
96
+ bunny
97
+ clamp
98
+ em-synchrony (~> 1.0)
99
+ em-websocket
100
+ eventmachine
101
+ faraday_middleware (~> 0.13.1)
102
+ faye (~> 1.2)
103
+ jwt
104
+ mysql2
105
+ prometheus-client
106
+ thin
107
+ prometheus-client (2.1.0)
108
+ pry (0.13.1)
109
+ coderay (~> 1.1)
110
+ method_source (~> 1.0)
111
+ pry-byebug (3.9.0)
112
+ byebug (~> 11.0)
113
+ pry (~> 0.13.0)
114
+ public_suffix (4.0.6)
115
+ rack (2.2.3)
116
+ rake (13.0.6)
117
+ rbtree (0.4.4)
118
+ rexml (3.2.5)
119
+ rspec (3.10.0)
120
+ rspec-core (~> 3.10.0)
121
+ rspec-expectations (~> 3.10.0)
122
+ rspec-mocks (~> 3.10.0)
123
+ rspec-core (3.10.1)
124
+ rspec-support (~> 3.10.0)
125
+ rspec-expectations (3.10.1)
126
+ diff-lcs (>= 1.2.0, < 2.0)
127
+ rspec-support (~> 3.10.0)
128
+ rspec-mocks (3.10.2)
129
+ diff-lcs (>= 1.2.0, < 2.0)
130
+ rspec-support (~> 3.10.0)
131
+ rspec-support (3.10.3)
132
+ set (1.0.2)
133
+ sorted_set (1.0.3)
134
+ rbtree
135
+ set (~> 1.0)
136
+ thin (1.8.1)
137
+ daemons (~> 1.0, >= 1.0.9)
138
+ eventmachine (~> 1.0, >= 1.0.4)
139
+ rack (>= 1, < 3)
140
+ thread_safe (0.3.6)
141
+ tzinfo (1.2.9)
142
+ thread_safe (~> 0.1)
143
+ webmock (3.14.0)
144
+ addressable (>= 2.8.0)
145
+ crack (>= 0.3.2)
146
+ hashdiff (>= 0.4.0, < 2.0.0)
147
+ websocket-driver (0.7.5)
148
+ websocket-extensions (>= 0.1.0)
149
+ websocket-extensions (0.1.5)
150
+
151
+ PLATFORMS
152
+ ruby
153
+
154
+ DEPENDENCIES
155
+ bundler (>= 1.3.0)
156
+ exchange-tron!
157
+ mocha (~> 1.8)
158
+ pry-byebug
159
+ rake (~> 13.0)
160
+ rspec (~> 3.0)
161
+ webmock (~> 3.5)
162
+
163
+ BUNDLED WITH
164
+ 2.2.26
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2019 ABC
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.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ ## Installation
2
+
3
+ Add this line to your application's Gemfile:
4
+
5
+ ```ruby
6
+ gem 'peatio-tron'
7
+ ```
8
+
9
+ And then execute:
10
+
11
+ $ bundle
12
+
13
+ Or install it yourself as:
14
+
15
+ $ gem install peatio-tron
16
+
17
+ ## Usage
18
+
19
+ TODO: Write usage instructions here
20
+
21
+ ## Development
22
+
23
+ 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.
24
+
25
+ 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).
26
+
27
+ ## Contributing
28
+
29
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/peatio-tron. 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.
30
+
31
+ ## License
32
+
33
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
34
+
35
+ ## Code of Conduct
36
+
37
+ Everyone interacting in the Peatio::Tron project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/peatio-tron/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 "peatio/tron"
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
@@ -0,0 +1,10 @@
1
+ - key: tron-testnet
2
+ name: Tron Testnet
3
+ client: tron
4
+ server: https://api.nileex.io/
5
+ height: 4621701
6
+ min_confirmations: 1
7
+ explorer:
8
+ address: https://nile.tronscan.org/#/address/#{address}
9
+ transaction: https://nile.tronscan.org/#/transaction/#{txid}
10
+ status: active
@@ -0,0 +1,59 @@
1
+ - id: trx
2
+ name: Tron
3
+ blockchain_key: tron-testnet
4
+ symbol: 'T'
5
+ type: coin
6
+ precision: 6
7
+ base_factor: 1_000_000
8
+ visible: true
9
+ deposit_enabled: true
10
+ withdrawal_enabled: true
11
+ min_deposit_amount: 0.1
12
+ min_collection_amount: 0.1
13
+ withdraw_limit_24h: 300
14
+ withdraw_limit_72h: 600
15
+ deposit_fee: 0
16
+ withdraw_fee: 0
17
+ position: 4
18
+ options: { }
19
+
20
+ - id: trz
21
+ name: TRONZ
22
+ blockchain_key: tron-testnet
23
+ symbol: 'TZ'
24
+ type: coin
25
+ precision: 6
26
+ base_factor: 1_000_000
27
+ visible: true
28
+ deposit_enabled: true
29
+ withdrawal_enabled: true
30
+ min_deposit_amount: 0.1
31
+ min_collection_amount: 0.1
32
+ withdraw_limit_24h: 300
33
+ withdraw_limit_72h: 600
34
+ deposit_fee: 0
35
+ withdraw_fee: 0
36
+ position: 4
37
+ options:
38
+ trc10_token_id: '1000016'
39
+
40
+ - id: jst
41
+ name: JUST GOV TOKEN
42
+ blockchain_key: tron-testnet
43
+ symbol: 'JG'
44
+ type: coin
45
+ precision: 18
46
+ base_factor: 1_000_000_000_000_000_000
47
+ visible: true
48
+ deposit_enabled: true
49
+ withdrawal_enabled: true
50
+ min_deposit_amount: 0.01
51
+ min_collection_amount: 0.01
52
+ withdraw_limit_24h: 300
53
+ withdraw_limit_72h: 600
54
+ deposit_fee: 0
55
+ withdraw_fee: 0
56
+ position: 4
57
+ options:
58
+ trc20_contract_address: 'TF17BgPaZYbz8oxbjhriubPDsA7ArKoLX3'
59
+ fee_limit: 40000000
@@ -0,0 +1,51 @@
1
+ - name: Tron Deposit Wallet
2
+ blockchain_key: tron-testnet
3
+ currency_ids: trx,jst,trz
4
+ address: 'TPx5k41UHxVCvuF7ymHbGeCfC95ommE63t'
5
+ kind: deposit
6
+ nsig: 1
7
+ max_balance: 0.0
8
+ status: active
9
+ gateway: tron
10
+ settings:
11
+ uri: https://api.nileex.io/
12
+ secret: '939ae0bca398d1f854937e1446eaedcfb4bb4864c1fac4cd31fda48b5d828964'
13
+
14
+ - name: Tron Hot Wallet
15
+ blockchain_key: tron-testnet
16
+ currency_ids: trx,jst,trz
17
+ address: 'TPx5k41UHxVCvuF7ymHbGeCfC95ommE63t' !
18
+ kind: hot
19
+ nsig: 1
20
+ max_balance: 100.0
21
+ status: active
22
+ gateway: tron
23
+ settings:
24
+ uri: https://api.nileex.io/
25
+ secret: '939ae0bca398d1f854937e1446eaedcfb4bb4864c1fac4cd31fda48b5d828964'
26
+
27
+ - name: Tron Warm Wallet
28
+ blockchain_key: tron-testnet
29
+ currency_ids: trx,jst,trz
30
+ address: 'TPx5k41UHxVCvuF7ymHbGeCfC95ommE63t'
31
+ kind: warm
32
+ nsig: 1
33
+ max_balance: 1000.0
34
+ status: active
35
+ gateway: tron
36
+ settings:
37
+ uri: https://api.nileex.io/
38
+ secret: '939ae0bca398d1f854937e1446eaedcfb4bb4864c1fac4cd31fda48b5d828964'
39
+
40
+ - name: Tron Fee Wallet
41
+ blockchain_key: tron-testnet
42
+ currency_ids: trx,jst,trz
43
+ address: 'TPx5k41UHxVCvuF7ymHbGeCfC95ommE63t'
44
+ kind: fee
45
+ nsig: 1
46
+ max_balance: 1000.0
47
+ status: active
48
+ gateway: tron
49
+ settings:
50
+ uri: https://api.nileex.io/
51
+ secret: '939ae0bca398d1f854937e1446eaedcfb4bb4864c1fac4cd31fda48b5d828964'
@@ -0,0 +1,42 @@
1
+ lib = File.expand_path("../lib", __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require "peatio/tron/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "exchange-tron"
7
+ spec.version = Peatio::Tron::VERSION
8
+ spec.authors = ["ABC"]
9
+ spec.email = [""]
10
+
11
+ spec.summary = %q{Gem for extending Peatio plugable system with Tron implementation.}
12
+ spec.description = %q{Tron gem which implements Peatio::Blockchain::Abstract & Peatio::Wallet::Abstract.}
13
+ spec.homepage = ""
14
+ spec.license = "MIT"
15
+
16
+ # Specify which files should be added to the gem when it is released.
17
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
19
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
+ end
21
+
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.17"
29
+ spec.add_dependency "memoist", "~> 0.16.0"
30
+ spec.add_dependency "digest-sha3", "~> 1.1.0"
31
+ spec.add_dependency "ed25519"
32
+ spec.add_dependency "ffi"
33
+ spec.add_dependency "peatio", ">= 2.6.5"
34
+ spec.add_dependency 'net-http-persistent', '~> 3.0.1'
35
+
36
+ spec.add_development_dependency "bundler", ">= 1.3.0"
37
+ spec.add_development_dependency "mocha", "~> 1.8"
38
+ spec.add_development_dependency "pry-byebug"
39
+ spec.add_development_dependency "rake", "~> 13.0"
40
+ spec.add_development_dependency "rspec", "~> 3.0"
41
+ spec.add_development_dependency "webmock", "~> 3.5"
42
+ end
@@ -0,0 +1,207 @@
1
+ module Peatio
2
+ module Tron
3
+ class Blockchain < Peatio::Blockchain::Abstract
4
+ include Encryption
5
+
6
+ DEFAULT_FEATURES = { case_sensitive: true, cash_addr_format: false }.freeze
7
+ TOKEN_EVENT_IDENTIFIER = 'ddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef'
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
+
18
+ @trc10 = []; @trc20 = []; @trx = []
19
+
20
+ @settings.merge!(settings.slice(*SUPPORTED_SETTINGS))
21
+ @settings[:currencies]&.each do |c|
22
+ if c.dig(:options, :trc10_token_id).present?
23
+ @trc10 << c
24
+ elsif c.dig(:options, :trc20_contract_address).present?
25
+ @trc20 << c
26
+ else
27
+ @trx << c
28
+ end
29
+ end
30
+ end
31
+
32
+ def fetch_block!(block_number)
33
+ client.json_rpc(path: 'wallet/getblockbynum', params: { num: block_number })
34
+ .fetch('transactions', []).each_with_object([]) do |tx, txs_array|
35
+
36
+ if %w[TransferContract TransferAssetContract].include? tx.dig('raw_data', 'contract')[0].fetch('type', nil)
37
+ next if invalid_transaction?(tx)
38
+ else
39
+ tx = client.json_rpc(path: 'wallet/gettransactioninfobyid', params: { value: tx['txID'] })
40
+ next if tx.nil? || invalid_trc20_transaction?(tx)
41
+ end
42
+
43
+ txs = build_transaction(tx.merge('block_number' => block_number)).map do |ntx|
44
+ Peatio::Transaction.new(ntx)
45
+ end
46
+
47
+ txs_array.append(*txs)
48
+ end.yield_self { |txs_array| Peatio::Block.new(block_number, txs_array) }
49
+ rescue Client::Error => e
50
+ raise Peatio::Blockchain::ClientError, e
51
+ end
52
+
53
+ def latest_block_number
54
+ client.json_rpc(path: 'wallet/getblockbylatestnum', params: { num: 1 })
55
+ .fetch('block')[0]['block_header']['raw_data']['number']
56
+ rescue Client::Error => e
57
+ raise Peatio::Blockchain::ClientError, e
58
+ end
59
+
60
+ def load_balance_of_address!(address, currency_id)
61
+ currency = @settings[:currencies].find { |c| c[:id] == currency_id.to_s }
62
+ raise UndefinedCurrencyError unless currency
63
+
64
+ if currency.dig(:options, :trc10_token_id).present?
65
+ client.json_rpc(path: 'wallet/getaccount',
66
+ params: { address: reformat_decode_address(address) }
67
+ ).fetch('assetV2', [])
68
+ .find { |a| a['key'] == currency.dig(:options, :trc10_token_id) }
69
+ .try(:fetch, 'value', 0)
70
+ elsif currency.dig(:options, :trc20_contract_address).present?
71
+ client.json_rpc(path: 'wallet/triggersmartcontract',
72
+ params: {
73
+ owner_address: reformat_decode_address(address),
74
+ contract_address: reformat_decode_address(currency.dig(:options, :trc20_contract_address)),
75
+ function_selector: 'balanceOf(address)',
76
+ parameter: abi_encode(reformat_decode_address(address)[2..42]) }
77
+ ).fetch('constant_result')[0].hex
78
+ else
79
+ client.json_rpc(path: 'wallet/getaccount',
80
+ params: { address: reformat_decode_address(address) }
81
+ ).fetch('balance', nil)
82
+ end.yield_self { |amount| convert_from_base_unit(amount.to_i, currency) }
83
+ rescue Client::Error => e
84
+ raise Peatio::Blockchain::ClientError, e
85
+ end
86
+
87
+ private
88
+
89
+ def build_transaction(tx_hash)
90
+ if tx_hash.has_key?('contract_address')
91
+ build_trc20_transaction(tx_hash)
92
+ else
93
+ case tx_hash['raw_data']['contract'][0]['type']
94
+ when 'TransferContract'
95
+ build_coin_transaction(tx_hash)
96
+ when 'TransferAssetContract'
97
+ build_trc10_transaction(tx_hash)
98
+ end
99
+ end
100
+ end
101
+
102
+ def build_trc10_transaction(tx_hash)
103
+ tx = tx_hash['raw_data']['contract'][0]
104
+ currencies = @trc10.select do |c|
105
+ c.dig(:options, :trc10_token_id) == decode_hex(tx['parameter']['value']['asset_name'])
106
+ end
107
+
108
+ formatted_txs = []
109
+ currencies.each do |currency|
110
+ formatted_txs << { hash: reformat_txid(tx_hash['txID']),
111
+ amount: convert_from_base_unit(tx['parameter']['value']['amount'], currency),
112
+ to_address: reformat_encode_address(tx['parameter']['value']['to_address']),
113
+ txout: 0,
114
+ block_number: tx_hash['block_number'],
115
+ currency_id: currency.fetch(:id),
116
+ status: 'success' }
117
+ end
118
+ formatted_txs
119
+ end
120
+
121
+ def build_trc20_transaction(tx_hash)
122
+ # Build invalid transaction for failed withdrawals
123
+ if trc20_transaction_status(tx_hash) == 'failed' && tx_hash.fetch('log', []).blank?
124
+ return build_invalid_trc20_transaction(tx_hash)
125
+ end
126
+
127
+ formatted_txs = []
128
+ tx_hash.fetch('log', []).each_with_index do |log, index|
129
+ next if log.fetch('topics', []).blank? || log.fetch('topics')[0] != TOKEN_EVENT_IDENTIFIER
130
+
131
+ # Skip if TRC20 contract address doesn't match.
132
+ currencies = @trc20.select do |c|
133
+ c.dig(:options, :trc20_contract_address) == reformat_encode_address("41#{log.fetch('address')}")
134
+ end
135
+ next if currencies.blank?
136
+
137
+ destination_address = reformat_encode_address("41#{log.fetch('topics').last[-40..-1]}")
138
+
139
+ currencies.each do |currency|
140
+ formatted_txs << { hash: reformat_txid(tx_hash.fetch('id')),
141
+ amount: convert_from_base_unit(log.fetch('data').hex, currency),
142
+ to_address: destination_address,
143
+ txout: index,
144
+ block_number: tx_hash['block_number'],
145
+ currency_id: currency.fetch(:id),
146
+ status: trc20_transaction_status(tx_hash)
147
+ }
148
+ end
149
+ end
150
+ formatted_txs
151
+ end
152
+
153
+ def build_coin_transaction(tx_hash)
154
+ tx = tx_hash['raw_data']['contract'][0]
155
+ @trx.map do |currency|
156
+ { hash: reformat_txid(tx_hash['txID']),
157
+ amount: convert_from_base_unit(tx['parameter']['value']['amount'], currency),
158
+ to_address: reformat_encode_address(tx['parameter']['value']['to_address']),
159
+ txout: 0,
160
+ block_number: tx_hash['block_number'],
161
+ currency_id: currency.fetch(:id),
162
+ status: 'success' }
163
+ end
164
+ end
165
+
166
+ def build_invalid_trc20_transaction(tx_hash)
167
+ currencies = @trc20.select do |c|
168
+ c.dig(:options, :trc20_contract_address) == reformat_encode_address(tx_hash.fetch('contract_address'))
169
+ end
170
+ return [] if currencies.blank?
171
+
172
+ currencies.each_with_object([]) do |currency, invalid_txs|
173
+ invalid_txs << { hash: reformat_txid(tx_hash.fetch('txID')),
174
+ block_number: tx_hash.fetch('block_number'),
175
+ currency_id: currency.fetch(:id),
176
+ status: trc20_transaction_status(tx_hash) }
177
+ end
178
+ end
179
+
180
+ def trc20_transaction_status(txn_hash)
181
+ txn_hash['receipt']['result'] == 'SUCCESS' ? 'success' : 'failed'
182
+ end
183
+
184
+ def invalid_transaction?(tx)
185
+ tx['raw_data']['contract'][0]['parameter']['value']['amount'].to_i == 0 \
186
+ || tx['ret'][0]['contractRet'] == 'REVERT'
187
+ end
188
+
189
+ def invalid_trc20_transaction?(tx)
190
+ tx.fetch('contract_address', '').blank? \
191
+ || tx.fetch('log', []).blank?
192
+ end
193
+
194
+ def convert_from_base_unit(value, currency)
195
+ value.to_d / currency.fetch(:base_factor).to_d
196
+ end
197
+
198
+ def client
199
+ @client ||= Client.new(settings_fetch(:server))
200
+ end
201
+
202
+ def settings_fetch(key)
203
+ @settings.fetch(key) { raise Peatio::Blockchain::MissingSettingError, key.to_s }
204
+ end
205
+ end
206
+ end
207
+ end